Element-wise comparison of two tensor and take the maximum value

how to get element-wise comparison and take element-wise maximum? is there a function in pytorch?I think a for loop is too slow
eg: tensor_1=[1,2,3,4] tensor_2=[-10,-20,10,20]
I want to get a tensor=[1,2,10,20]

1 Like

You can use torch.max(tensor_1, tensor_2). Check out http://pytorch.org/docs/stable/torch.html#torch.max

How about more than 2?

You could concatenate all tensors and use torch.max(tensor, dim=concat_dim).

3 Likes

Would I have to .cat along a specific axis?

Yes and it depends on your use case. I.e. if you want to calculate the max in dim1 for all tensors, you should concatenate them in the same dimension.
Let me know, if that would work for your use case.

1 Like