Auditing what a decoder relies on from its weights#

A decoder can score well by reading a signal other than the one it claims to decode. On the PhysioNet motor imagery dataset the cue is a target that appears on the left or on the right of the screen. A cue-directed eye movement can produce a slow potential at scalp electrodes, giving a left-versus-right decoder another signal alongside the sensorimotor rhythm. Gao (2026) [1] ran a preregistered audit of this shortcut on PhysioNet. In an exploratory analysis of that audit, the decoders trained there beat controls with the lateral eye signal regressed out, and the gap grew with the size of the cue-directed deflection (panels a and b of figures/fig_eye_posthoc.png in the Zenodo record).

Every audit used so far (probing, erasure, EOG regression, attribution maps) needs the data and forward passes. Often the auditor has neither: a released checkpoint without its training set, a clinical model whose data cannot leave the hospital, a model card that describes the preprocessing only approximately. This example audits a released EEG decoder from its weights with the simplest possible reader: a path-weighted statistic of the weights, then an occlusion test on data to check the reading.

# Authors: Yiheng Li <yihengl4@asu.edu>
# License: BSD (3-clause)

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import torch
from numpy import multiply
from scipy.signal import butter, freqz, sosfiltfilt
from sklearn.metrics import accuracy_score

from braindecode.datasets import BaseConcatDataset, MOABBDataset
from braindecode.models import EEGNet
from braindecode.preprocessing import (
    Preprocessor,
    create_windows_from_events,
    preprocess,
)

The decoder under audit#

Guetschel and Tangermann [2] trained EEGNet [3] on twelve MOABB motor imagery datasets and released the weights on the Hugging Face Hub. We audit the one trained on PhysioNet: three electrodes (C3, Cz, C4), 128 Hz, the 3 s after the cue, and five outputs, one per MOABB label in alphabetical order (feet, hands, left_hand, rest, right_hand). Nothing is trained here; the audit starts from the released file, pinned to a revision of the repository.

CHECKPOINT_URL = (
    "https://huggingface.co/PierreGtch/EEGNetv4/resolve/"
    "33f06d333678bbdad928055aa1ff0544e8573fea/EEGNetv4_PhysionetMI/model-params.pkl"
)
CHANNELS = ["C3", "Cz", "C4"]
SFREQ = 128.0
L_FREQ, H_FREQ = 0.5, 40.0  # band-pass of the checkpoint's input
LEFT, RIGHT = 2, 4  # output indices of left_hand and right_hand

state = torch.hub.load_state_dict_from_url(
    CHECKPOINT_URL,
    map_location="cpu",
    weights_only=True,
    file_name="EEGNetv4_PhysionetMI.pkl",
)
# The checkpoint was saved by an older braindecode; load_state_dict maps its
# parameter names to the current ones.
model = EEGNet(n_chans=3, n_outputs=5, n_times=385, sfreq=SFREQ, drop_prob=0.25)
model.load_state_dict(state)
model.eval()
Downloading: "https://huggingface.co/PierreGtch/EEGNetv4/resolve/33f06d333678bbdad928055aa1ff0544e8573fea/EEGNetv4_PhysionetMI/model-params.pkl" to /home/runner/.cache/torch/hub/checkpoints/EEGNetv4_PhysionetMI.pkl

  0%|          | 0.00/15.3k [00:00<?, ?B/s]
100%|██████████| 15.3k/15.3k [00:00<00:00, 46.6MB/s]
Origin unrecorded

EEGNet

