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

PyTorch · Function

torch.mean

Computes the mean of all tensor elements or reduces selected dimensions. It supports optional dtype conversion and dimension retention.

How it works

torch.mean computes the arithmetic mean of every element or reduces selected dimensions. The optional dtype casts the input before reduction, while keepdim preserves reduced dimensions as size 1.

Syntax

torch.mean(*input*, ***, *dtype=None*) → Tensor
torch.mean(*input*, *dim*, *keepdim=False*, ***, *dtype=None*, *out=None*) → Tensor

Parameters

NameTypeDescription
inputTensorThe input tensor, with floating-point or complex dtype.
dtypetorch.dtype, optionalOptional desired data type; the input is cast to it before reduction.
dimint or tuple of ints, optionalDimension or dimensions over which to reduce.
keepdimbool, optionalWhether to retain reduced dimensions as size 1.
outTensor, optionalOptional output tensor.

Returns

A Tensor containing the mean of all input elements or the means after reduction over the selected dimension(s).

Key points

  • Without dim, the function reduces the complete tensor to its overall mean.
  • Supplying dim computes means along the selected axis or axes.
  • A dimension list enables one reduction over multiple axes.
  • keepdim=True preserves reduced axes as singleton dimensions.
  • With keepdim=False, reduced axes are removed from the result.
  • dtype controls a pre-reduction cast and the resulting tensor's data type.

Common mistakes

  • An empty input produces nan because the mean of no elements is undefined.
  • The input tensor must use a floating-point or complex dtype.

Example: Overall and dimension-wise means

import torch

x = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
print(torch.mean(x))
print(torch.mean(x, dim=0, keepdim=True))

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

When to use it

Global reduction

Compute a single aggregate statistic from a tensor.

Axis-wise reduction

Average features, batches, or spatial axes independently.

Shape-preserving reductions

Maintain broadcast-compatible shapes after reducing dimensions.

Interview questions

QWhat does torch.mean return when no dimension is supplied?

With only input, torch.mean returns one scalar containing the mean of all elements. With dim, it reduces the specified dimension or dimensions instead.

Follow-up: What happens when dim is a list? It reduces across every dimension in that list.

QHow does keepdim affect the output shape?

keepdim=True leaves each reduced axis in the result with length one. With the default False, those axes are squeezed from the output shape.

Follow-up: Why use keepdim=True? It keeps shapes suitable for broadcasting against the unreduced tensor.

QWhat is the purpose of dtype?

dtype specifies the desired output data type. When provided, the input is cast to that dtype before the mean is calculated.

Follow-up: What input types does the documented function accept? Floating-point and complex tensors.

  • torch.nanmean
  • torch.squeeze
  • torch.Tensor.mean

Source: torch.mean — PyTorch 2.13 documentation. Checked 2026-10-16.