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

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:

主站蜘蛛池模板: 隆尧县| 广南县| 塔城市| 邵东县| 德化县| 连州市| 西城区| 老河口市| 花莲县| 休宁县| 舞阳县| 绥化市| 颍上县| 松阳县| 博爱县| 广灵县| 溆浦县| 株洲市| 自治县| 泸水县| 兴和县| 文水县| 连云港市| 浦城县| 夏津县| 奈曼旗| 泾川县| 景德镇市| 峨山| 盐亭县| 喜德县| 新建县| 启东市| 阿荣旗| 马尔康县| 任丘市| 从江县| 阜康市| 临江市| 万州区| 广西|