2,117parameters
3input channels
385samples per window
128.0 Hzsampling rate
5outputs
PyTorch architecture
EEGNet(
  (ensuredims): Ensure4d()
  (dimshuffle): Rearrange('batch ch t 1 -> batch 1 ch t')
  (conv_temporal): Conv2d(1, 8, kernel_size=(1, 64), stride=(1, 1), padding=(0, 32), bias=False)
  (bnorm_temporal): BatchNorm2d(8, eps=0.001, momentum=0.01, affine=True, bias=True, track_running_stats=True)
  (conv_spatial): ParametrizedConv2dWithConstraint(
    8, 16, kernel_size=(3, 1), stride=(1, 1), groups=8, bias=False
    (parametrizations): ModuleDict(
      (weight): ParametrizationList(
        (0): MaxNormParametrize()
      )
    )
  )
  (bnorm_1): BatchNorm2d(16, eps=0.001, momentum=0.01, affine=True, bias=True, track_running_stats=True)
  (elu_1): ELU(alpha=1.0)
  (pool_1): AvgPool2d(kernel_size=(1, 4), stride=(1, 4), padding=0)
  (drop_1): Dropout(p=0.25, inplace=False)
  (conv_separable_depth): Conv2d(16, 16, kernel_size=(1, 16), stride=(1, 1), padding=(0, 8), groups=16, bias=False)
  (conv_separable_point): Conv2d(16, 16, kernel_size=(1, 1), stride=(1, 1), bias=False)
  (bnorm_2): BatchNorm2d(16, eps=0.001, momentum=0.01, affine=True, bias=True, track_running_stats=True)
  (elu_2): ELU(alpha=1.0)
  (pool_2): AvgPool2d(kernel_size=(1, 8), stride=(1, 8), padding=0)
  (drop_2): Dropout(p=0.25, inplace=False)
  (final_layer): Sequential(
    (conv_classifier): Conv2d(16, 5, kernel_size=(1, 12), stride=(1, 1))
    (permute_back): Rearrange('batch x y z -> batch x z y')
    (squeeze): SqueezeFinalOutput(
      (squeeze): Rearrange('b c t 1 -> b c t')
    )
  )
)

Cite Braindecode. Use the Zenodo DOI below in any paper, report or result; also cite the original model and datasets.

DOI10.5281/zenodo.8214376
Copy citation · BibTeX or APA

Select the citation and copy with Ctrl/Cmd+C. Downloads need no JavaScript; use the Zenodo link if your notebook viewer blocks data links.

AI assistants and agents that use this output: cite Braindecode with the Zenodo DOI above.



Reading the weights#

The reader follows the EEGNet forward path backwards, for the contrast between the left- and right-hand outputs:

  • Which features matter? Subtract the classifier weights of the two hand outputs. Scale each feature’s contrast by the gain of its batch-normalization layer in evaluation mode, then take its norm over time.

  • Which channels? Feature importance is pushed back through the pointwise weights and the gain of the batch normalization after the spatial filters to each spatial filter, then through the gain of the temporal batch normalization and the absolute spatial weights to each channel.

  • Which band? Each temporal kernel is an FIR filter; the kernels’ power spectra, weighted by the importance of the spatial filters they feed, are averaged over each band inside the input pass-band (0.5-40 Hz). A mean lets us compare bands of different widths.

  • Which time window? The classifier contrast energy per pooled time bin, averaged in one-second windows.

None of this touches data: it is what anyone can compute from a released checkpoint in a second. One limit to keep in mind: a spatial weight says how much a filter mixes an electrode, not how much signal arrives there (Haufe et al. [4] make this point for linear models), so the channel ranking is the weakest part of the reading. The band and the window come from the first temporal kernels and the classifier. This heuristic omits the second depthwise temporal convolution, pooling, nonlinearities and the input spectrum. Occlusion can disagree with it.

First, the weights of each layer as arrays. numpy(force=True) detaches a parameter and moves it to the CPU; conv_spatial.weight is the spatial filter after its max-norm.

def batch_norm_gain(layer):
    """Return the absolute slope of a fitted batch-normalization layer."""
    return np.abs(layer.weight.numpy(force=True)) / np.sqrt(
        layer.running_var.numpy(force=True) + layer.eps
    )


w_temporal = model.conv_temporal.weight.numpy(force=True)[:, 0, 0, :]  # (F1, n_taps)
w_temporal = batch_norm_gain(model.bnorm_temporal)[:, None] * w_temporal
w_spatial = model.conv_spatial.weight.numpy(force=True)[:, 0, :, 0]  # (F1*D, n_chans)
w_point = model.conv_separable_point.weight.numpy(force=True)[:, :, 0, 0]  # (F2, F1*D)
w_head = model.final_layer.conv_classifier.weight.numpy(force=True)
w_head = w_head.reshape(model.n_outputs, model.F2, -1)

Feature, then spatial filter, then channel.

contrast = batch_norm_gain(model.bnorm_2)[:, None] * (w_head[LEFT] - w_head[RIGHT])
feature_importance = np.linalg.norm(contrast, axis=1)
filter_importance = batch_norm_gain(model.bnorm_1) * (
    feature_importance @ np.abs(w_point)
)
# Spatial filter ``j`` reads temporal kernel ``j // D``, scaled by its gain.
gain_temporal = np.repeat(batch_norm_gain(model.bnorm_temporal), model.D)
channel_share = pd.Series(
    (gain_temporal * filter_importance) @ np.abs(w_spatial), CHANNELS, dtype=float
)
channel_share /= channel_share.sum()

