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

new random native tensor:
tensor([[-0.9468, -1.0746,  1.8227],
        [-0.9522,  1.2517,  1.3312]])
new random tree tensor:
<Tensor 0x7f2c06d41a50>
├── 'a' --> tensor([[ 1.7685, -1.2975,  0.4621],
│                   [ 1.7087,  0.4430, -0.7300]])
└── 'b' --> <Tensor 0x7f2c06d41990>
    └── 'x' --> tensor([[-0.0872,  0.7193,  0.0609, -0.8283],
                        [-0.2441, -0.8372, -0.6667,  1.6437],
                        [ 0.3260,  0.6671,  0.3671, -0.3453]])