🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

tf.matmul()

AI & DATA SCIENCE // tf-matmul

tf.matmul() performs matrix multiplication between two tensors, following standard linear algebra rules, and is exactly what the @ operator calls on TensorFlow tensors.

Syntax

tf.matmul(a, b)
a @ b

Deep Dive Course

For two 2D tensors, matmul() requires the number of columns in a to match the number of rows in b, and computes the standard matrix product. For higher-dimensional tensors, matmul() treats the leading dimensions as a batch, performing independent matrix multiplications between corresponding matrix pairs across the batch — this batched behavior is exactly what makes it possible to multiply an entire batch of examples through a layer's weight matrix in a single call, rather than looping over each example individually.

1Understanding tf.matmul()

For two 2D tensors, matmul() requires the number of columns in a to match the number of rows in b, and computes the standard matrix product. For higher-dimensional tensors, matmul() treats the leading dimensions as a batch, performing independent matrix multiplications between corresponding matrix pairs across the batch — this batched behavior is exactly what makes it possible to multiply an entire batch of examples through a layer's weight matrix in a single call, rather than looping over each example individually.

💡

matmul()'s batched behavior for tensors with more than 2 dimensions lets you multiply an entire batch of matrices through another matrix, or batch of matrices, in one call — this is exactly the mechanism that lets a Dense layer process a whole batch of inputs simultaneously instead of one example at a time.

editor.html
import tensorflow as tf

A = tf.constant([[1, 2], [3, 4]])
B = tf.constant([[5, 6], [7, 8]])
print(tf.matmul(A, B))
localhost:3000

2Practical Example

Here is a real-world application of tf.matmul() showing how it is used in production TensorFlow code.

editor.html
import tensorflow as tf

batch_inputs = tf.ones([32, 10])
weights = tf.ones([10, 5])
output = tf.matmul(batch_inputs, weights)
print(output.shape)
localhost:3000

3Best Practices

Follow these guidelines when working with tf.matmul():

1. Use tf.matmul() or @ for true matrix multiplication, reserving * strictly for element-wise multiplication

2. Check that inner dimensions actually match, columns of the left matrix equal rows of the right, before multiplying, to catch shape errors early

3. Rely on matmul()'s batched behavior for processing an entire batch of examples through a weight matrix at once, instead of writing an explicit loop over the batch dimension

⚠️

Tip: matmul()'s batched behavior for tensors with more than 2 dimensions lets you multiply an entire batch of matrices through another matrix, or batch of matrices, in one call — this is exactly the mechanism that lets a Dense layer process a whole batch of inputs simultaneously instead of one example at a time.

editor.html
import tensorflow as tf

A = tf.constant([[1, 2], [3, 4]])
B = tf.constant([[5, 6], [7, 8]])
print(tf.matmul(A, B))
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf

A = tf.constant([[1, 2], [3, 4]])
B = tf.constant([[5, 6], [7, 8]])
print(tf.matmul(A, B))
Example 02Advanced Example
import tensorflow as tf

batch_inputs = tf.ones([32, 10])
weights = tf.ones([10, 5])
output = tf.matmul(batch_inputs, weights)
print(output.shape)

Best Practices

  • Use tf.matmul() or @ for true matrix multiplication, reserving * strictly for element-wise multiplication
  • Check that inner dimensions actually match, columns of the left matrix equal rows of the right, before multiplying, to catch shape errors early
  • Rely on matmul()'s batched behavior for processing an entire batch of examples through a weight matrix at once, instead of writing an explicit loop over the batch dimension

Interview Question

Why does multiplying a (32, 10) batch of inputs by a (10, 5) weight matrix with tf.matmul() produce an output of shape (32, 5)?

Hint: Think about what each dimension in the input and weight shapes actually represents.

The first dimension, 32, represents the batch size, the number of independent examples being processed together, and matmul() treats it as a batch dimension it preserves rather than something it multiplies away. The second dimension of the input, 10, must match the first dimension of the weight matrix, also 10, which represents the number of input features each weight row combines — that shared dimension is exactly what gets summed over during the matrix multiplication and disappears from the result. What remains is the batch dimension, 32, still representing each example, paired with the weight matrix's second dimension, 5, representing the number of output features each example now has, giving the final (32, 5) output shape.

Exercises

MediumPractice using tf.matmul() in a real scenario.
View Solution
import tensorflow as tf

A = tf.constant([[1, 2], [3, 4]])
B = tf.constant([[5, 6], [7, 8]])
print(tf.matmul(A, B))

Frequently Asked Questions

Why does multiplying a (32, 10) batch of inputs by a (10, 5) weight matrix with tf.matmul() produce an output of shape (32, 5)?

The first dimension, 32, represents the batch size, the number of independent examples being processed together, and matmul() treats it as a batch dimension it preserves rather than something it multiplies away. The second dimension of the input, 10, must match the first dimension of the weight matrix, also 10, which represents the number of input features each weight row combines — that shared dimension is exactly what gets summed over during the matrix multiplication and disappears from the result. What remains is the batch dimension, 32, still representing each example, paired with the weight matrix's second dimension, 5, representing the number of output features each example now has, giving the final (32, 5) output shape.

Related Functions

tf-multiplytf-reshapenp-matmul