shijithp
(Shijith)
June 8, 2022, 6:28am
#1
How to find the biggest of two pytorch tensors on size
>>> tensor1 = torch.empty(0)
>>> tensor2 = torch.empty(1)
>>> tensor1
tensor([])
>>> tensor2
tensor([5.9555e-34])
torch.maximum is returrning the empty tensor as the biggest tensor
>>> torch.maximum(tensor1,tensor2)
tensor([])
Is there a way to find the biggest tensor among two tensors (mostly 1d), base on the number of elements in the tensor.
desires output is
>>> maxfunc(tensor1,tensor2)
tensor([5.9555e-34])
Something like this should work:
def maxfunc(*tensors):
idx = torch.argmax(torch.tensor([t.nelement() for t in tensors]))
out = tensors[idx]
return out
tensor1 = torch.empty(0)
tensor2 = torch.empty(1)
tensor3 = torch.empty(10)
maxfunc(tensor1, tensor2)
maxfunc(tensor1, tensor2, tensor3)
maxfunc(tensor3, tensor1, tensor2)
maxfunc(tensor2)
shijithp
(Shijith)
June 8, 2022, 2:39pm
#3
Thanks @ ptrblck , is there any performance difference if i use len(tensor) or tensor.shape[0] (1d tensor)?
I don’t know if there is an expected performance difference (but you could certainly profile it).
However, these operations are also not used in my code.