# matmul¶

`paddle.` `matmul` ( x, y, transpose_x=False, transpose_y=False, name=None ) [source]

Applies matrix multiplication to two tensors. matmul follows the complete broadcast rules, and its behavior is consistent with np.matmul.

Currently, the input tensors’ number of dimensions can be any, matmul can be used to achieve the dot, matmul and batchmatmul.

The actual behavior depends on the shapes of \(x\), \(y\) and the flag values of `transpose_x`, `transpose_y`. Specifically:

• If a transpose flag is specified, the last two dimensions of the tensor are transposed. If the tensor is ndim-1 of shape, the transpose is invalid. If the tensor is ndim-1 of shape \([D]\), then for \(x\) it is treated as \([1, D]\), whereas for \(y\) it is the opposite: It is treated as \([D, 1]\).

The multiplication behavior depends on the dimensions of x and y. Specifically:

• If both tensors are 1-dimensional, the dot product result is obtained.

• If both tensors are 2-dimensional, the matrix-matrix product is obtained.

• If the x is 1-dimensional and the y is 2-dimensional, a 1 is prepended to its dimension in order to conduct the matrix multiply. After the matrix multiply, the prepended dimension is removed.

• If the x is 2-dimensional and y is 1-dimensional, the matrix-vector product is obtained.

• If both arguments are at least 1-dimensional and at least one argument is N-dimensional (where N > 2), then a batched matrix multiply is obtained. If the first argument is 1-dimensional, a 1 is prepended to its dimension in order to conduct the batched matrix multiply and removed after. If the second argument is 1-dimensional, a 1 is appended to its dimension for the purpose of the batched matrix multiple and removed after. The non-matrix (exclude the last two dimensions) dimensions are broadcasted according the broadcast rule. For example, if input is a (j, 1, n, m) tensor and the other is a (k, m, p) tensor, out will be a (j, k, n, p) tensor.

Parameters
• x (Tensor) – The input tensor which is a Tensor.

• y (Tensor) – The input tensor which is a Tensor.

• transpose_x (bool) – Whether to transpose \(x\) before multiplication.

• transpose_y (bool) – Whether to transpose \(y\) before multiplication.

• name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.

Returns

The output Tensor.

Return type

Tensor

Examples:

```import paddle
import numpy as np

# vector * vector
x_data = np.random.random().astype(np.float32)
y_data = np.random.random().astype(np.float32)
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
z = paddle.matmul(x, y)
print(z.numpy().shape)
# 

# matrix * vector
x_data = np.random.random([10, 5]).astype(np.float32)
y_data = np.random.random().astype(np.float32)
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
z = paddle.matmul(x, y)
print(z.numpy().shape)
# 

# batched matrix * broadcasted vector
x_data = np.random.random([10, 5, 2]).astype(np.float32)
y_data = np.random.random().astype(np.float32)
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
z = paddle.matmul(x, y)
print(z.numpy().shape)
# [10, 5]

# batched matrix * batched matrix
x_data = np.random.random([10, 5, 2]).astype(np.float32)
y_data = np.random.random([10, 2, 5]).astype(np.float32)
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
z = paddle.matmul(x, y)
print(z.numpy().shape)
# [10, 5, 5]

# batched matrix * broadcasted matrix
x_data = np.random.random([10, 1, 5, 2]).astype(np.float32)
y_data = np.random.random([1, 3, 2, 5]).astype(np.float32)
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
z = paddle.matmul(x, y)
print(z.numpy().shape)
# [10, 3, 5, 5]
```