5

Suppose x is a tensor in Pytorch. One can either write:

x_lowerthanzero = x.lt(0)

or:

x_lowerthanzero = (x<0)

with seemingly the exact same results. Many other operations have Pytorch built-in equivalents: x.gt(0) for (x>0), x.neg() for -x, x.mul() etc.

Is there a good reason to use one form over the other?

iacob
  • 20,084
  • 6
  • 92
  • 119
Larsupial
  • 53
  • 4

2 Answers2

4

They are equivalent. < is simply a more readable alias.

Python operators have canonical function mappings e.g:

Algebraic operations

Operation Syntax Function
Addition a + b add(a, b)
Subtraction a - b sub(a, b)
Multiplication a * b mul(a, b)
Division a / b truediv(a, b)
Exponentiation a ** b pow(a, b)
Matrix Multiplication a @ b matmul(a, b)

Comparisons

Operation Syntax Function
Ordering a < b lt(a, b)
Ordering a <= b le(a, b)
Equality a == b eq(a, b)
Difference a != b ne(a, b)
Ordering a >= b ge(a, b)
Ordering a > b gt(a, b)

You can check that these are indeed mapped to the respectively named torch functions here e.g:

def __lt__(self, other):
    return self.lt(other)
iacob
  • 20,084
  • 6
  • 92
  • 119
  • 1
    @Gulzar I answered the question title: "In Pytorch, is there a difference between (x<0) and x.lt(0)?" - the answers to your questions are the same as why these operators exist _generally_ in python - convenient, more readable aliases. – iacob Apr 06 '21 at 11:43
1

Usually there is no reason for using one over the other, they are mostly for convenience: Many of those methods do have for instance an out argument, which lets you specify a tensor in which to save the result, but you can just as well do that using the operators instead of the methods.

flawr
  • 10,814
  • 3
  • 41
  • 71
  • do you have a reference supporting "there is no reason for using one over the other" – Gulzar Apr 06 '21 at 11:47
  • 2
    No I don't: If you look at the [implementation](https://github.com/pytorch/pytorch/blob/fa8044d92f8a77b8008ca5e295a341abb9d26f13/torch/tensor.py) you see that the operators are even defined in terms of these methods. It is probably done as these methods are implemented in C, and then the operators are implemented in python, calling the C-api. – flawr Apr 06 '21 at 12:44