mxnet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] thomelane commented on a change in pull request #10607: New tutorial on how to create a new custom layer in Gluon
Date Thu, 19 Apr 2018 17:19:11 GMT
thomelane commented on a change in pull request #10607: New tutorial on how to create a new
custom layer in Gluon
URL: https://github.com/apache/incubator-mxnet/pull/10607#discussion_r182607051
 
 

 ##########
 File path: docs/tutorials/python/custom_layer.md
 ##########
 @@ -0,0 +1,247 @@
+
+# How to write a custom layer in Apache MxNet Gluon API
+
+While Gluon API for Apache MxNet comes with [a decent number of predefined layers](https://mxnet.incubator.apache.org/api/python/gluon/nn.html),
at some point one may find that a new layer is needed. Adding a new layer in Gluon API is
straightforward, yet there are a few things that one needs to keep in mind.
+
+In this article, I will cover how to create a new layer from scratch, how to use it, what
are possible pitfalls and how to avoid them.
+
+## The simplest custom layer
+
+To create a new layer in Gluon API, one must create a class that inherits from [Block](https://mxnet.incubator.apache.org/api/python/gluon/gluon.html#mxnet.gluon.Block)
class. This class provides the most basic functionality, and all predefined layers inherit
from it directly or via other subclasses. Because each layer in Apache MxNet inherits from
`Block`, words "layer" and "block" are used interchangeably inside of the Apache MxNet community.
+
+The only instance method needed to be implemented is [forward()](https://mxnet.incubator.apache.org/api/python/gluon/gluon.html#mxnet.gluon.Block.forward),
which defines what exactly your layer is going to do during forward propagation. Notice, that
it doesn't require to provide what the block should do during backpropagation. Backpropagation
pass for blocks is done by Apache MxNet for you. 
+
+In the example below, we define a new layer and implement `forward()` method to normalize
input data by fitting it into a range of [0, 1].
+
+
+```python
+# Do some initial imports used throughout this tutorial 
+from __future__ import print_function
+import mxnet as mx
+from mxnet import nd, gluon, autograd
+from mxnet.gluon.nn import Dense
+mx.random.seed(1)                      # Set seed for reproducable results
+```
+
+
+```python
+class NormalizationLayer(gluon.Block):
+    def __init__(self):
+        super(NormalizationLayer, self).__init__()
+
+    def forward(self, x):
+        return (x - nd.min(x)) / (nd.max(x) - nd.min(x))
+```
+
+The rest of methods of the `Block` class are already implemented, and majority of them are
used to work with parameters of a block. There is one very special method named [hybridize()](https://mxnet.incubator.apache.org/api/python/gluon/gluon.html#mxnet.gluon.Block.hybridize),
though, which I am going to cover before moving to a more complex example of a custom layer.
 
 Review comment:
   There is one very special method named hybridize(), though, which I am going to cover before
moving to a more complex example of a custom layer. -> We will now discuss a special method
called hybridize() before moving on to more complex examples of custom layers.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


With regards,
Apache Git Services

Mime
View raw message