braindecode.visualization.cascading_layer_reset#
- braindecode.visualization.cascading_layer_reset(model, deepcopy_first=True)[source]#
Yield the working model after each successive layer reset.
Walks the modules in reverse depth-first order (output → input) and yields
(layer_name, randomized_model)after each layer’sreset_parameterscall. Implements the cascading-randomization sanity check from Adebayo et al. (NeurIPS 2018), as applied to EEG decoders in Sujatha Ravindran & Contreras-Vidal (Sci Rep 2023). An attribution method whose maps survive every cascade level is suspicious — its output likely depends on architecture rather than on learned weights.- Parameters:
model (torch.nn.Module) – Trained model. The original model is not modified when
deepcopy_first=True(default).deepcopy_first (bool) – If True, deep-copy the model before mutating, so the caller’s
modelstays trained. Set False to skip the copy when memory is tight and the caller doesn’t need the trained weights again.
- Yields:
layer_name (str) – Dotted-path name of the layer just reset (e.g.
"final_layer.conv_classifier").randomized_model (torch.nn.Module) – The same working model at each step, with all layers from the named layer back to the output re-initialized. Compute the attribution before advancing the iterator. To keep a model at a particular step, save
copy.deepcopy(randomized_model)then.