🚀 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.reduce_sum()

AI & DATA SCIENCE // tf-reduce-sum

tf.reduce_sum() adds up the elements of a tensor along one or more specified axes, collapsing (reducing) that dimension down to a single value.

Syntax

tf.reduce_sum(input_tensor, axis=None)

Deep Dive Course

Without an axis argument, reduce_sum() collapses the entire tensor down to a single scalar sum of every element. Specifying an axis instead sums only along that dimension, collapsing it away while preserving the others — axis=0 sums down each column, collapsing rows, while axis=1 sums across each row, collapsing columns, the same axis convention used throughout NumPy and TensorFlow. The reduce prefix on this and similar functions, reduce_mean, reduce_max, etc., specifically signals that the operation collapses, or reduces, one or more dimensions of the input.

1Understanding tf.reduce_sum()

Without an axis argument, reduce_sum() collapses the entire tensor down to a single scalar sum of every element. Specifying an axis instead sums only along that dimension, collapsing it away while preserving the others — axis=0 sums down each column, collapsing rows, while axis=1 sums across each row, collapsing columns, the same axis convention used throughout NumPy and TensorFlow. The reduce prefix on this and similar functions, reduce_mean, reduce_max, etc., specifically signals that the operation collapses, or reduces, one or more dimensions of the input.

💡

Remember the axis argument names the dimension being collapsed away, not the one that remains in the result — axis=0 on a 2D tensor reduces rows down to a single value per column, which trips people up expecting the opposite.

editor.html
import tensorflow as tf

x = tf.constant([[1, 2, 3], [4, 5, 6]])
print(tf.reduce_sum(x))
localhost:3000

2Practical Example

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

editor.html
import tensorflow as tf

x = tf.constant([[1, 2, 3], [4, 5, 6]])
print(tf.reduce_sum(x, axis=1))
localhost:3000

3Best Practices

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

1. Double-check whether axis=0 or axis=1 matches your intent by testing on a small example, since the reduced-vs-remaining dimension is easy to get backwards

2. Use reduce_sum() over a mask tensor, 0s and 1s, as a common technique to count how many elements satisfy a condition

3. Pass keepdims=True when you need the reduced dimension to remain in the output shape as a size-1 axis, instead of being removed entirely, which matters for broadcasting the result back against the original tensor

⚠️

Tip: Remember the axis argument names the dimension being collapsed away, not the one that remains in the result — axis=0 on a 2D tensor reduces rows down to a single value per column, which trips people up expecting the opposite.

editor.html
import tensorflow as tf

x = tf.constant([[1, 2, 3], [4, 5, 6]])
print(tf.reduce_sum(x))
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf

x = tf.constant([[1, 2, 3], [4, 5, 6]])
print(tf.reduce_sum(x))
Example 02Advanced Example
import tensorflow as tf

x = tf.constant([[1, 2, 3], [4, 5, 6]])
print(tf.reduce_sum(x, axis=1))

Best Practices

  • Double-check whether axis=0 or axis=1 matches your intent by testing on a small example, since the reduced-vs-remaining dimension is easy to get backwards
  • Use reduce_sum() over a mask tensor, 0s and 1s, as a common technique to count how many elements satisfy a condition
  • Pass keepdims=True when you need the reduced dimension to remain in the output shape as a size-1 axis, instead of being removed entirely, which matters for broadcasting the result back against the original tensor

Interview Question

For a 2D tensor, why does tf.reduce_sum(x, axis=0) return one value per column instead of one value per row?

Hint: Think about what 'axis' actually names — the dimension being collapsed or the dimension that remains.

The axis argument specifies which dimension gets collapsed, or reduced away, by the operation — axis=0 refers to the row dimension, so specifying it tells TensorFlow to sum down through the rows for each column position, collapsing all the rows into one row of column-wise sums. The result therefore has one value per column, the dimension that remains after the row dimension is reduced, which is the opposite of what many people intuitively expect, since axis 0 sounds like it should describe what survives, not what's collapsed.

Exercises

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

x = tf.constant([[1, 2, 3], [4, 5, 6]])
print(tf.reduce_sum(x))

Frequently Asked Questions

For a 2D tensor, why does tf.reduce_sum(x, axis=0) return one value per column instead of one value per row?

The axis argument specifies which dimension gets collapsed, or reduced away, by the operation — axis=0 refers to the row dimension, so specifying it tells TensorFlow to sum down through the rows for each column position, collapsing all the rows into one row of column-wise sums. The result therefore has one value per column, the dimension that remains after the row dimension is reduced, which is the opposite of what many people intuitively expect, since axis 0 sounds like it should describe what survives, not what's collapsed.

Related Functions

tf-reduce-meantf-argmaxnp-sum