import torch
import torch.nn as nn
import torch.nn.functional as F
# Example of target with class indices
input = torch.randn(3, 5)
print(f"Input is {input}")
t = torch.tensor([1,1,2])
s = nn.Softmax(dim=1)
print(f"The softmax result is {s(input)}")
l = torch.log(s(input))
print(f"softmax + Log result is {l}")
ls = nn.LogSoftmax(dim=1)
print(f"The logsoftmax is {ls(input)}")
nl = F.nll_loss(l,t)
print(f"The softmax + log + nllloss is {nl}")
ce = F.cross_entropy(input,t)
print(f"The cross entropy result is {ce}")
softmax + log = logsoftmax, logsoftmax+ nllloss= crossentropy
最新推荐文章于 2024-07-31 16:48:31 发布