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 0x7f383bae6250> ├── 'a' --> tensor([1, 2, 3]) └── 'b' --> <Tensor 0x7f383b7fba30> └── 'x' --> tensor([[4, 5], [6, 7]]) new random native tensor: tensor([[-8.6448e-01, -1.7600e-04, -5.6286e-02], [ 1.8633e+00, -1.4158e+00, 7.1754e-01]]) new random tree tensor: <Tensor 0x7f383b3e9ee0> ├── 'a' --> tensor([[ 0.7686, -0.6480, 0.3835], │ [-0.0900, 0.4201, 0.3380]]) └── 'b' --> <Tensor 0x7f383bad4460> └── 'x' --> tensor([[ 0.0854, 1.3106, -0.5399, -3.5700], [-2.3662, -1.1613, -0.7575, 0.6214], [ 1.0569, -0.0149, 0.1693, -0.2165]]) |