TORCH.ADDCDIV
torch.addcdiv(input, tensor1, tensor2, *, value=1, out=None) -> Tensor
# input + value * tensor1 / tensor2
# input, tensor1, tensor2需要是可广播的tensor
t = torch.randn(1, 3)
t1 = torch.randn(1, 3)
t2 = torch.randn(3, 1)
torch.addcdiv(t, t1, t2, value=0.1)
tensor([[-1.5568, 2.7648, -0.1749],
[-1.4023, 2.7232, -0.1686],
[-1.6208, 2.7821, -0.1775]])
原地修改
Tensor.addcdiv_(tensor1, tensor2, *, value=1) -> Tensor
# 注意:原地修改input应该与最终结果的shape一致
>>>t = torch.randn(3, 3)
>>>t1 = torch.randn(1, 3)
>>>t2 = torch.randn(3, 1)
>>>torch.addcdiv(t, t1, t2, value=0.1)
tensor([[-0.0716, 0.0674, 0.1288],
[-0.8716, 1.0473, -1.9716],
[ 0.5501, 0.0090, 1.3449]])
>>>t.addcdiv_(t1, t2, value=0.1)
tensor([[-0.0716, 0.0674, 0.1288],
[-0.8716, 1.0473, -1.9716],
[ 0.5501, 0.0090, 1.3449]])
>>>t
tensor([[-0.0716, 0.0674, 0.1288],
[-0.8716, 1.0473, -1.9716],
[ 0.5501, 0.0090, 1.3449]])
最后,tensor.data
也可以完成与上述类似的操作,例如tensor.data.addcdiv_