braindecode.visualization.layer_grad_cam#

braindecode.visualization.layer_grad_cam(model, x, target, layer)[source]#

Compute a class activation map at a convolutional layer with Captum.

A layer with a three-dimensional output (batch, features, length), such as a temporal convolution, gives one map per trial of shape (batch, n_times): its last axis, which must be time, is linearly interpolated to n_times. A layer with a four-dimensional output (batch, features, channels, time) gives a map of shape (batch, n_chans, n_times): its two last axes are bilinearly interpolated to (n_chans, n_times). Its channel axis must hold n_chans entries, or one once the layer has mixed the channels, and its time axis more than one step; otherwise a ValueError names the layer. This rejects the layers of ShallowFBCSPNet and Deep4Net after their dimshuffle, which hold time on the height axis. Token sequences of transformers hold tokens on the features axis, so their maps are not maps over time. Localization is limited by the resolution of the selected layer.