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

Foundations: Tensors & Data

Mission Map
SECTOR foundations, MISSION 2, 25 min, 1 exercise

Understanding Tensor Shapes

How can follow one sensor over time?

Each sensor’s readings over time

Farah keeps Demir’s selected readings and opens the header beside them. You’ll use its sensor order to rebuild the grouping behind his chart.

Sensor layout controls

1 One row per moment

This list holds 12 readings, so its shape is [12]. Shape tells us the size of each axis. A list has one axis. A grid has two: one for rows and one for columns.

Demir’s selected readings, in recording order.
  1. A2
  2. B20
  3. C5
  4. D8
  5. A3
  6. B18
  7. C7
  8. D8
  9. A4
  10. B16
  11. C6
  12. D9

This selection lists the sensors in order: A, B, C, D. Each group of four readings belongs to one moment. Put each group in its own row. You’ll get three rows and four columns: shape [3, 4].

Have a guess, then check it. You can also build the table first. If you look at the table before answering, we’ll record that you used help.

Loading your saved work… This activity does not complete the lesson. Continue to the Challenge section for the coding task.

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

opens the recording and the chart sent her. The lines are there, but she wants to check which readings go into each line before adding the full recording to the chart.

opens the header sent with the recording. It lists all 64 sensors in the order the recorder saved their readings at each moment.

has the connection working. Let’s check what goes into each line before we trust the chart with the full recording.

keeps ’s selection: sensors A, B, C, and D at three moments. Last time, the mission supplied the code that grouped the readings by sensor. This time, you’ll build that part with her. The readings stay the same, so you can compare your result with his chart.

’s plan in JavaScript

’s selection has 12 readings. Three rows of four give the grid a of [3, 4]. Each row holds one moment, and each column follows one sensor.

Rows and columns are the grid’s two axes. We also call an axis a . The number of axes is the : this grid has rank 2. The original list has just one axis, so it has rank 1.

After predicting the second row in the activity, select one reading in the list or table. Follow its sensor, moment, and position through both views. For example, the recording contains two readings of 8 from sensor D. They belong to different moments. Their positions let you tell them apart.

The activity shows zero-based indexes beside the selected reading. A value at row index 1 and column index 3 belongs to the second moment and sensor D. The recording notes supply those meanings.

brings the 12 selected readings over to JavaScript. The full recording stays in the archive while you arrange them.

arrange-readings.tstypescript
import * as tf from '@tensorflow/tfjs';

const flat = tf.tensor1d([2, 20, 5, 8, 3, 18, 7, 8, 4, 16, 6, 9]);

// Keep sensors A, B, C, D in that order in each row.
const grid = flat.reshape([3, 4]);
console.log(grid.arraySync());
// [[2, 20, 5, 8],
//  [3, 18, 7, 8],
//  [4, 16, 6, 9]]

// Read the second row. JavaScript starts counting at 0.
const step1 = grid.arraySync()[1];
console.log(step1); // [3, 18, 7, 8]

// Give each sensor its own row, from the first reading to the last.
const bySensor = grid.transpose();
console.log(bySensor.arraySync());
// [[2, 3, 4],     // Sensor A
//  [20, 18, 16],  // Sensor B
//  [5, 7, 6],     // Sensor C
//  [8, 8, 9]]     // Sensor D

// Release the tensors when you are done with them.
tf.dispose([flat, grid, bySensor]);

needs four readings in each row. reshape([3, 4]) takes the list in order and starts a new row after every four values. The operation keeps all 12 readings: 3 × 4 = 12.

arraySync() copies the 's values into nested JavaScript arrays. grid.arraySync()[1] reads the second row. grid[1] does not index a tensor. To read one value from that row, use a second array index: grid.arraySync()[1][3] gives sensor D's reading at the second moment.

After the reshape, can see all four sensors at each time. Her chart needs a different view: one sensor per row. transpose() turns the columns into rows. The shape becomes [4, 3]: four sensors, with three readings each.

reshape([4, 3]) also makes four rows, but its first row is [2, 20, 5]. That mixes readings from sensors A, B, and C. transpose() gives sensor A its own row: [2, 3, 4]. The two results have the same shape, but only one keeps each sensor’s readings together.

JavaScript

Nested arrays
[rows.length, rows[0].length]

Machine Learning

Tensor.shape
grid.shape // [3, 4]
Structural Bridge
Where the analogy ends
For a rectangular array, rows.length gives the number of rows. rows[0].length gives the number of values in each row. A tensor stores those counts in grid.shape. The counts do not tell Farah which row belongs to a time or a sensor. That comes from the recording notes.
Optional: images, extra axes, and memory

The same idea works for images

A shape gives the size of each axis, in order. A list of ten values has shape [10]. A grid with three rows and four columns has shape [3, 4].

You can also group images into a batch to process them together. Suppose you have 32 color images, each 224 pixels high and 224 pixels wide. Their shape could be [32, 224, 224, 3]:

  • 32 images.
  • 224 pixels high per image.
  • 224 pixels wide per image.
  • 3 color channels per pixel: red, green, and blue.

Here, the color channels come last, giving us a channels-last layout. Other models expect [32, 3, 224, 224], with color channels before height and width. Prepare the images in the order your model expects.

For another example, [32, 5, 3] could hold 32 short recordings. Each covers five points in time, with three measurements at each point. Those measurements are features. The data’s format defines what each axis represents.

What happens to the data in memory?

Reshape uses row-major order: it reads across each row before moving down to the next. It makes new rows from that sequence. It does not work out which readings belong to a sensor.

Here, TensorFlow.js lets reshape() reuse the data already stored in flat. The exercise checks that grid reuses the data in flat through reshape(). Creating a tensor from copied values would allocate separate data. The arrays from arraySync() are separate JavaScript copies.

The example above releases its tensors after reading the results. In the challenge, keep grid and bySensor so the tests can check them. step1 is a regular JavaScript array, so it does not need dispose().

Swapping axes in a bigger tensor

With more than two axes, you can tell transpose() which order you want. Axes use zero-based indexes. tensor.transpose([1, 0, 2]) puts the second axis first and the first axis second. The third stays where it is. With no argument, TensorFlow.js reverses all the axes. Check what each axis means before choosing an order.

Challenge

hands you the same 12 readings. Build the grouping steps yourself: one row per moment, then one row per sensor. Run the code to see the chart your arrangement produces.