python logsumexp示例
【摘要】
pytorch 和python代码:
import numpy as npimport torch as tte = t.tensor([[-1.0000e+12, -1.0000e+02, 0.0000e+00], [-2.0000e+00, -1.0000e+12, 0.0000e+00]])...
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)
文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/122994935
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)