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 0x7f9c7264d190>
├── 'a' --> tensor([1, 2, 3])
└── 'b' --> <Tensor 0x7f9c7264d310>
    └── 'x' --> tensor([[4, 5],
                        [6, 7]])

new random native tensor:
tensor([[-1.9644, -0.1832,  0.3980],
        [ 1.1934, -0.0498,  0.3977]])
new random tree tensor:
<Tensor 0x7f9c10f056d0>
├── 'a' --> tensor([[ 2.4162,  0.8693,  0.5823],
│                   [-0.2450, -1.9357, -1.2070]])
└── 'b' --> <Tensor 0x7f9c10e2be10>
    └── 'x' --> tensor([[-1.0113, -0.0627, -0.4039,  0.2013],
                        [ 0.5440,  0.4539, -1.2979, -0.2313],
                        [ 0.7010,  0.4367,  0.3498,  0.8885]])