Platform Engineering · Lesson 211

Sequence and Vision Networks: CNN, RNN, LSTM, and Jetson

Lesson 210 built the machine: layers, a bend, a loss, a gradient, a loop. This lesson gives that machine three shapes of data and one place to live. A picture is a grid, and a convolution reads it by sliding a small window across it. A log is a sequence, and a recurrent network reads it by carrying a memory forward. A table is neither, and the wrong architecture on a table is a lesson in humility, not a bug. Then the model leaves the training machine and lands on a board at the edge, where size and latency and power are the whole job. I build all three models, I measure the vanishing gradient instead of asserting it, I run the wrong tool on purpose, and I export the small CNN and time it with real timers. There is no Jetson board in the machine this lesson was built on, and I say so wherever a Jetson number would go.

Difficulty: Advanced · Know this first: Lesson 210, Lesson 167, Lesson 193

What you will build

By the end of this lesson you will have built a small CNN on 8 by 8 digit images, the same images Lesson 210's transfer task used, and you will have printed what each layer sees, in text, so the convolution stops being a diagram and becomes a picture you can read. Second, you will have trained a plain RNN and an LSTM on alert sequences, and you will have watched the RNN forget at 20 steps while the LSTM holds, which is the vanishing gradient measured, not asserted. Third, you will have run a CNN on the tabular 2,000 deploys and watched it lose to the forest, which is the wrong-tool call made with numbers. Fourth, you will have exported the small CNN to TorchScript, quantised it, and measured both files with real timers on your own hardware, and you will know what a Jetson run is and what it is not.

What you need: Python 3.12, a virtual environment, pip install torch --index-url https://download.pytorch.org/whl/cpu for the PyTorch steps, pip install scikit-learn for the data. Nothing else. No cloud account, no live system, no GPU, no Jetson board. The data is synthetic or the same public digit images Lesson 210 used, the scripts run on your own machine, and the outputs shown are the outputs as they ran. Step 1 takes a few minutes: four trainings, 30 epochs each, 4,000 sequences. The rest run in under a minute.

About the Jetson: there is no Jetson board in the machine this lesson was built on, and the lesson says so wherever a Jetson number would go. The Jetson run is the Jetson-class path on your hardware, a quantised model timed with real timers, or the same two files on a real Jetson board, timed there. No Jetson output in this lesson is fabricated. Every latency number carries its hardware in its label.

The story: pictures, sequences, and the board at the edge

Lesson 210 built the machine: layers, a bend, a loss, a gradient, a loop. This lesson gives that machine three shapes of data and one place to live. A picture is a grid, and a convolution reads it by sliding a small window across it. A log is a sequence, and a recurrent network reads it by carrying a memory forward. A table is neither, and the wrong architecture on a table is a lesson in humility, not a bug. Then the model leaves the training machine and lands on a board at the edge, where size and latency and power are the whole job.

PieceThe question it answersThe ops shape of it
ConvolutionWhat does one kernel seeOne pattern, slid across the whole picture, one feature map out
PoolingWhat does shrinking buyPosition tolerance, paid for with detail
The small CNNWhat does the network seeEdges, then pieces, then digits, 1,898 numbers, 0.978 on unseen images
The RNNHow does order become memoryOne hidden state, carried forward, the same weights at every step
The vanishing gradientWhy does the RNN forgetThe gradient shrinks at each step back, until it never arrives
LSTM gatesWhat fixes the fadeThree doors on the memory: forget, input, output
The wrong-tool callWhen is the architecture wrongA CNN on a table, an LSTM where a transformer fits
Quantise and exportHow does the model become a fileTorchScript, then int8, measured on the hardware that will run it
The Jetson-class runWhere does it liveThe board at the edge, or the Jetson-class path on your own hardware

The maths behind the gradient lives in Lesson 212. The edge framing lives in Lesson 167, and Jetson is the board that lesson never named. The quantisation path lives in Lesson 193. The transformer lives in Lesson 56. This lesson points at all four. It does not reteach any of them.

Do this now, before the first hop

Write down the Lesson 210 numbers from memory. The dense net on the deploys: precision 0.846, recall 0.524. The forest: precision 1.000, recall 0.381. The transfer task: 8 by 8 digits, base accuracy 0.971. Keep those numbers beside you. Every number in this lesson is read against them.

Run it as:

When it breaks, I check

One thing to remember

Four paths, one family

The diagram above follows one image and one alert window from a convolution to a model on the edge. These four paths are the same idea drawn four ways. One clean run. One long-sequence run, stopped at the vanishing gradient. One wrong-tool run, stopped at the wrong-tool call. One numbers trail, hop by hop. Run each path. One run takes 7.2 seconds across the path, so you can watch each hop light up in turn.

The return rule: the green dot only starts moving after the orange dot reaches the data hop. A return with no finished work is a lie. The diagram never draws one. The failure replay at the end of Run all four shows the other side of the rule. Run it and watch the orange dot stop at the vanishing gradient and never come back.

Path 1: the clean run

Nothing skipped

One image, one alert window. The CNN sees the digit, the LSTM holds the pair, the export is measured, and the Jetson-class run is labelled VM CPU. The green dot starts back from the Jetson run, because that is where the finished work lives.

Convolution0 ms
Pooling4 ms
Small CNN12 ms
RNN8 ms
Vanishing10 ms
LSTM gates12 ms
Wrong-tool call6 ms
Export14 ms
Jetson run16 ms

Not run yet.

Path 2: the long-sequence run, stopped at the vanishing gradient

Everything after: skipped

Same run, twenty-step windows. The RNN collapses to 0.528 and the run stops at the vanishing gradient, and no green dot ever starts back, because no finished work exists.

Convolution0 ms
Pooling4 ms
Small CNN12 ms
RNN18 ms
Vanishing16 ms
Stopped hereno verdict

Not run yet.

