Skip to main content

How to Stack Tensors in TensorFlow with tf.stack

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Several tensors of the same shape often belong together: one per day, one per photo. tf.stack combines them into a single tensor with one extra axis, so they can be worked on all at once. This guide stacks three days of coffee sales in TensorFlow 2.22.0, two ways.

Syntax​

Syntax
tf.stack([<tensor 1>, <tensor 2>, ...], axis=<axis>)
PartWhat it means
[<tensor 1>, <tensor 2>, ...]A list of tensors that all have the same shape and type
axis=<axis>Optional. Where the new axis goes. The default, 0, puts it first, so each tensor becomes one row
ResultOne tensor with one more axis than each input. The new axis has one entry per tensor in the list

The tf.stack API page: it "Stacks a list of rank-R tensors into one rank-(R+1) tensor", and its input is "A list of Tensor objects with the same shape and type." On the shape: "Given a list of length N of tensors of shape (A, B, C); if axis == 0 then the output tensor will have the shape (N, A, B, C). If axis == 1 then the output tensor will have the shape (A, N, B, C)."

The Example​

Three cafes count the cups of coffee they sell. Each day is a tensor of three numbers, one per cafe: Monday [12, 15, 11], Tuesday [14, 13, 16] and Wednesday [10, 18, 12]. The script stacks the days into one table, first with a row per day, then with a row per cafe.

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Write the Script​

stack_demo.py
import tensorflow as tf

# Cups of coffee sold by three cafes, one tensor per day
monday = tf.constant([12, 15, 11])
tuesday = tf.constant([14, 13, 16])
wednesday = tf.constant([10, 18, 12])

print(tf.stack([monday, tuesday, wednesday]))
print(tf.stack([monday, tuesday, wednesday], axis=1))

The stack_demo.py script in a macOS-style code window: three tensors of coffee sales for Monday, Tuesday and Wednesday, stacked with tf.stack, once with the default axis and once with axis=1.

Each day has the shape (3,): three numbers in a row. Stacking three of them adds an axis of length 3, one entry per day.

Step 2: Run It​

Terminal
uv run python stack_demo.py
Output
tf.Tensor(
[[12 15 11]
[14 13 16]
[10 18 12]], shape=(3, 3), dtype=int32)
tf.Tensor(
[[12 14 10]
[15 13 18]
[11 16 12]], shape=(3, 3), dtype=int32)

The terminal after uv run python stack_demo.py: the first result has one row per day, 12 15 11, 14 13 16 and 10 18 12, and the second has one row per cafe, 12 14 10, 15 13 18 and 11 16 12, both with shape (3, 3).

Both results have the shape (3, 3), but they're arranged differently. With the default axis=0, each day became a row: Monday's 12 15 11 on top. With axis=1, the new axis went second, so each cafe became a row: the first cafe's 12 14 10 from Monday to Wednesday. The command works unchanged in PowerShell.

The first run in a new environment is slow, while TensorFlow loads for the first time.

Common Mistakes​

MistakeWhat happensDo this instead
Stacking tensors of different lengthsWith Tuesday as [14, 13]: InvalidArgumentError: … Shapes of all inputs must match: values[0].shape = [3] != values[1].shape = [2] [Op:Pack]Make every tensor the same shape first. Fixed: Shapes of all inputs must match … [Op:Pack] in tf.stack shows it with photos of different sizes
Stacking tensors of different typesWith [1.5, 2.5, 3.5] as the second tensor: InvalidArgumentError: cannot compute Pack as input #1(zero-based) was expected to be a int32 tensor but is a float tensor [Op:Pack]Convert them to one type with tf.cast first
Using tf.stack to join tensors end to endtf.stack adds an axis, giving a tableUse tf.concat: tf.concat([monday, tuesday], axis=0) returned one row of 6, [12 15 11 14 13 16]

FAQ​

How do I split the stacked tensor back into days?​

tf.unstack does the opposite: tf.unstack(days) returned a list of 3 tensors, and the second was Tuesday's [14 13 16].

How does this make a batch of images?​

A model takes several images at once, stacked along a new first axis. Two images of shape (180, 180, 3) stacked into (2, 180, 180, 3): 2 images of 180 × 180 pixels with 3 colour values each.

Conclusion​

tf.stack combined three days of shape (3,) into one (3, 3) tensor: one row per day with the default axis=0, one row per cafe with axis=1. Every tensor in the list must have the same shape and type, and tf.concat is the one to use for joining them end to end.

Additional Resources​