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

TensorFlow · Function

tf.reduce_sum

Computes sums across selected tensor dimensions. By default it reduces all dimensions and returns a one-element tensor; keepdims preserves reduced dimensions as size-one axes.

How it works

tf.reduce_sum adds tensor elements over selected dimensions. With no axis it produces a one-element tensor; with an axis it removes those dimensions unless keepdims=True, which leaves them as size-one dimensions.

Syntax

tf.math.reduce_sum(input_tensor, axis=None, keepdims=False, name=None)

Parameters

NameTypeDescription
input_tensorTensorTensor to reduce; it should have a numeric type.
axisint, list of int, or NoneDimensions to reduce. None reduces all dimensions; values must be in the range [-rank(input_tensor), rank(input_tensor)].
keepdimsboolIf true, retains reduced dimensions with length 1.
namestring or NoneOptional name for the operation.

Returns

The reduced tensor, with the same dtype as input_tensor.

Key points

  • Use it to aggregate tensor values across one or more dimensions.
  • The axis argument selects which dimensions participate in the reduction.
  • By default, each selected dimension is removed from the output shape.
  • Set keepdims to retain reduced axes as singleton dimensions for shape-compatible computations.
  • Omitting axis collapses the complete tensor to a one-element result.
  • The output preserves the input tensor's dtype.

Common mistakes

  • Each axis must be valid for the input rank; negative axes are allowed within the documented range.
  • Unlike NumPy, TensorFlow preserves the input dtype instead of upcasting uint8 and int32 results to int64.

Example: Reduce tensor dimensions

import tensorflow as tf

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

Creates a numeric matrix, sums every element, then reduces selected axes with and without retaining dimensions.

When to use it

Whole-tensor totals

Aggregate every value in a tensor into one scalar-like tensor.

Axis-wise aggregation

Compute column or row totals by selecting a particular dimension.

Shape-preserving reductions

Keep output rank compatible with broadcasting or later shape-sensitive operations.

Interview questions

QWhat does tf.reduce_sum do when axis is None?

It computes the sum across the dimensions named by axis. If axis is None, all dimensions are reduced and the result contains one element.

Follow-up: How would you preserve rank? Set keepdims=True, which retains every reduced dimension with length one.

QWhat is the effect of keepdims?

keepdims controls the shape after reduction. When false, reduced dimensions are removed; when true, they remain with length one.

Follow-up: For a rank-two tensor reduced over axis 1, what shapes result? Without keepdims the result is rank one; with it, the second dimension is size one.

QWhat are the input and output type guarantees?

The input must have a numeric type, and the returned tensor has the same dtype as input_tensor. The operation is the reduction form of elementwise tf.math.add.

Follow-up: Why can this differ from NumPy? NumPy may upcast uint8 and int32 to int64, whereas TensorFlow returns the input dtype.

  • tf.math.reduce_sum
  • tf.math.add
  • tf.reduce_mean
  • tf.reduce_prod
  • tf.sparse.reduce_sum

Source: tf.math.reduce_sum. Checked 2026-10-15.