淘先锋技术网

首页 1 2 3 4 5 6 7

1.用CrossEntropyLoss预测单个目标

loss = nn.CrossEntropyLoss()      #实例化交叉熵损失函数
Y = torch.tensor([0])     #预测是第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)   #计算loss
l2 = loss(Y_pred_bad, Y)
print(f'Pytorch Loss1:{l1.item():.4f}')   #小数点后保留4位
print(f'Pytorch Loss2:{l2.item():.4f}')
_, predictions1 = torch.max(Y_pred_good, 1)
_, predictions2 = torch.max(Y_pred_bad, 1)

在这里插入图片描述

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}')

在这里插入图片描述

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)    #二分类最后输出单元个数为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()