0

I need to create a list of lambda functions that would act on torch tensor. However the results are not returning what I expect. Here is the code:

import numpy as np
import torch

lb = list()
for i in range (2):
    lb.append(lambda Z: Z[...,1+i])

# to test
x = np.random.rand(10,3)
xt = torch.from_numpy(x)

print(lb[0](xt))
print(lb[1](xt))

This return the same results for both lambda functions. Which would be different if I simply do

lb = [lambda Z:Z[...,1], lambda Z:Z[...,2]]

Any suggestion on how I can do this with the for loop? Thanks

0 Answers0