Search code examples
arraysmultidimensional-arraypytorchtensortorch

tensor transformation in pytorch?


I have a tensor of shape (size, 1) and I want to convert it into of shape (size, lookback, 1) by shifting its values. A pandas equivalent is below

size = 7
lookback = 3

data = pd.DataFrame(np.arange(size), columns=['out'])  # input
y = np.full((len(data), lookback, 1), np.nan)          # required/output
for j in range(lookback):
    y[:, j, 0] = data['out'].shift(lookback - j - 1).fillna(method="bfill")

How can I acheive similar in pytorch?

Example input:

[0, 1, 2, 3, 4, 5, 6]

Desired output:

[[0. 0. 0.]
 [0. 0. 1.]
 [0. 1. 2.]
 [1. 2. 3.]
 [2. 3. 4.]
 [3. 4. 5.]
 [4. 5. 6.]]

Solution

  • You can use Tensor.unfold for this. First though you will need to pad the front of the tensor, for that you could use nn.functional.pad. E.g.

    import torch
    import torch.nn.functional as F
    
    size = 7
    loopback = 3
    
    data = torch.arange(size, dtype=torch.float)
    
    # pad front of data with 2 values
    # replicate padding requires 3d, 4d, or 5d tensor, hence the creation of two unitary dimensions before padding
    data_padded = F.pad(data[None, None, ...], (loopback - 1, 0), 'replicate')[0, 0, ...]
    # unfold with window size of 3 with step size of 1
    y = data_padded.unfold(dimension=0, size=loopback, step=1)
    

    which gives output of

    tensor([[0., 0., 0.],
            [0., 0., 1.],
            [0., 1., 2.],
            [1., 2., 3.],
            [2., 3., 4.],
            [3., 4., 5.],
            [4., 5., 6.]])