Dr. FarahThe data stream has structure — I can see repeating patterns at different scales. We need to understand its dimensional layout before we can analyze it.
Every tensor has a shape — a tuple of numbers describing how many elements exist along each dimension. If you've ever checked array.length or calculated rows × cols for a grid layout, you already think in shapes.
const shape = [arr.length, arr[0].length]tensor.shape // [3, 4]In frontend development, you work with shapes constantly. A responsive grid with 3 rows and 4 columns? That's shape [3, 4]. A list of 10 user cards? Shape [10]. A batch of 32 RGB images at 224×224 pixels? Shape [32, 224, 224, 3].
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] — same data, different layout
const matrix = flat.reshape([2, 3]);
console.log(matrix.shape); // [2, 3]
// Reshape to [3, 2] — still the same 6 numbers
const tall = flat.reshape([3, 2]);
console.log(tall.shape); // [3, 2]
// The total elements must match: 2*3 = 3*2 = 6
// This would throw: flat.reshape([2, 4]) — 2*4=8 ≠ 6The golden rule: reshaping never changes the data, only how it's organized. Think of it like CSS Grid — the same child elements can be arranged as 2×3 or 3×2 just by changing grid-template-columns.
Reshape a flat tensor into a matrix.
Reshape a 1D tensor of 6 elements into a 2x3 matrix.
const flat = tf.tensor([1, 2, 3, 4, 5, 6]); const matrix = null; // reshape to [2, 3]
The data dimensions are now clear — a 3D tensor of sensor readings.