Global average
Compute a single average from all values in a tensor.
TensorFlow · Function
tf.reduce_mean computes averages across selected tensor dimensions. It reduces all dimensions by default and can preserve reduced axes as length-one dimensions.
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.
tf.math.reduce_mean(input_tensor, axis=None, keepdims=False, name=None)| Name | Type | Description |
|---|---|---|
input_tensor | Tensor | Tensor to average; it should use a numeric type. |
axis | int, list of int, or None | Dimensions to reduce. None reduces all dimensions; values must be in [-rank(input_tensor), rank(input_tensor)). |
keepdims | bool | If true, retains reduced dimensions with length 1. |
name | string | Optional operation name. |
The reduced tensor.
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.
Compute a single average from all values in a tensor.
Average features or samples independently along one or more axes.
Keep output dimensions compatible with the original tensor for broadcasting.
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.
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.
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_meantf.reduce_sumtf.math.reduce_sumtf.keras.layers.GlobalAveragePooling3D