Foundations: Tensors & Data
Understanding Tensor Shapes
’s chart shows a line for each selected sensor. checks the code behind it to make sure each line uses the right readings.
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.
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.
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.

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.
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.
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.
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.
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.
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.
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.
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.