TensorFlow
Dot Product
Vectors
Machine Learning
Linear Algebra

Dot product of two vectors in tensorflow

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In TensorFlow, the dot product of two vectors is just the sum of element-wise products. For one-dimensional tensors, the clearest options are tf.tensordot, tf.reduce_sum(a * b), or, when shapes are prepared as matrices, tf.matmul.

Start with the mathematical meaning

If a and b are vectors of the same length, their dot product is:

  • multiply matching elements
  • add the results
  • produce one scalar value

TensorFlow follows exactly that idea.

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
6result = tf.reduce_sum(a * b)
7print(result.numpy())

Output:

text
32.0

That works because the element-wise product is [4, 10, 18], and the sum is 32.

Use tf.tensordot for an explicit dot product

For vector dot products, tf.tensordot makes the intent very clear.

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
6result = tf.tensordot(a, b, axes=1)
7print(result.numpy())

With axes=1, TensorFlow contracts one axis from each input, which is exactly what you want for two vectors.

This is a good choice when you may later generalize the same code to higher-rank tensors.

tf.matmul works when you reshape first

tf.matmul is meant for matrix multiplication, so plain one-dimensional vectors are not the most natural input. But if you reshape one vector to a row matrix and the other to a column matrix, you can still compute the dot product.

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
6row = tf.reshape(a, (1, -1))
7col = tf.reshape(b, (-1, 1))
8result = tf.matmul(row, col)
9
10print(result.numpy())

This returns a 1 x 1 matrix, not a scalar, so it is usually more verbose than necessary for simple vector work.

Match shapes and dtypes deliberately

TensorFlow requires compatible shapes and usually matching dtypes. If the vectors have different lengths, the dot product is undefined. If the dtypes differ, TensorFlow may raise an error instead of guessing the cast you wanted.

python
1import tensorflow as tf
2
3a = tf.constant([1, 2, 3], dtype=tf.float32)
4b = tf.constant([4, 5, 6], dtype=tf.float32)
5
6print(tf.tensordot(a, b, axes=1).numpy())

Being explicit about dtype is a good habit, especially in ML pipelines where tensors may come from several sources.

Batched cases are a different problem

Developers sometimes ask for a "dot product" but actually need one dot product per row in a batch. In that case, element-wise multiplication plus a reduction along the last axis is often the right pattern.

python
1import tensorflow as tf
2
3a = tf.constant([[1.0, 2.0], [3.0, 4.0]])
4b = tf.constant([[5.0, 6.0], [7.0, 8.0]])
5
6result = tf.reduce_sum(a * b, axis=1)
7print(result.numpy())

That gives one scalar per row pair. It is not the same as flattening everything into one giant dot product.

Choose the most readable form

For ordinary one-dimensional vectors, tf.reduce_sum(a * b) is often the most readable. tf.tensordot is great when you want to emphasize linear algebra structure or handle higher-rank tensors later. tf.matmul is better reserved for matrix-oriented code.

The best choice is the one that matches the shapes you already have and communicates the intent clearly.

Common Pitfalls

  • Using vectors of different lengths and expecting TensorFlow to infer a valid dot product anyway.
  • Reaching for tf.matmul on plain one-dimensional vectors without reshaping.
  • Confusing batched row-wise dot products with one global dot product across all elements.
  • Ignoring dtype mismatches between the input tensors.
  • Forgetting that tf.matmul returns a matrix shape when used with reshaped vectors.

Summary

  • A vector dot product in TensorFlow is the sum of element-wise products.
  • 'tf.reduce_sum(a * b) is the simplest form for one-dimensional vectors.'
  • 'tf.tensordot(a, b, axes=1) is a clear explicit dot-product API.'
  • 'tf.matmul works too, but only after reshaping vectors into matrix form.'
  • Pay attention to shapes and dtypes so the operation matches the linear algebra you intend.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.