Skip to main content

Coverage measures for neural networks

Why This Matters

For conventional code, coverage tells you which parts were never executed by your tests. For a neural network, every test executes every line of code: statement coverage is always 100% and tells you nothing. Yet some neurons may never have been activated by your test inputs, and the behaviour they encode is untested. Neural network coverage measures look at that instead.

What you will learn

  • Why code coverage does not work for neural networks
  • Three coverage measures: neuron coverage, k-multisection neuron coverage and neuron boundary coverage
  • How to calculate them for a small network, and what they can and cannot tell you

Syllabus: AI-3.4.3.

Why a different kind of coverage

A neural network’s code is a few loops of multiplication and addition. Every input goes through all of it. What differs between inputs is which neurons are activated, and how strongly. The idea behind neural network coverage: a test set that activates neurons in many different ways exercises more of the network’s learned behaviour than a test set that always activates the same neurons.

Three measures

Neuron coverage

The share of neurons whose output exceeds a threshold for at least one test input.

neuron coverage = neurons activated above the threshold / total neurons

The threshold is chosen: 0 (any activation) or higher. A higher threshold is stricter.

k-multisection neuron coverage (kMNC)

During training, each neuron had a range of outputs, from its lowest to its highest value. kMNC splits that range into k equal sections and measures how many sections the test inputs reach.

kMNC = sections reached / (k × number of neurons)

It rewards tests that exercise each neuron across its whole normal range, not just once.

Neuron boundary coverage (NBC)

The share of neurons for which a test input produced an output outside the range seen in training: below the minimum or above the maximum.

NBC = neurons with an output beyond their training range / total neurons

These are the corner cases: inputs that push the network into states it never saw while learning, where its behaviour is least predictable. (Research papers often count the lower and the upper boundary separately and divide by 2 × the number of neurons; the syllabus describes it per neuron.)

A worked example

The hidden layer from lesson 21: three neurons h1, h2, h3 with ReLU. During training, their outputs ranged from:

NeuronTraining minTraining max
h102.0
h201.0
h301.6

Three test inputs, and the hidden outputs they produce (calculated as in lesson 21):

Test inputh1h2h3
(1, 0)1.000
(2, 1)1.01.00
(0, 1)002.0

Neuron coverage, threshold 0 (output above 0): h1 (1.0), h2 (1.0) and h3 (2.0) are all activated by some test. 3/3 = 100%.

Neuron coverage, threshold 1.0 (output above 1.0): h1 reaches exactly 1.0, not above; h2 the same; h3 reaches 2.0. 1/3 = 33%. The same tests, a stricter threshold, a very different number. A coverage figure means nothing without its threshold.

kMNC with k = 4. Split each training range into 4 sections:

  • h1, range 0–2.0, sections of 0.5: outputs 0 (section 1) and 1.0 (section 3). 2 of 4.
  • h2, range 0–1.0, sections of 0.25: outputs 0 (section 1) and 1.0 (section 4, the top). 2 of 4.
  • h3, range 0–1.6, sections of 0.4: output 0 (section 1); 2.0 is outside the range and does not count for kMNC. 1 of 4.

kMNC = 5 / 12 = 42%. More than half of the neurons’ normal behaviour has not been exercised.

NBC. h3 produced 2.0, above its training maximum of 1.6. h1 and h2 stayed within range. 1/3 = 33%.

What a tester does with this: add test inputs that push h1 into sections 2 and 4 (outputs between 0.5 and 1.0, and above 1.5) and h3 into its middle sections. And look hard at the input (0, 1): it drives h3 outside anything seen in training, so the network’s answer for it deserves a check.

Value and limits

Value: coverage shows which parts of the network the tests have not exercised, and points to new test inputs. Tools can generate inputs that increase coverage.

Limits:

  • Little tool support. Commercial tools for neural network coverage are scarce; most implementations are research code.
  • Coverage does not prove correctness, or generalisation. A network can reach high coverage and still have learned the wrong thing. A famous example: a model that recognised wolves by the snow in the background. Every neuron can be covered while the model is relying on a spurious correlation.
  • High coverage is easy to reach with meaningless inputs. Random noise activates many neurons.

So the syllabus advises using coverage together with adversarial and metamorphic testing (lessons 45 and 46), which test behaviour rather than activation.

Exercise

A fourth test input (3, 0) produces the hidden outputs h1 = 3.0, h2 = 1.0, h3 = 0. Recalculate the three measures for all four test inputs (neuron coverage at threshold 1.0, kMNC with k = 4, and NBC).

Show suggested answer
  • Neuron coverage, threshold 1.0: h1 now reaches 3.0 (above 1.0), h3 reaches 2.0, h2 still only 1.0. 2/3 = 67%.
  • kMNC, k = 4: h1’s output 3.0 is above its training maximum of 2.0, so it does not add a section: h1 stays at 2 of 4. h2’s 1.0 is section 4, already reached. h3 unchanged. Still 5/12 = 42%.
  • NBC: h1 (3.0 > 2.0) and h3 (2.0 > 1.6) are beyond their training range. 2/3 = 67%.

The new input raised neuron coverage and boundary coverage, but did not exercise any more of the normal range. Each measure tells you something different.

Exam-style questions

Quiz

1. Which coverage measure divides each neuron’s output range from training into sections and measures how many sections are reached by the tests?

  • A. Neuron coverage
  • B. k-multisection neuron coverage
  • C. Neuron boundary coverage
  • D. Statement coverage
Show answer
B.
Quiz

2. Why is high neuron coverage not sufficient evidence that a neural network works correctly?

  • A. Neuron coverage cannot be measured
  • B. The network can still have learned spurious correlations; coverage measures activation, not whether the behaviour is right
  • C. Neuron coverage is always 100%
  • D. It only applies to the output layer
Show answer
B. That is why coverage is combined with adversarial and metamorphic testing.
Quiz

3. A test set activates 45 of the 60 neurons of a network above the chosen threshold. What is the neuron coverage?

  • A. 45%
  • B. 60%
  • C. 75%
  • D. 133%
Show answer
C. 45 / 60 = 75%.