ML Foundations

Choose whether NumPy batch joins add an axis

Compare stack and concatenate when joining two feature batches and check the resulting shape.

Example results for Choose whether NumPy batch joins add an axis. stack shape: 2, 2, 2
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

A new batch axis changes the contract

Two feature batches can be combined by introducing a new axis or by extending an existing row axis. Both operations can preserve all values while producing different tensor shapes. NumPy stack introduces a new axis; concatenate joins arrays along an existing one. [1, 2]

If each input is a two-row, two-feature batch, stacking on axis zero produces two batches of two rows. Concatenating on axis zero produces a single four-row matrix. A downstream function expecting rows and features needs the latter contract unless it explicitly understands a separate batch axis.

References: [1], [2]

Check shapes and the location of values

Run python examples/numpy_stack_batch_axis.py. Both inputs have shape (2, 2). The stack result has shape (2, 2, 2), while the concatenated result has shape (4, 2). Assertions also locate the second input as a complete slice of the stack and as the last two rows of the joined matrix. [1, 2]

Stack requires the input arrays to have the same shape. Concatenate requires matching shapes on all axes except the axis being joined, allowing different numbers of rows when axis zero is selected. Check the actual API and desired axis before relying on either operation. [1, 2]

If labels are joined separately, preserve the same row ordering. Equal shapes or a successful join do not prove that features and labels correspond semantically.

Executed with Python 3.12.14 and local NumPy; stdout and checksum are in private evidence.

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
stacked = np.stack([a, b], axis=0)
joined = np.concatenate([a, b], axis=0)
assert stacked.shape == (2, 2, 2)
assert joined.shape == (4, 2)
assert stacked[1].tolist() == b.tolist()
assert joined[2:].tolist() == b.tolist()
print("stack shape:", stacked.shape)
print("concat shape:", joined.shape)
print("second batch:", stacked[1].tolist())
print("joined final rows:", joined[2:].tolist())

Recorded output

stack shape: (2, 2, 2)
concat shape: (4, 2)
second batch: [[5, 6], [7, 8]]
joined final rows: [[5, 6], [7, 8]]

References: [1], [2]

Specify what each axis means

Write down batch, row and feature axes in the consumer contract before changing the join. A shape assertion catches an accidental extra dimension; a small value assertion can also catch incorrect ordering when shapes happen to match.

The fixture contains equal-sized two-dimensional integer batches. It does not address padding variable-length sequences, empty input sequences, memory usage or execution speed.

References: [1], [2]

Sources

  1. NumPy stack
  2. NumPy concatenate