How to do a column sum in Tensorflow?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In TensorFlow, a column sum is a reduction across axis 0 of a 2D tensor. The code is small, but the mental model matters: you are collapsing rows and keeping one result per column.
Most confusion comes from mixing up axis=0 and axis=1, especially once you move beyond a plain matrix into batched tensors. If you understand what dimension you are reducing, the rest is straightforward.
Use tf.reduce_sum With axis=0
For a standard matrix, axis=0 means "sum down the rows for each column":
Output:
That corresponds to:
- first column:
1 + 4 + 7 = 12 - second column:
2 + 5 + 8 = 15 - third column:
3 + 6 + 9 = 18
The result is a rank-1 tensor because the row dimension has been reduced away.
Compare Column Sums and Row Sums
The most common error is choosing the wrong axis. For the same matrix, axis=1 gives row sums:
Output:
So the quick rule is:
- '
axis=0means sum by column' - '
axis=1means sum by row'
That rule is worth memorizing because it shows up in many other TensorFlow reductions such as tf.reduce_mean, tf.reduce_max, and tf.reduce_min.
Preserve Dimensions When Later Code Expects Them
Some downstream operations expect the reduced result to keep its dimension for broadcasting or shape alignment. In that case, use keepdims=True:
Output:
This is useful when the result needs to remain rank-2 rather than collapsing into a flat vector.
Column Sums With Floating-Point Tensors
The same reduction works for floats:
This pattern is common in preprocessing, custom metrics, and feature aggregation pipelines. If you need column means instead of sums, replace reduce_sum with reduce_mean and keep the same axis logic.
Think Carefully About Higher-Rank Tensors
Once tensors become batched, the word "column" depends on which dimensions represent rows and columns in your data layout. For example, if the shape is [batch, rows, cols], then a per-batch column sum reduces the rows dimension:
Here, axis=1 is correct because dimension 1 represents rows inside each batch element. This is why blindly repeating "column sum means axis zero" can be misleading once the tensor is no longer a simple matrix.
The safer habit is:
- inspect the tensor shape
- decide which dimension you want to collapse
- apply the reduction to that dimension
Common Pitfalls
The biggest mistake is swapping axis=0 and axis=1. If the result looks transposed from what you expected, the axis is the first thing to check.
Another common issue is forgetting that reduction changes shape. A vector result may break later code that expected a matrix. keepdims=True is often the fix.
People also use the word "column" too casually for higher-rank tensors. Once batching or channel dimensions appear, define the layout explicitly before choosing an axis.
Finally, be aware of dtype. Large integer reductions may need a wider dtype, and floating-point aggregation can accumulate rounding error in long pipelines.
Summary
- Use
tf.reduce_sum(tensor, axis=0)for column sums on a plain 2D tensor. - Use
axis=1for row sums on that same matrix. - Add
keepdims=Truewhen later code needs the reduced dimension preserved. - For higher-rank tensors, choose the axis based on the actual layout, not on the word "column" alone.
- Check shape and dtype whenever a reduction result looks wrong.
Related reading
- How to Do a Simple CLI Query for a Saved Estimator Model?
- How to do batching in Tensorflow Serving?
- How to do matrix-scalar multiplication in TensorFlow?
- How to do multi-class image classification in keras?
- How to do gaussian/polynomial regression with scikit-learn?
- How to do gradient clipping in pytorch?
- How to do multi GPU training with Keras?
- How to do Multiclass classification with Keras?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.