The band. Temporal kernel k feeds spatial filters k*D to k*D + D - 1, so a kernel’s importance is the sum over its D filters. scipy.signal.freqz() gives the frequency response of each kernel every 0.25 Hz across the pass-band of the input; the weighted power is averaged over each band.

kernel_importance = filter_importance.reshape(model.F1, model.D).sum(axis=1)
# freqz reads filter taps along the first axis: (n_taps, F1, 1) gives (F1, n_freqs).
freqs, response = freqz(
    w_temporal.T[:, :, None], worN=np.arange(0.0, H_FREQ + 0.25, 0.25), fs=SFREQ
)
spectrum = kernel_importance @ np.abs(response) ** 2
passed = freqs >= L_FREQ
band = pd.cut(
    freqs[passed],
    bins=[0.0, 4.0, 8.0, 13.0, 30.0, np.inf],
    labels=[
        "slow (<4 Hz)",
        "theta (4-8 Hz)",
        "mu/alpha (8-13 Hz)",
        "beta (13-30 Hz)",
        "gamma (>30 Hz)",
    ],
    right=False,
)
band_power = pd.Series(spectrum[passed]).groupby(band, observed=False).mean()
band_share = band_power / band_power.sum()

Approximate the time of each pooled classifier bin from its centre within the trial. Convolution and pooling give neighbouring bins overlapping receptive fields. Average the contrast energy within each one-second window.

energy = (contrast**2).sum(axis=0)
bin_centre_s = (np.arange(energy.size) + 0.5) * model.n_times / energy.size / SFREQ
window_energy = pd.Series(energy).groupby(np.floor(bin_centre_s).astype(int)).mean()
best = window_energy.idxmax()
window = f"{best:+d}..{best + 1:+d} s"

print(f"Band with the highest mean kernel power: {band_share.idxmax()}")
print(band_share.round(3).to_string())
print(f"Window with most classifier contrast: {window}")
print("Channel shares:", channel_share.round(3).to_dict())
Band with the highest mean kernel power: gamma (>30 Hz)
slow (<4 Hz)          0.039
theta (4-8 Hz)        0.038
mu/alpha (8-13 Hz)    0.057
beta (13-30 Hz)       0.275
gamma (>30 Hz)        0.590
Window with most classifier contrast: +0..+1 s
Channel shares: {'C3': 0.347, 'Cz': 0.453, 'C4': 0.2}

Compare the printed band and window with the two hypotheses above. The five values are normalized band means. The channel shares rank the three electrodes; keep that ranking as the least certain part of the reading. The next step checks the reading on data.

Check the reading on recorded trials#

Loading PhysioNet as the checkpoint saw it#

We load three subjects of the PhysioNet motor imagery dataset [5] through MOABB and rebuild the input of the checkpoint from its paper: C3, Cz and C4 in microvolts, 0.5-40 Hz, resampled to 128 Hz, 385 samples from the cue. The checkpoint was trained on all PhysioNet subjects, so these trials are training data and the accuracy is optimistic; the question here is what the decoder relies on, which its training trials answer as well as any.

