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

Loss function

As we start with random values, our learnable parameters, w and b, will result in y_pred, which will not be anywhere close to the actual y. So, we need to define a function which tells the model how close its predictions are to the actual values. Since this is a regression problem, we use a loss function called sum of squared error (SSE). We take the difference between the predicted y and the actual y and square it. SSE helps the model to understand how close the predicted values are to the actual values. The torch.nn library has different loss functions, such as MSELoss and cross-entropy loss. However, for this chapter, let's implement the loss function ourselves:

def loss_fn(y,y_pred):
loss = (y_pred-y).pow(2).sum()
for param in [w,b]:
if not param.grad is None: param.grad.data.zero_()
loss.backward()
return loss.data[0]

Apart from calculating the loss, we also call the backward operation, which calculates the gradients of our learnable parameters, w and b. As we will use the loss function more than once, we remove any previously calculated gradients by calling the grad.data.zero_() operation. The first time we call the backward function, the gradients are empty, so we zero the gradients only when they are not None

主站蜘蛛池模板: 杨浦区| 黄石市| 宁波市| 韩城市| 镇巴县| 延川县| 黔西县| 油尖旺区| 灵寿县| 长岭县| 松江区| 益阳市| 枣庄市| 盘山县| 乌海市| 密云县| 清苑县| 曲周县| 云龙县| 绿春县| 阜平县| 咸丰县| 丹阳市| 昌平区| 德格县| 巴南区| 靖边县| 娄烦县| 镇雄县| 万年县| 拉孜县| 喀喇沁旗| 封丘县| 绥阳县| 广安市| 达拉特旗| 霸州市| 嵩明县| 开平市| 元江| 微山县|