Insert a fixed value into a tensor dimension before every N elements

What is the best way to insert a single fixed value into a tensor dimension before every N elements?

This is what I’ve come up with so far -

import torch

T, N, C = (720, 32, 256)

x = torch.empty((T, N, C))
M = 4

value = torch.tensor(24)

x = torch.cat([
    value.expand(T, N, C // M, 1),
    x.reshape(T, N, C // M, M)
], axis=-1).reshape(T, N, -1)


print(x.shape) # torch.Size([720, 32, 320])  
x = torch.nn.functional.pad(x.view(T, N, C // M, M), (1, 0, 0, 0, 0, 0, 0, 0), value=24).view(T, N, -1)

returns the same result faster.

1 Like