Tensor Like

class towhee.hub.builtin.operators.tensor_like.tensor_stack(axis=0)[source]

Stack the sequence of inputs along a new axis.

Parameters

axis (int, optional) – the axis of the result array along which the inputs are stacked. Defaults to 0.

Returns

the stacked array.

Return type

ndarray

Examples:

>>> import numpy as np
>>> from towhee.functional import DataCollection
>>> dc = (
...     DataCollection.range(10)
...         .map(lambda x: np.array([x]))
...         .batch(2)
...         .tensor_stack(axis=1)
... )
>>> dc.to_list()
[array([[0, 1]]), array([[2, 3]]), array([[4, 5]]), array([[6, 7]]), array([[8, 9]])]
class towhee.hub.builtin.operators.tensor_like.tensor_unstack(axis=0)[source]

Unstack an array along given axis.

Parameters

axis (int, optional) – the axis along which to unstack the array. Defaults to 0.

Returns

sequence of result arrays.

Return type

[ndarray]

Examples:

>>> import numpy as np
>>> from towhee.functional import DataCollection
>>> dc = (
...     DataCollection.range(3)
...         .map(lambda x: np.array([x, x*2]))
...         .tensor_unstack(axis=0)
... )
>>> dc.to_list()
[[array(0), array(0)], [array(1), array(2)], [array(2), array(4)]]
class towhee.hub.builtin.operators.tensor_like.tensor_concat(axis=0)[source]

Concat the sequence of inputs along a axis (no new axis)

Parameters

axis (int, optional) – the axis alone which to concat inputs. Defaults to 0.

Returns

the stacked array.

Return type

ndarray

Examples:

>>> import numpy as np
>>> from towhee.functional import DataCollection
>>> dc = (
...     DataCollection.range(10)
...         .map(lambda x: np.array([x]))
...         .batch(2)
...         .tensor_concat(axis=0)
... )
>>> dc.to_list()
[array([0, 1]), array([2, 3]), array([4, 5]), array([6, 7]), array([8, 9])]
class towhee.hub.builtin.operators.tensor_like.tensor_split(axis=0)[source]

Split the input array along a axis.

Parameters

axis (int, optional) – the axis along which to split the array. Defaults to 0.

Returns

list of result arrays.

Return type

[ndarray]

Examples:

>>> import numpy as np
>>> from towhee.functional import DataCollection
>>> dc = (
...     DataCollection.range(3)
...         .map(lambda x: np.array([[x, x*2]]))
...         .tensor_split(axis=1)
... )
>>> dc.to_list()
[[array([[0]]), array([[0]])], [array([[1]]), array([[2]])], [array([[2]]), array([[4]])]]
class towhee.hub.builtin.operators.tensor_like.tensor_normalize(axis=0)[source]

Normalize input tensor.

Examples:

>>> import numpy
>>> from towhee.functional import DataCollection
>>> dc = DataCollection([numpy.array([3, 4]), numpy.array([6,8])])
>>> dc.tensor_normalize().to_list()
[array([0.6, 0.8]), array([0.6, 0.8])]
class towhee.hub.builtin.operators.tensor_like.tensor_random(shape)[source]

Return a random tensor filled with random numbers from [0.0, 1.0).

Parameters

shape ([int], optional) – tensor shape. Defaults to None.

Returns

output tensor.

Return type

ndarray

Examples:

>>> from towhee.functional import DataCollection
>>> DataCollection.range(5).tensor_random(shape=[1,2,3]).map(lambda x: x.shape).to_list()
[(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)]