第4章分類のための線形ニューラルネットワーク
4.4.2. The Model4.4.2 モデル
We now have everything that we need to implement the softmax regression model. As in our linear regression example, each instance will be represented by a fixed-length vector. Since the raw data here consists of pixel images, we flatten each image, treating them as vectors of length 784. In later chapters, we will introduce convolutional neural networks, which exploit the spatial structure in a more satisfying way.
英語のヒント
これで、ソフトマックス回帰モデルを実装するのに必要なものがすべてそろった。線形回帰の例と同じく、各インスタンスは固定長のベクトルで表す。ここでの生のデータは画素の画像からなるので、各画像を平らにして、長さ784のベクトルとして扱う。後の章では、空間的な構造をもっと満足のいく形で活用する畳み込みニューラルネットワークを紹介する。
In softmax regression, the number of outputs from our network should be equal to the number of classes. Since our dataset has 10 classes, our network has an output dimension of 10. Consequently, our weights constitute a matrix plus a row vector for the biases. As with linear regression, we initialize the weights
W with Gaussian noise. The biases are initialized as zeros.英語のヒント
ソフトマックス回帰では、ネットワークの出力の数はクラスの数と等しくなければならない。このデータセットには10のクラスがあるので、ネットワークの出力の次元は10である。その結果、重みはの行列と、バイアスのためのの行ベクトルからなる。線形回帰と同じく、重み
Wはガウスノイズで初期化する。バイアスは0で初期化する。class SoftmaxRegressionScratch(d2l.Classifier):
def __init__(self, num_inputs, num_outputs, lr, sigma=0.01):
super().__init__()
self.save_hyperparameters()
self.W = torch.normal(0, sigma, size=(num_inputs, num_outputs),
requires_grad=True)
self.b = torch.zeros(num_outputs, requires_grad=True)
def parameters(self):
return [self.W, self.b]The code below defines how the network maps each input to an output. Note that we flatten each pixel image in the batch into a vector using
reshape before passing the data through our model.英語のヒント
下のコードは、ネットワークが各入力を出力へどう写すかを定義する。データをモデルに通す前に、
reshapeを使って、バッチ内の画素の各画像をベクトルに平らにしていることに注意してほしい。@d2l.add_to_class(SoftmaxRegressionScratch)
def forward(self, X):
X = X.reshape((-1, self.W.shape[0]))
return softmax(torch.matmul(X, self.W) + self.b)