dist

Documentation

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

Returns the p-norm of (input - other)

Examples:

>>> import torch
>>> import treetensor.torch as ttorch
>>> t1 = torch.randn(5)
>>> t1
tensor([-0.6566,  1.2243,  1.5018, -0.1492,  0.8947])
>>> t2 = torch.randn(5)
>>> t2
tensor([0.5898, 0.6839, 0.0388, 0.4649, 0.7964])
>>> ttorch.dist(t1, t2)
tensor(2.0911)

>>> tt1 = ttorch.randn({'a': (5, ), 'b': {'x': (6, )}})
>>> tt1
<Tensor 0x7f95f68495f8>
├── a --> tensor([-0.5491,  1.5006, -0.0483,  1.2282, -1.4837])
└── b --> <Tensor 0x7f95f68494e0>
    └── x --> tensor([-1.8414,  1.2913,  0.0943,  0.3473,  1.2717,  0.6013])
>>> tt2 = ttorch.randn({'a': (5, ), 'b': {'x': (6, )}})
>>> tt2
<Tensor 0x7f95f68ef2b0>
├── a --> tensor([ 0.1389, -0.7804, -1.3048, -1.1066,  1.3225])
└── b --> <Tensor 0x7f95f6849dd8>
    └── x --> tensor([ 1.4873,  0.2218, -0.1063, -0.8726, -0.6756,  0.4805])
>>> ttorch.dist(tt1, tt2)
<Tensor 0x7f95f6849358>
├── a --> tensor(4.5366)
└── b --> <Tensor 0x7f95f68494a8>
    └── x --> tensor(4.1904)

Torch Version Related

This documentation is based on torch.dist in torch v1.10.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.10.0+cu102 is listed below.

Description From Torch v1.10.0+cu102

torch.dist(input, other, p=2)Tensor

Returns the p-norm of (input - other)

The shapes of input and other must be broadcastable.

Args:

input (Tensor): the input tensor. other (Tensor): the Right-hand-side input tensor p (float, optional): the norm to be computed

Example:

>>> x = torch.randn(4)
>>> x
tensor([-1.5393, -0.8675,  0.5916,  1.6321])
>>> y = torch.randn(4)
>>> y
tensor([ 0.0967, -1.0511,  0.6295,  0.8360])
>>> torch.dist(x, y, 3.5)
tensor(1.6727)
>>> torch.dist(x, y, 3)
tensor(1.6973)
>>> torch.dist(x, y, 0)
tensor(inf)
>>> torch.dist(x, y, 1)
tensor(2.6537)