Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Function

tf.matmul

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
aTensorFirst input tensor; its innermost matrix is multiplied by the corresponding matrix in b.
bTensorSecond input tensor; its innermost matrix is multiplied by the corresponding matrix in a.
transpose_aboolIf true, transpose a before multiplication.
transpose_bboolIf true, transpose b before multiplication.
adjoint_aboolIf true, use the adjoint of a.
adjoint_bboolIf true, use the adjoint of b.
a_is_sparseboolIf true, treat a as sparse.
b_is_sparseboolIf true, treat b as sparse.
output_typeDTypeOptional output data type.
grad_aboolWhether to compute the gradient with respect to a.
grad_bboolWhether to compute the gradient with respect to b.
namestringOptional operation name.

Returns

A tf.Tensor of the same type as a and b; each innermost matrix is the product of the corresponding matrices.

Key points

  • The function performs matrix multiplication, not element-by-element multiplication.
  • Documented supported types include bfloat16, float16, float32, float64, int32, int64, complex64, and complex128.
  • Dimensions before the final two are treated as batch dimensions and must match.
  • Python's @ operator is an alternative spelling for matrix multiplication.
  • For batched tensors, corresponding innermost matrices are multiplied independently.

Common mistakes

  • Both inputs must have rank at least two after any requested transposition.
  • The two inputs must use the same data type.
  • The final two dimensions must have compatible matrix-multiplication sizes.

Example: Basic matrix multiplication

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.

When to use it

Compute matrix products

Dense linear algebra

Apply learned weights to batches

Neural-network layers

Multiply corresponding matrix batches

Batched computation

Use adjoint multiplication

Complex-valued computation

Interview questions

QHow does tf.matmul handle batched inputs?

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.

QWhat does tf.matmul return?

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.

QHow can tf.matmul transform its inputs?

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_transpose
  • tf.multiply
  • tf.keras.ops.matmul
  • tf.Variable

Source: tf.linalg.matmul | TensorFlow v2.16.1. Checked 2026-10-15.