You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2020/07/20 07:59:04 UTC

[GitHub] [incubator-mxnet] xidulu opened a new pull request #18758: [WIP] [Gluon] Add VAE demo

xidulu opened a new pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758


   ## Description ##
   Implement VAE with gluon.probability
   
   ## Checklist ##
   ### Essentials ###
   Please feel free to remove inapplicable items for your PR.
   - [ ] The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant [JIRA issue](https://issues.apache.org/jira/projects/MXNET/issues) created (except PRs with tiny changes)
   - [ ] Changes are complete (i.e. I finished coding on this PR)
   - [ ] All changes have test coverage:
   - Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
   - Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
   - Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
   - [ ] Code is well-documented: 
   - For user-facing API changes, API doc string has been updated. 
   - For new C++ functions in header files, their functionalities and arguments are documented. 
   - For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
   - Check the API doc at https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
   - [ ] To the best of my knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change
   
   ### Changes ###
   - [ ] Feature1, tests, (and when applicable, API doc)
   - [ ] Feature2, tests, (and when applicable, API doc)
   
   ## Comments ##
   - If this change is a backward incompatible change, why must this change be made.
   - Interesting edge cases to note here
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] xidulu commented on pull request #18758: [WIP] [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
xidulu commented on pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758#issuecomment-662178929


   @szha @leezu 
   I am wondering if MXNet's website currently support automatically converting markdown demos to notebook via notedown, like GluonNLP: https://gluon-nlp.mxnet.io/examples/sentiment_analysis/sentiment_analysis.html ? Or I believe it could be little bit weird to provide the example in a markdown form.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] szha commented on pull request #18758: [WIP] [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
szha commented on pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758#issuecomment-662183511


   not yet. I think we should provide the automation in CI first, which may take some time. in the meantime, I think we can check in the jupyter notebook for now with the expectation that it will be replaced by notedown in the future.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] szha merged pull request #18758: [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
szha merged pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] leezu commented on pull request #18758: [WIP] [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
leezu commented on pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758#issuecomment-662563255


   @xidulu @szha it's supported via https://github.com/apache/incubator-mxnet/blob/243ade93bcb8b7962d1faeb89c98409e3ae0d7a4/docs/python_docs/python/Makefile#L31-L33


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] xidulu commented on pull request #18758: [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
xidulu commented on pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758#issuecomment-671681466


   @szha @leezu 
   
   Anything else to polish?
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] xidulu commented on pull request #18758: [WIP] [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
xidulu commented on pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758#issuecomment-662566711


   @leezu 
   Thx for pointing that out, I will take a look at it.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] mxnet-bot commented on pull request #18758: [WIP] [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
mxnet-bot commented on pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758#issuecomment-660868637


   Hey @xidulu , Thanks for submitting the PR 
   All tests are already queued to run once. If tests fail, you can trigger one or more tests again with the following commands: 
   - To trigger all jobs: @mxnet-bot run ci [all] 
   - To trigger specific jobs: @mxnet-bot run ci [job1, job2] 
   *** 
   **CI supported jobs**: [unix-cpu, sanity, website, windows-cpu, windows-gpu, miscellaneous, clang, unix-gpu, centos-gpu, edge, centos-cpu]
   *** 
   _Note_: 
    Only following 3 categories can trigger CI :PR Author, MXNet Committer, Jenkins Admin. 
   All CI tests must pass before the PR can be merged. 
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] xidulu commented on pull request #18758: [WIP] [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
xidulu commented on pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758#issuecomment-661522975


   > Should we use https://pypi.org/project/notedown/ instead of committing the raw notebook? Would you recommend integrating the example into the website as well?
   
   @leezu That's sounds great, are there any existing examples that I can follow?


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] szhengac commented on pull request #18758: [WIP] [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
szhengac commented on pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758#issuecomment-661303220


   > Should we use https://pypi.org/project/notedown/ instead of committing the raw notebook? Would you recommend integrating the example into the website as well?
   
   Yes. I agree that using markdown is better. It is a good example to showcase the usuage of probability module so I think we can put it into the website.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] szhengac commented on a change in pull request #18758: [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
szhengac commented on a change in pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758#discussion_r461976035



