std

Documentation

treetensor.torch.std(input, *args, reduce=None, **kwargs)[source]

Returns the standard-deviation of all elements in the input tensor.

Examples:

>>> import torch
>>> import treetensor.torch as ttorch
>>> t = torch.randn((2, 3)) * 30
>>> t
tensor([[ 25.5133,  24.2050,   8.1067],
        [ 22.7316, -17.8863, -37.9171]])
>>> ttorch.std(t)
tensor(26.3619)

>>> tt = ttorch.randn({
...     'a': (2, 3),
...     'b': {'x': (3, 4)},
... }) * 30
>>> tt
<Tensor 0x7f7c7288ca58>
├── a --> tensor([[-48.6580,  30.9506, -16.1800],
│                 [ 37.6667,  10.3850,  -5.7679]])
└── b --> <Tensor 0x7f7c7288c978>
    └── x --> tensor([[-17.9371,   8.4873, -49.0445,   4.7368],
                      [ 21.3990, -11.2385, -15.9331, -41.6838],
                      [ -7.1814, -38.1301,  -2.2320,  10.1392]])
>>> ttorch.std()
tensor(25.6854)
>>> ttorch.std(tt, reduce=False)
<Tensor 0x7f7c7288c470>
├── a --> tensor(32.0483)
└── b --> <Tensor 0x7f7c7288c3c8>
    └── x --> tensor(22.1754)
>>> ttorch.std(tt, dim=1)
<Tensor 0x7f1321ca1c50>
├── a --> tensor([40.0284, 21.9536])
└── b --> <Tensor 0x7f1321ca1fd0>
    └── x --> tensor([26.4519, 25.9011, 20.5223])

Torch Version Related

This documentation is based on torch.std 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.std(input, dim, unbiased, keepdim=False, *, out=None)Tensor

If unbiased is True, Bessel’s correction will be used. Otherwise, the sample deviation is calculated, without any correction.

Args:

input (Tensor): the input tensor. dim (int or tuple of ints): the dimension or dimensions to reduce.

Keyword args:

unbiased (bool): whether to use Bessel’s correction (\(\delta N = 1\)). keepdim (bool): whether the output tensor has dim retained or not. out (Tensor, optional): the output tensor.

torch.std(input, unbiased)Tensor

Calculates the standard deviation of all elements in the input tensor.

If unbiased is True, Bessel’s correction will be used. Otherwise, the sample deviation is calculated, without any correction.

Args:

input (Tensor): the input tensor. unbiased (bool): whether to use Bessel’s correction (\(\delta N = 1\)).

Example:

>>> a = torch.tensor([[-0.8166, -1.3802, -0.3560]])
>>> torch.std(a, unbiased=False)
tensor(0.4188)