multi_dot

paddle.linalg. multi_dot ( x, name=None ) [source]

Multi_dot is an operator that calculates multiple matrix multiplications.

Supports inputs of float16(only GPU support), float32 and float64 dtypes. This function does not support batched inputs.

The input tensor in [x] must be 2-D except for the first and last can be 1-D. If the first tensor is a 1-D vector of shape(n, ) it is treated as row vector of shape(1, n), similarly if the last tensor is a 1D vector of shape(n, ), it is treated as a column vector of shape(n, 1).

If the first and last tensor are 2-D matrix, then the output is also 2-D matrix, otherwise the output is a 1-D vector.

Multi_dot will select the lowest cost multiplication order for calculation. The cost of multiplying two matrices with shapes (a, b) and (b, c) is a * b * c. Given matrices A, B, C with shapes (20, 5), (5, 100), (100, 10) respectively, we can calculate the cost of different multiplication orders as follows: - Cost((AB)C) = 20x5x100 + 20x100x10 = 30000 - Cost(A(BC)) = 5x100x10 + 20x5x10 = 6000

In this case, multiplying B and C first, then multiply A, which is 5 times faster than sequential calculation.

Parameters
  • x ([Tensor]) – The input tensors which is a list Tensor.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

The output Tensor.

Return type

Tensor

Examples

>>> import paddle

>>> # A * B
>>> A = paddle.rand([3, 4])
>>> B = paddle.rand([4, 5])
>>> out = paddle.linalg.multi_dot([A, B])
>>> print(out.shape)
[3, 5]

>>> # A * B * C
>>> A = paddle.rand([10, 5])
>>> B = paddle.rand([5, 8])
>>> C = paddle.rand([8, 7])
>>> out = paddle.linalg.multi_dot([A, B, C])
>>> print(out.shape)
[10, 7]