栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Pytorch torch.add() torch.add

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pytorch torch.add() torch.add

简介

torch.add():对两个张量进行相加,若格式不同则以复制的方式进行扩容后再相加。
以 “下划线 _” 结尾的 ,均为in-place。
可以简单理解为:修改了对应变量中的数值。
Torch里面所有带 “下划线 ” 的操作,都是in-place的。
torch.add
():对两个张量进行相加,但格式需相同。

实例代码
x = torch.arange(1., 6.)
print(x)
print(torch.topk(x, 3))

a = torch.randn(4)
print(a)
b = torch.randn(4, 1)
print(b)
# alpha * b + a, 维度不够的地方自动扩容
print(torch.add(a, b, alpha=10))

print("p, q:")
p = torch.randn(4)
print(p)
q = torch.randn(4)
print(q)
p.add(q, alpha=10)
print(p)
p.add_(q, alpha=10)
print(p)
运行结果
tensor([1., 2., 3., 4., 5.])
torch.return_types.topk(
values=tensor([5., 4., 3.]),
indices=tensor([4, 3, 2]))
tensor([ 0.4467, -1.0670, -0.2640, -0.9347])
tensor([[ 1.4514],
        [-1.9113],
        [-0.3101],
        [-0.3147]])
tensor([[ 14.9611,  13.4474,  14.2505,  13.5797],
        [-18.6661, -20.1797, -19.3767, -20.0474],
        [ -2.6547,  -4.1683,  -3.3653,  -4.0360],
        [ -2.7007,  -4.2144,  -3.4114,  -4.0821]])
p, q:
tensor([ 1.6037,  1.4193, -0.4076,  1.3390])
tensor([0.2374, 1.1236, 0.8439, 0.7530])
tensor([ 1.6037,  1.4193, -0.4076,  1.3390])
tensor([ 3.9780, 12.6551,  8.0318,  8.8687])
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1025977.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号