Search code examples
pythonpytorchtensor

How can I convert a tensor with the shape of [1, 3, 64, 64] to [1, 4, 64, 64] with the newly added layer being the same as the previous?


I have a PyTorch tensor with the shape of [1, 3, 64, 64], and I want to convert it to the shape [1, 4, 64, 64] while setting the value of the newly added layer to be the same as the previous layer in the same dimension (eg newtensor[0][3] = oldtensor[0][2])

Note that my tensor has requires_grad=True, so I cannot use resize_()

How can I do this?


Solution

  • Get a slice from the old tensor, and concatenate it to the new tensor along dimension 1.

    tslice = old[:,-1:,:,:]
    new = torch.cat((old,tslice), dim = 1)