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

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

主站蜘蛛池模板: 建平县| 城口县| 崇信县| 吉首市| 祁东县| 苍溪县| 萝北县| 蒙城县| 桃园市| 沿河| 保康县| 陈巴尔虎旗| 清镇市| 桂平市| 郓城县| 榆社县| 上思县| 海伦市| 开化县| 勃利县| 鄢陵县| 大新县| 碌曲县| 沙湾县| 女性| 大连市| 贵南县| 达州市| 重庆市| 奉节县| 武山县| 社会| 定结县| 汉源县| 肇东市| 都江堰市| 调兵山市| 元谋县| 科尔| 永修县| 甘谷县|