pytorch 和python代码:
import numpy as np
import torch as t
te = t.tensor([[-1.0000e+12, -1.0000e+02, 0.0000e+00],
[-2.0000e+00, -1.0000e+12, 0.0000e+00]])
print(te) # [2,3]
logumsexp0= t.logsumexp(te,dim=0) # 两行之间列分别计算log (sum(...)) 值
print(logumsexp0)
res = t.logsumexp(te,dim=1) # 一般都是最后一个维度计算
print(res)
res = t.sum(res)
print(res)
res_0=np.log(np.sum(np.exp(te.numpy()[0])))
res_1=np.log(np.sum(np.exp(te.numpy()[1])))
print(res_0)
print(res_1)
如果数据改为:
import numpy as np
import torch as t
te = t.tensor([[-1000, -1000, 1000],
[-2.0000e+00, -1.0000e+12, 0.0000e+00]])
res_0=np.log(np.sum(np.exp(te.numpy()[0])))
这个代码会报错:
RuntimeWarning: overflow encountered in exp
这个不报错:
import numpy as np
import torch as t
from scipy.special import logsumexp
sci_0= logsumexp(te.numpy()[0])
print("sci_0",sci_0)
sci_0= logsumexp(te.numpy()[1])
print("sci_1",sci_0)
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/jacke121/article/details/122994935
内容来源于网络,如有侵权,请联系作者删除!