import numpy as np
import torch
import torch.nn as nn
def cross_entropy_error(y, t):
delta = 1e-7 # 添加一个微小值可以防止负无限大(np.log(0))的发生。
return -np.sum(t * np.log(y + delta))
input = torch.Tensor([[0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]])
target = torch.tensor([[0.0, 0, 1, 0, 0, 0, 0, 0, 0, 0]])
loss = nn.BCELoss(reduction='none') # pytorch
lossinput = torch.sigmoid(input) # 使用BCELoss前,要用sigmoid
output = loss(lossinput, target)
print("nn.BCELoss计算loss的结果:", output)
print() # pytorch
print("cross_entropy_error计算的loss:")
print(cross_entropy_error(lossinput.data.numpy(), target.data.numpy())) # numpy