Quick Start

Create a Tree-based Tensor

You can create a tree-based tensor or a native tensor like the following example code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import builtins
import os
from functools import partial

import treetensor.torch as torch

print = partial(builtins.print, sep=os.linesep)

if __name__ == '__main__':
    t1 = torch.tensor([[1, 2, 3],
                       [4, 5, 6]])
    print('new native tensor:', t1)

    t2 = torch.tensor({
        'a': [1, 2, 3],
        'b': {'x': [[4, 5], [6, 7]]},
    })
    print('new tree tensor:', t2)

    t3 = torch.randn(2, 3)
    print('new random native tensor:', t3)

    t4 = torch.randn({
        'a': (2, 3),
        'b': {'x': (3, 4)},
    })
    print('new random tree tensor:', t4)

The output should be like below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
new native tensor:
tensor([[1, 2, 3],
        [4, 5, 6]])
new tree tensor:
<Tensor 0x7ff8001fb110>
├── 'a' --> tensor([1, 2, 3])
└── 'b' --> <Tensor 0x7ff8001fb350>
    └── 'x' --> tensor([[4, 5],
                        [6, 7]])

new random native tensor:
tensor([[-0.6454,  0.5779,  0.7039],
        [-1.2114, -1.2516, -0.7848]])
new random tree tensor:
<Tensor 0x7ff79eab67d0>
├── 'a' --> tensor([[-1.2910, -0.1934,  0.6845],
│                   [-2.6583,  1.0714,  1.4906]])
└── 'b' --> <Tensor 0x7ff79e9def90>
    └── 'x' --> tensor([[-0.2397, -1.9090, -0.3129, -0.2794],
                        [-1.6579,  0.0133,  0.6379, -0.4406],
                        [-0.7128,  1.2459, -0.5514, -1.0883]])