欢迎光临散文网 会员登陆 & 注册

PyTorch Tutorial 11 - Softmax and Cro...

2023-02-16 17:08 作者:Mr-南乔  | 我要投稿

教程Python代码如下:


import torch

import torch.nn as nn

import numpy as np


"""softmax把分类输出标准化成概率分布,cross-entropy(交叉熵)刻画预测分类和真实结果之间的相似度"""

def softmax(x):

  return np.exp(x) / np.sum(np.exp(x), axis=0)


x = np.array([2.0, 1.0, 0.1])

outputs = softmax(x)

print('softmax numpy:', outputs)


loss = nn.CrossEntropyLoss()


# 3 samples

Y = torch.tensor([2, 0, 1])


# nsamples x nclasses = 3*3

Y_pred_good = torch.tensor([[0.1, 1.0, 2.1], [2.0, 1.0, 0.1], [0.1, 3.0, 0.1]])

Y_pred_bad = torch.tensor([[2.1, 1.0, 0.1], [0.1, 1.0, 2.1], [0.1, 3.0, 0.1]])


l1 = loss(Y_pred_good, Y)

l2 = loss(Y_pred_bad, Y)


print(l1.item())

print(l2.item())


_, predictions1 = torch.max(Y_pred_good, 1)

_, predictions2 = torch.max(Y_pred_bad, 1)

print(predictions1)

print(predictions2)

PyTorch Tutorial 11 - Softmax and Cro...的评论 (共 条)

分享到微博请遵守国家法律