I have a tensor
a of shape
(1, N, 1). I need to left shift the tensor along dimension
1 and add a new value as replacement. I have found a way to make this work and following is the code.
a = torch.from_numpy(np.array([1, 2, 3])) a = a.unsqueeze(0).unsqeeze(2) # (1, 3, 1), my data resembles this shape, therefore the two unsqueeze # want to left shift a along dim 1 and insert a new value at the end # I achieve the required shifts using the following code b = a.squeeze c = b.roll(shifts=-1) c[-1] = 4 c = c.unsqueeze(0).unsqueeze(2) # c = [[, , ]]
My question is, is there a simpler way to do this? Thanks.