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 0x7f4bf7cd6250> ├── 'a' --> tensor([1, 2, 3]) └── 'b' --> <Tensor 0x7f4bf79fba30> └── 'x' --> tensor([[4, 5], [6, 7]]) new random native tensor: tensor([[0.4173, 0.4777, 0.7328], [0.6321, 0.2784, 1.0704]]) new random tree tensor: <Tensor 0x7f4bf75e9ee0> ├── 'a' --> tensor([[-1.2351, 1.1970, -0.2185], │ [-0.4415, 0.7200, -0.2820]]) └── 'b' --> <Tensor 0x7f4bf7cc4460> └── 'x' --> tensor([[-1.1630, -1.7738, -0.0266, 0.9209], [-2.4688, 0.0977, -0.4680, 1.1742], [-0.0277, -1.0709, 0.6655, 1.9821]]) |