Issues with Softmax Cross Entropy Loss inferred shape

I have a simple RNN with BATCH x SEQUENCE x FEATURES as input data shape.
My loss is the softmax cross entropy loss and my output as well as my label is [32, 2] (BATCH x FEATURES) but i always get “Shape inconsistent, Provided [32, 2], inferred shape [32, 1]”. I have 2 classes and my labels are all [0, 1] or [1, 0].
The documentation says that the “label‘s shape should be pred‘s shape”, so what did I do wrong?

		x = x.as_in_context(ctx)
		print(x.shape) # 32, 10, 15
		y = y.as_in_context(ctx)
		print(y.shape) # 32, 2

		output = net(x)
		print(output.shape) # 32, 2

		loss = l_cross_entropy(output, y)

Hi,

I haven’t used this directly myself, but from reading the documentation [1] and issue tracker [2] I understand that - in order to use one-hot encoded labels - you need to create the Loss class as follows:
gluon.loss.SoftmaxCrossEntropyLoss(sparse_label=False)

hth,

Lieven

[1] https://mxnet.incubator.apache.org/api/python/gluon/loss.html?highlight=softmaxcrossentropyloss#mxnet.gluon.loss.SoftmaxCrossEntropyLoss
[2] https://github.com/apache/incubator-mxnet/issues/8350

1 Like

Thank you. I should have read the documentation more precisely.