Tensor¶
- class tripy.Tensor(data: Any, dtype: dtype | None = None, device: device | None = None, name: str | None = None, fetch_stack_info: bool = True)[source]¶
Bases:
object
A tensor is a multi-dimensional array that contains elements of a uniform data type.
- Parameters:
data (Any) – The data with which to initialize the tensor.
dtype (dtype | None) – The data type of the tensor.
device (device | None) – The device on which to allocate the tensor.
name (str | None) – The name of the tensor. If provided, this must be a unique string.
fetch_stack_info (bool) – Whether to fetch stack information for the tensor. Stack information allows Tripy to generate much higher quality error messages at the cost of a small overhead when initializing the tensor.
Example
1tensor = tp.Tensor([1.0, 2.0, 3.0], dtype=tp.float32)
>>> tensor tensor([1.0000, 2.0000, 3.0000], dtype=float32, loc=gpu:0, shape=(3,))
- __add__(other: Tensor | Number) Tensor ¶
Performs an elementwise sum.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([1, 2]) 2b = tp.Tensor([2, 3]) 3output = a + b
>>> a tensor([1, 2], dtype=int32, loc=gpu:0, shape=(2,)) >>> b tensor([2, 3], dtype=int32, loc=gpu:0, shape=(2,)) >>> output tensor([3, 5], dtype=int32, loc=gpu:0, shape=(2,))
- __eq__(other: Tensor | Number) Tensor ¶
Performs an ‘equal’ comparison.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([2, 3]) 2b = tp.Tensor([2, 5]) 3output = b == a
>>> a tensor([2, 3], dtype=int32, loc=gpu:0, shape=(2,)) >>> b tensor([2, 5], dtype=int32, loc=gpu:0, shape=(2,)) >>> output tensor([True, False], dtype=bool, loc=gpu:0, shape=(2,))
- __floordiv__(other: Tensor | Number) Tensor ¶
Performs an elementwise floor division.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([4.0, 6.0]) 2b = tp.Tensor([3.0, 4.0]) 3output = a // b
>>> a tensor([4.0000, 6.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> b tensor([3.0000, 4.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> output tensor([1.0000, 1.0000], dtype=float32, loc=gpu:0, shape=(2,))
- __ge__(other: Tensor | Number) Tensor ¶
Performs a ‘greater than or equal’ comparison.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([2, 3]) 2b = tp.Tensor([2, 1]) 3output = b >= a
>>> a tensor([2, 3], dtype=int32, loc=gpu:0, shape=(2,)) >>> b tensor([2, 1], dtype=int32, loc=gpu:0, shape=(2,)) >>> output tensor([True, False], dtype=bool, loc=gpu:0, shape=(2,))
- __getitem__(index: slice | int | Tensor | Sequence[slice | int | Tensor]) Tensor ¶
Returns a tensor containing a slice of this tensor.
- Parameters:
- Returns:
A tensor containing the slice of this tensor.
- Return type:
Example
1input = tp.reshape(tp.arange(6, dtype=tp.float32), (1, 2, 3, 1)) 2output = input[:, 1:2, :-1, 0]
>>> input tensor( [[[[0.0000], [1.0000], [2.0000]], [[3.0000], [4.0000], [5.0000]]]], dtype=float32, loc=gpu:0, shape=(1, 2, 3, 1)) >>> output tensor( [[[3.0000, 4.0000]]], dtype=float32, loc=gpu:0, shape=(1, 1, 2))
Example: Negative step size
1input = tp.arange(10) 2output = input[8:2:-1]
>>> input tensor([0.0000, 1.0000, 2.0000, 3.0000, 4.0000, 5.0000, 6.0000, 7.0000, 8.0000, 9.0000], dtype=float32, loc=gpu:0, shape=(10,)) >>> output tensor([8.0000, 7.0000, 6.0000, 5.0000, 4.0000, 3.0000], dtype=float32, loc=gpu:0, shape=(6,))
- __gt__(other: Tensor | Number) Tensor ¶
Performs a ‘greater than’ comparison.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([2, 3]) 2b = tp.Tensor([3, 1]) 3output = b > a
>>> a tensor([2, 3], dtype=int32, loc=gpu:0, shape=(2,)) >>> b tensor([3, 1], dtype=int32, loc=gpu:0, shape=(2,)) >>> output tensor([True, False], dtype=bool, loc=gpu:0, shape=(2,))
- __le__(other: Tensor | Number) Tensor ¶
Performs a ‘less than or equal’ comparison.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([2, 3]) 2b = tp.Tensor([2, 5]) 3output = b <= a
>>> a tensor([2, 3], dtype=int32, loc=gpu:0, shape=(2,)) >>> b tensor([2, 5], dtype=int32, loc=gpu:0, shape=(2,)) >>> output tensor([True, False], dtype=bool, loc=gpu:0, shape=(2,))
- __lt__(other: Tensor | Number) Tensor ¶
Performs a ‘less than’ comparison.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([2, 3]) 2b = tp.Tensor([1, 5]) 3output = b < a
>>> a tensor([2, 3], dtype=int32, loc=gpu:0, shape=(2,)) >>> b tensor([1, 5], dtype=int32, loc=gpu:0, shape=(2,)) >>> output tensor([True, False], dtype=bool, loc=gpu:0, shape=(2,))
- __matmul__(other: Tensor) Tensor ¶
Performs matrix multiplication between two tensors.
If both tensors are 1D, a dot product is performed.
If both tensors are 2D, matrix multiplication is performed.
If either argument, but not both, is 1D, matrix-vector multiplication is performed.
- If both tensors are 2D or higher dimensional and have differnt ranks, a dimension is inserted
and batched matrix multiplication is performed with broadcast of relevant dimension.
- Parameters:
- Returns:
A new tensor.
- Return type:
Example
1a = tp.iota((2, 3), dtype=tp.float32) 2b = tp.iota((3, 2), dtype=tp.float32) 3 4output = a @ b
>>> a tensor( [[0.0000, 0.0000, 0.0000], [1.0000, 1.0000, 1.0000]], dtype=float32, loc=gpu:0, shape=(2, 3)) >>> b tensor( [[0.0000, 0.0000], [1.0000, 1.0000], [2.0000, 2.0000]], dtype=float32, loc=gpu:0, shape=(3, 2)) >>> output tensor( [[0.0000, 0.0000], [3.0000, 3.0000]], dtype=float32, loc=gpu:0, shape=(2, 2))
- __mod__(other: Tensor | Number) Tensor ¶
Performs a modulo operation.
- Parameters:
- Returns:
A new tensor with the broadcasted shape containing the result of the modulo operation.
- Return type:
Example
1a = tp.Tensor([4.0, 6.0]) 2b = tp.Tensor([3.0, 4.0]) 3output = a % b
>>> a tensor([4.0000, 6.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> b tensor([3.0000, 4.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> output tensor([1.0000, 2.0000], dtype=float32, loc=gpu:0, shape=(2,))
- __mul__(other: Tensor | Number) Tensor ¶
Performs an elementwise multiplication.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([1.0, 2.0]) 2b = tp.Tensor([2.0, 3.0]) 3output = a * b
>>> a tensor([1.0000, 2.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> b tensor([2.0000, 3.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> output tensor([2.0000, 6.0000], dtype=float32, loc=gpu:0, shape=(2,))
- __ne__(other: Tensor | Number) Tensor ¶
Performs a ‘not equal’ comparison.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([2, 3]) 2b = tp.Tensor([1, 3]) 3output = b != a
>>> a tensor([2, 3], dtype=int32, loc=gpu:0, shape=(2,)) >>> b tensor([1, 3], dtype=int32, loc=gpu:0, shape=(2,)) >>> output tensor([True, False], dtype=bool, loc=gpu:0, shape=(2,))
- __pow__(other: Tensor | Number) Tensor ¶
Performs an elementwise exponentiation.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([1.0, 2.0]) 2b = tp.Tensor([2.0, 3.0]) 3output = a ** b
>>> a tensor([1.0000, 2.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> b tensor([2.0000, 3.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> output tensor([1.0000, 8.0000], dtype=float32, loc=gpu:0, shape=(2,))
- __radd__(other: Tensor | Number) Tensor ¶
Performs an elementwise sum.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([1, 2]) 2b = tp.Tensor([2, 3]) 3output = a + b
>>> a tensor([1, 2], dtype=int32, loc=gpu:0, shape=(2,)) >>> b tensor([2, 3], dtype=int32, loc=gpu:0, shape=(2,)) >>> output tensor([3, 5], dtype=int32, loc=gpu:0, shape=(2,))
- __rfloordiv__(other: Tensor | Number) Tensor ¶
Performs an elementwise floor division.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = 2 2b = tp.Tensor([2.0, 3.0]) 3output = a // b
>>> b tensor([2.0000, 3.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> output tensor([1.0000, 0.0000], dtype=float32, loc=gpu:0, shape=(2,))
- __rmod__(other: Tensor | Number) Tensor ¶
Performs a modulo operation.
- Parameters:
- Returns:
A new tensor with the broadcasted shape containing the result of the modulo operation.
- Return type:
Example
1a = tp.Tensor([4.0, 6.0]) 2output = 2 % a
>>> a tensor([4.0000, 6.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> output tensor([2.0000, 2.0000], dtype=float32, loc=gpu:0, shape=(2,))
- __rmul__(other: Tensor | Number) Tensor ¶
Performs an elementwise multiplication.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([1.0, 2.0]) 2b = tp.Tensor([2.0, 3.0]) 3output = a * b
>>> a tensor([1.0000, 2.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> b tensor([2.0000, 3.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> output tensor([2.0000, 6.0000], dtype=float32, loc=gpu:0, shape=(2,))
- __rpow__(other: Tensor | Number) Tensor ¶
Performs an elementwise exponentiation.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = 2.0 2b = tp.Tensor([2.0, 3.0]) 3output = a ** b
>>> b tensor([2.0000, 3.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> output tensor([4.0000, 8.0000], dtype=float32, loc=gpu:0, shape=(2,))
- __rsub__(other: Tensor | Number) Tensor ¶
Performs an elementwise subtraction.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = 1 2b = tp.Tensor([1, 2]) 3output = a - b
>>> b tensor([1, 2], dtype=int32, loc=gpu:0, shape=(2,)) >>> output tensor([0, -1], dtype=int32, loc=gpu:0, shape=(2,))
- __rtruediv__(other: Tensor | Number) Tensor ¶
Performs an elementwise division.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = 6.0 2b = tp.Tensor([2.0, 3.0]) 3output = a / b
>>> b tensor([2.0000, 3.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> output tensor([3.0000, 2.0000], dtype=float32, loc=gpu:0, shape=(2,))
- __sub__(other: Tensor | Number) Tensor ¶
Performs an elementwise subtraction.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([2, 3]) 2b = tp.Tensor([1, 2]) 3output = a - b
>>> a tensor([2, 3], dtype=int32, loc=gpu:0, shape=(2,)) >>> b tensor([1, 2], dtype=int32, loc=gpu:0, shape=(2,)) >>> output tensor([1, 1], dtype=int32, loc=gpu:0, shape=(2,))
- __truediv__(other: Tensor | Number) Tensor ¶
Performs an elementwise division.
- Parameters:
- Returns:
A new tensor with the broadcasted shape.
- Return type:
Example
1a = tp.Tensor([4.0, 6.0]) 2b = tp.Tensor([2.0, 3.0]) 3output = a / b
>>> a tensor([4.0000, 6.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> b tensor([2.0000, 3.0000], dtype=float32, loc=gpu:0, shape=(2,)) >>> output tensor([2.0000, 2.0000], dtype=float32, loc=gpu:0, shape=(2,))
- property shape: List[DimensionSize]¶
Represents the shape of the tensor.
- Returns:
A shape tensor containing the shape of this tensor.
Example
1input = tp.ones((8, 2)) 2shape = input.shape
>>> input tensor( [[1.0000, 1.0000], [1.0000, 1.0000], [1.0000, 1.0000], [1.0000, 1.0000], [1.0000, 1.0000], [1.0000, 1.0000], [1.0000, 1.0000], [1.0000, 1.0000]], dtype=float32, loc=gpu:0, shape=(8, 2)) >>> shape [8, 2]