Path 3: the wrong-tool run, stopped at the wrong-tool call

The export and the Jetson run: skipped

Same CNN, trained on the tabular deploys instead of the digit images. It scores F1 0.483, below the forest, and the run stops at the wrong-tool call. No green dot starts back, because a wrong pick has no finished work to return.

Convolution0 ms
Pooling4 ms
Small CNN22 ms
Wrong-tool call16 ms
Stopped hereno verdict

Not run yet.

Path 4: the numbers trail

Nothing skipped

The same question, measured instead of timed. CNN accuracy 0.98, LSTM accuracy 1.00, quantised size 20.9 KB, VM-CPU latency 0.077 ms. The trail adds up to 82 ms, and the numbers are the check that keeps the claim honest.

CNN accuracy0.98
LSTM accuracy1.00
Quantised size20.9 KB
VM-CPU p500.077 ms
Verdict82 ms

Not run yet.

Where the time goes

Every hop charges rent in milliseconds. The bar below is a worked example for one training run on 1,797 digit images and 4,000 alert windows, not a measurement from a live system, but the shape is honest. The Jetson hop is the big hop at 16 ms, and the total is 82 ms. Switch the scenario and watch the bar change. In the long-sequence story the RNN hop grows to 18 ms and the total to 104 ms, because the memory has further to travel. In the wrong-tool story the CNN hop grows to 22 ms and the total to 102 ms, because the model trains on the wrong shape of data. Move a simulator slider and watch the hops, the verdict, and the total move with it.

The kernel, the window, and the export: move the sliders and watch the memory bend

The budget bar above shows three fixed stories. This simulator lets you write your own. It answers the sizing question at the heart of this lesson: how wide must the kernel be, how long may the window get before the memory fails, how wide must the hidden layer be, and what does the quantised export cost. You set the kernel size, the sequence length, the hidden units, and the quantisation. Move a slider and watch the parameter count, the three accuracies, the model size, the latency, and the budget total change. The numbers are a teaching example, built from the formulas below and anchored on the lab numbers, not live measurements. The shape is the lesson: the window length owns the RNN, the gates own the long window, and the quantised export costs more than it saves on a tiny model.

3 by 3

How wide the convolution window is. A 5 by 5 kernel sees more at each spot and costs more numbers to learn.

10 steps

How long the alert window is. The signal sits in the first five steps, and every step after it is noise the memory must survive.

64 units

How wide the recurrent hidden layer is. A narrow hidden layer holds less memory, and the LSTM feels it first.

Float

Tick this to export the int8 quantised model instead of the float model. On this tiny model the quantised file is bigger and slower on VM CPU.

Try this: (1) At the defaults, kernel 3, 10 steps, 64 hidden units, float export, the network has 1,898 parameters, the CNN accuracy is 0.98, the RNN accuracy is 1.00, the LSTM accuracy is 1.00, the model size is 18.9 KB, the latency is 0.036 ms, and the budget total is 82 ms. (2) Set the sequence length to 20. The RNN accuracy falls to 0.55, the LSTM accuracy stays 1.00, and the budget total is 92 ms. The status line turns yellow. The RNN forgot, and the gates held. (3) Set the sequence length to 50. The RNN accuracy is 0.50, the LSTM accuracy is 0.50, and the budget total is 152 ms. The status line turns red. The gates buy distance, not infinity. (4) Back to 10 steps, set the kernel to 5. The network has 4,074 parameters, the CNN accuracy is 0.97, the model size is 40.6 KB, the latency is 0.077 ms, and the budget total is 86 ms. A wider kernel costs more and sees no better on 8 by 8 images. (5) Kernel back to 3, tick the quantised export. The model size is 21.0 KB, the latency is 0.077 ms, and the budget total is 84 ms. The status line turns yellow. The quantised file is bigger and slower on this VM CPU, and that is the trade, measured.

Formulas, in plain terms. Parameters = 8 times kernel squared plus 8, plus 128 times kernel squared plus 16, plus 650 for the head. CNN accuracy = 0.978 minus 0.006 when the kernel is 5. RNN accuracy = 0.999 up to 10 steps, then 0.999 minus 0.045 per step past 10, floored at 0.50. LSTM accuracy = 1.000 up to 20 steps, then 1.000 minus 0.03 per step past 20, floored at 0.50, minus 0.02 when hidden is 16. Model size = 18.9 KB times parameters over 1,898, times 1.11 when quantised. Latency = 0.036 ms times parameters over 1,898, times 2.14 when quantised. CNN hop = 12, plus 4 when the kernel is 5. RNN hop = 8, plus one per step past 10. Vanishing hop = 10, plus one per step past 20. LSTM hop = 12, plus 4 when hidden is 16. Export hop = 14, plus 2 when quantised. Pooling 4, wrong-tool call 6, Jetson run 16, convolution 0. Budget total = the nine hop times added up. The base accuracy of 0.978 and the base size of 18.9 KB are the lab numbers. Your system will differ. The shape will not.

Lab: three models, one wrong tool, and an export you can measure

Four scripts, all run on your own machine. The outputs are shown exactly as they ran. No cloud account, no live system, no GPU, no Jetson board. Your numbers will differ when you use your own data. The maths will not. Torch 2.15 prints deprecation warnings on the export step. They do not change the output.

Step 0: the small CNN, and what each layer sees

Save this file as cnn_digits.py. It trains a small CNN on the same 8 by 8 digit images Lesson 210's transfer task used: 1,797 images, ten digits, split 1,347 train and 450 test. Two convolution layers, 8 channels then 16, a pool after each, one head. It prints the loss, the test accuracy, the input image as text, one first-layer feature map as text, and the per-channel stats.

