TensorFlow
column sum
machine learning
data manipulation
tutorial

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.

Practice ML system design

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":

python
1import tensorflow as tf
2
3matrix = tf.constant([
4    [1, 2, 3],
5    [4, 5, 6],
6    [7, 8, 9],
7], dtype=tf.int32)
8
9column_sum = tf.reduce_sum(matrix, axis=0)
10print(column_sum)

Output:

text
tf.Tensor([12 15 18], shape=(3,), dtype=int32)

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:

python
row_sum = tf.reduce_sum(matrix, axis=1)
print(row_sum)

Output:

text
tf.Tensor([ 6 15 24], shape=(3,), dtype=int32)

So the quick rule is:

  • 'axis=0 means sum by column'
  • 'axis=1 means 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:

python
column_sum = tf.reduce_sum(matrix, axis=0, keepdims=True)
print(column_sum)
print(column_sum.shape)

Output:

text
tf.Tensor([[12 15 18]], shape=(1, 3), dtype=int32)

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:

python
1features = tf.constant([
2    [0.5, 1.5],
3    [2.0, 3.0],
4    [1.5, 2.5],
5], dtype=tf.float32)
6
7column_sum = tf.reduce_sum(features, axis=0)
8print(column_sum)

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:

python
1tensor = tf.constant([
2    [[1, 2], [3, 4]],
3    [[5, 6], [7, 8]],
4], dtype=tf.int32)
5
6result = tf.reduce_sum(tensor, axis=1)
7print(result)

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:

  1. inspect the tensor shape
  2. decide which dimension you want to collapse
  3. 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=1 for row sums on that same matrix.
  • Add keepdims=True when 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.