Tensors, Models & Inference
Tensors & Backends
import * as tf from '@tensorflow/tfjs';
const t = tf.tensor([1, 2, 3, 4], [2, 2]);
console.log(await t.array()); // async — the WebGL backend may not have
// finished the GPU computation yet
// Backends: cpu (plain JS), webgl (GPU, browser), wasm, or tfjs-node
// (native TensorFlow C++ binding — far faster than WebGL for server use)
await tf.setBackend('webgl');
console.log(tf.getBackend());Loading & Running a Pretrained Model
const model = await tf.loadLayersModel('model.json');
// Python-trained models need tensorflowjs_converter first:
// tensorflowjs_converter --input_format=keras model.h5 web_model/
const input = tf.tensor2d([[0.5, 0.2, 0.1, 0.9]]);
const prediction = model.predict(input);
console.log(await prediction.data());
input.dispose();
prediction.dispose(); // WebGL memory is NOT garbage-collected by JS —
// forgetting this leaks GPU memory over timeMemory Management with tf.tidy
// In a per-frame webcam inference loop, unmanaged intermediate tensors
// accumulate fast — tf.tidy auto-disposes everything except the return value
function classifyFrame(model, videoElement) {
return tf.tidy(() => {
const img = tf.browser.fromPixels(videoElement);
const resized = tf.image.resizeBilinear(img, [224, 224]);
const normalized = resized.div(255.0).expandDims(0);
return model.predict(normalized); // only this survives tf.tidy's cleanup
});
}
// Without tf.tidy, a webcam demo running at 30fps can crash the tab
// within minutes from GPU memory exhaustion.Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free