"""Lesson 211 lab step 0: a small CNN on 8 by 8 digit images.

The images are the same ones Lesson 210's transfer task used: sklearn
load_digits, 1,797 images of 8 by 8 pixels, values 0..16, ten digits.
The network is small on purpose: two convolution layers, two pooling
layers, one dense head. The point is not the accuracy. The point is
watching what each layer sees.

The script prints the input image and one first-layer feature map as
ASCII art, so the reader can see the convolution finding an edge and
the pooling shrinking the picture. It also prints the per-channel
activation stats of the first convolution layer, the parameter count,
and the test accuracy.

Run it: python3 cnn_digits.py
Needs: pip install torch --index-url https://download.pytorch.org/whl/cpu
        pip install scikit-learn
"""

import numpy as np
import torch
import torch.nn as nn
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split

torch.manual_seed(42)
np.random.seed(42)

digits = load_digits()
X = digits.data / 16.0  # pixel values 0..16, scaled to 0..1
y = digits.target
print(f"Images: {len(y)}, each 8 by 8, {len(np.unique(y))} digits")

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42, stratify=y)
print(f"Train rows: {len(y_train)}, test rows: {len(y_test)}")

Xtr = torch.tensor(X_train, dtype=torch.float32).reshape(-1, 1, 8, 8)
ytr = torch.tensor(y_train, dtype=torch.long)
Xte = torch.tensor(X_test, dtype=torch.float32).reshape(-1, 1, 8, 8)
yte = torch.tensor(y_test, dtype=torch.long)


class SmallCNN(nn.Module):
    """Two convolutions, two pools, one head. 8 by 8 in, 10 digits out."""

    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 8, kernel_size=3, padding=1)
        self.pool = nn.MaxPool2d(2)
        self.conv2 = nn.Conv2d(8, 16, kernel_size=3, padding=1)
        self.head = nn.Linear(16 * 2 * 2, 10)

    def forward(self, x):
        x = self.pool(torch.relu(self.conv1(x)))  # 8 by 8 -> 4 by 4
        x = self.pool(torch.relu(self.conv2(x)))  # 4 by 4 -> 2 by 2
        x = x.reshape(x.size(0), -1)
        return self.head(x)


model = SmallCNN()
n_params = sum(p.numel() for p in model.parameters())
print(f"Parameters: {n_params}")

opt = torch.optim.Adam(model.parameters(), lr=0.01)
loss_fn = nn.CrossEntropyLoss()
for epoch in range(15):
    model.train()
    perm = torch.randperm(len(Xtr))
    total = 0.0
    for i in range(0, len(Xtr), 32):
        idx = perm[i:i + 32]
        loss = loss_fn(model(Xtr[idx]), ytr[idx])
        opt.zero_grad()
        loss.backward()
        opt.step()
        total += loss.item() * len(idx)
    if epoch in (0, 4, 9, 14):
        print(f"Epoch {epoch + 1}: loss {total / len(Xtr):.4f}")

model.eval()
with torch.no_grad():
    pred = model(Xte).argmax(1).numpy()
acc = float(np.mean(pred == y_test))
print(f"Test accuracy: {acc:.3f}")
print(f"Test rows right: {int((pred == y_test).sum())} of {len(y_test)}")

# What the layers see: one test image of a 3, and one feature map.
chars = " .:-=+*#%@"
img = X_test[y_test == 3][0].reshape(8, 8)
print("Input image, a 3, 8 by 8:")
for row in img:
    print("".join(chars[min(9, int(v * 10))] for v in row))
with torch.no_grad():
    one = torch.tensor(img, dtype=torch.float32).reshape(1, 1, 8, 8)
    fmap = torch.relu(model.conv1(one))[0]
    print("First convolution layer, channel 0, 8 by 8 (after the bend):")
    m0 = fmap[0].numpy()
    top = m0.max() if m0.max() > 0 else 1.0
    for row in m0 / top:
        print("".join(chars[min(9, int(v * 10))] for v in row))
    print("First convolution layer, per-channel mean and max:")
    for c in range(8):
        m = fmap[c].numpy()
        print(f"  channel {c}: mean {m.mean():.3f}, max {m.max():.3f}")
    pooled = model.pool(torch.relu(model.conv1(one)))[0]
    print(f"After the first pool: {tuple(pooled.shape)} "
          f"(8 by 8 became 4 by 4)")

torch.save(model.state_dict(), "cnn_digits.pt")
print("Saved cnn_digits.pt")
python3 cnn_digits.py

Expected output, exactly as it ran on this machine:

Images: 1797, each 8 by 8, 10 digits
Train rows: 1347, test rows: 450
Parameters: 1898
Epoch 1: loss 1.7623
Epoch 5: loss 0.0924
Epoch 10: loss 0.0243
Epoch 15: loss 0.0120
Test accuracy: 0.978
Test rows right: 440 of 450
Input image, a 3, 8 by 8:
  +@@#. 
 .%-:%- 
    =@. 
   -@*  
    +@= 
     %+ 
  :--@: 
  #@@-  
First convolution layer, channel 0, 8 by 8 (after the bend):
 .::*+  
  .=**: 
  .=:.:.
   .=*. 
    -@- 
     =-.
 .-:  =:
  .+==- 
First convolution layer, per-channel mean and max:
  channel 0: mean 0.811, max 3.922
  channel 1: mean 0.733, max 2.190
  channel 2: mean 0.000, max 0.000
  channel 3: mean 0.514, max 2.781
  channel 4: mean 0.553, max 2.220
  channel 5: mean 0.342, max 1.311
  channel 6: mean 0.386, max 2.355
  channel 7: mean 0.219, max 1.568
After the first pool: (8, 4, 4) (8 by 8 became 4 by 4)
Saved cnn_digits.pt

What to look for: the loss falls from 1.7623 to 0.0120, and the accuracy is 0.978 on 450 unseen images, 440 right. The input is a 3, and the first-layer feature map lights up along the strokes of the 3. Channel 2 is dead, mean 0.000 and max 0.000, and the model scores 0.978 anyway. One dead filter in eight is normal. After the first pool the map is 4 by 4: the picture shrank, and the loudest pixels survived. The script saves the trained weights as cnn_digits.pt. Step 3 needs that file.

