zackchase / mxnet-the-straight-dope

An interactive book on deep learning. Much easy, so MXNet. Wow. [Straight Dope is growing up] ---> Much of this content has been incorporated into the new Dive into Deep Learning Book available at https://d2l.ai/.
https://d2l.ai/
Apache License 2.0
2.56k stars 727 forks source link

Shape inconsistent error while using SoftmaxCrossEntropyLoss #560

Closed sharmalakshay93 closed 5 years ago

sharmalakshay93 commented 5 years ago

Hi, I'm following this tutorial. Running

L = gluon.loss.SoftmaxCrossEntropyLoss()
loss = L(output, labels)

results in the following exception:

mxnet.base.MXNetError: Shape inconsistent, Provided = [100,2], inferred shape=[100,1]

The shapes of the labels and output seem consistent:

labels.shape:  (100, 2)
output.shape:  (100, 2)

The model is defined as follows:

model = mx.gluon.nn.Sequential()
with model.name_scope():
    model.add(mx.gluon.rnn.LSTM(256, dropout=0.2, layout='NTC'))
    model.add(mx.gluon.rnn.LSTM(128, dropout=0.2, layout='NTC'))
    model.add(mx.gluon.nn.Dense(2, flatten=True))

Any clues on what I might be doing incorrectly here? Thanks!

sharmalakshay93 commented 5 years ago

Solved by following this