1.用CrossEntropyLoss预测单个目标
loss = nn.CrossEntropyLoss()
Y = torch.tensor([0])
Y_pred_good = torch.tensor([[2.0, 1.0, 0.1]])
Y_pred_bad = torch.tensor([[0.5, 2.0, 0.3]])
l1 = loss(Y_pred_good, Y)
l2 = loss(Y_pred_bad, Y)
print(f'Pytorch Loss1:{l1.item():.4f}')
print(f'Pytorch Loss2:{l2.item():.4f}')
_, predictions1 = torch.max(Y_pred_good, 1)
_, predictions2 = torch.max(Y_pred_bad, 1)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/45f4151f558814c9bbc3a88cd159fb76.png)
2.用CrossEntropyLoss预测多个目标
Y = torch.tensor([2,0,1])
Y_pred_good = torch.tensor(
[[0.1, 0.2, 3.9],
[1.2, 0.1, 0.3],
[0.3, 2.2, 0.2]])
Y_pred_bad = torch.tensor(
[[0.9, 0.2, 0.1],
[0.1, 0.3, 1.5],
[1.2, 0.2, 0.5]])
l1 = loss(Y_pred_good, Y)
l2 = loss(Y_pred_bad, Y)
print(f'Batch Loss1: {l1.item():.4f}')
print(f'Batch Loss2:{l2.item():.4f}')
_, predictions1 = torch.max(Y_pred_good, 1)
_, predictions2 = torch.max(Y_pred_bad, 1)
print(f'Actual class:{Y}, Y_pred1:{predictions1}, Y_pred2:{predictions2}')
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6ebec778118f4174709f5b885b5687f4.png)
3.二分类使用BCELoss损失函数
class NeuralNet1(nn.Module):
def __init__(self, input_size, hidden_size):
super(NeuralNet1, self).__init__()
self.linear1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.linear2 = nn.Linear(hidden_size, 1)
def forward(self, x):
out = self.linear1(x)
out = self.relu(out)
out = self.linear2(out)
y_pred = torch.sigmoid(out)
return y_pred
model = NeuralNet1(input_size=28*28, hidden_size=5)
criterion = nn.BCELoss()
4.多分类使用CrossEntropyLoss损失函数
class NeuralNet2(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(NeuralNet2, self).__init__()
self.linear1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.linear2 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
out = self.linear1(x)
out = self.relu(out)
out = self.linear2(out)
return out
model = NeuralNet2(input_size=28*28, hidden_size=5, num_classes=3)
criterion = nn.CrossEntropyLoss()