Cross Entropy Loss for a Sequence (Time Series) of Output?

The output of my network is a tensor of size torch.Size([time_steps, 20, 29]).

20 is the batch size, and 29 is the number of classes. time_steps is variable and depends on the input.

My targets has the form torch.Size([time_steps, 20]).

How can I calculate the loss using nn.CrossEntropyLoss function?

It should be noticed that the loss should be the sum of the loss of time_steps cross entropy loss operations.

1 Like
output = output.view(-1,29)
target = target.view(-1)
criterion = nn.CrossEntropyLoss(29)
loss = criterion(output,target)
2 Likes

Why do I need pass 29 to CrossEntropyLoss?

Could I just use nn.CrossEntropyLoss()?

For pytorch 3,this worked fine for me

output = output.view(-1,29)
target = target.view(-1)
criterion = nn.CrossEntropyLoss()
loss = criterion(output,target)