python logsumexp示例

x33g5p2x  于2022-02-18 转载在 Python  
字(0.8k)|赞(0)|评价(0)|浏览(358)
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)

相关文章