PyTorch Tutorial 11 - Softmax and Cro...

教程Python代碼如下:
import torch
import torch.nn as nn
import numpy as np
"""softmax把分類(lèi)輸出標(biāo)準(zhǔn)化成概率分布,cross-entropy(交叉熵)刻畫(huà)預(yù)測(cè)分類(lèi)和真實(shí)結(jié)果之間的相似度"""
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)