An Overview of Nineteen Loss Functions in Pytorch

Click the "Little White Learns Vision" above, choose to add "Star" or "Top"
Heavy content delivered to you immediately
For academic sharing only, does not represent the position of this public account, please contact for deletion if there is any infringement
Reprinted from:Author:mingo_敏

Original link: https://blog.csdn.net/shanglianlm/article/details/85019768

Introduction
This article summarizes nineteen loss functions, introducing their mathematical formulas and code implementations, hoping everyone can master them proficiently.

01

Basic Usage
criterion = LossCriterion() # Constructor has its own parameters
loss = criterion(x, y) # Call standard also has parameters

02

Loss Functions

2-1 L1 Norm Loss L1Loss

Calculates the absolute difference between output and target.

torch.nn.L1Loss(reduction='mean')

Parameters:

reduction – three values, none: no reduction; mean: returns the average of losses; sum: returns the sum of losses. Default: mean.

2-2 Mean Squared Error Loss MSELoss

Calculates the mean squared difference between output and target.

torch.nn.MSELoss(reduction='mean')

Parameters:

reduction – three values, none: no reduction; mean: returns the average of losses; sum: returns the sum of losses. Default: mean.

2-3 Cross Entropy Loss CrossEntropyLoss

Effective for training classification problems with C categories. The optional parameter weight must be a 1D Tensor, weights will be assigned to each category. Very effective for unbalanced training sets.

In multi-class tasks, softmax activation function + cross-entropy loss function is often used, because cross-entropy describes the difference between two probability distributions, while the neural network outputs a vector, not in the form of a probability distribution. So the softmax activation function is needed to “normalize” a vector into the form of a probability distribution, and then use the cross-entropy loss function to calculate loss.

An Overview of Nineteen Loss Functions in Pytorch

torch.nn.CrossEntropyLoss(weight=None, ignore_index=-100, reduction='mean')

Parameters:

weight (Tensor, optional) – Custom weights for each category. Must be a Tensor of length C

ignore_index (int, optional) – Sets a target value that will be ignored, thus not affecting the input gradient.

reduction – three values, none: no reduction; mean: returns the average of losses; sum: returns the sum of losses. Default: mean.

2-4 KL Divergence Loss KLDivLoss

Calculates the KL divergence between input and target. KL divergence can be used to measure the distance between different continuous distributions, and is very effective when performing direct regression on the space of continuous output distributions (discrete sampling).

torch.nn.KLDivLoss(reduction='mean')

Parameters:

reduction – three values, none: no reduction; mean: returns the average of losses; sum: returns the sum of losses. Default: mean.

2-5 Binary Cross Entropy Loss BCELoss

Cross-entropy calculation function for binary classification tasks. Used to measure reconstruction errors, e.g., in autoencoders. Note that the target value t[i] ranges between 0 and 1.

torch.nn.BCELoss(weight=None, reduction='mean')

Parameters:

weight (Tensor, optional) – Custom weights for each batch element’s loss. Must be a Tensor of length “nbatch”

pos_weight (Tensor, optional) – Custom weights for each positive sample’s loss. Must be a Tensor of length “classes”

2-6 BCEWithLogitsLoss

The BCEWithLogitsLoss function integrates the Sigmoid layer into the BCELoss class. This version is numerically more stable than using a simple Sigmoid layer and BCELoss, because it combines these two operations into one layer, allowing the use of log-sum-exp techniques for numerical stability.

torch.nn.BCEWithLogitsLoss(weight=None, reduction='mean', pos_weight=None)

Parameters:

weight (Tensor, optional) – Custom weights for each batch element’s loss. Must be a Tensor of length “nbatch”

pos_weight (Tensor, optional) – Custom weights for each positive sample’s loss. Must be a Tensor of length “classes”

2-7 Margin Ranking Loss

torch.nn.MarginRankingLoss(margin=0.0, reduction='mean')

For each instance in the mini-batch, the loss function is as follows:

An Overview of Nineteen Loss Functions in Pytorch

Parameters:

margin: default value 0

2-8 Hinge Embedding Loss

torch.nn.HingeEmbeddingLoss(margin=1.0, reduction='mean')

For each instance in the mini-batch, the loss function is as follows:

Parameters:

An Overview of Nineteen Loss Functions in Pytorch

margin: default value 1

2-9 Multi-label Classification Loss MultiLabelMarginLoss

torch.nn.MultiLabelMarginLoss(reduction='mean')

For each sample in the mini-batch, the loss is calculated as follows:

An Overview of Nineteen Loss Functions in Pytorch

2-10 Smooth L1 Loss SmoothL1Loss

Also known as Huber loss function.

torch.nn.SmoothL1Loss(reduction='mean')

An Overview of Nineteen Loss Functions in Pytorch

Where

An Overview of Nineteen Loss Functions in Pytorch

