pytorch函数中的out参数

pytorch函数中的out参数


引自: https://discuss.pytorch.org/t/whats-the-benefit-of-using-out-parameter-in-tensor-operations/31728

在pytorch中 out 参数指的是 把** 函数得到的张量结果**赋值给out 这个参数输入的张量
例如

import torch

a = torch.randn(2, 3)
print(a)

b = torch.randn(2, 3)

c = torch.add(a, b, out=a)
print(c)    
print(a)

输出结果为

tensor([[ 0.8782, -0.7053, -0.4344],
        [-0.6081, -0.0034, -2.6207]])
tensor([[ 0.9943, -1.4616,  0.4937],
        [ 0.5885,  1.0083, -2.1066]])
tensor([[ 0.9943, -1.4616,  0.4937],
        [ 0.5885,  1.0083, -2.1066]])

当第二次输出a的结果时,a已经被add()重新赋值。使用out = a,相当于执行了操作
a = add(a, b)赋值操作

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值