官术网_书友最值得收藏!

Loss functions

Once we have defined our network architecture, we are left with two important steps. One is calculating how good our network is at performing a particular task of regression, classification, and the next is optimizing the weight.

The optimizer (gradient descent) generally accepts a scalar value, so our loss function should generate a scalar value that has to be minimized during our training. Certain use cases, such as predicting where an obstacle is on the road and classifying it to a pedestrian or not, would require two or more loss functions. Even in such scenarios, we need to combine the losses to a single scalar for the optimizer to minimize. We will discuss examples of combining multiple losses to a single scalar in detail with a real-world example in the last chapter.

In the previous chapter, we defined our own loss function. PyTorch provides several implementations of commonly used loss functions. Let's take a look at the loss functions used for regression and classification.

The commonly used loss function for regression problems is mean square error (MSE). It is the same loss function we implemented in our previous chapter. We can use the loss function implemented in PyTorch, as follows:

loss = nn.MSELoss()
input = Variable(torch.randn(3, 5), requires_grad=True)
target = Variable(torch.randn(3, 5))
output = loss(input, target)
output.backward()

For classification, we use a cross-entropy loss. Before looking at the math for cross-entropy, let's understand what a cross-entropy loss does. It calculates the loss of a classification network predicting the probabilities, which should sum up to one, like our softmax layer. A cross-entropy loss increases when the predicted probability diverges from the correct probability. For example, if our classification algorithm predicts 0.1 probability for the following image to be a cat, but it is actually a panda, then the cross-entropy loss will be higher. If it predicts similar to the actual labels, then the cross-entropy loss will be lower:

Let's look at a sample implementation of how this actually happens in Python code:

def cross_entropy(true_label, prediction):
    if true_label == 1:
        return -log(prediction)
    else:
        return -log(1 - prediction)

To use a cross-entropy loss in a classification problem, we really do not need to be worried about what happens inside—all we have to remember is that, the loss will be high when our predictions are bad and low when predictions are good. PyTorch provides us with an implementation of the loss, which we can use, as follows:

loss = nn.CrossEntropyLoss()
input = Variable(torch.randn(3, 5), requires_grad=True)
target = Variable(torch.LongTensor(3).random_(5))
output = loss(input, target)
output.backward()

Some of the other loss functions that come as part of PyTorch are as follows:

主站蜘蛛池模板: 乌鲁木齐市| 黄山市| 柳州市| 皮山县| 万山特区| 岐山县| 朝阳县| 新兴县| 凌云县| 开平市| 天水市| 安庆市| 新龙县| 合阳县| 淮安市| 屏东县| 桃源县| 三江| 鱼台县| 临沧市| 府谷县| 满城县| 五原县| 浦城县| 农安县| 东港市| 榆社县| 澄城县| 奉新县| 平湖市| 普兰店市| 上杭县| 临沂市| 赣州市| 油尖旺区| 丰宁| 乐至县| 宁晋县| 眉山市| 古交市| 台山市|