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

new random native tensor:
tensor([[ 0.2900, -0.4627,  0.4809],
        [-2.0313,  0.4323,  0.3110]])
new random tree tensor:
<Tensor 0x7fcea30db610>
├── 'a' --> tensor([[-1.6083,  0.5723, -0.5470],
│                   [ 0.2003, -0.1147,  1.0115]])
└── 'b' --> <Tensor 0x7fcf427ab100>
    └── 'x' --> tensor([[-1.7594,  1.1741,  1.0055, -0.4391],
                        [ 0.6803, -0.3735,  0.6807, -0.5961],
                        [-0.3457,  0.7145, -1.1454, -2.0874]])