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 0x7fee8fc96250> ├── 'a' --> tensor([1, 2, 3]) └── 'b' --> <Tensor 0x7fee8f9bba30> └── 'x' --> tensor([[4, 5], [6, 7]]) new random native tensor: tensor([[-0.1443, -0.4468, 0.7725], [ 0.5694, -2.8689, 0.5835]]) new random tree tensor: <Tensor 0x7fee8ed54d90> ├── 'a' --> tensor([[-1.3367, 1.5045, -0.5545], │ [ 0.5042, -0.1285, 0.2397]]) └── 'b' --> <Tensor 0x7fee8fc84460> └── 'x' --> tensor([[ 0.1627, 1.3776, 0.6958, -0.3439], [ 0.1151, 1.3172, 1.1026, -0.7454], [-2.0887, 0.5039, 0.3364, -0.7728]]) |