gunz_ml.analysis package
Deep Learning Analysis Modules.
This package provides tools for inspecting models, analyzing training dynamics, and evaluating error patterns.
- gunz_ml.analysis.compute_confusion_matrix(preds: torch.Tensor | ndarray, targets: torch.Tensor | ndarray, num_classes: int) ndarray[source]
Computes the confusion matrix.
- Parameters:
preds (torch.Tensor or numpy.ndarray) – Predicted class indices (1D).
targets (torch.Tensor or numpy.ndarray) – True class indices (1D).
num_classes (int) – Total number of classes.
- Returns:
Confusion matrix of shape (num_classes, num_classes). Rows represent true classes, columns represent predicted classes.
- Return type:
- gunz_ml.analysis.compute_gradient_norms(model: torch.nn.Module, batch: Any, criterion: torch.nn.Module, device: torch.device) dict[str, float][source]
Computes the L2 norm of gradients for a single batch.
- Parameters:
model (nn.Module) – The PyTorch model.
batch (Any) – A batch of data (inputs, targets) compatible with the model.
criterion (nn.Module) – Loss function.
device (torch.device) – Device to run the computation on.
- Returns:
A dictionary mapping parameter names to their gradient L2 norms.
- Return type:
- gunz_ml.analysis.count_parameters(model: torch.nn.Module) dict[str, int][source]
Counts total, trainable, and non-trainable parameters.
- gunz_ml.analysis.estimate_flops(model: torch.nn.Module, input_sample: torch.Tensor) float[source]
Estimates floating point operations (FLOPs) for a single forward pass.
This is a simplified estimation supporting Linear and Conv2d layers.
- Parameters:
model (nn.Module) – The PyTorch model.
input_sample (torch.Tensor) – A sample input tensor (batch size should ideally be 1 for normalized FLOPs, but the function calculates total FLOPs for the given batch).
- Returns:
Estimated number of floating point operations.
- Return type:
- gunz_ml.analysis.get_top_k_errors(preds: torch.Tensor, targets: torch.Tensor, probs: torch.Tensor, k: int = 5) List[Dict[str, Any]][source]
Identifies the top-k misclassified examples based on confidence.
- Parameters:
preds (torch.Tensor) – Predicted class indices.
targets (torch.Tensor) – True class indices.
probs (torch.Tensor) – Probabilities of the predicted classes (confidence).
k (int, optional) – Number of examples to retrieve. Defaults to 5.
- Returns:
List of dictionaries containing index, target, prediction, and confidence.
- Return type:
- gunz_ml.analysis.layer_stats(model: torch.nn.Module) dict[str, dict[str, float]][source]
Computes statistics (mean, std, min, max) for weight parameters.
Submodules
gunz_ml.analysis.dynamics module
Analysis modules for training dynamics (gradients, logs).
- gunz_ml.analysis.dynamics.compute_gradient_norms(model: torch.nn.Module, batch: Any, criterion: torch.nn.Module, device: torch.device) dict[str, float][source]
Computes the L2 norm of gradients for a single batch.
- Parameters:
model (nn.Module) – The PyTorch model.
batch (Any) – A batch of data (inputs, targets) compatible with the model.
criterion (nn.Module) – Loss function.
device (torch.device) – Device to run the computation on.
- Returns:
A dictionary mapping parameter names to their gradient L2 norms.
- Return type:
gunz_ml.analysis.errors module
Analysis modules for error analysis (confusion matrix, top-k errors).
- gunz_ml.analysis.errors.compute_confusion_matrix(preds: torch.Tensor | ndarray, targets: torch.Tensor | ndarray, num_classes: int) ndarray[source]
Computes the confusion matrix.
- Parameters:
preds (torch.Tensor or numpy.ndarray) – Predicted class indices (1D).
targets (torch.Tensor or numpy.ndarray) – True class indices (1D).
num_classes (int) – Total number of classes.
- Returns:
Confusion matrix of shape (num_classes, num_classes). Rows represent true classes, columns represent predicted classes.
- Return type:
- gunz_ml.analysis.errors.get_top_k_errors(preds: torch.Tensor, targets: torch.Tensor, probs: torch.Tensor, k: int = 5) List[Dict[str, Any]][source]
Identifies the top-k misclassified examples based on confidence.
- Parameters:
preds (torch.Tensor) – Predicted class indices.
targets (torch.Tensor) – True class indices.
probs (torch.Tensor) – Probabilities of the predicted classes (confidence).
k (int, optional) – Number of examples to retrieve. Defaults to 5.
- Returns:
List of dictionaries containing index, target, prediction, and confidence.
- Return type:
gunz_ml.analysis.internals module
Analysis modules for model internals (complexity, parameters, weights).
- gunz_ml.analysis.internals.count_parameters(model: torch.nn.Module) dict[str, int][source]
Counts total, trainable, and non-trainable parameters.
- gunz_ml.analysis.internals.estimate_flops(model: torch.nn.Module, input_sample: torch.Tensor) float[source]
Estimates floating point operations (FLOPs) for a single forward pass.
This is a simplified estimation supporting Linear and Conv2d layers.
- Parameters:
model (nn.Module) – The PyTorch model.
input_sample (torch.Tensor) – A sample input tensor (batch size should ideally be 1 for normalized FLOPs, but the function calculates total FLOPs for the given batch).
- Returns:
Estimated number of floating point operations.
- Return type:
gunz_ml.analysis.runner module
Runner for the Deep Learning Analysis mode.