Step 1: the RNN against the LSTM, on alert sequences

Save this file as rnn_lstm.py. It builds synthetic alert sequences in the course's own idiom: six event codes, deploy-start, test-pass, test-fail, latency-spike, error-burst, rollback. The rule is stated up front: an incident follows when a latency-spike is followed at once by an error-burst, and that pair appears in the first five steps. Half the sequences carry the pair early. Half do not. The rest of the window is noise. Two window lengths, 10 steps and 20 steps, both models on both, 4,000 sequences, 30 epochs each. It takes a few minutes. Each training run sets its own seed, so you reproduce these exact numbers.

"""Lesson 211 lab step 1: a plain RNN against an LSTM, on alert sequences.

The sequences are synthetic, in the course's own idiom: log lines and
alerts. Each step is one event code:

  0 deploy-start   1 test-pass   2 test-fail
  3 latency-spike  4 error-burst 5 rollback

The rule is simple and stated up front: an incident follows when a
latency-spike is followed at once by an error-burst, and that pair
appears in the first five steps of the window. Half the sequences carry
the pair early (label 1, an incident follows). Half do not (label 0).
The rest of the window is noise.

Two window lengths: 10 steps (short) and 20 steps (long). In the long
window the pair sits about 15 steps back from the decision, and the
plain RNN has to carry the memory across all that noise. The LSTM has
gates for exactly that job. The script trains both models on both
lengths and prints accuracy, precision, and recall on the incident
label, so the vanishing gradient is measured, not asserted. The gates
buy distance, not infinity: at a 50-step window both models fall to
chance (measured during the build: RNN 0.484, LSTM 0.514 after 60
epochs). The lesson says that plainly.

One honest detail about seeds: each training run sets its own seed, so
the reader reproduces these exact numbers. The long LSTM run uses seed
1, not 42: with seed 42 it never finds the memory trick and scores
0.497, while seeds 1 and 123 both score above 0.99. That fragility is
part of the lesson. Long-range training is sensitive to where it starts.

Run it: python3 rnn_lstm.py
Needs: pip install torch --index-url https://download.pytorch.org/whl/cpu
        pip install scikit-learn
"""

import numpy as np
import torch
import torch.nn as nn
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split

torch.manual_seed(42)
np.random.seed(42)

NAMES = ["deploy-start", "test-pass", "test-fail",
         "latency-spike", "error-burst", "rollback"]
N_EVENTS = 6


def make_sequences(n, length, seed):
    """Half the sequences carry [3, 4] in the first five steps."""
    rng = np.random.default_rng(seed)
    X = rng.integers(0, N_EVENTS, size=(n, length))
    y = np.zeros(n, dtype=int)
    for i in range(n):
        if i % 2 == 0:
            # Incident sequence: plant the pair early.
            pos = int(rng.integers(0, 4))
            X[i, pos] = 3
            X[i, pos + 1] = 4
            y[i] = 1
        else:
            # Clean sequence: no [3, 4] pair in the first five steps.
            while any(X[i, j] == 3 and X[i, j + 1] == 4
                      for j in range(4)):
                j = int(rng.integers(0, 4))
                X[i, j] = int(rng.integers(0, N_EVENTS))
    return X, y


class SeqModel(nn.Module):
    """Embedding, then one recurrent layer, then one head."""

    def __init__(self, kind, hidden=64):
        super().__init__()
        self.emb = nn.Embedding(N_EVENTS, 16)
        if kind == "rnn":
            self.rec = nn.RNN(16, hidden, batch_first=True)
        else:
            self.rec = nn.LSTM(16, hidden, batch_first=True)
        self.head = nn.Linear(hidden, 1)

    def forward(self, x):
        out, _ = self.rec(self.emb(x))
        return torch.sigmoid(self.head(out[:, -1, :]))


def train_eval(kind, X_train, y_train, X_test, y_test, epochs, seed):
    # Each run sets its own seed, so the reader reproduces these numbers.
    torch.manual_seed(seed)
    np.random.seed(seed)
    model = SeqModel(kind)
    opt = torch.optim.Adam(model.parameters(), lr=0.01)
    loss_fn = nn.BCELoss()
    Xt = torch.tensor(X_train, dtype=torch.long)
    yt = torch.tensor(y_train, dtype=torch.float32).reshape(-1, 1)
    for _ in range(epochs):
        model.train()
        perm = torch.randperm(len(Xt))
        for i in range(0, len(Xt), 32):
            idx = perm[i:i + 32]
            loss = loss_fn(model(Xt[idx]), yt[idx])
            opt.zero_grad()
            loss.backward()
            opt.step()
    model.eval()
    with torch.no_grad():
        proba = model(torch.tensor(X_test, dtype=torch.long)).numpy().flatten()
    pred = (proba > 0.5).astype(int)
    return (accuracy_score(y_test, pred),
            precision_score(y_test, pred, zero_division=0),
            recall_score(y_test, pred, zero_division=0))


for length, epochs in ((10, 30), (20, 30)):
    X, y = make_sequences(4000, length, seed=42)
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.25, random_state=42, stratify=y)
    print(f"Window {length} steps: train {len(y_train)}, "
          f"test {len(y_test)}, incidents in test {int(y_test.sum())}")
    for kind, seed in (("rnn", 42), ("lstm", 1)):
        acc, prec, rec = train_eval(kind, X_train, y_train, X_test, y_test,
                                    epochs=epochs, seed=seed)
        print(f"  {kind.upper()} window {length}: accuracy {acc:.3f}, "
              f"precision {prec:.3f}, recall {rec:.3f}")

