1

Coming from Keras, I try to reproduce my simple model with MXNet to make prediction using Module.

I'm using that simple dataset: https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data

I've got 13 inputs (from alcohol to Proline) that I want to send to the model, and I need to classify the first column that is "wine type", so I create a nd.array that have 3 entries.


x = data.values[: , 1:14]
y = data.values[:, 0]

X = mx.nd.array(x)
Y = []
for i, v in enumerate(y):
    d = [0,0,0]
    d[int(v)-1] = 1
    Y.append(d)
Y = mx.nd.array(Y)
Y.shape, X.shape
# ((178, 3), (178, 13))

Then I create the model and a NDIterator:


net = mx.symbol.Variable('winechemical')
net = mx.symbol.FullyConnected(net, num_hidden=64)
net = mx.symbol.Activation(net, act_type='relu')
net = mx.symbol.FullyConnected(net, num_hidden=32)
net = mx.symbol.Activation(net, act_type='relu')
net = mx.symbol.FullyConnected(net, num_hidden=16)
net = mx.symbol.SoftmaxOutput(net, name='wineclass')

model = Module(symbol=net, context=mx.cpu(),
                  data_names=['winechemical'],
                  label_names=['wineclass_label'])

gen = mx.io.NDArrayIter(X, label=Y, 
                        batch_size=10, 
                        shuffle=True, data_name='winechemical', 
                        label_name='wineclass_label')

But when I try to "train" the model using the "fit" method, I got this error:

model.fit(gen, num_epoch=5)

[...]
Error in operator wineclass: Shape inconsistent, Provided = [10,3], inferred shape=[10]

I'm pretty sure that I don't understand the shape to uses as I'm coming from Keras that use different shape... But where am I wrong ?

Thanks for your help.

Metal3d
  • 2,905
  • 1
  • 23
  • 29

2 Answers2

1

You already found the solution yourself. But in case you run into a similar problem again, you could use mx.visualization.print_summary() This function is very useful to inspect the shapes of the different layers in a model.

NRauschmayr
  • 131
  • 1
  • Yes that was what I did. I didn't realize that I made a mistake by setting 16 outputs instead of 3. I already answered myself the question :) but thanks a lot – Metal3d Dec 04 '18 at 16:57
0

My god, sorry... I didn't see that I let 16 outputs instead of 3...

Metal3d
  • 2,905
  • 1
  • 23
  • 29