Loss Functions#

Classes#

class BaseLoss(loss_func, *args, **kwargs)[source]#

Bases: Module

A base class for loss functions.

This class is used to add all loss function classes to the list of safe PyTorch classes when loading saved architectures.

Parameters:
loss_functype

Loss function class to be used

*args

Optional arguments to be passed to loss_func

**kwargs

Optional keyword arguments to be passed to loss_func

forward(output, target)[source]#

Forward pass of the loss function.

Parameters:
outputTensor

Output from the network with shape (N,…), where N is the number of elements N predictions from the network

targetTensor

Target values with shape (N,…)

Returns:
Tensor

Loss value with shape (1)

class GeneralLoss(loss_func, *args, **kwargs)[source]#

Bases: BaseLoss

A general loss function that can be used with any torch.nn loss function class.

Parameters:
loss_funcstr

Loss function name from torch.nn

*args

Optional arguments to be passed to loss_func

**kwargs

Optional keyword arguments to be passed to loss_func

extra_repr()[source]#

Return the extra representation of the module.

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

class CrossEntropyLoss(*args, **kwargs)[source]#

Bases: GeneralLoss

Cross entropy loss function.

Parameters:
*args

Optional arguments to be passed to CrossEntropyLoss

**kwargs

Optional keyword arguments to be passed to CrossEntropyLoss

class GaussianNLLLoss(*args, dim=1, **kwargs)[source]#

Bases: GeneralLoss

Gaussian negative log likelihood loss function.

Parameters:
dimint, Optional

Dimension of the target that contains the mean and uncertainty, default = 1

*args

Optional arguments to be passed to GaussianNLLLoss

**kwargs

Optional keyword arguments to be passed to GaussianNLLLoss

extra_repr()[source]#

Return the extra representation of the module.

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(output, target)[source]#

Forward pass of the loss function.

Parameters:
outputTensor

Output from the network with shape (N,…), where N is the number of elements N predictions from the network

targetTensor

Target values with shape (N,…)

Returns:
Tensor

Loss value with shape (1)

class MSELoss(*args, **kwargs)[source]#

Bases: GeneralLoss

Mean Squared Error (MSE) loss function.

Parameters:
*args

Optional arguments to be passed to MSELoss

**kwargs

Optional keyword arguments to be passed to MSELoss