detach

Documentation

treetensor.torch.detach(input)[source]

Detach tensor from calculation graph.

Examples:

>>> import torch
>>> import treetensor.torch as ttorch
>>> tt = ttorch.randn({
...     'a': (2, 3),
...     'b': {'x': (3, 4)},
... })
>>> tt.requires_grad_(True)
>>> tt
<Tensor 0x7f5881338eb8>
├── a --> tensor([[ 2.5262,  0.7398,  0.7966],
│                 [ 1.3164,  1.2248, -2.2494]], requires_grad=True)
└── b --> <Tensor 0x7f5881338e10>
    └── x --> tensor([[ 0.3578,  0.4611, -0.6668,  0.5356],
                      [-1.4392, -1.2899, -0.0394,  0.8457],
                      [ 0.4492, -0.5188, -0.2375, -1.2649]], requires_grad=True)

>>> ttorch.detach(tt)
<Tensor 0x7f588133a588>
├── a --> tensor([[ 2.5262,  0.7398,  0.7966],
│                 [ 1.3164,  1.2248, -2.2494]])
└── b --> <Tensor 0x7f588133a4e0>
    └── x --> tensor([[ 0.3578,  0.4611, -0.6668,  0.5356],
                      [-1.4392, -1.2899, -0.0394,  0.8457],
                      [ 0.4492, -0.5188, -0.2375, -1.2649]])

Torch Version Related

This documentation is based on torch.detach 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__)'