CrossEntropyLoss

import numpy as np

  y = np.array([1, 0, 0])
  z = np.array([0.2, 0.1, -0.1])
  y_pred = np.exp(z) / np.exp(z).sum()
  loss = (-y * np.log(y_pred)).sum()
  print(loss)

import torch

y = torch.LongTensor([0])
z = torch.Tensor([[0.2, 0.1, -0.1]])
criterion = torch.nn.CrossEntropyLoss()
loss = criterion(z, y)
print(loss)

CrossEntropyLoss <==> LogSoftmax + NLLLoss

  

posted @ 2020-09-24 22:03  OliYoung  阅读(154)  评论(0)    收藏  举报