NeuroTorch

neurotorch.metrics package

Submodules

neurotorch.metrics.base module

class neurotorch.metrics.base.BaseMetrics(model: BaseModel, metrics_names: Any = 'all', device: device | None = None)

Bases: object

This base class is used to compute metrics for a model.

Attributes:
  • model (BaseModel): The model to evaluate.

  • metrics_names (List[str]): The metrics to compute.

  • device (torch.device): The device in which the metrics will be computed.

METRICS_NAMES_SEP = ' '
__call__(data_loader: DataLoader, verbose: bool | int = False) Dict[str, Any]

Compute the metrics for the given data_loader.

Parameters:
  • data_loader (DataLoader) – The data loader to use to compute the metrics.

  • verbose (Union[bool, int]) – 0: no progress bar, 1: single progress bar, 2: progress bar for each metrics True: verbose is set to 1, False: verbose is set to 0.

Returns:

The metrics computed.

Return type:

Dict[str, Any]

__init__(model: BaseModel, metrics_names: Any = 'all', device: device | None = None)

Constructor for the BaseMetrics class.

Parameters:
  • model (BaseModel) – the model to evaluate.

  • metrics_names (Any) – the metrics to compute.

  • device (Optional[torch.device]) – The device in which the metrics will be computed.

classmethod get_all_metrics_names() List[str]

Get all the metrics names.

Returns:

The metrics names.

Return type:

List[str]

static get_all_metrics_names_to_func() Dict[str, Callable]

Get all the metrics names and their associated functions.

Raises:

NotImplementedError – This method must be implemented in the child class.

classmethod get_unique_metrics_names() List[str]

Get all the unique metrics names.

Returns:

The unique metrics names.

Return type:

List[str]

classmethod get_unwrap_all_metrics_names_to_func() Dict[str, Callable]

Get all the metrics names and their associated functions. The metrics names are unwrapped.

Returns:

The metrics names and their associated functions.

Return type:

Dict[str, Callable]

property metrics_functions: Dict[str, Callable]

Get the metrics functions to compute.

Returns:

The metrics functions to compute.

Return type:

Dict[str, Callable]

neurotorch.metrics.classification module

class neurotorch.metrics.classification.ClassificationMetrics(model: BaseModel, metrics_names: Any = 'all', device: device | None = None)

Bases: BaseMetrics

This class is used to compute metrics for a classification model.

__call__(data_loader: DataLoader, verbose: bool | int = False) Dict[str, Any]

Compute the metrics for the given data_loader. :param data_loader: The data loader to use to compute the metrics. :param verbose: 0: no progress bar, 1: single progress bar, 2: progress bar for each metrics

True: verbose is set to 1, False: verbose is set to 0.

Returns:

The metrics computed.

static accuracy(model: BaseModel | None = None, dataloader: DataLoader | None = None, y_true: ndarray | None = None, y_pred: ndarray | None = None, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) float | Dict[str, float]
static auc(model: BaseModel | None = None, dataloader: DataLoader | None = None, y_true: ndarray | None = None, y_pred: ndarray | None = None, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) float | Dict[str, float]
static compute_y_true_y_pred(model: BaseModel, dataloader: DataLoader, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) Tuple[Dict[str, ndarray], Dict[str, ndarray]] | Tuple[ndarray, ndarray]
static confusion_matrix(model: BaseModel | None = None, dataloader: DataLoader | None = None, y_true: ndarray | None = None, y_pred: ndarray | None = None, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) Dict[str, ndarray] | ndarray
static f1(model: BaseModel | None = None, dataloader: DataLoader | None = None, y_true: ndarray | None = None, y_pred: ndarray | None = None, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) float | Dict[str, float]
static get_all_metrics_names_to_func() Dict[str, Callable]

Get all the metrics names and their associated functions.

Raises:

NotImplementedError – This method must be implemented in the child class.

static precision(model: BaseModel | None = None, dataloader: DataLoader | None = None, y_true: ndarray | None = None, y_pred: ndarray | None = None, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) float | Dict[str, float]
static recall(model: BaseModel | None = None, dataloader: DataLoader | None = None, y_true: ndarray | None = None, y_pred: ndarray | None = None, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) float | Dict[str, float]

neurotorch.metrics.losses module

class neurotorch.metrics.losses.NLLLoss(target_as_one_hot: bool = False, **kwargs)

Bases: NLLLoss

Class used to compute the negative log likelihood loss.

\(\text{NLLLoss}(x, y) = -\sum_{i=1}^n y_i \log(x_i)\)

The difference between this class and the PyTorch NLLLoss class is that this class allows ND inputs and targets by flattening the inputs to 2D and the targets to 1D. If the target is marked as one-hot encoded, then the target will be converted to a 1D tensor of class indices by taking the argmax of the last dimension.

__init__(target_as_one_hot: bool = False, **kwargs)

Constructor for the NLLLoss class.

Parameters:

target_as_one_hot – Whether the target is one-hot encoded. Defaults to False.

:keyword arguments : Arguments for the PyTorch NLLLoss class. See the PyTorch documentation for more details.

