mindspore.ops.einsum
- mindspore.ops.einsum(equation, *operands)[source]
According to the Einstein summation Convention (Einsum), the product of the input tensor elements is summed along the specified dimension.
Note
The sublist format is also supported. For example, ops.einsum(op1, sublist1, op2, sublist2, …, sublist_out). In this format, equation can be derived by the sublists which are made up of Python's Ellipsis and list of integers in [0, 52). Each operand is followed by a sublist and an output sublist is at the end.
The value can contain only letters, commas, ellipsis and arrow. The letters represent input tensor dimension, commas represent separate tensors, ellipsis indicates the tensor dimension that you do not care about, the left of the arrow indicates the input tensors, and the right of it indicates the desired output dimension.
- Parameters
- Returns
Tensor
- Supported Platforms:
GPU
Examples
>>> import mindspore >>> import numpy as np >>> x = mindspore.tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) >>> equation = "i->" >>> output = mindspore.ops.einsum(equation, x) >>> print(output) [7.] >>> x = mindspore.tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) >>> y = mindspore.tensor(np.array([2.0, 4.0, 3.0]), mindspore.float32) >>> equation = "i,i->i" >>> output = mindspore.ops.einsum(equation, x, y) >>> print(output) [ 2. 8. 12.] >>> x = mindspore.tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mindspore.float32) >>> y = mindspore.tensor(np.array([[2.0, 3.0], [1.0, 2.0], [4.0, 5.0]]), mindspore.float32) >>> equation = "ij,jk->ik" >>> output = mindspore.ops.einsum(equation, x, y) >>> print(output) [[16. 22.] [37. 52.]] >>> x = mindspore.tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mindspore.float32) >>> equation = "ij->ji" >>> output = mindspore.ops.einsum(equation, x) >>> print(output) [[1. 4.] [2. 5.] [3. 6.]] >>> x = mindspore.tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mindspore.float32) >>> equation = "ij->j" >>> output = mindspore.ops.einsum(equation, x) >>> print(output) [5. 7. 9.] >>> x = mindspore.tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mindspore.float32) >>> equation = "...->" >>> output = mindspore.ops.einsum(equation, x) >>> print(output) [21.] >>> x = mindspore.tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32) >>> y = mindspore.tensor(np.array([2.0, 4.0, 1.0]), mindspore.float32) >>> equation = "j,i->ji" >>> output = mindspore.ops.einsum(equation, x, y) >>> print(output) [[ 2. 4. 1.] [ 4. 8. 2.] [ 6. 12. 3.]] >>> x = mindspore.tensor([1, 2, 3, 4], mindspore.float32) >>> y = mindspore.tensor([1, 2], mindspore.float32) >>> output = mindspore.ops.einsum(x, [..., 1], y, [..., 2], [..., 1, 2]) >>> print(output) [[1. 2.] [2. 4.] [3. 6.] [4. 8.]]