Feature Attribution¶
- confetti.attribution.cam.cam(model, X_data: ndarray)¶
Compute Class Activation Maps (CAMs) [1]. for 1D convolutional classifiers.
This function computes class activation maps for a trained model by combining the activations of the final convolutional layer with the weights of the final classifier layer. It highlights the temporal regions most influential for the model’s predicted class.
Both Keras and PyTorch models are supported. For Keras models, the function inspects
Conv1DandDenselayers. For PyTorch models, it inspectsConv1dandLinearlayers via forward hooks.- Parameters:
model (keras.Model, torch.nn.Module, or TorchModelAdapter) – A trained model containing at least one 1-D convolutional layer followed by a linear classification layer.
X_data (ndarray of shape (n_samples, timesteps, channels)) – Input multivariate time series for which CAMs will be computed.
- Returns:
The class activation map for each input instance.
- Return type:
ndarray of shape (n_samples, timesteps)
- Raises:
CONFETTIConfigurationError – If no convolutional layer is found, if no classifier layer is present, or if the number of filters in the final convolutional layer does not match the input dimensionality of the classifier layer.
Note
The CAM for a given instance is defined as:
CAM(t) = Σ_k w[k, c] * A[k, t]
- where:
A[k, t]is the activation of filterkat timesteptfrom the final convolutional layer.w[:, c]are the weights connecting each filter to the predicted classcin the final dense layer.
References
- confetti.attribution.cam_visualization.visualize_cam(weights: ndarray, instance_index: int, theme: str = 'light') None¶
Visualize the Class Activation Map (CAM) for a single instance.
This function produces a matplotlib plot showing the normalized class activation map for a chosen instance. The weights for all instances are first min–max normalized across the dataset, and the selected CAM is displayed over time as a line chart with a heatmap strip beneath it.
- Parameters:
weights (ndarray of shape (n_instances, timesteps)) – The CAM weights computed for each instance.
instance_index (int) – Index of the instance whose CAM should be visualized.
theme ({"light", "dark"}, default="light") – Color theme for the plot.
- Raises:
IndexError – If
instance_indexis out of range forweights.ValueError – If
weightsis not a 2D array.CONFETTIConfigurationError – If
themeis not a recognized value.
Note
The CAM values are normalized using
MinMaxScalerprior to visualization to enable clearer comparison across instances.