resolvent
crossentropyloss (predicted value, label)
the required input dimensions are:
-
- when there is batch, the predicted value dimension is 2 and the size is
[batch]_ When size, n]
-
- , the dimension of label is 1, and the size is
[batch]_ Size]
-
- when there is no batch, the dimension of the predicted value is 2, the size is
[M, n]
-
- , the dimension of the label is 1, and the size is
[M]
Problem analysis
One case can illustrate:
import torch
import torch.nn as nn
import numpy as np
a = torch.tensor(np.random.random((30, 5)))
b = torch.tensor(np.random.randint(0, 4, (30))).long()
loss = nn.CrossEntropyLoss()
print("a的维度:", a.size()) # torch.Size([30, 5])
print("b的维度:", b.size()) # torch.Size([30])
print(loss(a, b)) # tensor(1.6319, dtype=torch.float64)