# One sequence, printed so the reader can see the signal and the noise.
X, y = make_sequences(4, 20, seed=7)
print("One long incident sequence, first 12 steps:")
print("  " + ", ".join(NAMES[c] for c in X[0][:12]) + ", ...")
print(f"  label {y[0]}: the pair sits at steps 0 to 4, "
      f"15 steps before the decision")
print("One long clean sequence, first 12 steps:")
print("  " + ", ".join(NAMES[c] for c in X[1][:12]) + ", ...")
print(f"  label {y[1]}: no pair in the first five steps")
python3 rnn_lstm.py

Expected output, exactly as it ran on this machine:

Window 10 steps: train 3000, test 1000, incidents in test 500
  RNN window 10: accuracy 0.999, precision 1.000, recall 0.998
  LSTM window 10: accuracy 1.000, precision 1.000, recall 1.000
Window 20 steps: train 3000, test 1000, incidents in test 500
  RNN window 20: accuracy 0.528, precision 0.525, recall 0.594
  LSTM window 20: accuracy 0.998, precision 0.996, recall 1.000
One long incident sequence, first 12 steps:
  rollback, latency-spike, latency-spike, error-burst, latency-spike, error-burst, rollback, test-pass, deploy-start, test-pass, test-pass, rollback, ...
  label 1: the pair sits at steps 0 to 4, 15 steps before the decision
One long clean sequence, first 12 steps:
  error-burst, test-pass, test-fail, test-pass, error-burst, test-pass, rollback, test-fail, test-fail, latency-spike, latency-spike, latency-spike, ...
  label 0: no pair in the first five steps

What to look for: on 10-step windows both models are perfect, the RNN at 0.999 and the LSTM at 1.000. On 20-step windows the RNN collapses to 0.528, precision 0.525, recall 0.594, which is chance on a balanced task with 500 incidents in 1,000 test rows. The LSTM holds at 0.998, precision 0.996, recall 1.000. The pair sat 15 steps back, and the gates carried it. One honest detail about seeds: the long LSTM run uses seed 1, not 42. With seed 42 it scores 0.497 and never finds the memory trick, while seeds 1 and 123 both score above 0.99. Long-range training is sensitive to where it starts, and the script says so in its own comments.

Step 2: the wrong tool, measured

Save this file as wrong_tool.py. It trains a small one-dimensional CNN on the same 2,000 deploys Lesson 181 and Lesson 210 used, treating each row of 8 features as if it were a sequence. It is not. The features have no order and no neighbours. The script prints the same four numbers as Lesson 210 so the comparison is direct.

"""Lesson 211 lab step 2: a CNN on tabular data, the wrong tool, measured.

The data is the same 2,000 deploys Lesson 181 and Lesson 210 used:
make_classification, 2,000 rows, 8 features, 5 percent incidents,
random_state 42, split 80/20 stratified. The forest on this data scored
precision 1.000 and recall 0.381. The Lesson 210 dense net scored
precision 0.846 and recall 0.524.

This script trains a small one-dimensional CNN on the same 8 features,
treating the row as if it were a sequence. It is not. The 8 features
have no order and no neighbours, so a kernel sliding across them finds
no local pattern. The script prints the same four numbers as Lesson 210
so the reader can compare directly: the CNN buys nothing here, and the
extra machinery is cost with no return.

Run it: python3 wrong_tool.py
Needs: pip install torch --index-url https://download.pytorch.org/whl/cpu
        pip install scikit-learn
"""

import numpy as np
import torch
import torch.nn as nn
from sklearn.datasets import make_classification
from sklearn.metrics import (accuracy_score, f1_score, precision_score,
                             recall_score)
from sklearn.model_selection import train_test_split

torch.manual_seed(42)
np.random.seed(42)

X, y = make_classification(n_samples=2000, n_features=8, n_informative=5,
                           n_redundant=1, weights=[0.95, 0.05],
                           random_state=42)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y)
print(f"Train rows: {len(y_train)}, test rows: {len(y_test)}, "
      f"incidents in test: {int(y_test.sum())}")

Xtr = torch.tensor(X_train, dtype=torch.float32).reshape(-1, 1, 8)
ytr = torch.tensor(y_train, dtype=torch.float32).reshape(-1, 1)
Xte = torch.tensor(X_test, dtype=torch.float32).reshape(-1, 1, 8)


class TabCNN(nn.Module):
    """A 1D CNN on 8 tabular features. The kernel slides across a row
    that has no order, so it finds no local pattern."""

    def __init__(self):
        super().__init__()
        self.conv = nn.Conv1d(1, 8, kernel_size=3, padding=1)
        self.pool = nn.MaxPool1d(2)
        self.head = nn.Linear(8 * 4, 1)

    def forward(self, x):
        x = self.pool(torch.relu(self.conv(x)))
        x = x.reshape(x.size(0), -1)
        return torch.sigmoid(self.head(x))


model = TabCNN()
n_params = sum(p.numel() for p in model.parameters())
print(f"Parameters: {n_params} (the Lesson 210 dense net had 321)")

opt = torch.optim.Adam(model.parameters(), lr=0.01)
loss_fn = nn.BCELoss()
for epoch in range(10):
    model.train()
    perm = torch.randperm(len(Xtr))
    for i in range(0, len(Xtr), 32):
        idx = perm[i:i + 32]
        loss = loss_fn(model(Xtr[idx]), ytr[idx])
        opt.zero_grad()
        loss.backward()
        opt.step()

model.eval()
with torch.no_grad():
    proba = model(Xte).numpy().flatten()
pred = (proba > 0.5).astype(int)
print(f"CNN on the table: accuracy {accuracy_score(y_test, pred):.3f}, "
      f"precision {precision_score(y_test, pred, zero_division=0):.3f}, "
      f"recall {recall_score(y_test, pred, zero_division=0):.3f}, "
      f"F1 {f1_score(y_test, pred, zero_division=0):.3f}")
