Part II Chapter 8

Training and generalization

Contents
  1. Measuring how wrong the network is
  2. Training: many small nudges
  3. Watch a network train
  4. Generalization and overfitting
  5. Scaling the inputs
  6. Checkpoints: every epoch, saved

Chapter 7 ended with a trained network whose first layer listens where the shares leak. Before training, its weights were random numbers. This chapter covers how training turns them into that profile, and how to check that what the network learned is a property of the chip, not of the particular traces it was shown.

Measuring how wrong the network is

Training needs one number that says how wrong the network is, so it can push that number down. For a trace whose true label is, say, Hamming weight 4, the network outputs a probability for each of the 9 labels. The loss for that trace is minus the logarithm of the probability it gave the true label:

true label: 4
network gives p(4) = 0.5    →  loss −ln(0.5)   = 0.69
network gives p(4) = 0.1    →  loss −ln(0.1)   = 2.30
network gives p(4) = 0.001  →  loss −ln(0.001) = 6.91

A sure, correct answer costs nothing; a confident wrong one costs a lot. Averaged over many traces this is the cross-entropy, the loss every network in the paper is trained on.

Two reference points make the number readable. A network that spreads its probability evenly over the 9 labels scores −ln(1/9) = 2.20. One that has learned nothing from the trace but knows how often each label occurs does better: Hamming weight 4 alone covers 70 of the 256 byte values, 27%, and giving every label its natural frequency scores 1.76. Call that the guessing line. A network only knows something about the trace when its loss goes below it.

Training: many small nudges

Training repeats one step many times:

  1. Take a batch of profiling traces with their labels. The paper uses batches of 400.
  2. Run them through the network and compute the average loss.
  3. For every weight, work out which direction, up or down, would lower that loss, and move it a small step that way. This is gradient descent; the library computes the directions automatically.

One pass over the whole profiling set is an epoch. For ESHARD, 90,000 profiling traces in batches of 400 make 225 steps per epoch, and the paper trains for 100 epochs: 22,500 small adjustments to the network’s 61,329 numbers. The paper’s models use Adam, a version of gradient descent that adapts the step size of each weight as it goes. The ESHARD and CHES_CTF models also add a small penalty for large weights, so a weight that doesn’t help lower the loss drifts to zero. That penalty is why the 3D network at the end of Chapter 7 kept so few connections.

Watch a network train

Here is that 3D network again, built from scratch with random weights and trained on the simulated chip from Chapter 6. The top panel shows how much weight its first layer puts on each of the 60 samples; the leak sits in the fourth cycle. The bottom panel tracks two losses after every epoch: on the training traces it learns from, and on 500 test traces it never sees during training.

training set
First-layer weight per samplecycle 1cycle 2cycle 3cycle 4cycle 5cycle 6weight0102030405059time sample Training and test loss per epoch0123guessingby frequency0255075100lossepoch

Epoch 0 of 100 · training loss 2.46 · test loss 2.47 · test accuracy 11% (always guessing the most common label: 26%)

The network from the end of Chapter 7, with fresh random weights: its first layer listens to every sample about equally, and both losses sit above the guessing line. Press Train. Each epoch is one pass over the 1,500 training traces in batches of 20; the test set is 500 traces the network never trains on.

With 1,500 training traces, both losses drop within a few epochs and stay together, and the first layer’s weight gathers on the fourth cycle. The test accuracy ends near 70%; always answering the most common label would get about a quarter right. Then switch to 60 traces and train again. The training loss heads for zero, but the test loss stops falling after about five epochs and then climbs, never getting under the guessing line. The weights tell the same story: they stay spread over every cycle.

Generalization and overfitting

The 60-trace network didn’t learn the chip; it learned its 60 traces. A network with hundreds of free numbers can fit the noise of a few dozen traces exactly, the way a student can memorize the answers to one practice exam without understanding the subject. That is overfitting, and its sign is the one in the widget: the training loss keeps improving while the loss on unseen traces gets worse. The opposite, doing as well on new traces as on the training ones, is generalization.

This is why Chapter 5 split every dataset into a profiling set and an attack set. The attack traces are never used for training, so they act as the test set: a network that does well on them learned something about the device, not about its training traces.

Real networks overfit too. The paper’s ESHARD network has 61,329 numbers, far fewer than the 90,000 traces × 1,400 samples it trains on, and still, reloaded at each of its 100 epochs:

ESHARD: training loss keeps falling, attack loss turns back upgeneralizesmemorizes1.51.61.71.81.92.0120406080100epochlossbest on attack traces: epoch 12attack tracesnever trained onguessingby frequencytraining traceslearned from
The paper’s ESHARD network, reloaded from each of its 100 checkpoints. Blue: loss on 20,000 of the profiling traces it trained on. Red: loss on the 10,000 attack traces it never saw. Dashed: the guessing line, a model that knows only how often each label occurs. Green: the epochs where the network does better than guessing on traces it never saw.

At epoch 4 the loss on attack traces drops below the guessing line; the paper finds the same epoch with a different measure, which Chapter 10 introduces. It is lowest at epoch 12, 1.73. From there the two curves part: the training loss keeps falling, to 1.53 at epoch 100, while the attack loss climbs back over the guessing line at epoch 28 and ends at 1.97, worse than knowing nothing. The network that generalized at epoch 12 spent the rest of its training memorizing.

The paper trains to the end anyway, because its subject is what happens during training: when a network starts to generalize and what it learned at that moment. The useful networks sit in the green band, and the jump into it is what the later chapters study.

The loss has a limit as a verdict. Two curves that stay together say the network didn’t memorize; they don’t say it is good enough to recover a key. A loss of 1.73 against a guessing line of 1.76 is a small edge per trace. Whether that edge adds up to a key byte is the next chapter’s question.

Scaling the inputs

One practical step comes before any training. The datasets store traces in different units, ASCADr as raw 8-bit oscilloscope codes and CHES_CTF as 16-bit floats, and gradient descent behaves badly when inputs have arbitrary offsets and scales. So every sample is standardized: subtract its mean and divide by its standard deviation, both measured on the first 20,000 profiling traces.

scaled sample = (raw sample − mean) / std        →  mostly between −3 and +3

The attack traces are scaled with those same profiling statistics; nothing is ever fitted on attack data. The widget above does the same with its own training traces.

Checkpoints: every epoch, saved

The authors saved the network’s weights after every epoch and published them with the datasets on Zenodo (opens in a new tab). Each file is a checkpoint: mlp_eshard__01.weights.h5 through mlp_eshard__100.weights.h5 for ESHARD, and the same for the other models, up to epoch 200 for CHES_CTF. Loading checkpoint 12 into a freshly built network gives back the network exactly as it stood after epoch 12. The figure above was made that way, without training anything.

Training all the paper’s models takes under an hour on a desktop GPU, so the checkpoints don’t exist because training is expensive. They exist because training is random: the starting weights and the order of the batches change from run to run, and so can the epoch where a network starts to generalize. The authors’ own notebook warns that retrained models give different results. With the checkpoints, every chapter here studies the same networks the paper did, epoch by epoch.

The model notebook (opens in a new tab) loads checkpoints, prints each layer and counts the weights.

The network now outputs, for every trace, a probability for each Hamming weight of the S-box output. It never names a key byte. How do many weak per-trace probabilities point to one key byte out of 256? Chapter 9 turns them into an attack.