reduce_sum by certain dimension
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Reducing a tensor by sum along a certain dimension means summing values across one or more axes while leaving the other axes intact. The operation is simple once you remember one rule: the axis you pass is the axis that disappears unless you explicitly keep it. Most confusion comes from mixing up rows and columns or from expecting the output shape to stay the same.
The Basic Idea of reduce_sum
In TensorFlow, tf.reduce_sum sums elements across an axis:
Output:
No axis was provided, so TensorFlow summed every element in the tensor and returned a scalar.
Sum Along One Axis
Now sum by a specific dimension:
Output:
Interpretation:
- '
axis=0collapses rows and sums each column' - '
axis=1collapses columns and sums each row'
That is the core pattern for most reduce_sum questions.
Think in Terms of Shape
Shape reasoning makes the axis argument much easier to understand.
If x has shape (2, 3):
- reduce on
axis=0and the0axis disappears, leaving shape(3,) - reduce on
axis=1and the1axis disappears, leaving shape(2,)
So a good debugging habit is:
When the output shape surprises you, the axis choice is usually the reason.
Keep the Reduced Dimension with keepdims=True
Sometimes you want the reduced axis to remain as a size-1 dimension, especially for broadcasting:
Output:
Without keepdims=True, the shape would be (2,). Keeping dimensions is often helpful in normalization or broadcasting workflows.
Reduce Across Multiple Dimensions
You can reduce across more than one axis at once:
Output:
Here TensorFlow sums across the last two dimensions and keeps only the first one.
This is common in batch-oriented code, where you want one summary value per batch item.
Negative Axes Also Work
TensorFlow lets you refer to axes from the end:
axis=-1 means "the last dimension," so this gives the same result as axis=1 for a rank-2 tensor.
Negative axes are handy when the exact rank may vary but the last feature dimension still has the same meaning.
A Practical Example
Suppose you have a batch of model outputs shaped:
and you want the sum across timesteps:
The result shape is:
because the time dimension was reduced away.
This kind of shape reasoning is more important than memorizing examples.
Common Pitfalls
The biggest mistake is mixing up axis=0 and axis=1. In a matrix, axis=0 reduces rows and leaves columns, while axis=1 reduces columns and leaves rows.
Another issue is forgetting that the reduced dimension disappears unless keepdims=True is used. That often causes unexpected broadcasting or shape-mismatch problems later.
Developers also sometimes pass data shaped differently than they assume. When the input shape is wrong, the axis argument can look wrong even though the real issue is the tensor layout.
Finally, reducing all dimensions by accident can return a scalar when you expected a vector or matrix. If that happens, check whether you omitted axis entirely.
Summary
- '
reduce_sumsums values across the axis or axes you specify.' - The reduced axis disappears unless you set
keepdims=True. - '
axis=0andaxis=1have different meanings because they reduce different dimensions.' - Output shape is usually the best guide to whether the reduction axis was chosen correctly.
- For debugging, print both the input shape and the reduced output shape before assuming the math is wrong.
Related reading
- Reordering a list to maximize difference of adjacent elements
- Repeatedly removing the maximum average subarray
- Representing continuous probability distributions
- Resources of techniques use for collision detection in 2D?
- Reverse factorial
- Rotated rectangle rasterisation algorithm
- Rotating a point about another point 2D
- Round number to nearest integer

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.