tf.multiply
tf.matmul
dot product
TensorFlow
matrix operations

tf.multiply vs tf.matmul to calculate the dot product

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

tf.multiply and tf.matmul both combine tensors, but they perform fundamentally different operations. If you are trying to compute a dot product, choosing the wrong one can give you either the wrong shape or a runtime error.

What Each Operation Actually Does

tf.multiply(a, b) performs element-wise multiplication. Each position in a is multiplied by the corresponding position in b, after TensorFlow applies normal broadcasting rules.

python
1import tensorflow as tf
2
3a = tf.constant([1.0, 2.0, 3.0])
4b = tf.constant([4.0, 5.0, 6.0])
5
6product = tf.multiply(a, b)
7print(product.numpy())  # [ 4. 10. 18.]

This is not a dot product yet. It is only the pairwise multiplication step.

tf.matmul(a, b) performs matrix multiplication. It follows linear algebra rules, so the inner dimensions must match. That means it expects tensors that behave like matrices, not plain rank-1 vectors.

python
1left = tf.constant([[1.0, 2.0, 3.0]])
2right = tf.constant([[4.0], [5.0], [6.0]])
3
4result = tf.matmul(left, right)
5print(result.numpy())  # [[32.]]

Here the result is the scalar dot product stored in a 1 x 1 matrix.

Calculating a Dot Product the Right Way

A vector dot product is the sum of element-wise products. In formula form, it is the sum of a[i] * b[i] across every index. With TensorFlow, the most direct way to express that idea for rank-1 tensors is:

python
1import tensorflow as tf
2
3a = tf.constant([1.0, 2.0, 3.0])
4b = tf.constant([4.0, 5.0, 6.0])
5
6dot = tf.reduce_sum(tf.multiply(a, b))
7print(dot.numpy())  # 32.0

This works well when you already have vectors and want a scalar.

You can also use tf.matmul, but you need to reshape the inputs into a row vector and a column vector first:

python
1import tensorflow as tf
2
3a = tf.constant([1.0, 2.0, 3.0])
4b = tf.constant([4.0, 5.0, 6.0])
5
6a_row = tf.reshape(a, [1, -1])
7b_col = tf.reshape(b, [-1, 1])
8
9dot = tf.matmul(a_row, b_col)
10print(dot.numpy())  # [[32.]]

This form becomes useful when the vectors are part of a larger matrix pipeline and you want to stay inside matrix multiplication semantics.

Better Alternatives for Intent

For readability, TensorFlow also offers APIs that express the operation more directly:

python
1import tensorflow as tf
2
3a = tf.constant([1.0, 2.0, 3.0])
4b = tf.constant([4.0, 5.0, 6.0])
5
6dot = tf.tensordot(a, b, axes=1)
7print(dot.numpy())  # 32.0

tf.tensordot is often the cleanest choice when you mean a dot product and do not want to reshape tensors manually.

If you are multiplying a matrix by a vector rather than a vector by a vector, tf.linalg.matvec is often a better fit than either raw tf.multiply or an awkwardly shaped tf.matmul.

When to Use tf.multiply

Use tf.multiply when you need element-wise scaling, masking, or pairwise operations that preserve the original structure.

python
1import tensorflow as tf
2
3logits = tf.constant([2.5, 1.0, -0.5])
4mask = tf.constant([1.0, 0.0, 1.0])
5
6masked = tf.multiply(logits, mask)
7print(masked.numpy())  # [ 2.5  0.  -0.5]

That is the right tool when every element stays aligned with its partner.

When to Use tf.matmul

Use tf.matmul for real matrix multiplication, such as neural network layers, projections, attention score calculations, and batched linear algebra.

python
1import tensorflow as tf
2
3inputs = tf.constant([[1.0, 2.0]])
4weights = tf.constant([[0.5, 1.0], [1.5, -1.0]])
5
6outputs = tf.matmul(inputs, weights)
7print(outputs.numpy())  # [[ 3.5 -1. ]]

This is not element-wise multiplication. TensorFlow combines rows and columns according to matrix rules.

Common Pitfalls

The most common mistake is expecting tf.multiply(a, b) to return a scalar dot product. It only returns the element-wise products. You still need tf.reduce_sum or another reduction step.

Another common mistake is calling tf.matmul directly on rank-1 tensors. TensorFlow treats matmul as a matrix operation, so one-dimensional inputs usually need reshaping first.

Broadcasting can also hide bugs. tf.multiply may silently expand one tensor to match another shape, which can produce a result that runs without raising an error but is mathematically different from what you intended.

Finally, watch your dtypes. Mixing float32 and int32 tensors may force explicit casting before the operation succeeds.

Summary

  • 'tf.multiply is element-wise multiplication, not a full dot product'
  • A vector dot product can be written as tf.reduce_sum(tf.multiply(a, b))
  • 'tf.matmul performs matrix multiplication and usually needs reshaped vectors'
  • 'tf.tensordot(a, b, axes=1) is often the clearest TensorFlow dot-product API'
  • Use tf.matmul for linear algebra workflows and tf.multiply for pairwise tensor operations

Course illustration
Course illustration

All Rights Reserved.