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)赋值操作