Reputation: 31
When i using the cross-entropy loss as a loss function,i get this dimension out of range error.
This is my code:
self.ce = nn.CrossEntropyLoss()
def forward(self, pred, y):
loss = 0
for w_, p_, y_ in zip(self.weights, pred, y):
loss += w_ * self.ce(p_, y_)
return loss
when i run this code :
the value of p_:tensor(1.00000e-02 *[-0.7625, 5.8737], device='cuda:0')
the value of w_:tensor(1., device='cuda:0')
the value of y_:tensor(0, device='cuda:0')
Upvotes: 1
Views: 2711
Reputation: 5162
For cross entropy there should be the same number of labels as predictions.
In your specific case the dimensions of y_ and p_ should match which they don't as y_ is a 0 dimensional scalar and p_ is 1x2.
Upvotes: 1