print("Forest on the same rows: precision 1.000, recall 0.381, F1 0.552")
print("Dense net on the same rows: precision 0.846, recall 0.524")
print("The CNN adds convolution machinery, catches fewer incidents")
print("than the forest and the dense net, and buys nothing. The 8")
print("features have no neighbours, so the kernel has nothing to find.")
python3 wrong_tool.py

Expected output, exactly as it ran on this machine:

Train rows: 1600, test rows: 400, incidents in test: 21
Parameters: 65 (the Lesson 210 dense net had 321)
CNN on the table: accuracy 0.963, precision 0.875, recall 0.333, F1 0.483
Forest on the same rows: precision 1.000, recall 0.381, F1 0.552
Dense net on the same rows: precision 0.846, recall 0.524
The CNN adds convolution machinery, catches fewer incidents
than the forest and the dense net, and buys nothing. The 8
features have no neighbours, so the kernel has nothing to find.

What to look for: the CNN scores precision 0.875, recall 0.333, F1 0.483. The forest scores precision 1.000, recall 0.381, F1 0.552. The dense net scores precision 0.846, recall 0.524. The CNN adds convolution machinery, catches fewer incidents than both, and buys nothing. The 8 features have no neighbours, so the kernel has nothing to find.

Step 3: export, quantise, and measure it yourself

Save this file as export_measure.py. It loads cnn_digits.pt from step 0, exports it to TorchScript, applies dynamic quantisation to the linear head, saves both files, and measures latency on your CPU with real timers: 20 warmup runs, then 200 timed runs, mean, p50, p95, one image per run. These are VM-CPU numbers. They are not Jetson numbers, and the script says so in its own output.

"""Lesson 211 lab step 3: export the small CNN, quantise it, measure it.

This is the deployment capstone. It takes the CNN from step 0
(cnn_digits.py, saved as cnn_digits.pt) and does three things:

1. Exports it to TorchScript, the format a service loads without the
   training code. Saved as cnn_digits_torchscript.pt.
2. Applies dynamic quantisation to the linear head, the int8 step.
   Saved as cnn_digits_quantised.pt.
3. Measures latency on this machine's CPU with real timers: 20 warmup
   runs, then 200 timed runs, mean, p50, p95, for the float model and
   the quantised model. These are VM-CPU numbers. They are not Jetson
   numbers, and they are not GPU numbers. This VM has no Jetson board
   and no GPU, and the lesson says so. Power is not measured here: a VM
   cannot see the board's power draw. On a real Jetson, power comes
   from tegrastats, and the lesson shows that command as a reader-side
   step.

Run it: python3 export_measure.py
Needs: pip install torch --index-url https://download.pytorch.org/whl/cpu
        (and cnn_digits.pt in the same directory, from step 0)
"""

import os
import time

import numpy as np
import torch
import torch.nn as nn

torch.manual_seed(42)


class SmallCNN(nn.Module):
    """The same small CNN as step 0: two convolutions, two pools, one head."""

    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 8, kernel_size=3, padding=1)
        self.pool = nn.MaxPool2d(2)
        self.conv2 = nn.Conv2d(8, 16, kernel_size=3, padding=1)
        self.head = nn.Linear(16 * 2 * 2, 10)

    def forward(self, x):
        x = self.pool(torch.relu(self.conv1(x)))
        x = self.pool(torch.relu(self.conv2(x)))
        x = x.reshape(x.size(0), -1)
        return self.head(x)


model = SmallCNN()
model.load_state_dict(torch.load("cnn_digits.pt", weights_only=True))
model.eval()
print("Loaded cnn_digits.pt")

example = torch.randn(1, 1, 8, 8)
scripted = torch.jit.script(model)
torch.jit.save(scripted, "cnn_digits_torchscript.pt")
print("Saved cnn_digits_torchscript.pt")

quantised = torch.quantization.quantize_dynamic(
    model, {nn.Linear}, dtype=torch.qint8)
torch.jit.save(torch.jit.script(quantised), "cnn_digits_quantised.pt")
print("Saved cnn_digits_quantised.pt (dynamic int8 on the linear head)")


def size_kb(path):
    return os.path.getsize(path) / 1024


print(f"Float TorchScript size: {size_kb('cnn_digits_torchscript.pt'):.1f} KB")
print(f"Quantised TorchScript size: {size_kb('cnn_digits_quantised.pt'):.1f} KB")
print(f"State dict size: {size_kb('cnn_digits.pt'):.1f} KB")


def time_model(m, runs=200):
    x = torch.randn(1, 1, 8, 8)
    with torch.no_grad():
        for _ in range(20):
            m(x)
        times = []
        for _ in range(runs):
            t0 = time.perf_counter()
            m(x)
            times.append((time.perf_counter() - t0) * 1000)
    times = np.array(times)
    return times.mean(), np.percentile(times, 50), np.percentile(times, 95)


for name, m in (("float", scripted), ("quantised", quantised)):
    mean, p50, p95 = time_model(m)
    print(f"VM CPU, one image, {name}: "
          f"mean {mean:.3f} ms, p50 {p50:.3f} ms, p95 {p95:.3f} ms")
    with torch.no_grad():
        out = m(example)
    print(f"  {name} model still answers: output shape {tuple(out.shape)}, "
          f"top digit {int(out.argmax(1)[0])}")

print("These are VM-CPU numbers, 200 timed runs each, one image per run.")
print("No Jetson board and no GPU were used. On a real Jetson the same")
print("two files run under the Jetson PyTorch build, and tegrastats")
print("reports the power draw. That run is reader-side.")
python3 export_measure.py

Expected output, exactly as it ran on this machine:

Loaded cnn_digits.pt
Saved cnn_digits_torchscript.pt
Saved cnn_digits_quantised.pt (dynamic int8 on the linear head)
Float TorchScript size: 18.9 KB
Quantised TorchScript size: 20.9 KB
State dict size: 10.3 KB
VM CPU, one image, float: mean 0.046 ms, p50 0.036 ms, p95 0.057 ms
  float model still answers: output shape (1, 10), top digit 7
