MLP Example from "Straight Dope" Throwing NotImplementedError

Hi All!

I’m running the Multilayer Perceptron example from “Gluon, The Straight Dope”, and as soon as I get to the part where I send data to the network, I’m getting a NotImplementedError, which strikes me as incredibly odd.

I’ve installed mxnet and mxnet version 1.1.0 using pip.

You can see the network in question at the link, but here’s the relevant code:

class MLP(gluon.Block):
    def __init__(self, **kwargs):
        super(MLP, self).__init__(**kwargs)
        with self.name_scope():
            self.dense0 = gluon.nn.Dense(64)
            self.dense1 = gluon.nn.Dense(64)
            self.dense2 = gluon.nn.Dense(10)
            
        def forward(self, x):
            x = nd.relu(self.dense0(x))
            x = nd.relu(self.dense1(x))
            x = self.dense2(x)
            return x

net = MLP()
net.collect_params().initialize(mx.init.Normal(sigma=.01), ctx=model_ctx)

data = nd.ones((1, 784))
net(data.as_in_context(model_ctx))

And the stacktrace is:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-5-b3e69bb45f2b> in <module>()
      1 data = nd.ones((1, 784))
----> 2 net(data.as_in_context(model_ctx))

.../site-packages/mxnet/gluon/block.py in __call__(self, *args)
    358     def __call__(self, *args):
    359         """Calls forward. Only accepts positional arguments."""
--> 360         return self.forward(*args)
    361 
    362     def forward(self, *args):

.../site-packages/mxnet/gluon/block.py in forward(self, *args)
    370         """
    371         # pylint: disable= invalid-name
--> 372         raise NotImplementedError
    373 
    374 

NotImplementedError: 

If your code looks exactly like the above, you have extra tabs in the forward() function. Basically you’ve put forward() under __init__() rather than it being a function of MLP class.

1 Like