[Python] Torch: how to insert a tensor into another tensor at certain index

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have a padded tensor X with shape (B, T1, C) and a padded tensor Y with shape (B, T2, C), I also know the sample lengths L for X. I want to insert the samples of X into Y at certain index I and pad at the end.

    For example, if I = 5, the goal of the transformation is something like:

    inputs = []
    for i in range(X.shape[0]):
    input = torch.cat([Y[0:5],
    X[:L],
    Y[5:],
    torch.zeros(max(L) - L, Y.shape[2])],
    dim=0)
    inputs.append(input)
    outputs = torch.stack(inputs, dim=0)


    I want to know how to do this in tensor version instead of the for loop, which is too slow for training.

    Continue reading...

Compartilhe esta Página