Compute matrix products
Dense linear algebra
TensorFlow · Function
Multiplies two tensors as matrices, using their final two dimensions and matching outer batch dimensions. Optional flags can transpose or take adjoints of either input.
tf.matmul multiplies the innermost matrix dimensions of two tensors. It can also transpose or take adjoints of either input through flags, while outer dimensions represent matching batches.
tf.matmul(a, b, transpose_a=False, transpose_b=False, adjoint_a=False, adjoint_b=False, a_is_sparse=False, b_is_sparse=False, output_type=None, grad_a=False, grad_b=False, name=None)| Name | Type | Description |
|---|---|---|
a | Tensor | First input tensor; its innermost matrix is multiplied by the corresponding matrix in b. |
b | Tensor | Second input tensor; its innermost matrix is multiplied by the corresponding matrix in a. |
transpose_a | bool | If true, transpose a before multiplication. |
transpose_b | bool | If true, transpose b before multiplication. |
adjoint_a | bool | If true, use the adjoint of a. |
adjoint_b | bool | If true, use the adjoint of b. |
a_is_sparse | bool | If true, treat a as sparse. |
b_is_sparse | bool | If true, treat b as sparse. |
output_type | DType | Optional output data type. |
grad_a | bool | Whether to compute the gradient with respect to a. |
grad_b | bool | Whether to compute the gradient with respect to b. |
name | string | Optional operation name. |
A tf.Tensor of the same type as a and b; each innermost matrix is the product of the corresponding matrices.
import tensorflow as tf
a = tf.constant([[1., 2.], [3., 4.]])
b = tf.constant([[5., 6.], [7., 8.]])
result = tf.matmul(a, b)
print(result.numpy())Multiplies two rank-2 tensors and prints their matrix product.
Dense linear algebra
Neural-network layers
Batched computation
Complex-valued computation
tf.matmul computes matrix products using the last two dimensions as matrix dimensions; preceding dimensions form matching batches. With no transpose or adjoint flags, each output element is the sum over the shared inner index.
Follow-up: How can a matrix be multiplied without explicitly creating a transposed copy? Use transpose_a or transpose_b, which are provided as arguments to the operation.
The operation returns a tf.Tensor with the same type as both inputs. For ordinary multiplication, each batch's output matrix is formed from the corresponding matrices in a and b.
Follow-up: What constraint applies before the operation can produce that output? The inputs must have compatible inner matrix dimensions after any requested transformations.
The signature exposes transpose_a, transpose_b, adjoint_a, and adjoint_b. Transpose flags rearrange an input, while adjoint flags represent the conjugate transpose for complex values.
Follow-up: Why prefer a transpose flag over explicitly applying matrix_transpose? TensorFlow documents the kwargs as a minimal-cost approach for transposing arguments.
tf.linalg.matrix_transposetf.multiplytf.keras.ops.matmultf.Variable