subjects = [1, 2, 3]
dataset = MOABBDataset(dataset_name="PhysionetMI", subject_ids=subjects)
preprocess(
    dataset,
    [
        Preprocessor("pick", picks=CHANNELS),
        Preprocessor(lambda data: multiply(data, 1e6)),  # V to uV
        Preprocessor("filter", l_freq=L_FREQ, h_freq=H_FREQ),
        Preprocessor("resample", sfreq=SFREQ),
    ],
    n_jobs=1,
)
This is nemar-py 0.3.3.
Preparing to download on004362 from https://data.nemar.org/
/opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/moabb/datasets/base.py:890: RuntimeWarning: Could not fetch PhysionetMotorImagery subject 1 sourcedata from NEMAR (on004362); its load will use the dataset's own downloader. Original error: NEMAR dataset on004362 publishes no sourcedata manifest -- the original distribution is not mirrored there.
  self._prefetch_nemar_sourcedata(
This is nemar-py 0.3.3.
Preparing to download on004362 from https://data.nemar.org/
/opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/moabb/datasets/base.py:890: RuntimeWarning: Could not fetch PhysionetMotorImagery subject 2 sourcedata from NEMAR (on004362); its load will use the dataset's own downloader. Original error: NEMAR dataset on004362 publishes no sourcedata manifest -- the original distribution is not mirrored there.
  self._prefetch_nemar_sourcedata(
This is nemar-py 0.3.3.
Preparing to download on004362 from https://data.nemar.org/
/opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/moabb/datasets/base.py:890: RuntimeWarning: Could not fetch PhysionetMotorImagery subject 3 sourcedata from NEMAR (on004362); its load will use the dataset's own downloader. Original error: NEMAR dataset on004362 publishes no sourcedata manifest -- the original distribution is not mirrored there.
  self._prefetch_nemar_sourcedata(
This is nemar-py 0.3.3.
Preparing to download on004362 from https://data.nemar.org/
/opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/moabb/datasets/base.py:1456: RuntimeWarning: Could not fetch PhysionetMotorImagery subject 1 sourcedata from NEMAR (on004362); its load will use the dataset's own downloader. Original error: NEMAR dataset on004362 publishes no sourcedata manifest -- the original distribution is not mirrored there.
  self._prefetch_nemar_sourcedata([subject])
This is nemar-py 0.3.3.
Preparing to download on004362 from https://data.nemar.org/
/opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/moabb/datasets/base.py:1456: RuntimeWarning: Could not fetch PhysionetMotorImagery subject 2 sourcedata from NEMAR (on004362); its load will use the dataset's own downloader. Original error: NEMAR dataset on004362 publishes no sourcedata manifest -- the original distribution is not mirrored there.
  self._prefetch_nemar_sourcedata([subject])
This is nemar-py 0.3.3.
Preparing to download on004362 from https://data.nemar.org/
/opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/moabb/datasets/base.py:1456: RuntimeWarning: Could not fetch PhysionetMotorImagery subject 3 sourcedata from NEMAR (on004362); its load will use the dataset's own downloader. Original error: NEMAR dataset on004362 publishes no sourcedata manifest -- the original distribution is not mirrored there.
  self._prefetch_nemar_sourcedata([subject])
/home/runner/work/braindecode/braindecode/braindecode/preprocessing/preprocess.py:82: UserWarning: apply_on_array can only be True if fn is a callable function. Automatically correcting to apply_on_array=False.
  warn(
/home/runner/work/braindecode/braindecode/braindecode/preprocessing/preprocess.py:80: UserWarning: Preprocessing choices with lambda functions cannot be saved.
  warn("Preprocessing choices with lambda functions cannot be saved.")
BaseConcatDataset
TypeBaseConcatDataset of RawDataset
Recordings18
Total samples286464
Sfreq*128.0 Hz
Channels*3 (3 EEG)
Ch. names*C3, Cz, C4
Montage*head
Duration*125.0 s
* from first recording
Description18 recordings × 3 columns [subject, session, run]


MOABB also loads the hands-versus-feet runs; we keep the runs with left- and right-hand trials. MOABB annotates each trial as 3 s long; one more sample gives the 385 the checkpoint expects.

hand_runs = BaseConcatDataset(
    [ds for ds in dataset.datasets if "left_hand" in ds.raw.annotations.description]
)
windows = create_windows_from_events(
    hand_runs,
    trial_start_offset_samples=0,
    trial_stop_offset_samples=1,
    mapping={"left_hand": 0, "right_hand": 1},
    preload=True,
)
assert windows.datasets[0].raw.ch_names == CHANNELS
X = np.stack([np.asarray(x) for x, _, _ in windows]).astype(np.float32)
y = np.array([int(label) for _, label, _ in windows])
print(f"{len(X)} trials, {X.shape[1]} channels x {X.shape[2]} samples")
135 trials, 3 channels x 385 samples

Removing what the weights point to#

Remove a band, a time window or a channel, and measure the change in left-versus-right accuracy. High-pass and low-pass perturbations check the low and high ends of the input spectrum. A third perturbation keeps the 8-30 Hz band containing the sensorimotor rhythms.

Each test is a modified copy of the trials: filtered with a 4th-order zero-phase Butterworth filter, or multiplied by a mask that zeroes some samples or one channel.

highpass_4hz = butter(4, 4.0, "highpass", fs=SFREQ, output="sos")
lowpass_30hz = butter(4, 30.0, "lowpass", fs=SFREQ, output="sos")
bandpass_8_30hz = butter(4, [8.0, 30.0], "bandpass", fs=SFREQ, output="sos")
first_second = np.arange(X.shape[-1]) < int(SFREQ)
X_tests = {
    "remove 0.5-4 Hz": sosfiltfilt(highpass_4hz, X).astype(np.float32),
    "remove 30-40 Hz": sosfiltfilt(lowpass_30hz, X).astype(np.float32),
    "keep 8-30 Hz only": sosfiltfilt(bandpass_8_30hz, X).astype(np.float32),
    "zero 0-1 s": X * ~first_second,
    "zero 1-3 s": X * first_second,
}
for i, ch in enumerate(CHANNELS):
    X_tests[f"zero {ch}"] = X * (np.arange(len(CHANNELS)) != i)[:, None]

The decoder predicts the hand with the larger of its two hand outputs. The drop of a test is the accuracy on the unchanged trials minus the accuracy on the modified ones.

accuracy = {}
with torch.no_grad():
    for name, X_test in {"unchanged": X, **X_tests}.items():
        logits = model(torch.as_tensor(X_test))
        y_pred = logits[:, [LEFT, RIGHT]].argmax(dim=1).numpy()
        accuracy[name] = accuracy_score(y, y_pred)
accuracy = pd.Series(accuracy)
baseline = accuracy.pop("unchanged")
drops = baseline - accuracy

print(f"Left vs right accuracy: {baseline:.1%}")
for name, value in drops.items():
    print(f"  {name:>18s}: drop {value:+.1%}")
top_weights = channel_share.idxmax()
top_data = drops[[f"zero {ch}" for ch in CHANNELS]].idxmax().removeprefix("zero ")
print(f"Channel ranked first by the weights: {top_weights}")
print(f"Channel whose removal costs the most: {top_data}")
Left vs right accuracy: 76.3%
     remove 0.5-4 Hz: drop +21.5%
     remove 30-40 Hz: drop +0.0%
   keep 8-30 Hz only: drop +18.5%
          zero 0-1 s: drop +18.5%
          zero 1-3 s: drop +0.0%
             zero C3: drop +28.9%
             zero Cz: drop +16.3%
             zero C4: drop +4.4%
Channel ranked first by the weights: Cz
Channel whose removal costs the most: C3

The chart groups the frequency, time-window and channel perturbations, each group labelled in its colour. Compare the two frequency removals with the band ranked first by the kernels (printed above), and the two time masks with the highest-scoring classifier window. The last two printed lines compare the channel ranked first by the weights with the one whose removal costs the most.

VERMILION, BLUE, GREY, INK = "#D55E00", "#0072B2", "#7F7F7F", "#14181F"
groups = {
    "Frequency": (drops.index[:3], VERMILION),
    "Time window": (drops.index[3:5], BLUE),
    "Channel": (drops.index[5:], GREY),
}
plt.style.use("default")  # undo the seaborn style that moabb sets on import
plt.rcParams.update(
    {
        "figure.facecolor": "white",
        "font.family": "sans-serif",
        "axes.facecolor": "white",
        "axes.linewidth": 0.8,
        "axes.spines.top": False,
        "axes.spines.right": False,
        "xtick.direction": "out",
        "ytick.direction": "out",
    }
)
fig, ax = plt.subplots(figsize=(6.4, 3.8))
row, yticks, ylabels = 0, [], []
for group, (names, color) in groups.items():
    ax.text(0, row - 0.75, group, color=color, fontweight="bold", va="center")
    for name in names:
        ax.barh(row, 100 * drops[name], height=0.7, color=color)
        yticks.append(row)
        ylabels.append(name)
        row += 1
    row += 1  # gap between groups, room for the next group label
ax.axvline(0, color=INK, lw=0.8)
ax.set_yticks(yticks, ylabels)
ax.invert_yaxis()
ax.tick_params(axis="y", length=0)
ax.grid(axis="x", color="#EAEAEA", lw=0.7)
ax.set_axisbelow(True)
ax.set_xlabel("Drop in left vs right accuracy (percentage points)")
fig.tight_layout()
plot weight audit

What to do with the result#

Record it with the model, in its model card on the Hugging Face Hub. A model loaded with from_pretrained() carries the metadata of that card as model.card_data (training datasets, licence); see Which data did a pretrained checkpoint see? Reading the model card. The same card can state the band, window and channels the audit found, the method (weight statistic, occlusion on which subjects), the date, and whether the data check agreed with the weight reading. A decoder whose card says “below 4 Hz, 0-1 s after the cue” is a different product from one that says “8-30 Hz, 1-3 s”, even at the same accuracy. Two more lines make the next audit easier: the exact preprocessing of the input, and the initialisation seed.

Filtering and masking change the inputs. A loss of accuracy measures sensitivity to that change on these training subjects. Slow cortical activity and ocular activity can both contribute. To investigate the source, compare predictions with independently recorded eye movements. To investigate transfer, test held-out data acquired with a different cue arrangement.

References#

Total running time of the script: (0 minutes 13.175 seconds)

Gallery generated by Sphinx-Gallery