import torch
import numpy as np
import torch.nn.functional as F
import math
from functools import reduce
exp = math.exp
log = math.log
#设置实验数据
input = torch.tensor([[2.5, -0.5, 0.1],
[-1.1, 2.5, 0.0],
[1.2, 2.2, 3.1]], dtype=torch.float)
labels = torch.tensor([1, 0, 2], dtype=torch.long)
#python实现log_softmax计算
def log_softmax(p):
m,n = p.size()
sm_arr = []
for i in range(m):
exp_xlist = list(map(lambda x: exp(x),p[i]))
tmp_sum = reduce(lambda x,y:x+y,exp_xlist)
tmp_list = [log(x/tmp_sum) for x in exp_xlist]
tmp_arr = np.array(tmp_list)
sm_arr.append(tmp_arr)
sm_arr = np.array(sm_arr)
return sm_arr
#方法1 先调用自定义log_softmax函数计算softmax,再调用nll_loss函数计算损失;
#(1.1) 先采用自定义log_softmax函数计算softmax
ety = log_softmax(input)
ety = torch.from_numpy(ety).to(torch.float32)
探究negative log likelihood loss(nll_loss)、cross_entropy与one hot编码
最新推荐文章于 2024-06-09 09:57:53 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)