Reshaping and Flattening

Reshaping changes how the same numbers are arranged. It should not change what the numbers are.

Suppose an image has shape (2, 3).

[
  [1, 2, 3],
  [4, 5, 6]
]

Flattening turns it into one long vector:

[1, 2, 3, 4, 5, 6]

The shape changed from (2, 3) to (6), but the number of entries stayed the same.

The entry count must match

Reshaping is valid only when the total number of entries is preserved.

(2, 3) has 2 x 3 = 6 entries
(6)    has 6 entries
(3, 2) has 3 x 2 = 6 entries

All three shapes can hold the same six numbers.

But (2, 3) cannot be reshaped into (4, 2) without adding or removing entries, because:

2 x 3 = 6
4 x 2 = 8

Why this matters

Flattening is common when a model turns a structured object into a feature vector. For example, a small image, patch, or activation map may be flattened before a dense layer.

Flattening can be useful, but it also removes visible structure. After flattening, nearby pixels, positions, or channels become just entries in a vector unless the model architecture preserves that structure elsewhere.

DL-C01-T04-001Exercise: Flatten a small array

An array has shape (4, 5). What is its flattened size?

Compute it first, then check your number.

HintCount entries

Multiply the axis sizes.

SolutionWork it out

The array has 4 x 5 = 20 entries, so the flattened vector has size 20.

DL-C01-T04-002Exercise: Check a reshape

Can shape (3, 4) be reshaped into (2, 6) without changing the number of entries? Enter 1 for yes or 0 for no.

Compute it first, then check your number.

HintCompare products

Compute 3 x 4 and 2 x 6.

SolutionWork it out

3 x 4 = 12 and 2 x 6 = 12. The entry count is preserved, so the reshape is possible.

DL-C01-T04-003Exercise: Reject an invalid reshape

Can shape (3, 4) be reshaped into (5, 3) without changing the number of entries? Enter 1 for yes or 0 for no.

Compute it first, then check your number.

HintCompare products

The entry count must match exactly.

SolutionWork it out

3 x 4 = 12, but 5 x 3 = 15. The reshape is not valid without adding or removing values.