sign

Documentation

treetensor.torch.sign(input, *args, **kwargs)[source]

Returns a tree of new tensors with the signs of the elements of input.

Examples:

>>> import torch
>>> import treetensor.torch as ttorch
>>> ttorch.sign(ttorch.tensor([12, 0, -3]))
tensor([ 1,  0, -1])

>>> ttorch.sign(ttorch.tensor({
...     'a': [12, 0, -3],
...     'b': {'x': [[-3, 1], [0, -2]]},
... }))
<Tensor 0x7f1c81d02d30>
├── a --> tensor([ 1,  0, -1])
└── b --> <Tensor 0x7f1c81d02a60>
    └── x --> tensor([[-1,  1],
                      [ 0, -1]])

Torch Version Related

This documentation is based on torch.sign in torch v2.4.1+cu121. Its arguments’ arrangements depend on the version of pytorch you installed.

If some arguments listed here are not working properly, please check your pytorch’s version with the following command and find its documentation.

1
python -c 'import torch;print(torch.__version__)'

The arguments and keyword arguments supported in torch v2.4.1+cu121 is listed below.

Description From Torch v2.4.1+cu121

torch.sign(input, *, out=None)Tensor

Returns a new tensor with the signs of the elements of input.

\[\text{out}_{i} = \operatorname{sgn}(\text{input}_{i})\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.tensor([0.7, -1.2, 0., 2.3])
>>> a
tensor([ 0.7000, -1.2000,  0.0000,  2.3000])
>>> torch.sign(a)
tensor([ 1., -1.,  0.,  1.])