What Does the "Greater Than" Operator ">" Mean for Pytorch Tensors?
I Have a Tensor It Defined as: Import Torch It = Torch. Tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], Device='Cuda:0') Given This Definition, What Does It > 0 Then...
I have a tensor it defined as:
import torch
it = torch.tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], device='cuda:0')
Given this definition, what does it > 0 then mean?
2 Answers
Using the > operator is the same as using the torch.gt() function.
In other words,
it > 0
is the same as
torch.gt(it, 0)
and it returns a ByteTensor (a Boolean tensor) of the same shape as it where out[i] is True if it[i] > 0 and False otherwise.
As seen from the question it is a 1-d tensor consisting of 10 elements.
When we write it > 0 each element of the tensor it is compared with 0 and the output is set as True or False depending upon whether the number is greater than 0 or not. The result is also a 1-d boolean tensor consisting of True and False values.
In your case, you will get a 1-d tensor like this: [False, False, False, False, False, False, False, False, False, False,] as none of the elements in it is equal to 0.
In more simpler terms,
If result is a variable (actually a 1-d tenosr) to store the output having the same shape as it then the equation result= it > 0 can be written as:
if it[i]>0:
result[i]= True
else:
result[i]= False
But when it's performed as result= it > 0, the execution is much faster than writing our own for/while loop.
Hope this helps you.