sub

Documentation

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

Subtracts other, scaled by alpha, from input.

Examples:

>>> import torch
>>> import treetensor.torch as ttorch
>>> ttorch.sub(
...     ttorch.tensor([1, 2, 3]),
...     ttorch.tensor([3, 5, 11]),
... )
tensor([-2, -3, -8])

>>> ttorch.sub(
...     ttorch.tensor({
...         'a': [1, 2, 3],
...         'b': {'x': [[3, 5], [9, 12]]},
...     }),
...     ttorch.tensor({
...         'a': [3, 5, 11],
...         'b': {'x': [[31, -15], [13, 23]]},
...     })
... )
<Tensor 0x7f11b139ccc0>
├── a --> tensor([-2, -3, -8])
└── b --> <Tensor 0x7f11b139cc18>
    └── x --> tensor([[-28,  20],
                      [ -4, -11]])

Torch Version Related

This documentation is based on torch.sub 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.sub(input, other, *, alpha=1, out=None)Tensor

Subtracts other, scaled by alpha, from input.

\[\text{{out}}_i = \text{{input}}_i - \text{{alpha}} \times \text{{other}}_i\]

Supports broadcasting to a common shape, type promotion, and integer, float, and complex inputs.

Args:

input (Tensor): the input tensor. other (Tensor or Number): the tensor or number to subtract from input.

Keyword args:

alpha (Number): the multiplier for other. out (Tensor, optional): the output tensor.

Example:

>>> a = torch.tensor((1, 2))
>>> b = torch.tensor((0, 1))
>>> torch.sub(a, b, alpha=2)
tensor([1, 0])