How to iterate over a group of tensor and pass the elements from each group to a function?
Suppose you have 3 tensors of the same size:

a = torch.randn(3,3)                    
a = ([[ 0.1945,  0.8583,  2.6479],       
    [-0.1000,  1.2136, -0.3706],
    [-0.0094,  0.4279, -0.6840]])

b = torch.randn(3, 3)
b = ([[-1.1155,  0.2106, -0.2183],
    [ 1.6610, -0.6953,  0.0052],
    [-0.8955,  0.0953, -0.7737]])

c = torch.randn(3, 3)
c = ([[-0.2303, -0.3427, -0.4990],
    [-1.1254,  0.4432,  0.3999],
    [ 0.2489, -0.9459, -0.5576]])


In Lua (torch7), they have this function:

[self] map2(tensor1, tensor2, function(x, xt1, xt2))


which applies the given function to all elements of self.

My questions are:


Is there any similar function in python (pytorch)?
Is there any pythonic method to iterate over the 3 tensors and get the respective elements of each tensor without using for loop and indices?


For example:

0.1945 -1.1155 -0.2303
0.8583 0.2106 -0.3427
2.6479 -0.2183 -0.4990
-0.1000 1.6610 -1.1254
...


Edit_1: I have also tried itertools.zip_longest and zip, but the results are not as I expected as mentioned above

You can use Python's map function similar to what you have mentioned. Like this:

&gt;&gt;&gt; tensor_list = [torch.tensor([i, i, i]) for i in range(3)]
&gt;&gt;&gt; list(map(lambda x: x**2, tensor_list))
[tensor([0, 0, 0]), tensor([1, 1, 1]), tensor([4, 4, 4])]
&gt;&gt;&gt; 


EDIT: For a PyTorch only approach you can use torch.Tensor.apply_ (Note this does the changes in place and doesn't return a new tensor)

&gt;&gt;&gt; x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
&gt;&gt;&gt; x.apply_(lambda y: y ** 2)
tensor([[ 1,  4,  9],
        [16, 25, 36],
        [49, 64, 81]])
&gt;&gt;&gt; 

