[英]Pytorch - nn.CrossEntropyLoss
I want to apply nn.CrossEntropyLoss我想申请 nn.CrossEntropyLoss
output = model(data)
output.shape -> 1,30,7 (batch, frame, class)
label.shape -> 1,30 (batch, frame)
In this case,在这种情况下,
label = label.squeeze(0)
output = output.squeeze(0)
criterion = nn.CrossEntropyLoss()
loss = criterion(outputs, targets)
can I solve this?我能解决这个问题吗?
But if the batch size is 2但如果批量大小为 2
output.shape is 2,30,7 and label.shape is 2, 30 output.shape 是 2,30,7 和 label.shape 是 2, 30
How to apply loss = criterion(outputs, targets)
如何应用loss = criterion(outputs, targets)
The loss function nn.CrossEntropyLoss
can be applied to multi-dim predictions:损失 function nn.CrossEntropyLoss
可以应用于多维预测:
All you need is to make sure your C
dimension (7 in your case) is the second:您所需要的只是确保您的C
维度(在您的情况下为 7)是第二个:
output = output.transpose(1, 2) # B,30,7 -> B,7,30
loss = criterion(outputs, targets)
You do not need to change targets
at all.您根本不需要更改targets
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.