0

I am new to mxnet, I am trying to do this code:

from mxnet import nd, sym
from mxnet.gluon import nn

class HybridNet(nn.HybridBlock):
    def __init__(self, **kwargs):
         super(HybridNet, self).__init__(**kwargs)
         self.hidden = nn.Dense(10)
         self.output = nn.Dense(2)

    def hybrid_forward(self, F, x):
         print('F: ', F)
         print('x: ', x.shape)

         x = F.relu(self.hidden(x))
         print('hidden: ', x.shape)

         x = F.relu(self.hidden(x))
         print('hidden: ', x.shape)

    return self.output(x)


    net = HybridNet()

    net.initialize()

    x = nd.random.normal(shape=(1, 4))
    net(x)

however, it got this error: MXNetError: Shape inconsistent, Provided = [10,4], inferred shape=(10,10)

but if I change self.hidden = nn.Dense(10) to self.hidden = nn.Dense(4), the error would not exist any more. but I cannot understand why, anyone could explain this to me? thank you

chengjun zhang
  • 101
  • 1
  • 1
  • 9

1 Answers1

0

The problem is that you reusing the same hidden layer twice with a different size of the input.

  1. When you call x = F.relu(self.hidden(x)) for the first time, hidden layer automatically finds that your input size is 4 because of your input: x = nd.random.normal(shape=(1, 4)).
  2. It outputs NDArray of size 10, (because of 10 neurons).
  3. Then you use the hidden layer again, but now with the input of size 10... The layer is already initialized and expects to receive an input of size 4, but gets size 10, so it fails.

To fix that, introduce another hidden layer of any arbitrary size:

from mxnet import nd, sym
from mxnet.gluon import nn


class HybridNet(nn.HybridBlock):
    def __init__(self, **kwargs):
        super(HybridNet, self).__init__(**kwargs)
        self.hidden1 = nn.Dense(10)
        self.hidden2 = nn.Dense(20)
        self.output = nn.Dense(2)

    def hybrid_forward(self, F, x):
        print('F: ', F)
        print('x: ', x.shape)

        x = F.relu(self.hidden1(x))
        print('hidden: ', x.shape)

        x = F.relu(self.hidden2(x))
        print('hidden: ', x.shape)

        return self.output(x)


net = HybridNet()
net.initialize()

x = nd.random.normal(shape=(1, 4))
net(x)

Sergei
  • 1,617
  • 15
  • 31