如何在pytorch中将张量插入另一个张量

时间:2018-11-22 21:15:27

标签: pytorch

我有形状为conda config --add channels conda-forge 的pytorch张量,例如(batch_size, step, vec_size),我们称它为A。

我还有另一个Tensor(32, 64, 128),例如Tensor(batch_size, vec_size),我们称之为B。

我想将B插入到A轴1的某个位置。插入位置在Tensor(32, 128)中给出,命名为P。

我知道pytorch中没有空张量(像一个空列表),因此,我将A初始化为零,然后在A轴1的某个位置添加B。

Tensor(batch_size)

我在做什么就像

A = Variable(torch.zeros(batch_size, step, vec_size))

但是我得到一个错误:

for i in range(batch_size):
    pos = P[i]
    A[i][pos] = A[i][pos] + B[i]

然后,我在循环内各复制一个A:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

这对于autograd来说非常慢,我想知道是否有更好的解决方案?谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用掩码而不是克隆。

请参见下面的代码

# setup
batch, step, vec_size = 64, 10, 128 
A = torch.rand((batch, step, vec_size))
B = torch.rand((batch, vec_size))
pos = torch.randint(10, (64,)).long()

# computations
# create a mask where pos is 0 if it is to be replaced
mask = torch.ones( (batch, step)).view(batch,step,1).float()
mask[torch.arange(batch), pos]=0

# expand B to have same dimension as A and compute the result
result = A*mask + B.unsqueeze(dim=1).expand([-1, step, -1])*(1-mask)

这样可以避免使用for循环和克隆。

相关问题