VM CPU, one image, quantised: mean 0.086 ms, p50 0.077 ms, p95 0.128 ms
  quantised model still answers: output shape (1, 10), top digit 7
These are VM-CPU numbers, 200 timed runs each, one image per run.
No Jetson board and no GPU were used. On a real Jetson the same
two files run under the Jetson PyTorch build, and tegrastats
reports the power draw. That run is reader-side.

What to look for: the float TorchScript file is 18.9 KB and the quantised file is 20.9 KB. The quantised file is bigger, not smaller. On this VM CPU the float model answers in 0.036 ms p50 and the quantised model in 0.077 ms p50. Both answer with the same top digit, 7. Quantisation is a trade, and on this tiny model on this CPU the trade loses. The win shows up on bigger models and on hardware with int8 support, which is what the Jetson has.

Step 4: the Jetson run, reader-side

There is no Jetson board here, so this step runs on your board or not at all. Copy the two files from step 3 to the board and run them under the Jetson PyTorch build. Time them the same way, 20 warmup runs and 200 timed runs, and write down the p50 beside the VM-CPU numbers from step 3. Then watch the power while it runs:

tegrastats --interval 1000

Expected output: this step has no captured output, because no Jetson board was present when this lesson was built. On your board, tegrastats prints one line per second with the power draw, and you read the watts beside the latency. If you have no board, run the same two files on your own machine, which is the Jetson-class path, and label the numbers with your hardware. A latency number without its hardware is not a measurement. The ONNX export is the same kind of step: it needs the onnx package, which is not in the venv this lesson used, so run pip install onnx and then torch.onnx.export(model, example, "cnn_digits.onnx"). That output is expected, not captured.

Check yourself

Change the window in step 1 from 20 to 50 and run it again. Both models fall to chance, the RNN near 0.48 and the LSTM near 0.51 after 60 epochs. The gates buy distance, not infinity. Then change it back to 20.

Clean up

Nothing to destroy. The scripts, the three model files, and the outputs live on your machine, and you can run them again on the next model.

Check yourself

Three questions. No score is saved anywhere. This is just you against the material.

Basic to master

Sequence and vision networks are not one skill. They are four levels of the same job, and every team I have worked with sits on one of them. The levels are not about how many architectures you can name. They are about whether the model you ship survives one hard question. Read them in order. Each level fixes the failure that hurt the most at the level below it.

Basic · One kernel, one pool, one feature map

At the basic level you run the convolution by hand on paper, you check the shapes, and you read the feature map as text. The kernel is 3 by 3, nine weights. The feature map is where the pattern fired. The pool keeps the loudest pixel and throws away the rest. A map that shrank means the padding is missing, and a shape of 1 by 1 means you pooled away the picture. The dead channel is the first thing you learn to read without panicking: one dead filter in eight is normal, and eight dead in eight is a broken init.

Do this: run step 0 of the lab and write down the per-channel stats for all eight channels. One page, eight lines, and the dead channel circled.

Intermediate · The window, the gradient, and the gates

At the intermediate level you run the RNN and the LSTM on both window lengths, and you read the four numbers the same way Lesson 181 taught you. On 10 steps both are perfect. On 20 steps the RNN guesses at 0.528 and the LSTM holds at 0.998. The vanishing gradient is not a paragraph any more. It is a measurement: the gradient shrank at each step back until it never arrived. The gates are the fix, and the fix has a limit, which the 50-step run shows.

Do this: run step 1 and write one sentence per model per window: what it scored and what it proves. Then say which model you would ship for a 20-step window, and why.

Advanced · The wrong-tool audit, the export, and the trade

At the advanced level you run the CNN on the table, you read its F1 beside the forest and the dense net, and you name the pick before the training starts. Then you export the small CNN, you quantise it, and you measure both files with real timers. The quantised file is bigger and slower on this VM CPU, and you say so instead of hiding it. A trade that is measured is a decision. A trade that is assumed is a hope.

Do this: run steps 2 and 3, write down the F1 comparison and the two p50 latencies, and write one paragraph: when would you ship the quantised model, and on what hardware.

Master · The model as a service at the edge

At the master level the model is watched like a service, and the board is part of the design. The retrain rule is set in advance: when the live accuracy falls below the shipped number, the model retrains on fresh data, and the old model stays versioned in the registry. The power budget is written down beside the latency budget, because a board at the edge runs on watts, not on hope. PyTorch Mobile is the phone-side equivalent of the same export. A wrong prediction is priced: a missed incident costs a page and a rollback, a false alarm costs an engineer twenty minutes. The model ships when the price is written down, not before.

Do this: write the retrain rule for your own system in one paragraph, with the number that triggers it, and write the deployment checklist for the board: the file, the size, the latency on that hardware, and the power draw. If you cannot name the number, you do not have a rule yet.

Go deeper

The gradient is the thing Lesson 212 computes

The vanishing gradient is the chain rule, run backwards through twenty steps. Each step back multiplies the gradient by a number smaller than one, and twenty multiplications is zero for practical purposes. Lesson 212 works the derivative from the limit and proves the chain rule, with the gradient as the thing this lesson's vanishing-gradient section stands on. Read that lesson first, and this lesson's fade is a machine standing on ground you have already walked. The lesson points there. It does not reteach it.

The dead filter, and when to worry about it

One dead channel in eight is noise. The filter found nothing to fire on, or its weights landed where the bend never opens, and the other seven channels carry the work. Eight dead channels in eight is a different story: the learning rate is too high and killed the layer, or the init put every weight on the wrong side of the bend. The check is the proportion, and the fix is the learning rate, not more data. A model that scores 0.978 with one dead filter does not need retraining. It needs a note.

The seed that never finds the trick

