add¶
Documentation¶
-
treetensor.torch.
add
(input, other, *args, **kwargs)[source]¶ Adds the scalar
other
to each element of theinput
input and returns a new resulting tree tensor.Examples:
>>> import torch >>> import treetensor.torch as ttorch >>> ttorch.add( ... ttorch.tensor([1, 2, 3]), ... ttorch.tensor([3, 5, 11]), ... ) tensor([ 4, 7, 14]) >>> ttorch.add( ... 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 0x7f11b139c710> ├── a --> tensor([ 4, 7, 14]) └── b --> <Tensor 0x7f11b139c630> └── x --> tensor([[ 34, -10], [ 22, 35]])
Torch Version Related
This documentation is based on torch.add in torch v1.9.0+cu102. 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 v1.9.0+cu102 is listed below.
Description From Torch v1.9.0+cu102¶
-
torch.
add
(input, other, *, out=None) → Tensor¶ Adds the scalar
other
to each element of the inputinput
and returns a new resulting tensor.\[\text{out} = \text{input} + \text{other}\]If
input
is of type FloatTensor or DoubleTensor,other
must be a real number, otherwise it should be an integer.- Args:
input (Tensor): the input tensor. other (Number): the number to be added to each element of
input
- Keyword arguments:
out (Tensor, optional): the output tensor.
Example:
>>> a = torch.randn(4) >>> a tensor([ 0.0202, 1.0985, 1.3506, -0.6056]) >>> torch.add(a, 20) tensor([ 20.0202, 21.0985, 21.3506, 19.3944])
Each element of the tensor
other
is multiplied by the scalaralpha
and added to each element of the tensorinput
. The resulting tensor is returned.The shapes of
input
andother
must be broadcastable.\[\text{out} = \text{input} + \text{alpha} \times \text{other}\]If
other
is of type FloatTensor or DoubleTensor,alpha
must be a real number, otherwise it should be an integer.- Args:
input (Tensor): the first input tensor other (Tensor): the second input tensor
- Keyword args:
alpha (Number): the scalar multiplier for
other
out (Tensor, optional): the output tensor.
Example:
>>> a = torch.randn(4) >>> a tensor([-0.9732, -0.3497, 0.6245, 0.4022]) >>> b = torch.randn(4, 1) >>> b tensor([[ 0.3743], [-1.7724], [-0.5811], [-0.8017]]) >>> torch.add(a, b, alpha=10) tensor([[ 2.7695, 3.3930, 4.3672, 4.1450], [-18.6971, -18.0736, -17.0994, -17.3216], [ -6.7845, -6.1610, -5.1868, -5.4090], [ -8.9902, -8.3667, -7.3925, -7.6147]])