Hassan Al-RashidA model that works in your notebook is a hypothesis. A model that works at 3 AM on a solar farm in the Atacama Desert with no internet — that is a product. Start simple. Get something running on the device.
Your first edge model does not need to be complex. In frontend development, you start with basic form validation before building elaborate input pipelines. Edge ML follows the same principle: start with a simple anomaly detector, prove it works on the hardware, then iterate.
function validate(input) {
return input >= min && input <= max;
}function isAnomaly(reading, mean, std) {
return Math.abs(reading - mean) > 3 * std;
}Client-side form validation checks if input falls within expected bounds. Anomaly detection does the same thing — but it learns the bounds from data instead of hardcoding them.
import * as tf from '@tensorflow/tfjs';
// Sensor data: [vibration, temperature, pressure, rpm]
// Each reading is a 4-element vector
// Build a tiny model — must fit in edge device memory
function buildAnomalyDetector(): tf.Sequential {
const model = tf.sequential();
model.add(tf.layers.dense({
inputShape: [4],
units: 8,
activation: 'relu'
}));
model.add(tf.layers.dense({
units: 1,
activation: 'sigmoid' // 0 = normal, 1 = anomaly
}));
model.compile({
optimizer: 'adam',
loss: 'binaryCrossentropy',
metrics: ['accuracy']
});
return model;
}
const model = buildAnomalyDetector();
// Check model size — critical for edge deployment
const modelParams = model.countParams();
console.log(`Parameters: ${modelParams}`); // 49 parameters
console.log(`Estimated size: ${(modelParams * 4 / 1024).toFixed(1)}KB`); // ~0.2KB
// Run inference on a sensor reading
const sensorReading = tf.tensor2d([[0.85, 72.3, 14.7, 3200]]);
const prediction = model.predict(sensorReading) as tf.Tensor;
const score = prediction.dataSync()[0];
console.log(`Anomaly score: ${score.toFixed(4)}`);
// Clean up tensors — memory management matters on edge devices
sensorReading.dispose();
prediction.dispose();Notice the dispose() calls. On a device with 512MB RAM, you cannot afford memory leaks. Every tensor must be explicitly freed. This is like removing event listeners in frontend code to prevent memory leaks — except the consequences are a frozen gateway device at a remote site.
Build a simple anomaly detector for Terra Grid sensor data.
Build a TensorFlow.js sequential model with: an input dense layer (inputShape [4], 8 units, relu activation) and an output dense layer (1 unit, sigmoid activation). Compile with adam optimizer and binaryCrossentropy loss.
const model = tf.sequential(); // Add a dense layer: inputShape [4], 8 units, relu // your code here // Add output layer: 1 unit, sigmoid // your code here // Compile with adam and binaryCrossentropy // your code here
Your first edge model is deployed to Site 12. It detects anomalies in real time.
Next: understanding when to use edge vs cloud