forward(inputs: Tensor, target: Tensor) Tensor

Calculate the NLL loss.

Parameters:
  • inputs – The input.

  • target – The target.

Returns:

The NLL loss.

ignore_index: int
class neurotorch.metrics.losses.PVarianceLoss(negative: bool = False, reduction: str = 'mean', **kwargs)

Bases: Module

Class used to compute the P-Variance loss.

\(\text{P-Variance}(x, y) = 1 - \frac{\text{MSE}(x, y)}{\text{Var}(y)}\)

Attributes:
  • criterion (nn.MSELoss): The MSE loss.

  • negative (bool): Whether to return the negative P-Variance loss.

  • reduction (str): The reduction method to use. If ‘mean’, the output will be averaged. If ‘feature’, the

    output will be the shape of the last dimension of the input. If ‘none’, the output will be the same shape as the input.

__init__(negative: bool = False, reduction: str = 'mean', **kwargs)

Constructor for the PVarianceLoss class.

Parameters:
  • negative (bool) – Whether to return the negative P-Variance loss.

  • reduction (str) – The reduction method to use. If ‘mean’, the output will be averaged. If ‘feature’, the output will be the shape of the last dimension of the input. If ‘none’, the output will be the same shape as the input. Defaults to ‘mean’.

:keyword arguments : epsilon: The epsilon value to use to prevent division by zero. Defaults to 1e-5.

forward(x, y)

Calculate the P-Variance loss.

Parameters:
  • x – The first input.

  • y – The second input.

Returns:

The P-Variance loss.

mean_std_over_batch(x, y)

Calculate the mean and standard deviation of the P-Variance loss over the batch.

Parameters:
  • x – The first input.

  • y – The second input.

Returns:

The mean and standard deviation of the P-Variance loss over the batch.

training: bool
class neurotorch.metrics.losses.RMSELoss

Bases: Module

Class used to compute the RMSE loss.

\(\text{RMSE}(x, y) = \sqrt{\frac{1}{n}\sum_{i=1}^n (x_i - y_i)^2}\)

Attributes:
  • criterion (nn.MSELoss): The MSE loss.

__init__()

Constructor for the RMSELoss class.

forward(x, y)

Calculate the RMSE loss.

Parameters:
  • x – The first input.

  • y – The second input.

Returns:

The RMSE loss.

training: bool
class neurotorch.metrics.losses.SMSEloss(reduction: str = 'mean', **kwargs)

Bases: Module

__init__(reduction: str = 'mean', **kwargs)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(inputs: Tensor, target: Tensor) Tensor

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool

neurotorch.metrics.regression module

class neurotorch.metrics.regression.RegressionMetrics(model: BaseModel, metrics_names: Any = 'all', device: device | None = None)

Bases: BaseMetrics

EPSILON = 1e-12
__call__(data_loader: DataLoader, verbose: bool | int = False) Dict[str, Any]

Compute the metrics for the given data_loader. :param data_loader: The data loader to use to compute the metrics. :param verbose: 0: no progress bar, 1: single progress bar, 2: progress bar for each metrics

True: verbose is set to 1, False: verbose is set to 0.

Returns:

The metrics computed.

static compute_p_var(y_true, y_pred, device: device | None = None, reduction: str = 'mean') Tensor
static compute_y_true_y_pred(model: BaseModel, dataloader: DataLoader, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) Tuple[Dict[str, ndarray], Dict[str, ndarray]] | Tuple[ndarray, ndarray]
static d2_tweedie(model: BaseModel | None = None, dataloader: DataLoader | None = None, y_true: ndarray | None = None, y_pred: ndarray | None = None, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) float | Dict[str, float]
static get_all_metrics_names_to_func() Dict[str, Callable]

Get all the metrics names and their associated functions.

Raises:

NotImplementedError – This method must be implemented in the child class.

static mean_absolute_error(model: BaseModel | None = None, dataloader: DataLoader | None = None, y_true: ndarray | None = None, y_pred: ndarray | None = None, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) float | Dict[str, float]
static mean_squared_error(model: BaseModel | None = None, dataloader: DataLoader | None = None, y_true: ndarray | None = None, y_pred: ndarray | None = None, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) float | Dict[str, float]
static p_var(model: BaseModel | None = None, dataloader: DataLoader | None = None, y_true: ndarray | None = None, y_pred: ndarray | None = None, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) float | Dict[str, float]

Compute the p-value of the variance of the prediction. :param model: The model to use for computing the p-value. :param dataloader: The dataloader to use for computing the p-value. :param y_true: The true values. :param y_pred: The predicted values. :param device: The device to use for computing the p-value. :param verbose: Whether to print progress. :param desc: The description to use for the progress bar. :param p_bar_position: The position of the progress bar. :return: The p-value of the variance of the prediction.

static r2(model: BaseModel | None = None, dataloader: DataLoader | None = None, y_true: ndarray | None = None, y_pred: ndarray | None = None, device: device | None = None, verbose: bool = False, desc: str | None = None, p_bar_position: int = 0) float | Dict[str, float]

Module contents