21 篇文章 3 订阅

# 1.用CrossEntropyLoss预测单个目标

loss = nn.CrossEntropyLoss()      #实例化交叉熵损失函数
Y = torch.tensor([0])     #预测是第0个
Y_pred_good = torch.tensor([[2.0, 1.0, 0.1]])
l1 = loss(Y_pred_good, Y)   #计算loss
print(f'Pytorch Loss1:{l1.item():.4f}')   #小数点后保留4位
print(f'Pytorch Loss2:{l2.item():.4f}')

_, predictions1 = torch.max(Y_pred_good, 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]])
[[0.9, 0.2, 0.1],
[0.1, 0.3, 1.5],
[1.2, 0.2, 0.5]])

l1 = loss(Y_pred_good, Y)
print(f'Batch Loss1: {l1.item():.4f}')
print(f'Batch Loss2:{l2.item():.4f}')

_, predictions1 = torch.max(Y_pred_good, 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()

• 4
点赞
• 10
收藏
觉得还不错? 一键收藏
• 打赏
• 1
评论
04-13 482

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。