[Python] Passing sections of tensor to function (can I avoid looping)?

Discussão em 'Python' iniciado por Stack, Julho 27, 2025.

  1. Stack

    Stack Membro Participativo

    If I have this tensor

    ks=[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0. 8.0]


    and a function

    myfunc(k:torch.tensor, s:float)->torch.tensor:
    # do something
    return torch.tensor([2.3, 3.4, 5.1, 2.2])


    Return value of myfunc is not a constant of the above numbers. Its just to illustrate the tensor size. At the moment, I am looping as follows.

    ks_indices = torch.tensor(list(range(4))
    for i in range(5):
    k_set = torch.index_select(ks, 0, ks_indices.view(-1))
    result_tensor = myfunc(k_set, ks[0])
    ks = torch.roll(ks, -1)


    Is it possible to avoid the loop and make a single tensor call to get all five result_tensor's as a 5x4 tensor in one go?

    Continue reading...

Compartilhe esta Página