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

new random native tensor:
tensor([[ 0.1875, -0.2799, -0.5441],
        [-0.1897,  0.4704, -0.5737]])
new random tree tensor:
<Tensor 0x7f2db58df070>
├── 'a' --> tensor([[ 0.1016, -1.6815, -1.2394],
│                   [ 2.4800, -0.3574, -0.2664]])
└── 'b' --> <Tensor 0x7f2db65bf190>
    └── 'x' --> tensor([[ 0.6640, -1.4243,  0.6565,  0.4917],
                        [-1.0477, -0.4803,  0.9895,  1.3029],
                        [ 1.4071, -1.3841, -0.3736,  0.3501]])