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

TensorFlow · Function

tf.reduce_mean

tf.reduce_mean computes averages across selected tensor dimensions. It reduces all dimensions by default and can preserve reduced axes as length-one dimensions.

How it works

The function averages values over the dimensions selected by axis. With no axis, it reduces every dimension to one scalar; with keepdims enabled, reduced dimensions remain as size-one axes, which preserves a broadcast-friendly shape.

Syntax

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

Parameters

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

Returns

The reduced tensor.

Key points

  • Averages tensor elements along selected dimensions.
  • The axis argument controls which dimensions participate in the reduction.
  • Leaving axis unspecified produces a one-element result from the whole tensor.
  • Use keepdims=True when the output should preserve reduced axes as size-one dimensions.
  • axis accepts the dimensions selected for averaging.
  • The input tensor is required to use a numeric data type.
  • name can optionally label the TensorFlow operation.

Common mistakes

  • Each axis index must fall within the valid range for the input tensor's rank.
  • Do not provide duplicate dimensions in axis.

Example: Reduce tensor means

import tensorflow as tf

x = tf.constant([[1., 1.], [2., 2.]])
print(tf.reduce_mean(x).numpy())
print(tf.reduce_mean(x, axis=0).numpy())
print(tf.reduce_mean(x, axis=1, keepdims=True).numpy())

Computes the overall mean, column means, and row means while retaining the reduced row dimension.

When to use it

Global average

Compute a single average from all values in a tensor.

Axis-wise aggregation

Average features or samples independently along one or more axes.

Shape-preserving reduction

Keep output dimensions compatible with the original tensor for broadcasting.

Interview questions

QWhat does tf.reduce_mean compute, and what happens when axis is None?

It computes the mean of input_tensor across the dimensions listed in axis. If axis is omitted or None, every dimension is reduced and the result contains one element.

Follow-up: How can the result retain reduced dimensions? Set keepdims=True; each reduced dimension remains with length one.

QWhat is the effect of keepdims?

keepdims=False is the default, so reduced dimensions are removed from the result shape. With keepdims=True, those dimensions remain but have length one.

Follow-up: Why might retaining them help? The resulting shape is often easier to broadcast against the original tensor.

QWhat input and output requirements does the function document?

input_tensor must have a numeric type. axis identifies dimensions to reduce, and the function returns the reduced tensor.

Follow-up: What naming option is available? name optionally assigns a name to the operation.

  • tf.math.reduce_mean
  • tf.reduce_sum
  • tf.math.reduce_sum
  • tf.keras.layers.GlobalAveragePooling3D

Source: tf.math.reduce_mean | TensorFlow v2.16.1. Checked 2026-10-15.