Foundations: Tensors & Data
Understanding Tensor Shapes
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.
JavaScript
Nested arrays
[rows.length, rows[0].length]Machine Learning
Tensor.shape
grid.shape // [3, 4]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]:
32images.224pixels high per image.224pixels wide per image.3color 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.

