How to insert a value in a fixed positon of pytorch

Viewed 64

I have a PyTorch tensor

x = [[1,2,3,4,5]]

Now I want to add a value to a fixed position of the tensor x, for example, I want to add 11 in position 3 then the x will be

x= [[1,2,3,11,4,5]]

How can I perform this operation in Pytorch?

1 Answers

Dynamically extending arrays to arbitrary sizes along the non-singleton dimensions, such as the ones you mentioned, are unsupported in PyTorch mainly because the memory is pre-allocated during tensor construction and set to fixed size depending on the data type. The only way to grow non-singleton dimension size is to create a new (empty/zero) tensor with the target shape and insert values at the desired position(s), while also copying values.

In [24]: z = torch.zeros(1, 6)
In [27]: t
Out[27]: tensor([[1, 2, 3, 4, 5]])

In [30]: z[:, :3] = t[:, :3]
In [33]: z[:, -2:] = t[:, -2:]
In [36]: z[z == 0] = 11

In [37]: z
Out[37]: tensor([[ 1.,  2.,  3., 11.,  4.,  5.]])

However, if you'd have instead wanted to expand the tensor along the singleton dimension, then that's easy to achieve using tensor.expand(new_shape). In the below example, we expand the tensor t to length 3 along the 0th dimension, which is originally a singleton dimension.

# make a copy for in-place modification since `expand()` returns a view
In [64]: t_expd = t.expand(3, -1).clone()

In [65]: t_expd
Out[65]: 
tensor([[1, 2, 3, 4, 5],
        [1, 2, 3, 4, 5],
        [1, 2, 3, 4, 5]])

# modify 2nd and 3rd rows
In [66]: t_expd[1:, ...] = 23

In [67]: t_expd
Out[67]: 
tensor([[ 1,  2,  3,  4,  5],
        [23, 23, 23, 23, 23],
        [23, 23, 23, 23, 23]])
Related