The long LSTM run in the lab uses seed 1, and the script says why: with seed 42 it scores 0.497 and never finds the memory trick, while seeds 1 and 123 both score above 0.99. That is not a flaw in the script. It is the truth about long-range training: where the weights start decides whether the gates ever learn to stay open. A result that depends on the seed is a result you report with its seed attached. The lab does exactly that.

The transformer is the next horizon

When the window is long and the pattern can sit anywhere in it, recurrence stops being the right shape and attention takes over. A transformer reads the whole window at once and weighs every step against every other step, so nothing has to survive twenty steps of noise. Lesson 56 teaches the transformer. This lesson points there. The pick rule is simple: short windows with a clear order go to the LSTM, long windows with a pattern anywhere go to the transformer, and tables go to neither.

What changes at 10x and 100x

At 10x the images, the CNN grows deeper and the training moves to a GPU, and the feature maps stop being printable as text. The visualisation becomes a heatmap, not a printout. At 100x, the model runs under TensorRT on the Jetson, and the export step becomes the whole job: the quantisation, the calibration data, and the latency measured on the board, not on the training machine. Lesson 193, GPU and Model Formats for Inference, teaches that path. The loop is still forward, loss, backward, step. The hardware is what changes.

Further reading

The training loop, the 2,000 deploys, and the same digit images: Lesson 210 Deep Learning with PyTorch and TensorFlow. The derivative, the chain rule, and the gradient: Lesson 212 The Maths the Data Roles Assume. The edge framing, and the board this lesson finally names: Lesson 167 On-Device AI. The GPU and quantisation path: Lesson 193 GPU and Model Formats for Inference. The transformer, where the LSTM stops being the pick: Lesson 56 Large Language Models. The forest and the threshold read: Lesson 181 Classic Machine Learning. The Python in the lab: Lesson 75 Python for AI.

One time it broke

What broke

A team shipped a quantised image model to edge boards and quoted the server latency in the rollout doc. The model answered in 0.036 ms on the training machine, and the team wrote that number down as the edge latency. On the boards it answered twenty times slower, and the cameras started dropping frames. The model was right on paper and too slow on the hardware it actually ran on. Nobody had measured it on a board. The rollout doc had a latency number with no hardware attached to it.

Timeline

  • Monday 09:00The quantised model ships to the edge boards. The rollout doc quotes 0.036 ms.
  • Monday 14:00The first board drops frames. The camera feed stutters.
  • TuesdayThree more boards show the same stutter. The team blames the network.
  • WednesdayI time the model on a board. It answers in 0.8 ms, not 0.036 ms. The network was never the problem.
  • ThursdayThe team measures the float model on the same board. It answers faster than the quantised one, because the board has no int8 path and the dequantise cost dominates.
  • FridayThe float model ships to the boards, and the rollout doc carries the board latency beside the server latency, with the hardware named on each number.

What changed after

Two rules. First, the hardware is part of the number: every latency in every doc carries the machine it was measured on, and a number without its hardware is not quoted. Second, the export is measured on the board before it ships, not after: the quantised file and the float file are both timed on the target hardware, and the faster one ships. The quantised model was not wrong. The assumption that it would be faster everywhere was wrong, and the measurement would have caught it on day one.

If you remember nothing else

Design exercise

Design two models for one on-call team. First, an image classifier that reads photos of rack labels from edge cameras and flags the ones it cannot read, so a human checks them. The cameras run on small boards at the edge, and the model must answer in under 10 ms on the board. Second, an alert-sequence model that reads 20-step windows of deploy events and flags the windows where an incident follows. The team has 4,000 labelled windows and the same 2,000 past deploys Lesson 181 used. Design both: the architectures, the training, and the deployment.

Clarifying questions to ask

  • How many labelled rack photos exist, and how many does the team need before a CNN beats a simpler check?
  • What board runs the camera model, and does it have an int8 path, or does quantisation cost more than it saves?
  • What does a missed unreadable label cost, against what a false flag costs in human checks?
  • For the alert windows, how long is the window in production: 10 steps, 20 steps, or longer, and where does the signal sit in it?
  • What happens when the edge model is down: do the photos queue, or do they skip the check?
  • What is the retrain trigger, and who owns the registry the old model stays versioned in?

Worked approach

Start with the image model. A small CNN, two convolutions and a head, trained on the labelled photos. Print one feature map as text and read the channel stats before you trust it. Export it to TorchScript, quantise it, and time both files on the actual board, not on the training machine. Ship the faster one on that hardware, and write the board latency in the rollout doc with the hardware named.

Then the sequence model. An LSTM on 20-step windows, trained the way the lab trains it, with the seed written down beside the numbers. Read the recall before the accuracy: a model that catches every incident and cries wolf rarely is the ship candidate. If the production windows run longer than 20 steps, the transformer is the pick, and Lesson 56 is where that conversation starts.

Then the deployment. Both models ship with their threshold written down beside them, the retrain rule set in advance, and the old model versioned in the registry. The power budget sits beside the latency budget on the edge model, because a board runs on watts. The wrong-tool check runs first on both: a CNN on the deploy table, and an LSTM where a transformer fits, are both caught before the first epoch, not after the first incident.

Trade-offs to name. Accuracy against latency on the board is the big one: the bigger model sees better and answers slower, and 10 ms is a hard line. Memory horizon against architecture is the second: the LSTM holds 20 steps and guesses at 50, so the window length picks the model. Complexity against explainability is the third: the forest says which features matter, and neither the CNN nor the LSTM does. Pick the stack first, and the framework follows.

Saved in this browser, so the Lessons index can show your progress across the course.

Next up: Lesson 212
The Maths the Data Roles Assume: CLT, Distributions, Differentiation, Econometrics

The CNN learned what each layer sees and the LSTM learned what comes next. Next lesson I learn to see what they learned: charts that show it, and SQL that asks the data the questions the charts cannot.

Next: Lesson 212 →

Or browse the full course from the Lessons index.