Repository navigation
FIX MaxNormLinear constraining input columns instead of output units - #1476
Conversation
MaxNorm took the norm of the (out_features, in_features) weight over dim=0, so it bounded the weights of each input feature rather than the incoming weights of each output unit, which is what the Keras Dense(kernel_constraint=max_norm()) it reproduces does (and what LinearWithConstraint does). This affects the classification layer of ATCNet and EEGTCNet.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1476 +/- ##
=======================================
Coverage 91.32% 91.32%
=======================================
Files 170 170
Lines 22075 22076 +1
=======================================
+ Hits 20159 20160 +1
Misses 1916 1916 🚀 New features to boost your workflow:
|
|
Benchmark is necessary, could be in the bnci with moabb |
# Conflicts: # docs/whats_new.rst
|
BNCI benchmark as requested (MOABB BNCI2014_001, session 1 train / session 2 test, subjects 1–3, seeds 0–2, 100 epochs, CPU, Voyager). Each pair starts from identical initial weights, data order and dropout streams; only the max-norm axis differs (master
With this PR every output unit stays within the max-norm limit (checked after training in all 18 runs). ATCNet benefits clearly; EEGTCNet is within seed noise (small, mixed). Integrated head |
Resolve changelog overlap while retaining master and this PR entry. Tree verified by Voyager follow-up tests.
antonsoo
left a comment
There was a problem hiding this comment.
Tested dfa9ed0 against base 05b764d with PyTorch 2.7 on CPU and an RTX 5070. The new regression fails on base; 73 relevant module/model tests pass on head (pytest test/unit_tests/models/{test_modules,test_models}.py -k 'maxnorm or max_norm or atcnet or eegtcnet').
I also checked ATCNet's mean and concat heads and EEGTCNet on recorded PhysioNet S001R04 EEG: two seeds on each device, four Adam steps. All 12 base runs exceeded the per-output bound; all 12 head runs obey it. An independent row-by-row constraint agrees with forward/backward evaluation from the same weights and optimizer state (largest gradient difference 7.4e-7). Base checkpoints load strictly on head; saving model and optimizer after two steps and resuming preserves the following predictions and model state exactly.
This is a focused numerical/checkpoint review. I did not repeat the BNCI accuracy benchmark or full suite.
…axis # Conflicts: # docs/whats_new.rst
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the per-output max-norm correction.
No open issues 🎉
What changed in this PR
Corrects MaxNormLinear to constrain each output unit’s incoming weights, matching Keras Dense max-norm behavior for ATCNet and EEGTCNet classification layers.
Changes:
- Uses row-wise norms in
MaxNorm.forwardandright_inverse. - Updates the reference calculation and adds a regression test for per-output constraints.
- Documents that existing checkpoints still load, but effective classification weights may change.
| File | Description |
|---|---|
test/unit_tests/models/test_modules.py |
Updates the reference norm axis and tests per-output constraints. |
docs/whats_new.rst |
Explains the correction and checkpoint behavior. |
braindecode/modules/parametrization.py |
Applies row-wise norms and documents the intended constraint. |
Review effort: Balanced
# Conflicts: # docs/whats_new.rst
MaxNormLinearsays it is the equivalent of KerasDense(..., kernel_constraint=max_norm()). ATCNet and EEGTCNet use it for their classification layer, asmax_norm(0.25)on the finalDenseof the official code.A Keras
Densekernel has shape(in_features, out_features), andmax_norm()defaults toaxis=0. That bounds the incoming weights of each output unit (Keras docs: "In a Dense layer the weight matrix has shape (input_dim, output_dim), set axis to 0 to constrain each weight vector of length (input_dim,)"). A torchnn.Linearweight is(out_features, in_features), so the matching norm is overdim=1, one per row.LinearWithConstraint/MaxNormParametrizealready do this. TheMaxNormparametrization behindMaxNormLineartook the norm overdim=0, one per input column. So it bounded the wrong vectors: rows over the bound were kept, and rows within the bound were rescaled.Repro (master)
With the fix these give
[2.0000, 0.1000]and0.2500. With ATCNet's defaults (32 input features), a row could reachsqrt(32) * 0.25 = 1.41.Fix
MaxNorm.forwardandright_inversetake the norm overdim=1. The clamping formula is unchanged.old_maxnormintest_modules.py(the reference fortest_new_vs_old_maxnorm_are_identical) copied the old axis, so it is updated too.The stored weights (
parametrizations.weight.original) keep their shape, so ATCNet / EEGTCNet checkpoints saved before still load. Their effective classification weights can differ, though, and the whats_new entry says so.Tests
New
test_max_norm_linear_constrains_each_output_unitfails on master and passes with the fix.test_new_vs_old_maxnorm_are_identical(3 cases) passes with the updated reference. Also run individually and passing:test_atcnet_conv_max_norm,test_atcnet_source_optimizer_param_groups(2),test_eegtcnet_tcn_batchnorm,test_atcnet_n_params,test_atcnet_concat_uses_every_window.Full test files
Run on a Linux CPU node, on this branch rebased on current
masterand onmasteritself, with-m "not network and not huggingface":test_modules.py test_models.py test_integration.py test_model_contract.py test_pretrained_compat.py (test/unit_tests/models/).Branch: 2986 passed, 181 skipped, 7 failed.
master: 2985 passed, 181 skipped, 7 failed. The 7 failures aretest_model_compiledcases that also fail onmasterin that environment (torch inductor could not findopenssl). No test fails only on this branch.