From f8349bc464df7bf8c2b9b63182117d0d8c6b57f7 Mon Sep 17 00:00:00 2001 From: HansBug Date: Thu, 16 Sep 2021 21:27:39 +0800 Subject: [PATCH] doc(hansbug): update the documentations in treetensor.torch.funcs --- treetensor/torch/funcs.py | 617 +++++++++++++++++++++++++++----------- 1 file changed, 435 insertions(+), 182 deletions(-) diff --git a/treetensor/torch/funcs.py b/treetensor/torch/funcs.py index a7a5f8b61..0acba8718 100644 --- a/treetensor/torch/funcs.py +++ b/treetensor/torch/funcs.py @@ -44,18 +44,15 @@ def zeros(*args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.zeros(2, 3) # the same as torch.zeros(2, 3) - torch.tensor([[0.0, 0.0, 0.0], - [0.0, 0.0, 0.0]]) - - >>> ttorch.zeros({ - >>> 'a': (2, 3), - >>> 'b': (4, ), - >>> }) - ttorch.tensor({ - 'a': torch.tensor([[0.0, 0.0, 0.0], - [0.0, 0.0, 0.0]]), - 'b': torch.tensor([0.0, 0.0, 0.0, 0.0]), - }) + tensor([[0., 0., 0.], + [0., 0., 0.]]) + + >>> ttorch.zeros({'a': (2, 3), 'b': {'x': (4, )}}) + + ├── a --> tensor([[0., 0., 0.], + │ [0., 0., 0.]]) + └── b --> + └── x --> tensor([0., 0., 0., 0.]) """ return torch.zeros(*args, **kwargs) @@ -72,18 +69,18 @@ def zeros_like(input, *args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.zeros_like(torch.randn(2, 3)) # the same as torch.zeros_like(torch.randn(2, 3)) - torch.tensor([[0.0, 0.0, 0.0], - [0.0, 0.0, 0.0]]) + tensor([[0., 0., 0.], + [0., 0., 0.]]) >>> ttorch.zeros_like({ - >>> 'a': torch.randn(2, 3), - >>> 'b': torch.randn(4, ), - >>> }) - ttorch.tensor({ - 'a': torch.tensor([[0.0, 0.0, 0.0], - [0.0, 0.0, 0.0]]), - 'b': torch.tensor([0.0, 0.0, 0.0, 0.0]), - }) + ... 'a': torch.randn(2, 3), + ... 'b': {'x': torch.randn(4, )}, + ... }) + + ├── a --> tensor([[0., 0., 0.], + │ [0., 0., 0.]]) + └── b --> + └── x --> tensor([0., 0., 0., 0.]) """ return torch.zeros_like(input, *args, **kwargs) @@ -100,18 +97,15 @@ def randn(*args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.randn(2, 3) # the same as torch.randn(2, 3) - torch.tensor([[-0.1524, -0.6836, 0.2071], - [-1.0407, 0.2497, -0.2317]]) - - >>> ttorch.randn({ - >>> 'a': (2, 3), - >>> 'b': (4, ), - >>> }) - ttorch.tensor({ - 'a': torch.tensor([[-0.2399, -1.3437, 0.0656], - [-0.3137, -0.3177, -3.0176]]) - 'b': torch.tensor([-1.3047, 0.0188, -0.3311, 0.3112]), - }) + tensor([[-0.8534, -0.5754, -0.2507], + [ 0.0826, -1.4110, 0.9748]]) + + >>> ttorch.randn({'a': (2, 3), 'b': {'x': (4, )}}) + + ├── a --> tensor([[ 0.5398, 0.7529, -2.0339], + │ [-0.5722, -1.1900, 0.7945]]) + └── b --> + └── x --> tensor([-0.7181, 0.1670, -1.3587, -1.5129]) """ return torch.randn(*args, **kwargs) @@ -129,18 +123,18 @@ def randn_like(input, *args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.randn_like(torch.ones(2, 3)) # the same as torch.randn_like(torch.ones(2, 3)) - torch.tensor([[-1.3912, 2.3161, 1.0146], - [-0.3242, 0.5288, 2.4341]]) + tensor([[ 1.8436, 0.2601, 0.9687], + [ 1.6430, -0.1765, -1.1732]]) >>> ttorch.randn_like({ - >>> 'a': torch.ones(2, 3), - >>> 'b': torch.ones(4, ), - >>> }) - ttorch.tensor({ - 'a': torch.tensor([[ 1.0548, -0.4282, 2.2030], - [-0.5305, -0.2601, -1.2560]]) - 'b': torch.tensor([ 0.4502, 0.3977, -0.5329, 0.3459]), - }) + ... 'a': torch.ones(2, 3), + ... 'b': {'x': torch.ones(4, )}, + ... }) + + ├── a --> tensor([[-0.1532, 1.3965, -1.2956], + │ [-0.0750, 0.6475, 1.1421]]) + └── b --> + └── x --> tensor([ 0.1730, 1.6085, 0.6487, -1.1022]) """ return torch.randn_like(input, *args, **kwargs) @@ -148,6 +142,25 @@ def randn_like(input, *args, **kwargs): @doc_from(torch.randint) @func_treelize() def randint(*args, **kwargs): + """ + In ``treetensor``, you can use ``randint`` to create a tree of tensors with numbers + in an integer range. + + Examples:: + + >>> import torch + >>> import treetensor.torch as ttorch + >>> ttorch.randint(10, (2, 3)) # the same as torch.randint(10, (2, 3)) + tensor([[3, 4, 5], + [4, 5, 5]]) + + >>> ttorch.randint(10, {'a': (2, 3), 'b': {'x': (4, )}}) + + ├── a --> tensor([[5, 3, 7], + │ [8, 1, 8]]) + └── b --> + └── x --> tensor([8, 8, 2, 4]) + """ return torch.randint(*args, **kwargs) @@ -155,6 +168,28 @@ def randint(*args, **kwargs): @doc_from(torch.randint_like) @func_treelize() def randint_like(input, *args, **kwargs): + """ + In ``treetensor``, you can use ``randint_like`` to create a tree of tensors with numbers + in an integer range. + + Examples:: + + >>> import torch + >>> import treetensor.torch as ttorch + >>> ttorch.randint_like(torch.ones(2, 3), 10) # the same as torch.randint_like(torch.ones(2, 3), 10) + tensor([[0., 5., 0.], + [2., 0., 9.]]) + + >>> ttorch.randint_like({ + ... 'a': torch.ones(2, 3), + ... 'b': {'x': torch.ones(4, )}, + ... }, 10) + + ├── a --> tensor([[3., 6., 1.], + │ [8., 9., 5.]]) + └── b --> + └── x --> tensor([4., 4., 7., 1.]) + """ return torch.randint_like(input, *args, **kwargs) @@ -169,18 +204,15 @@ def ones(*args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.ones(2, 3) # the same as torch.ones(2, 3) - torch.tensor([[1.0, 1.0, 1.0], - [1.0, 1.0, 1.0]]) - - >>> ttorch.ones({ - >>> 'a': (2, 3), - >>> 'b': (4, ), - >>> }) - ttorch.tensor({ - 'a': torch.tensor([[1.0, 1.0, 1.0], - [1.0, 1.0, 1.0]]), - 'b': torch.tensor([1.0, 1.0, 1.0, 1.0]), - }) + tensor([[1., 1., 1.], + [1., 1., 1.]]) + + >>> ttorch.ones({'a': (2, 3), 'b': {'x': (4, )}}) + + ├── a --> tensor([[1., 1., 1.], + │ [1., 1., 1.]]) + └── b --> + └── x --> tensor([1., 1., 1., 1.]) """ return torch.ones(*args, **kwargs) @@ -197,18 +229,18 @@ def ones_like(input, *args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.ones_like(torch.randn(2, 3)) # the same as torch.ones_like(torch.randn(2, 3)) - torch.tensor([[1.0, 1.0, 1.0], - [1.0, 1.0, 1.0]]) + tensor([[1., 1., 1.], + [1., 1., 1.]]) >>> ttorch.ones_like({ - >>> 'a': torch.randn(2, 3), - >>> 'b': torch.randn(4, ), - >>> }) - ttorch.tensor({ - 'a': torch.tensor([[1.0, 1.0, 1.0], - [1.0, 1.0, 1.0]]), - 'b': torch.tensor([1.0, 1.0, 1.0, 1.0]), - }) + ... 'a': torch.randn(2, 3), + ... 'b': {'x': torch.randn(4, )}, + ... }) + + ├── a --> tensor([[1., 1., 1.], + │ [1., 1., 1.]]) + └── b --> + └── x --> tensor([1., 1., 1., 1.]) """ return torch.ones_like(input, *args, **kwargs) @@ -224,18 +256,15 @@ def full(*args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.full((2, 3), 2.3) # the same as torch.full((2, 3), 2.3) - torch.tensor([[2.3, 2.3, 2.3], - [2.3, 2.3, 2.3]]) - - >>> ttorch.ones({ - >>> 'a': (2, 3), - >>> 'b': (4, ), - >>> }, 2.3) - ttorch.tensor({ - 'a': torch.tensor([[2.3, 2.3, 2.3], - [2.3, 2.3, 2.3]]), - 'b': torch.tensor([2.3, 2.3, 2.3, 2.3]), - }) + tensor([[2.3000, 2.3000, 2.3000], + [2.3000, 2.3000, 2.3000]]) + + >>> ttorch.full({'a': (2, 3), 'b': {'x': (4, )}}, 2.3) + + ├── a --> tensor([[2.3000, 2.3000, 2.3000], + │ [2.3000, 2.3000, 2.3000]]) + └── b --> + └── x --> tensor([2.3000, 2.3000, 2.3000, 2.3000]) """ return torch.full(*args, **kwargs) @@ -253,18 +282,18 @@ def full_like(input, *args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.full_like(torch.randn(2, 3), 2.3) # the same as torch.full_like(torch.randn(2, 3), 2.3) - torch.tensor([[2.3, 2.3, 2.3], - [2.3, 2.3, 2.3]]) + tensor([[2.3000, 2.3000, 2.3000], + [2.3000, 2.3000, 2.3000]]) >>> ttorch.full_like({ - >>> 'a': torch.randn(2, 3), - >>> 'b': torch.randn(4, ), - >>> }, 2.3) - ttorch.tensor({ - 'a': torch.tensor([[2.3, 2.3, 2.3], - [2.3, 2.3, 2.3]]), - 'b': torch.tensor([2.3, 2.3, 2.3, 2.3]), - }) + ... 'a': torch.randn(2, 3), + ... 'b': {'x': torch.randn(4, )}, + ... }, 2.3) + + ├── a --> tensor([[2.3000, 2.3000, 2.3000], + │ [2.3000, 2.3000, 2.3000]]) + └── b --> + └── x --> tensor([2.3000, 2.3000, 2.3000, 2.3000]) """ return torch.full_like(input, *args, **kwargs) @@ -281,18 +310,15 @@ def empty(*args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.empty(2, 3) # the same as torch.empty(2, 3) - torch.tensor([[ 6.6531e-39, 8.2002e-35, 1.3593e-43], - [ 0.0000e+00, -4.0271e-20, 4.5887e-41]]) - - >>> ttorch.empty({ - >>> 'a': (2, 3), - >>> 'b': (4, ), - >>> }) - ttorch.tensor({ - 'a': torch.tensor([[-1.1736e+27, 3.0918e-41, -1.1758e+27], - [ 3.0918e-41, 8.9683e-44, 0.0000e+00]]), - 'b': torch.tensor([-4.0271e-20, 4.5887e-41, -1.1213e+27, 3.0918e-41]), - }) + tensor([[-1.3267e-36, 3.0802e-41, 2.3000e+00], + [ 2.3000e+00, 2.3000e+00, 2.3000e+00]]) + + >>> ttorch.empty({'a': (2, 3), 'b': {'x': (4, )}}) + + ├── a --> tensor([[-3.6515e+14, 4.5900e-41, -1.3253e-36], + │ [ 3.0802e-41, 2.3000e+00, 2.3000e+00]]) + └── b --> + └── x --> tensor([-3.6515e+14, 4.5900e-41, -3.8091e-38, 3.0802e-41]) """ return torch.empty(*args, **kwargs) @@ -310,18 +336,18 @@ def empty_like(input, *args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.empty_like(torch.randn(2, 3)) # the same as torch.empty_like(torch.randn(2, 3), 2.3) - torch.tensor([[-4.0271e-20, 4.5887e-41, -4.0271e-20], - [ 4.5887e-41, 4.4842e-44, 0.0000e+00]]) + tensor([[-3.6515e+14, 4.5900e-41, -1.3266e-36], + [ 3.0802e-41, 4.4842e-44, 0.0000e+00]]) >>> ttorch.empty_like({ - >>> 'a': torch.randn(2, 3), - >>> 'b': torch.randn(4, ), - >>> }) - ttorch.tensor({ - 'a': torch.tensor([[-1.1978e+27, 3.0918e-41, -1.1976e+27], - [ 3.0918e-41, 8.9683e-44, 0.0000e+00]]), - 'b': torch.tensor([-4.0271e-20, 4.5887e-41, -4.0271e-20, 4.5887e-41]), - }) + ... 'a': torch.randn(2, 3), + ... 'b': {'x': torch.randn(4, )}, + ... }) + + ├── a --> tensor([[-3.6515e+14, 4.5900e-41, -3.6515e+14], + │ [ 4.5900e-41, 1.1592e-41, 0.0000e+00]]) + └── b --> + └── x --> tensor([-1.3267e-36, 3.0802e-41, -3.8049e-38, 3.0802e-41]) """ return torch.empty_like(input, *args, **kwargs) @@ -339,19 +365,13 @@ def all(input, *args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.all(torch.tensor([True, True])) # the same as torch.all - torch.tensor(True) + tensor(True) - >>> ttorch.all(ttorch.tensor({ - >>> 'a': [True, True], - >>> 'b': [True, True], - >>> })) - torch.tensor(True) + >>> ttorch.all(ttorch.tensor({'a': [True, True], 'b': {'x': [True, True]}})) + tensor(True) - >>> ttorch.all(ttorch.tensor({ - >>> 'a': [True, True], - >>> 'b': [True, False], - >>> })) - torch.tensor(False) + >>> ttorch.all(ttorch.tensor({'a': [True, True], 'b': {'x': [True, False]}})) + tensor(False) .. note:: @@ -360,14 +380,13 @@ def all(input, *args, **kwargs): If what you need is a tree of boolean tensors, you should do like this >>> ttorch.tensor({ - >>> 'a': [True, True], - >>> 'b': [True, False], - >>> }).map(torch.all) - ttorch.tensor({ - 'a': torch.tensor(True), - 'b': torch.tensor(False), - }) - + ... 'a': [True, True], + ... 'b': {'x': [True, False]}, + ... }).map(lambda x: torch.all(x)) + + ├── a --> tensor(True) + └── b --> + └── x --> tensor(False) """ return torch.all(input, *args, **kwargs) @@ -385,19 +404,13 @@ def any(input, *args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.any(torch.tensor([False, False])) # the same as torch.any - torch.tensor(False) + tensor(False) - >>> ttorch.any(ttorch.tensor({ - >>> 'a': [True, False], - >>> 'b': [False, False], - >>> })) - torch.tensor(True) + >>> ttorch.any(ttorch.tensor({'a': [True, False], 'b': {'x': [False, False]}})) + tensor(True) - >>> ttorch.any(ttorch.tensor({ - >>> 'a': [False, False], - >>> 'b': [False, False], - >>> })) - torch.tensor(False) + >>> ttorch.any(ttorch.tensor({'a': [False, False], 'b': {'x': [False, False]}})) + tensor(False) .. note:: @@ -407,13 +420,12 @@ def any(input, *args, **kwargs): >>> ttorch.tensor({ >>> 'a': [True, False], - >>> 'b': [False, False], - >>> }).map(torch.any) - ttorch.tensor({ - 'a': torch.tensor(True), - 'b': torch.tensor(False), - }) - + >>> 'b': {'x': [False, False]}, + >>> }).map(lambda x: torch.any(x)) + + ├── a --> tensor(True) + └── b --> + └── x --> tensor(False) """ return torch.any(input, *args, **kwargs) @@ -423,6 +435,37 @@ def any(input, *args, **kwargs): @tireduce(torch.min) @func_treelize(return_type=TreeObject) def min(input, *args, **kwargs): + """ + In ``treetensor``, you can get the ``min`` result of a whole tree with this function. + + Example:: + + >>> import torch + >>> import treetensor.torch as ttorch + >>> ttorch.min(torch.tensor([1.0, 2.0, 1.5])) # the same as torch.min + tensor(1.) + + >>> ttorch.min(ttorch.tensor({ + ... 'a': [1.0, 2.0, 1.5], + ... 'b': {'x': [[1.8, 0.9], [1.3, 2.5]]}, + ... })) + tensor(0.9000) + + .. note:: + + In this ``min`` function, the return value should be a tensor with single value. + + If what you need is a tree of tensors, you should do like this + + >>> ttorch.tensor({ + ... 'a': [1.0, 2.0, 1.5], + ... 'b': {'x': [[1.8, 0.9], [1.3, 2.5]]}, + ... }).map(lambda x: torch.min(x)) + + ├── a --> tensor(1.) + └── b --> + └── x --> tensor(0.9000) + """ return torch.min(input, *args, **kwargs) @@ -431,6 +474,37 @@ def min(input, *args, **kwargs): @tireduce(torch.max) @func_treelize(return_type=TreeObject) def max(input, *args, **kwargs): + """ + In ``treetensor``, you can get the ``max`` result of a whole tree with this function. + + Example:: + + >>> import torch + >>> import treetensor.torch as ttorch + >>> ttorch.max(torch.tensor([1.0, 2.0, 1.5])) # the same as torch.max + tensor(2.) + + >>> ttorch.max(ttorch.tensor({ + ... 'a': [1.0, 2.0, 1.5], + ... 'b': {'x': [[1.8, 0.9], [1.3, 2.5]]}, + ... })) + tensor(2.5000) + + .. note:: + + In this ``max`` function, the return value should be a tensor with single value. + + If what you need is a tree of tensors, you should do like this + + >>> ttorch.tensor({ + ... 'a': [1.0, 2.0, 1.5], + ... 'b': {'x': [[1.8, 0.9], [1.3, 2.5]]}, + ... }).map(lambda x: torch.max(x)) + + ├── a --> tensor(2.) + └── b --> + └── x --> tensor(2.5000) + """ return torch.max(input, *args, **kwargs) @@ -439,6 +513,37 @@ def max(input, *args, **kwargs): @tireduce(torch.sum) @func_treelize(return_type=TreeObject) def sum(input, *args, **kwargs): + """ + In ``treetensor``, you can get the ``sum`` result of a whole tree with this function. + + Example:: + + >>> import torch + >>> import treetensor.torch as ttorch + >>> ttorch.sum(torch.tensor([1.0, 2.0, 1.5])) # the same as torch.sum + tensor(4.5000) + + >>> ttorch.sum(ttorch.tensor({ + ... 'a': [1.0, 2.0, 1.5], + ... 'b': {'x': [[1.8, 0.9], [1.3, 2.5]]}, + ... })) + tensor(11.) + + .. note:: + + In this ``sum`` function, the return value should be a tensor with single value. + + If what you need is a tree of tensors, you should do like this + + >>> ttorch.tensor({ + ... 'a': [1.0, 2.0, 1.5], + ... 'b': {'x': [[1.8, 0.9], [1.3, 2.5]]}, + ... }).map(lambda x: torch.sum(x)) + + ├── a --> tensor(4.5000) + └── b --> + └── x --> tensor(6.5000) + """ return torch.sum(input, *args, **kwargs) @@ -447,28 +552,33 @@ def sum(input, *args, **kwargs): @func_treelize() def eq(input, other, *args, **kwargs): """ + In ``treetensor``, you can get the equality of the two tree tensors with :func:`eq`. Examples:: >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.eq( - >>> torch.tensor([[1, 2], [3, 4]]), - >>> torch.tensor([[1, 1], [4, 4]]), - >>> ) - torch.tensor([[ True, False], - [False, True]]) + ... torch.tensor([[1, 2], [3, 4]]), + ... torch.tensor([[1, 1], [4, 4]]), + ... ) + tensor([[ True, False], + [False, True]]) >>> ttorch.eq( - >>> ttorch.tensor({ - >>> 'a': [[1, 2], [3, 4]], - >>> 'b': [1.0, 1.5, 2.0], - >>> }), - >>> ttorch.tensor({ - >>> 'a': [[1, 1], [4, 4]], - >>> 'b': [1.3, 1.2, 2.0], - >>> }), - >>> ) + ... ttorch.tensor({ + ... 'a': [[1, 2], [3, 4]], + ... 'b': [1.0, 1.5, 2.0], + ... }), + ... ttorch.tensor({ + ... 'a': [[1, 1], [4, 4]], + ... 'b': [1.3, 1.2, 2.0], + ... }), + ... ) + + ├── a --> tensor([[ True, False], + │ [False, True]]) + └── b --> tensor([False, False, True]) """ return torch.eq(input, other, *args, **kwargs) @@ -477,6 +587,35 @@ def eq(input, other, *args, **kwargs): @doc_from(torch.ne) @func_treelize() def ne(input, other, *args, **kwargs): + """ + In ``treetensor``, you can get the non-equality of the two tree tensors with :func:`ne`. + + Examples:: + + >>> import torch + >>> import treetensor.torch as ttorch + >>> ttorch.ne( + ... torch.tensor([[1, 2], [3, 4]]), + ... torch.tensor([[1, 1], [4, 4]]), + ... ) + tensor([[False, True], + [ True, False]]) + + >>> ttorch.ne( + ... ttorch.tensor({ + ... 'a': [[1, 2], [3, 4]], + ... 'b': [1.0, 1.5, 2.0], + ... }), + ... ttorch.tensor({ + ... 'a': [[1, 1], [4, 4]], + ... 'b': [1.3, 1.2, 2.0], + ... }), + ... ) + + ├── a --> tensor([[False, True], + │ [ True, False]]) + └── b --> tensor([ True, True, False]) + """ return torch.ne(input, other, *args, **kwargs) @@ -484,6 +623,35 @@ def ne(input, other, *args, **kwargs): @doc_from(torch.lt) @func_treelize() def lt(input, other, *args, **kwargs): + """ + In ``treetensor``, you can get less-than situation of the two tree tensors with :func:`lt`. + + Examples:: + + >>> import torch + >>> import treetensor.torch as ttorch + >>> ttorch.lt( + ... torch.tensor([[1, 2], [3, 4]]), + ... torch.tensor([[1, 1], [4, 4]]), + ... ) + tensor([[False, False], + [ True, False]]) + + >>> ttorch.lt( + ... ttorch.tensor({ + ... 'a': [[1, 2], [3, 4]], + ... 'b': [1.0, 1.5, 2.0], + ... }), + ... ttorch.tensor({ + ... 'a': [[1, 1], [4, 4]], + ... 'b': [1.3, 1.2, 2.0], + ... }), + ... ) + + ├── a --> tensor([[False, False], + │ [ True, False]]) + └── b --> tensor([ True, False, False]) + """ return torch.lt(input, other, *args, **kwargs) @@ -491,6 +659,35 @@ def lt(input, other, *args, **kwargs): @doc_from(torch.le) @func_treelize() def le(input, other, *args, **kwargs): + """ + In ``treetensor``, you can get less-than-or-equal situation of the two tree tensors with :func:`le`. + + Examples:: + + >>> import torch + >>> import treetensor.torch as ttorch + >>> ttorch.le( + ... torch.tensor([[1, 2], [3, 4]]), + ... torch.tensor([[1, 1], [4, 4]]), + ... ) + tensor([[ True, False], + [ True, True]]) + + >>> ttorch.le( + ... ttorch.tensor({ + ... 'a': [[1, 2], [3, 4]], + ... 'b': [1.0, 1.5, 2.0], + ... }), + ... ttorch.tensor({ + ... 'a': [[1, 1], [4, 4]], + ... 'b': [1.3, 1.2, 2.0], + ... }), + ... ) + + ├── a --> tensor([[ True, False], + │ [ True, True]]) + └── b --> tensor([ True, False, True]) + """ return torch.le(input, other, *args, **kwargs) @@ -498,6 +695,35 @@ def le(input, other, *args, **kwargs): @doc_from(torch.gt) @func_treelize() def gt(input, other, *args, **kwargs): + """ + In ``treetensor``, you can get greater-than situation of the two tree tensors with :func:`gt`. + + Examples:: + + >>> import torch + >>> import treetensor.torch as ttorch + >>> ttorch.gt( + ... torch.tensor([[1, 2], [3, 4]]), + ... torch.tensor([[1, 1], [4, 4]]), + ... ) + tensor([[False, True], + [False, False]]) + + >>> ttorch.gt( + ... ttorch.tensor({ + ... 'a': [[1, 2], [3, 4]], + ... 'b': [1.0, 1.5, 2.0], + ... }), + ... ttorch.tensor({ + ... 'a': [[1, 1], [4, 4]], + ... 'b': [1.3, 1.2, 2.0], + ... }), + ... ) + + ├── a --> tensor([[False, True], + │ [False, False]]) + └── b --> tensor([False, True, False]) + """ return torch.gt(input, other, *args, **kwargs) @@ -505,6 +731,35 @@ def gt(input, other, *args, **kwargs): @doc_from(torch.ge) @func_treelize() def ge(input, other, *args, **kwargs): + """ + In ``treetensor``, you can get greater-than-or-equal situation of the two tree tensors with :func:`ge`. + + Examples:: + + >>> import torch + >>> import treetensor.torch as ttorch + >>> ttorch.ge( + ... torch.tensor([[1, 2], [3, 4]]), + ... torch.tensor([[1, 1], [4, 4]]), + ... ) + tensor([[ True, True], + [False, True]]) + + >>> ttorch.ge( + ... ttorch.tensor({ + ... 'a': [[1, 2], [3, 4]], + ... 'b': [1.0, 1.5, 2.0], + ... }), + ... ttorch.tensor({ + ... 'a': [[1, 1], [4, 4]], + ... 'b': [1.3, 1.2, 2.0], + ... }), + ... ) + + ├── a --> tensor([[ True, True], + │ [False, True]]) + └── b --> tensor([False, True, True]) + """ return torch.ge(input, other, *args, **kwargs) @@ -521,23 +776,22 @@ def equal(input, other): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.equal( - >>> torch.tensor([1, 2, 3]), - >>> torch.tensor([1, 2, 3]), - >>> ) # the same as torch.equal + ... torch.tensor([1, 2, 3]), + ... torch.tensor([1, 2, 3]), + ... ) # the same as torch.equal True >>> ttorch.equal( - >>> ttorch.tensor({ - >>> 'a': torch.tensor([1, 2, 3]), - >>> 'b': torch.tensor([[4, 5], [6, 7]]), - >>> }), - >>> ttorch.tensor({ - >>> 'a': torch.tensor([1, 2, 3]), - >>> 'b': torch.tensor([[4, 5], [6, 7]]), - >>> }), - >>> ) + ... ttorch.tensor({ + ... 'a': torch.tensor([1, 2, 3]), + ... 'b': torch.tensor([[4, 5], [6, 7]]), + ... }), + ... ttorch.tensor({ + ... 'a': torch.tensor([1, 2, 3]), + ... 'b': torch.tensor([[4, 5], [6, 7]]), + ... }), + ... ) True - """ return torch.equal(input, other) @@ -553,17 +807,16 @@ def tensor(*args, **kwargs): >>> import torch >>> import treetensor.torch as ttorch >>> ttorch.tensor(True) # the same as torch.tensor(True) - torch.tensor(True) + tensor(True) >>> ttorch.tensor([1, 2, 3]) # the same as torch.tensor([1, 2, 3]) - torch.tensor([1, 2, 3]) + tensor([1, 2, 3]) >>> ttorch.tensor({'a': 1, 'b': [1, 2, 3], 'c': [[True, False], [False, True]]}) - ttorch.Tensor({ - 'a': torch.tensor(1), - 'b': torch.tensor([1, 2, 3]), - 'c': torch.tensor([[True, False], [False, True]]), - }) - + + ├── a --> tensor(1) + ├── b --> tensor([1, 2, 3]) + └── c --> tensor([[ True, False], + [False, True]]) """ return torch.tensor(*args, **kwargs) -- GitLab