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_reshape(shape)[source]¶
Reshape input tensor.
Examples:
>>> import numpy as np >>> from towhee import dc >>> df = dc['tensor']([np.array([i, j]) for i, j in zip(range(3), range(3))]) >>> df.tensor_reshape['tensor', 'new_tensor'](shape = [2, 1]).map(lambda x: x.new_tensor.shape).to_list() [(2, 1), (2, 1), (2, 1)]
>>> df.to_column() >>> df.tensor_reshape['tensor', 'new_new_tensor'](shape = [2, 1])['new_new_tensor'].map(lambda x: x.shape).to_list() [(2, 1), (2, 1), (2, 1)]
- 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:
>>> import numpy as np >>> from towhee import dc >>> df = dc['a'](range(3)) >>> df.tensor_random['a', 'b'](shape = [2, 1]).map(lambda x: x.b.shape).to_list() [(2, 1), (2, 1), (2, 1)]
>>> df.to_column() >>> df.tensor_random['a', 'c'](shape = [2, 1])['c'].map(lambda x: x.shape).to_list() [(2, 1), (2, 1), (2, 1)]