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

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

主站蜘蛛池模板: 炎陵县| 黎川县| 凯里市| 沂源县| 临朐县| 平遥县| 东乡| 仁怀市| 永福县| 东阿县| 宿松县| 科技| 垫江县| 金坛市| 隆林| 周至县| 济南市| 新绛县| 泸定县| 广东省| 五常市| 仁怀市| 阿坝| 民丰县| 抚州市| 津南区| 洛阳市| 平塘县| 闸北区| 恩平市| 正安县| 彝良县| 榕江县| 柳林县| 徐水县| 中阳县| 西青区| 神池县| 叙永县| 科技| 手游|