Foundations: Tensors & Data
Introduction to Tensors

The Archimedes receives a mysterious data stream from deep space.

What have the parsing rules found in the stream from Sector 7-G?

Six hours, fourteen minutes. I have applied 4,096 parsing rules to the stream. All have failed. None of my rules describes it.


Then we need someone who doesn't start from rules. Demir, you think in data structures; start there.
Start with one packet: store its readings, inspect the result, and release the before the next packet arrives. A tensor adds named operations and a to numeric data.
Inspect One Packet
A tensor is a numeric array with a shape, the size of each axis. A flat list of three readings has shape [3]. TensorFlow.js, imported as tf, supplies operations on those numbers.
import * as tf from '@tensorflow/tfjs';
const streamValues = [0.3, 0.7, 0.1];
const reading = tf.tensor(streamValues);
const shape = reading.shape;
const values = Array.from(reading.dataSync());
// sum() creates a temporary tensor. tidy releases it after the read.
const total = tf.tidy(() => reading.sum().dataSync()[0]);
console.log(shape); // [3]
console.log(values); // approximately [0.3, 0.7, 0.1], stored as float32
console.log(total); // approximately 1.1
reading.dispose(); // release the retained tensor when finishedThe shape tells you how the readings are arranged. dataSync() returns their values as a ; Array.from() copies them into the plain JavaScript array used in the challenge. The sum is a numerical operation, not a trained model. You have not taught the system to classify anything yet.
A tensor's counts its axes: a scalar number has rank 0, a flat vector has rank 1, and a matrix has rank 2. A [2, 3] matrix has two axes and six values. The next lesson asks which values belong together when the shape changes.
Dispose a tensor after its final use. In the example, reading lives until dispose(). The temporary sum lives only inside the synchronous tf.tidy() callback. That keeps repeated packets from accumulating tensors.
Keeping Repeated Runs Small
Dispose tensors when you finish with them, on every backend. tf.memory().numTensors counts tensors that have not been disposed. It is useful for detecting unexpected growth, but it is not a browser-process memory measurement.
GPU resources need explicit cleanup. CPU values use JavaScript-managed storage, but relying on garbage collection leaves TFJS's bookkeeping behind and makes code unsafe when it switches backends. Initialization, intentional caches and retained model can also raise the count; growth alone does not identify a leak.
Use a cleanup scope for temporary tensors and dispose anything you retain:
// Tool 1: dispose(), explicit cleanup
const reading = tf.tensor1d([0.3, 0.7, 0.1]);
console.log(tf.memory().numTensors); // compare with the count before creating reading
reading.dispose(); // release its backend resources
console.log(tf.memory().numTensors); // back to the earlier count if nothing else changed
// Tool 2: tf.tidy(), automatic, for everything intermediate.
// Every tensor created inside the callback is disposed when it
// returns, EXCEPT the one you return.
const result = tf.tidy(() => {
const a = tf.tensor1d([1, 2, 3]); // disposed automatically
const b = tf.tensor1d([4, 5, 6]); // disposed automatically
return a.add(b); // survives: it's the return value
});
console.log(await result.array()); // [5, 7, 9]
result.dispose(); // a returned tensor outlives tidy, so the caller owns ittf.tidy() runs a synchronous callback and disposes its temporary tensors. Do not pass it an async function. Return a tensor you need to read asynchronously, await its data outside the callback, then dispose it. Unlike a React effect cleanup, tidy finishes as soon as the callback returns.
You can try the channel-layout experiment now and return to this challenge. That preview does not mark either lesson complete.
Challenge
Now it's your turn. Create your first tensor from a JavaScript array.
One more thing lands in the archive between lessons, starting now: DISPATCH intercepts, relay chatter from other crews working the same band on other missions. This is the first.
DISPATCH archive · field intercept #02-T: Yara's collective in Nova Canvas ships stacks of images with the color channel as the second axis, shape
[batch, channels, h, w]. This course's TFJS models put the channel last,[batch, h, w, channels]. Two of those axis names are new; lesson 2 explains them. Different domain, different layout, same primitive.