2-11 Logistic Loss for Binary Classification SoftMarginLoss

torch.nn.SoftMarginLoss(reduction='mean')

An Overview of Nineteen Loss Functions in Pytorch

2-12 Multi-label One-vs-All Loss MultiLabelSoftMarginLoss

torch.nn.MultiLabelSoftMarginLoss(weight=None, reduction='mean')

An Overview of Nineteen Loss Functions in Pytorch

2-13 Cosine Loss CosineEmbeddingLoss

torch.nn.CosineEmbeddingLoss(margin=0.0, reduction='mean')

An Overview of Nineteen Loss Functions in Pytorch

Parameters:

margin: default value 0

2-14 Multi-class Hinge Loss MultiMarginLoss

torch.nn.MultiMarginLoss(p=1, margin=1.0, weight=None, reduction='mean')

An Overview of Nineteen Loss Functions in Pytorch

Parameters:

p=1 or 2 Default: 1

margin: default value 1

2-15 Triplet Loss TripletMarginLoss

torch.nn.TripletMarginLoss(margin=1.0, p=2.0, eps=1e-06, swap=False, reduction='mean')

An Overview of Nineteen Loss Functions in Pytorch

Where:

An Overview of Nineteen Loss Functions in Pytorch

2-16 CTC Loss CTCLoss

CTC (Connectionist Temporal Classification) loss can automatically align data that is not aligned, mainly used for training serialized data without prior alignment, such as speech recognition, OCR recognition, etc.

torch.nn.CTCLoss(blank=0, reduction='mean')

Parameters:

reduction – three values, none: no reduction; mean: returns the average of losses; sum: returns the sum of losses. Default: mean.

2-17 Negative Log Likelihood Loss NLLLoss

Negative log likelihood loss. Used for training classification problems with C categories.

torch.nn.NLLLoss(weight=None, ignore_index=-100, reduction='mean')

Parameters:

weight (Tensor, optional) – Custom weights for each category. Must be a Tensor of length C

ignore_index (int, optional) – Sets a target value that will be ignored, thus not affecting the input gradient.

2-18 NLLLoss2d

Negative log likelihood loss for image input. It calculates the negative log likelihood loss for each pixel.

torch.nn.NLLLoss2d(weight=None, ignore_index=-100, reduction='mean')

Parameters:

weight (Tensor, optional) – Custom weights for each category. Must be a Tensor of length C

reduction – three values, none: no reduction; mean: returns the average of losses; sum: returns the sum of losses. Default: mean.

2-19 Poisson NLL Loss PoissonNLLLoss

Negative log likelihood loss for a target value following a Poisson distribution

torch.nn.PoissonNLLLoss(log_input=True, full=False, eps=1e-08, reduction='mean')

Parameters:

log_input (bool, optional) – If set to True, loss will be calculated according to the formula exp(input) – target * input. If set to False, loss will be calculated according to input – target * log(input+eps).

full (bool, optional) – Whether to compute the full loss, i.e., including the Stirling approximation term target * log(target) – target + 0.5 * log(2 * pi * target).

eps (float, optional) – Default value: 1e-8

References

http://www.voidcn.com/article/p-rtzqgqkz-bpg.html

Download 1: OpenCV-Contrib Extension Module Chinese Tutorial

Reply "Extension Module Chinese Tutorial" in the "Little White Learns Vision" public account backend to download the first OpenCV extension module tutorial in Chinese on the internet, covering more than twenty chapters including extension module installation, SFM algorithm, stereo vision, target tracking, biological vision, super-resolution processing, etc.

Download 2: Python Vision Practical Project 52 Lectures

Reply "Python Vision Practical Project" in the "Little White Learns Vision" public account backend to download 31 vision practical projects including image segmentation, mask detection, lane detection, vehicle counting, eyeliner addition, license plate recognition, character recognition, emotion detection, text content extraction, facial recognition, etc., to help quickly learn computer vision.

Download 3: OpenCV Practical Project 20 Lectures

Reply "OpenCV Practical Project 20 Lectures" in the "Little White Learns Vision" public account backend to download 20 practical projects based on OpenCV to achieve advanced learning of OpenCV.

Group Chat

Welcome to join the reader group of the public account to communicate with peers. Currently, there are WeChat groups for SLAM, 3D vision, sensors, autonomous driving, computational photography, detection, segmentation, recognition, medical imaging, GAN, algorithm competitions, etc. (will be gradually subdivided in the future). Please scan the WeChat number below to join the group, with the note: "Nickname + School/Company + Research Direction", for example: "Zhang San + Shanghai Jiao Tong University + Vision SLAM". Please follow the format for the note, otherwise, it will not be approved. After successful addition, you will be invited to the relevant WeChat group based on your research direction. Please do not send advertisements in the group, otherwise, you will be removed from the group. Thank you for your understanding~

Leave a Comment