プリンキピア

第3章回帰のための線形ニューラルネットワーク

3.4.2. Defining the Loss Function3.4.2 損失関数の定義

Since updating our model requires taking the gradient of our loss function, we ought to define the loss function first. Here we use the squared loss function in (3.1.5). In the implementation, we need to transform the true value y into the predicted value’s shape y_hat. The result returned by the following method will also have the same shape as y_hat. We also return the averaged loss value among all examples in the minibatch.
英語のヒント
モデルの更新には損失関数の勾配をとる必要があるので、まず損失関数を定義すべきである。ここでは(3.1.5)の二乗損失関数を使う。実装では、本当の値yを予測値の形状y_hatに変換する必要がある。次のメソッドが返す結果もy_hatと同じ形状になる。また、ミニバッチのすべての事例にわたって平均した損失の値を返す。
@d2l.add_to_class(LinearRegressionScratch)  #@save
def loss(self, y_hat, y):
    l = (y_hat - y) ** 2 / 2
    return l.mean()