div

Documentation

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

Divides each element of the input input by the corresponding element of other.

Examples:

>>> import torch
>>> import treetensor.torch as ttorch
>>> ttorch.div(ttorch.tensor([ 0.3810,  1.2774, -0.2972, -0.3719,  0.4637]), 0.5)
tensor([ 0.7620,  2.5548, -0.5944, -0.7438,  0.9274])

>>> ttorch.div(
...     ttorch.tensor([1.3119, 0.0928, 0.4158, 0.7494, 0.3870]),
...     ttorch.tensor([-1.7501, -1.4652,  0.1379, -1.1252,  0.0380]),
... )
tensor([-0.7496, -0.0633,  3.0152, -0.6660, 10.1842])

>>> ttorch.div(
...     ttorch.tensor({
...         'a': [ 0.3810,  1.2774, -0.2972, -0.3719,  0.4637],
...         'b': {
...             'x': [1.3119, 0.0928, 0.4158, 0.7494, 0.3870],
...             'y': [[[ 1.9579, -0.0335,  0.1178],
...                    [ 0.8287,  1.4520, -0.4696]],
...                   [[-2.1659, -0.5831,  0.4080],
...                    [ 0.1400,  0.8122,  0.5380]]],
...         },
...     }),
...     ttorch.tensor({
...         'a': 0.5,
...         'b': {
...             'x': [-1.7501, -1.4652,  0.1379, -1.1252,  0.0380],
...             'y': [[[-1.3136,  0.7785, -0.7290],
...                    [ 0.6025,  0.4635, -1.1882]],
...                   [[ 0.2756, -0.4483, -0.2005],
...                    [ 0.9587,  1.4623, -2.8323]]],
...         },
...     }),
... )
<Tensor 0x7f11b139c198>
├── a --> tensor([ 0.7620,  2.5548, -0.5944, -0.7438,  0.9274])
└── b --> <Tensor 0x7f11b139c320>
    ├── x --> tensor([-0.7496, -0.0633,  3.0152, -0.6660, 10.1842])
    └── y --> tensor([[[-1.4905, -0.0430, -0.1616],
                       [ 1.3754,  3.1327,  0.3952]],

                      [[-7.8589,  1.3007, -2.0349],
                       [ 0.1460,  0.5554, -0.1900]]])

Torch Version Related

This documentation is based on torch.div in torch v2.0.1+cu117. 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.0.1+cu117 is listed below.

Description From Torch v2.0.1+cu117

torch.div(input, other, *, rounding_mode=None, out=None)Tensor

Divides each element of the input input by the corresponding element of other.

\[\text{out}_i = \frac{\text{input}_i}{\text{other}_i}\]

Note

By default, this performs a “true” division like Python 3. See the rounding_mode argument for floor division.

Supports broadcasting to a common shape, type promotion, and integer, float, and complex inputs. Always promotes integer types to the default scalar type.

Args:

input (Tensor): the dividend other (Tensor or Number): the divisor

Keyword args:

rounding_mode (str, optional): Type of rounding applied to the result:

  • None - default behavior. Performs no rounding and, if both input and other are integer types, promotes the inputs to the default scalar type. Equivalent to true division in Python (the / operator) and NumPy’s np.true_divide.

  • "trunc" - rounds the results of the division towards zero. Equivalent to C-style integer division.

  • "floor" - rounds the results of the division down. Equivalent to floor division in Python (the // operator) and NumPy’s np.floor_divide.

out (Tensor, optional): the output tensor.

Examples:

>>> x = torch.tensor([ 0.3810,  1.2774, -0.2972, -0.3719,  0.4637])
>>> torch.div(x, 0.5)
tensor([ 0.7620,  2.5548, -0.5944, -0.7438,  0.9274])

>>> a = torch.tensor([[-0.3711, -1.9353, -0.4605, -0.2917],
...                   [ 0.1815, -1.0111,  0.9805, -1.5923],
...                   [ 0.1062,  1.4581,  0.7759, -1.2344],
...                   [-0.1830, -0.0313,  1.1908, -1.4757]])
>>> b = torch.tensor([ 0.8032,  0.2930, -0.8113, -0.2308])
>>> torch.div(a, b)
tensor([[-0.4620, -6.6051,  0.5676,  1.2639],
        [ 0.2260, -3.4509, -1.2086,  6.8990],
        [ 0.1322,  4.9764, -0.9564,  5.3484],
        [-0.2278, -0.1068, -1.4678,  6.3938]])

>>> torch.div(a, b, rounding_mode='trunc')
tensor([[-0., -6.,  0.,  1.],
        [ 0., -3., -1.,  6.],
        [ 0.,  4., -0.,  5.],
        [-0., -0., -1.,  6.]])

>>> torch.div(a, b, rounding_mode='floor')
tensor([[-1., -7.,  0.,  1.],
        [ 0., -4., -2.,  6.],
        [ 0.,  4., -1.,  5.],
        [-1., -1., -2.,  6.]])