Course outline

Deep Learning / Tensor and learning foundations / Lesson 7

Broadcasting and Axis Semantics

Reason about shape expansion and detect valid operations that apply values along the wrong axis.

About 3 min readContent reviewed
On this page

Lesson goals

  • Describe intended batch and feature axes.
  • Compare feature-wise and row-wise additions.
  • Check broadcasting with recognizable synthetic values.

Name axes before operations

A tensor shape gives axis sizes but not their meaning. For a matrix of two examples and two features, both axes happen to have size two. Adding a length-two vector can therefore be numerically possible along more than one conceptual axis. Write the intended mapping before relying on a libraryโ€™s broadcasting rules.

Broadcasting often treats missing leading dimensions as size one and expands compatible dimensions conceptually. Exact rules belong to the library in use. Even when an operation succeeds and the result has the expected shape, it may add a per-example value where a per-feature value was intended.

Add a feature bias

The following self-contained list example makes feature-wise addition explicit. Each row is an example; the first bias applies to the first feature in every row and the second bias applies to the second feature. It illustrates the intended arithmetic without requiring a tensor library.

python Feature-wise addition
batch = [[1, 2], [3, 4]]
bias = [10, 20]
print([[value + bias[j] for j, value in enumerate(row)] for row in batch])

The result is [[11, 22], [13, 24]]: bias 10 affects feature one and bias 20 affects feature two.

Compare a row-wise operation

Using the row index instead applies one value to both features of each example. The resulting matrix has the same shape as the feature-wise result but different values. A shape-only test cannot distinguish these meanings; use asymmetric values and check individual positions against the data contract.

python Row-wise addition
batch = [[1, 2], [3, 4]]
bias = [10, 20]
print([[value + bias[i] for value in row] for i, row in enumerate(batch)])

The result is [[11, 12], [23, 24]] because the first row receives 10 and the second row receives 20.

Check the library operation

For an actual tensor, inspect the documentation and make dimensions explicit when needed, for example by adding a singleton dimension at the intended position. Test both a non-square batch and a single example. A non-square fixture can expose an accidental alignment that a square fixture allows, but also verify the numerical values.

Preserve the axis contract

Record shape and semantic labels at model boundaries, and assert expected dimensions before the operation. Compare a tiny batch against a manual calculation and include an unequal batch and feature count. When batching or model architecture changes, rerun the fixture; successful execution alone does not verify the intended transformation.

Common errors and fixes

A bias changes examples instead of features.

Why it happens
The vector was aligned to the wrong semantic axis.
How to fix it
Reshape or index the vector for the intended feature axis.
Prevent it next time
Name axes and check values on an asymmetric fixture.

A test passes for a square matrix but fails for a different batch size.

Why it happens
Equal dimensions hid an incorrect alignment.
How to fix it
Correct the mapping and test a non-square input.
Prevent it next time
Include different batch and feature sizes in tests.

A shape-compatible result is assumed correct.

Why it happens
Only output shape was checked.
How to fix it
Compare individual output values with a hand calculation.
Prevent it next time
Retain a small fixture with recognizable values.

2 practice exercises

1. For [[1, 2], [3, 4]] and feature bias [10, 20], calculate the result.

Show worked solution

Add 10 to the first feature and 20 to the second in each row: [[11, 22], [13, 24]]. The two rows remain separate examples.

2. Why can a two-by-two fixture hide an axis error?

Show worked solution

Both dimensions have size two, so an incorrectly aligned length-two vector may still be accepted. Test unequal dimensions and verify individual values against the intended mapping.

Key takeaways

  • Shape compatibility does not establish semantic correctness.
  • Use explicit axis meanings and recognizable values.
  • Test unequal dimensions as well as ordinary batches.

Official references