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 0x7f6d42b3d250> ├── 'a' --> tensor([1, 2, 3]) └── 'b' --> <Tensor 0x7f6d42abba30> └── 'x' --> tensor([[4, 5], [6, 7]]) new random native tensor: tensor([[-1.1509, -1.2110, 0.4545], [-0.0437, 0.0095, -0.3049]]) new random tree tensor: <Tensor 0x7f6d42769ee0> ├── 'a' --> tensor([[-0.1972, -1.9303, -0.9943], │ [ 1.4699, -0.0021, -0.8833]]) └── 'b' --> <Tensor 0x7f6d42b2b460> └── 'x' --> tensor([[ 0.8235, -0.7934, -0.1878, -0.0060], [ 0.2179, -0.0474, 1.2822, -1.1818], [ 1.2408, 0.1210, -1.6937, 0.6986]]) |