Repository navigation
FIX get_output_shape updating BatchNorm running stats - #1420
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The functional fix is sound; the remaining changelog example correction is nonblocking.
1 open finding
What changed in this PR
Prevents output-shape inference from changing BatchNorm statistics or drawing dropout masks in EEG models.
Changes:
- Runs the shape probe in eval mode and restores individual module modes afterward.
- Adds regression coverage for statistics and mode preservation.
- Documents the behavior change.
| File | Description |
|---|---|
| test/unit_tests/models/test_base.py | Tests statistics preservation, mode restoration, and EEGNet initialization. |
| docs/whats_new.rst | Adds a release note for the fix. |
| braindecode/models/base.py | Temporarily switches the probe to eval mode with restoration in finally. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| Bug fixes | ||
| ========== | ||
|
|
||
| - :meth:`braindecode.models.base.EEGModuleMixin.get_output_shape` runs its probe forward pass in eval mode and restores each module's mode afterwards, so it no longer updates BatchNorm running statistics or draws dropout masks; models that call it in ``__init__`` (e.g. :class:`braindecode.models.EEGNet`, :class:`braindecode.models.EEGConformer`) start with untouched BatchNorm statistics (:gh:`1420` by `Adam Mounir`_). |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1420 +/- ##
=======================================
Coverage 90.13% 90.14%
=======================================
Files 167 167
Lines 21815 21848 +33
=======================================
+ Hits 19663 19694 +31
- Misses 2152 2154 +2 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

EEGModuleMixin.get_output_shape()runs a forward pass on a zero input undertorch.inference_mode(), but does not switch the model to eval mode. In train mode (the default for a freshly built model), BatchNorm still updates its running statistics with that all-zeros batch, and dropout draws masks (consuming RNG).Deep4Netwithrunning_mean=5,running_var=4, one call moves them to4.5/3.6.__init__, so every new instance already hasnum_batches_tracked == 1and running stats pulled toward the zero input.Fix
Record
module.trainingfor every module, callself.eval(), run the probe, then restore each module's own flag infinally. Restoring each flag, rather than callingself.train(was_training), keeps submodules that the user had put in eval on purpose (e.g. a frozen encoder) as they were.Tests
New
test_get_output_shape_leaves_batchnorm_stats_and_modes_unchanged. It checks that the BN stats andnum_batches_trackedare unchanged, that both the model's and a frozen submodule's modes are restored, and thatEEGNetstarts withnum_batches_tracked == 0. It fails onmaster.test_base.py: 50 passed.test_models.py: 694 passed, 6 skipped.