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 0x7f8b0a9bd250>
├── 'a' --> tensor([1, 2, 3])
└── 'b' --> <Tensor 0x7f8b0a93ba30>
    └── 'x' --> tensor([[4, 5],
                        [6, 7]])
new random native tensor:
tensor([[-0.6972,  0.1486,  2.3812],
        [-0.5509,  0.3637,  1.7873]])
new random tree tensor:
<Tensor 0x7f8b0a5e9ee0>
├── 'a' --> tensor([[ 1.6505, -1.6343,  0.7266],
│                   [-1.1697,  0.9796,  0.3503]])
└── 'b' --> <Tensor 0x7f8b0a9ab460>
    └── 'x' --> tensor([[ 0.4693, -0.0212,  1.3313,  0.3956],
                        [-0.3996,  1.0222, -1.4236, -1.2373],
                        [ 0.1592, -1.7313, -1.9491,  2.0583]])
 |