## Introduction to PyTorch NLLLOSS

PyTorch NLLLOSS has its long-form as Negative Log-Likelihood Loss which is the metric that is extensively used while dealing with C classes whenever the training of the C classes is being performed. In this article, we will have a detailed overview of What is PyTorch NLLLOSS, how to use PyTorch NLLLOSS, PyTorch NLLLOSS parameters, corresponding related examples, and finally, concludes our statement.

### What is PyTorch NLLLOSS?

PyTorch NLLLOSS is the metric used extensively in training the models especially in the case where we have our training set in an unbalanced condition. We can also provide one of the optional arguments named weight which must have its value specified as one dimensional tensor for each of the individual classes for setting the corresponding weights to them.

Log probabilities should be added in each of the class while providing the input in case of forward call. The size of the tensor input is expected to be either (minimum batch, C, d1, d2, …., dn) or (minimum batch, C) where the value of n is expected to be greater than or equal to 1 when the tensor is of n dimensions. Whenever we have the inputs of a higher dimension, we should go for using the first size. For example when we are dealing with two-dimensional images and we need to compute the value of NLL loss of PyTorch per pixel of the image.

When the mode of reduction is set to the value None that means we are about to calculate the unreduced loss, we can describe the same with the following formula –

Loss(x,y) = L = {l1,l2,l3,….ln}^T, ln = -WynXnyn, wc = weight[c].1 where the value of c is not equal to the ignore index value. In the above syntax, the value of x is the input, N is the value of the size of the batch, y is the target value while W is the value of weight. When the value of the reduction is not set to any which by default means mean then the loss is calculated as shown by using the below formula –

Loss(x,y)= { ∑ N to n=1 1/ ∑ n=1to N wyn * ln when the value of reduction is mean of simply loss is the {∑ N to n=1 ln when the value of reduction is set to sum.

### How to use PyTorch NLLLOSS?

Log probabilities are obtained by adding a new layer of Log soft max simply as the last layer of the neural network while making use in neural networks. If you want to avoid the addition of a new layer for this then you are free to make use of CrossEntropyLoss. [0, N-1 ] is the range of the class index between which we can expect the loss as a target.

Here, N will be equal to the value of the number of classes. The acceptance of the class index is also acceptable in the loss provided if we specify the ignore index value. In this scenario, also allows you to specify the class index having the value that doesn’t belong to the range of the class.

### PyTorch NLLLOSS Parameters

Various parameters described and used in the above syntax of PyTorch NLLLOSS are described in detail here –

- Size average – This is the optional Boolean value and is deprecated for usage. Inside a particular batch, the default behavior is the calculation of the average of each loss of the loss element inside the batch. In case of certain losses, each of the sample values may contain multiple associated elements. When the value of the size average parameter is set to false then inside each of the mini-batch, the value of losses is being summed up. By default, when not specified the value is set to true and this value is ignored when the value of the reducing parameter is set to false.
- Weight – This is the optional parameter whose value should eb in tensor format. Each of the classes is assigned manually the rescaling weight. Whenever specified, the value should be a tensor of N size. By default, the value of that tensor is considered to contain all the ones in it.
- Ignore index – This is also an optional parameter having the integer value. This is the value of target that is completely ignored and also not being considered in the gradient value of the input. When the value of the parameter size average is set to true then the average of loss is being made considering all the non ignored target values.
- Reduction – This is the optional string parameter used to specify the reduction that needs to be applied in the output value when the type of output is either mean, none or sum. The value of mean corresponds to the specification that the weighted mean of the output is considered, none means that none of the reduction is applicable, sum corresponds to the specification that the values of the output will be summed up. The parameters reduce and size average are completely deprecated and in the near future if we try to specify any of the values of those two parameters then it will be overridden by the reduction. When not specified the value of this parameter is treated to be mean.
- Reduce – This is the optional string value which is deprecated for now. The default value when not specified set to true which gives the average of loss or observations being summed up for each of the batch considering the value of size average. If the value of this parameter is set to false then the loss is returned per element of the batch and the value of the size average parameter is completely ignored.

### Examples

Let us consider one example as shown in the below code –

19 Online Courses | 29 Hands-on Projects| 178+ Hours| Verifiable Certificate of Completion

4.7

View Course

`sampleEducbaModel = neuralNetwork.LogSoftmax(dim=1)`

sampleObtainedLoss = neuralNetwork.NLLLOSS()

# Specification of the size of sampleInput is 3 * 5

sampleInput = torch.randn(3, 5, requires_grad=True)

# individual element should contain the value that lies in the range of 0 to C (0 inclusive)

sampleTarget = torch.tensor([1, 0, 4])

achievedOutput = sampleObtainedLoss(sampleEducbaModel(sampleInput), sampleTarget)

achievedOutput.backward()

print ('Retrieved Result: ', achievedOutput)

The execution of the above program gives the output as shown below –

### Conclusion

PyTorch NLLLOSS is used for calculating the negative log-likelihood function which can be used only for the models that have softmax function applicable for the activation layer of output.

### Recommended Articles

This is a guide to PyTorch NLLLOSS. Here we discuss the Introduction, What is PyTorch NLLLOSS?, How to use PyTorch NLLLOSS?, Example, and code. You may also have a look at the following articles to learn more –