##########
File path: example/probability/VAE/VAE.md
##########
@@ -0,0 +1,251 @@
+<!--- Licensed to the Apache Software Foundation (ASF) under one -->
+<!--- or more contributor license agreements.  See the NOTICE file -->
+<!--- distributed with this work for additional information -->
+<!--- regarding copyright ownership.  The ASF licenses this file -->
+<!--- to you under the Apache License, Version 2.0 (the -->
+<!--- "License"); you may not use this file except in compliance -->
+<!--- with the License.  You may obtain a copy of the License at -->
+
+<!---   http://www.apache.org/licenses/LICENSE-2.0 -->
+
+<!--- Unless required by applicable law or agreed to in writing, -->
+<!--- software distributed under the License is distributed on an -->
+<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
+<!--- KIND, either express or implied.  See the License for the -->
+<!--- specific language governing permissions and limitations -->
+<!--- under the License. -->
+
+
+# VAE with Gluon.probability 
+
+In this example, we will demonstrate how you can implement a Variational Auto-encoder(VAE) with Gluon.probability and MXNet's latest NumPy API.
+
+
+```{.python .input}
+import numpy as np
+import mxnet as mx
+from mxnet import autograd, gluon, np, npx
+from mxnet.gluon import nn
+import mxnet.gluon.probability as mgp
+import matplotlib.pyplot as plt
+
+npx.set_np()

Review comment:
       add comment on why we include this line. most of users are not familiar with numpy api.

##########
File path: example/probability/VAE/VAE.md
##########
@@ -0,0 +1,251 @@
+<!--- Licensed to the Apache Software Foundation (ASF) under one -->
+<!--- or more contributor license agreements.  See the NOTICE file -->
+<!--- distributed with this work for additional information -->
+<!--- regarding copyright ownership.  The ASF licenses this file -->
+<!--- to you under the Apache License, Version 2.0 (the -->
+<!--- "License"); you may not use this file except in compliance -->
+<!--- with the License.  You may obtain a copy of the License at -->
+
+<!---   http://www.apache.org/licenses/LICENSE-2.0 -->
+
+<!--- Unless required by applicable law or agreed to in writing, -->
+<!--- software distributed under the License is distributed on an -->
+<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
+<!--- KIND, either express or implied.  See the License for the -->
+<!--- specific language governing permissions and limitations -->
+<!--- under the License. -->
+
+
+# VAE with Gluon.probability 
+
+In this example, we will demonstrate how you can implement a Variational Auto-encoder(VAE) with Gluon.probability and MXNet's latest NumPy API.
+
+
+```{.python .input}
+import numpy as np
+import mxnet as mx
+from mxnet import autograd, gluon, np, npx
+from mxnet.gluon import nn
+import mxnet.gluon.probability as mgp
+import matplotlib.pyplot as plt
+
+npx.set_np()
+data_ctx = mx.cpu()
+model_ctx = mx.gpu(0)
+```
+
+## Dataset
+
+We will use MNIST here for simplicity purpose.
+
+
+```{.python .input}
+def load_data(batch_size):
+    mnist_train = gluon.data.vision.MNIST(train=True)
+    mnist_test = gluon.data.vision.MNIST(train=False)
+    num_worker = 4
+    transformer = gluon.data.vision.transforms.ToTensor()
+    return (gluon.data.DataLoader(mnist_train.transform_first(transformer),
+                                batch_size, shuffle=True,
+                                num_workers=num_worker),
+          gluon.data.DataLoader(mnist_test.transform_first(transformer),
+                                batch_size, shuffle=False,
+                                num_workers=num_worker))
+                                 
+```
+
+## Model definition
+
+
+```{.python .input}
+class VAE(gluon.HybridBlock):
+    def __init__(self, n_hidden=256, n_latent=2, n_layers=1, n_output=784, act_type='relu', **kwargs):
+        r"""
+        n_hidden : number of hidden units in each layer
+        n_latent : dimension of the latent space
+        n_layers : number of layers in the encoder and decoder network
+        n_output : dimension of the observed data
+        """
+        self.soft_zero = 1e-10
+        self.n_latent = n_latent
+        self.output = None
+        self.mu = None
+        super(VAE, self).__init__(**kwargs)
+        self.encoder = nn.HybridSequential()
+        for _ in range(n_layers):
+            self.encoder.add(nn.Dense(n_hidden, activation=act_type))
+        self.encoder.add(nn.Dense(n_latent*2, activation=None))
+        self.decoder = nn.HybridSequential()
+        for _ in range(n_layers):
+            self.decoder.add(nn.Dense(n_hidden, activation=act_type))
+        self.decoder.add(nn.Dense(n_output, activation='sigmoid'))
+        
+    def encode(self, x):
+        r"""
+        Given a batch of x,
+        return the encoder's output
+        """
+        h = self.encoder(x)
+        loc_scale = np.split(h, 2, 1)
+        loc = loc_scale[0]
+        log_variance = loc_scale[1]
+        scale = np.exp(0.5 * log_variance)

