fn

sign

Tensor
sign(input: Tensor)
source

Element-wise sign function.

Returns -1 for negative entries, 0 for zero, and +1 for positive entries.

Parameters

inputTensor
Input tensor.

Returns

Tensor

Element-wise result with the same shape as input.

Notes

Mathematical definition:

outi=sgn(inputi)={1xi<00xi=0+1xi>0\text{out}_i = \mathrm{sgn}(\text{input}_i) = \begin{cases} -1 & x_i < 0 \\ 0 & x_i = 0 \\ +1 & x_i > 0 \end{cases}

Gradient is zero almost everywhere (treated as zero in autograd).

Examples

>>> import lucid
>>> x = lucid.tensor([1.0, 2.0, 3.0])
>>> lucid.sign(x)
Tensor([...])