第3章回帰のための線形ニューラルネットワーク
3.4. Linear Regression Implementation from Scratch3.4 線形回帰をゼロから実装する
We are now ready to work through a fully functioning implementation of linear regression. In this section, we will implement the entire method from scratch, including (i) the model; (ii) the loss function; (iii) a minibatch stochastic gradient descent optimizer; and (iv) the training function that stitches all of these pieces together. Finally, we will run our synthetic data generator from Section 3.3 and apply our model on the resulting dataset. While modern deep learning frameworks can automate nearly all of this work, implementing things from scratch is the only way to make sure that you really know what you are doing. Moreover, when it is time to customize models, defining our own layers or loss functions, understanding how things work under the hood will prove handy. In this section, we will rely only on tensors and automatic differentiation. Later, we will introduce a more concise implementation, taking advantage of the bells and whistles of deep learning frameworks while retaining the structure of what follows below.
英語のヒント
これで、完全に機能する線形回帰の実装に取り組む準備ができた。この節では、手法全体をゼロから実装する。(i)モデル、(ii)損失関数、(iii)ミニバッチ確率的勾配降下法の最適化手法、(iv)これらの部品をすべてつなぎ合わせる訓練関数を含む。最後に、3.3節の合成データの生成器を走らせ、得られたデータセットにモデルを当てはめる。現代の深層学習フレームワークはこの作業のほぼすべてを自動化できるが、ゼロから実装することこそ、自分が何をしているのかを本当にわかっていると確かめる唯一の方法である。さらに、自分の層や損失関数を定義してモデルをカスタマイズする段になれば、内部で物事がどう動いているかを理解していることが役に立つ。この節では、テンソルと自動微分だけに頼る。後で、以下の構造は保ちつつ、深層学習フレームワークの多彩な機能を活用した、より簡潔な実装を紹介する。
%matplotlib inline
import torch
from d2l import torch as d2l