Review comment:
       why do we use 0.5?

##########
File path: example/probability/VAE/VAE.md
##########
@@ -0,0 +1,251 @@
+<!--- Licensed to the Apache Software Foundation (ASF) under one -->
+<!--- or more contributor license agreements.  See the NOTICE file -->
+<!--- distributed with this work for additional information -->
+<!--- regarding copyright ownership.  The ASF licenses this file -->
+<!--- to you under the Apache License, Version 2.0 (the -->
+<!--- "License"); you may not use this file except in compliance -->
+<!--- with the License.  You may obtain a copy of the License at -->
+
+<!---   http://www.apache.org/licenses/LICENSE-2.0 -->
+
+<!--- Unless required by applicable law or agreed to in writing, -->
+<!--- software distributed under the License is distributed on an -->
+<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
+<!--- KIND, either express or implied.  See the License for the -->
+<!--- specific language governing permissions and limitations -->
+<!--- under the License. -->
+
+
+# VAE with Gluon.probability 
+
+In this example, we will demonstrate how you can implement a Variational Auto-encoder(VAE) with Gluon.probability and MXNet's latest NumPy API.
+
+
+```{.python .input}
+import numpy as np
+import mxnet as mx
+from mxnet import autograd, gluon, np, npx
+from mxnet.gluon import nn
+import mxnet.gluon.probability as mgp
+import matplotlib.pyplot as plt
+
+npx.set_np()
+data_ctx = mx.cpu()
+model_ctx = mx.gpu(0)
+```
+
+## Dataset
+
+We will use MNIST here for simplicity purpose.
+
+
+```{.python .input}
+def load_data(batch_size):
+    mnist_train = gluon.data.vision.MNIST(train=True)
+    mnist_test = gluon.data.vision.MNIST(train=False)
+    num_worker = 4
+    transformer = gluon.data.vision.transforms.ToTensor()
+    return (gluon.data.DataLoader(mnist_train.transform_first(transformer),
+                                batch_size, shuffle=True,
+                                num_workers=num_worker),
+          gluon.data.DataLoader(mnist_test.transform_first(transformer),
+                                batch_size, shuffle=False,
+                                num_workers=num_worker))
+                                 
+```
+
+## Model definition
+
+
+```{.python .input}
+class VAE(gluon.HybridBlock):
+    def __init__(self, n_hidden=256, n_latent=2, n_layers=1, n_output=784, act_type='relu', **kwargs):
+        r"""
+        n_hidden : number of hidden units in each layer
+        n_latent : dimension of the latent space
+        n_layers : number of layers in the encoder and decoder network
+        n_output : dimension of the observed data
+        """
+        self.soft_zero = 1e-10
+        self.n_latent = n_latent
+        self.output = None
+        self.mu = None
+        super(VAE, self).__init__(**kwargs)
+        self.encoder = nn.HybridSequential()
+        for _ in range(n_layers):
+            self.encoder.add(nn.Dense(n_hidden, activation=act_type))
+        self.encoder.add(nn.Dense(n_latent*2, activation=None))
+        self.decoder = nn.HybridSequential()
+        for _ in range(n_layers):
+            self.decoder.add(nn.Dense(n_hidden, activation=act_type))
+        self.decoder.add(nn.Dense(n_output, activation='sigmoid'))
+        
+    def encode(self, x):
+        r"""
+        Given a batch of x,
+        return the encoder's output
+        """
+        h = self.encoder(x)
+        loc_scale = np.split(h, 2, 1)

Review comment:
       add comment




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] szha edited a comment on pull request #18758: [WIP] [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
szha edited a comment on pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758#issuecomment-662183511


   ~not yet. I think we should provide the automation in CI first, which may take some time. in the meantime, I think we can check in the jupyter notebook for now with the expectation that it will be replaced by notedown in the future.~


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] szha commented on pull request #18758: [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
szha commented on pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758#issuecomment-671745759


   Thanks for the demo, @xidulu!


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-mxnet] szha commented on pull request #18758: [Gluon] Add VAE demo

Posted by GitBox <gi...@apache.org>.
szha commented on pull request #18758:
URL: https://github.com/apache/incubator-mxnet/pull/18758#issuecomment-671681748


   I'm ok with merging.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org