Issue
I want to use the Crossentropyloss of pytorch but somehow my code only works with batchsize 2, so i am asuming there is something wrong with the shapes of target and output. I get following error:
Value Error: Expected target size (50, 2), got torch.Size([50, 3])
My targetsize is (N=50,batchsize=3)
and the output of my model is (N=50, batchsize=3, number of classes =2)
. Before the output layer my shape is (N=50,batchsize=3,dimensions=64).
How do i need to change the shapes so that the Crossentropyloss works?
Solution
Without further information about your model, here's what I would do. You have a many-to-many RNN which outputs (seq_len, batch_size, nb_classes)
and the target is (seq_len, seq_len)
. The nn.CrossEntropyLoss
module can take additional dimensions (batch_size, nb_classes, d1, d2, ..., dK)
as an input.
You could make it work by permuting the axes, such that the outputted tensor is of shape (batch_size, nb_classes, seq_len)
. This should make it happen:
output = output.permute(0, 2, 1)
Additionally, your target will also have to change to be (batch_size, seq_len)
:
target = target.permute(1, 0)
Answered By - Ivan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.