Skip to content
Advertisement

Whats the purpose of torch.positive?

From the documentation:

torch.positive(input)Tensor

Returns input. Throws a runtime error if input is a bool tensor.

It just returns the input and throws error if its a bool tensor, but that’s not an efficient nor readable way of checking if a tensor is bool.

Advertisement

Answer

It seems like pytorch added pytorch.positive in parity with numpy which has a function of the same name.

So back to your question, positive is a unary operator which basically multiplies everything by +1. This is not a particularly useful operation, but is symmetric to negative which multiplies everything by -1.

>>> import numpy as np
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.positive(a)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.negative(a)
array([ 0, -1, -2, -3, -4, -5, -6, -7, -8, -9])

So why make a positive function that effectively returns (a copy of) the array you passed? It can be used to write general code where e.g. numpy.positive and numpy.negative can be used dynamically based on some condition.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement