Skip to content
Reading as a guest: progress is saved in this browser. Create a free account and we’ll carry your progress over.

Foundations: Tensors & Data

Understanding Tensor Shapes

Mission Map
2 / 6 · Concept
◎ Concept

Shape describes the size of each dimension in a tensor.

ARCHIMEDES SENSOR LOG · STREAM 7-G · Repeating structure detected at two distinct scales. Dimensional layout: unresolved. ARIA parse status: failed, 4,096 of 4,096 rules.

Every has a : a tuple of numbers describing how many elements exist along each . If you've ever checked array.length or calculated rows × cols for a grid layout, you already think in shapes.

You will be able to

  • Read and interpret tensor shapes
  • Predict where each value lands after a reshape (row-major order)
  • Choose reshape or transpose from the axis meanings and required values
  • Recognize common shapes in ML (batches, sequences, images)

The Wrong Kind of Sixty-Four

Farah has a candidate layout for the stream: [450, 64], meaning 450 readings by 64 channels. The plotter needs one row per channel, shape [64, 450]. Demir reaches for reshape. Will each channel's values stay together?

Work it out on a small packet first. Each input row is a time step; each column is one channel. The numbers mark positions so you can follow exactly where they move.

The axis names explain the result: input[time][channel] must become output[channel][time]. A transpose swaps those axes. A reshape changes row lengths while preserving flat order. Both can produce the requested dimensions, so checking shape alone cannot catch this mistake.

Read Other Shapes

A list of ten values has shape [10]. A of 32 RGB images can have shape [32, 224, 224, 3]: examples, height, width, and color channels. A batch is a group of examples processed together. Record these axis meanings; the dimensions alone cannot tell you what the data represents.

For later in the course, [32, 5, 3] means 32 windows, five time steps in each, and three per step. The same indexing discipline applies: decide what each axis means before transforming the data.

Challenge

Reshape a flat tensor into a matrix.

ARCHIMEDES SENSOR LOG · STREAM 7-G · Repeating structure detected at two distinct scales. Dimensional layout: unresolved. ARIA parse status: failed, 4,096 of 4,096 rules.

Dr. Farah

That log line is the most interesting thing on this ship. The stream isn't a wall of numbers; it has structure. Before we analyze anything, I want to know its dimensional layout.

Every has a : a tuple of numbers describing how many elements exist along each . If you've ever checked array.length or calculated rows × cols for a grid layout, you already think in shapes.

The Wrong Kind of Sixty-Four

Farah has a candidate layout for the stream: [450, 64], meaning 450 readings by 64 channels. The plotter needs one row per channel, shape [64, 450]. Demir reaches for reshape. Will each channel's values stay together?

Work it out on a small packet first. Each input row is a time step; each column is one channel. The numbers mark positions so you can follow exactly where they move.

Channel experiment

Give the plotter one row per sensor

Rows arrive in time order. Each column belongs to one channel. These numbers mark positions in a small teaching packet; they are not signal measurements.

Channel layout controls

Input: [3 times, 4 channels]. Selected channel values are marked *.

Input: [3 times, 4 channels]
TimeCh 0Ch 1Ch 2Ch 3
00*123
14*567
28*91011

A prediction is optional. You can try either operation or open the explanation.

Both operations produce shape [4, 3]. Which keeps a channel together?

Loading experiment draft… This activity does not mark the lesson complete. Continue through the lesson to its coding challenge.

This tab keeps the practice record. Sign in to save a summary across devices. Answers stay in this browser.

The axis names explain the result: input[time][channel] must become output[channel][time]. A transpose swaps those axes. A reshape changes row lengths while preserving flat order. Both can produce the requested dimensions, so checking shape alone cannot catch this mistake.

Reference: flat order, reshape code, and the full archive example

Reshape preserves flat order

Here's the question that actually trips people up when you reshape: not what the new shape is, but which value ends up where. The answer: reshape fills the new shape row by row (row-major order). Watch where each value lands:

tensor-shapes.tstypescript
import * as tf from '@tensorflow/tfjs';

// Shape [6]: a 1D vector with 6 elements
const flat = tf.tensor([1, 2, 3, 4, 5, 6]);
console.log(flat.shape);  // [6]

// Reshape to [2, 3]: fills row by row
const matrix = flat.reshape([2, 3]);
matrix.print();
// [[1, 2, 3],
//  [4, 5, 6]]

// Reshape to [3, 2]: SAME flat order, different row breaks
const tall = flat.reshape([3, 2]);
tall.print();
// [[1, 2],
//  [3, 4],
//  [5, 6]]

// Note what did NOT happen: [3, 2] is not the transpose of [2, 3].
// matrix.transpose() would give [[1, 4], [2, 5], [3, 6]], different
// values in different places. Reshape rewraps the line; transpose
// reorders it.

// The total elements must match: 2*3 = 3*2 = 6
// This would throw: flat.reshape([2, 4]) (2*4=8 ≠ 6)
flat.dispose();
matrix.dispose();
tall.dispose();

Reshape preserves the logical order of values. Read the flattened values as one sequence and place the new row breaks without moving any values. This describes the operation's result; physical storage depends on the backend.

Apply the rule to the archive

Here is that rule with the stakes attached. Farah's layout hunt has produced a candidate grid for the first stretch of the stream: [450, 64], 450 readings by 64 channels. The channel plotter wants channels as rows, shape [64, 450], and Demir reaches for the tool he just learned.

garbled-channels.tstypescript
import * as tf from '@tensorflow/tfjs';

// Stand-in for the archive block: each value is its own position in the
// flat stream, so you can see exactly where every number ends up.
const block = tf.tidy(() => tf.range(0, 450 * 64).reshape([450, 64]));  // [readings, channels]

// WRONG: reshape to [64, 450]. Right shape, garbled data.
const wrong = block.reshape([64, 450]);
console.log(wrong.arraySync()[0].slice(0, 5));  // [0, 1, 2, 3, 4]
// "Channel 0" is now the first 450 values of the flat stream: all 64
// channels of reading 0, then all of reading 1, seven full readings
// and change (448 = 7 * 64) fused into one fake trace.

// RIGHT: transpose. Same target shape, values actually reordered.
const right = block.transpose();
console.log(right.arraySync()[0].slice(0, 5));  // [0, 64, 128, 192, 256]
// Channel 0 is every 64th value: one per reading, 450 in all.
// right.arraySync()[1] starts [1, 65, 129, ...]: channel 1, same story.

// Both shapes are [64, 450], but only right preserves channel membership.
tf.dispose([block, wrong, right]);

The element count is valid in both cases, so no error warns you about the mixed channels. Record the axis meanings with the shape: block[time][channel] becomes right[channel][time]. Reshape preserves the flat sequence; transpose swaps the two axes.

Read Other Shapes

A list of ten values has shape [10]. A of 32 RGB images can have shape [32, 224, 224, 3]: examples, height, width, and color channels. A batch is a group of examples processed together. Record these axis meanings; the dimensions alone cannot tell you what the data represents.

JavaScript

Array.length
const shape = [arr.length, arr[0].length]

Machine Learning

Tensor.shape
tensor.shape // [3, 4]
Structural Bridge
Where the analogy ends
`Array.length` returns one number for a 1D structure. `Tensor.shape` returns an array of dimensions (`[batch, features]`, `[batch, h, w, channels]`, etc) and the rank itself can vary. A shape has one entry per axis; broadcasting rules depend on the full shape vector, not a single length.

For later in the course, [32, 5, 3] means 32 windows, five time steps in each, and three per step. The same indexing discipline applies: decide what each axis means before transforming the data.

Challenge

Reshape a flat tensor into a matrix.