第3章回帰のための線形ニューラルネットワーク
3.3.2. Reading the Dataset3.3.2 データセットの読み込み
Training machine learning models often requires multiple passes over a dataset, grabbing one minibatch of examples at a time. This data is then used to update the model. To illustrate how this works, we implement the
get_dataloader method, registering it in the SyntheticRegressionData class via add_to_class (introduced in Section 3.2.1). It takes a batch size, a matrix of features, and a vector of labels, and generates minibatches of size batch_size. As such, each minibatch consists of a tuple of features and labels. Note that we need to be mindful of whether we’re in training or validation mode: in the former, we will want to read the data in random order, whereas for the latter, being able to read data in a pre-defined order may be important for debugging purposes.英語のヒント
機械学習モデルの訓練では、データセットを何度もたどり、一度に事例のミニバッチを一つずつつかむ必要があることが多い。このデータはモデルの更新に使われる。この仕組みを示すために、
get_dataloaderメソッドを実装し、add_to_class(3.2.1節で紹介した)でSyntheticRegressionDataクラスに登録する。このメソッドは、バッチの大きさ、特徴量の行列、ラベルのベクトルを受け取り、大きさbatch_sizeのミニバッチを生成する。そのため、各ミニバッチは特徴量とラベルのタプルからなる。訓練モードか検証モードかに注意する必要があることに注意してほしい。前者ではデータを無作為な順序で読みたいが、後者では、デバッグのためにあらかじめ決めた順序でデータを読めることが大事かもしれない。@d2l.add_to_class(SyntheticRegressionData)
def get_dataloader(self, train):
if train:
indices = list(range(0, self.num_train))
# The examples are read in random order
random.shuffle(indices)
else:
indices = list(range(self.num_train, self.num_train+self.num_val))
for i in range(0, len(indices), self.batch_size):
batch_indices = torch.tensor(indices[i: i+self.batch_size])
yield self.X[batch_indices], self.y[batch_indices]To build some intuition, let’s inspect the first minibatch of data. Each minibatch of features provides us with both its size and the dimensionality of input features. Likewise, our minibatch of labels will have a matching shape given by
batch_size.英語のヒント
直観をつかむために、データの最初のミニバッチを調べてみよう。特徴量の各ミニバッチから、その大きさと入力の特徴量の次元数の両方がわかる。同様に、ラベルのミニバッチは、
batch_sizeで与えられる対応する形状をもつ。X, y = next(iter(data.train_dataloader()))
print('X shape:', X.shape, '\ny shape:', y.shape)X shape: torch.Size([32, 2])
y shape: torch.Size([32, 1])While seemingly innocuous, the invocation of
iter(data.train_dataloader()) illustrates the power of Python’s object-oriented design. Note that we added a method to the SyntheticRegressionData class after creating the data object. Nonetheless, the object benefits from the ex post facto addition of functionality to the class.英語のヒント
一見何でもないようだが、
iter(data.train_dataloader())の呼び出しは、Pythonのオブジェクト指向設計の力を示している。dataオブジェクトを作った後で、SyntheticRegressionDataクラスにメソッドを加えたことに注意してほしい。それでも、オブジェクトは、クラスへの機能の事後的な追加の恩恵を受けている。Throughout the iteration we obtain distinct minibatches until the entire dataset has been exhausted (try this). While the iteration implemented above is good for didactic purposes, it is inefficient in ways that might get us into trouble with real problems. For example, it requires that we load all the data in memory and that we perform lots of random memory access. The built-in iterators implemented in a deep learning framework are considerably more efficient and they can deal with sources such as data stored in files, data received via a stream, and data generated or processed on the fly. Next let’s try to implement the same method using built-in iterators.
英語のヒント
反復を通じて、データセット全体を使い切るまで、異なるミニバッチが得られる(試してみよう)。上で実装した反復は教育目的にはよいが、実際の問題では困ったことになりうる非効率さがある。たとえば、すべてのデータをメモリに読み込み、無作為なメモリアクセスを大量に行う必要がある。深層学習フレームワークに実装された組み込みのイテレータはかなり効率がよく、ファイルに保存されたデータ、ストリームで受け取るデータ、その場で生成・処理されるデータといった源を扱える。次に、組み込みのイテレータを使って同じメソッドを実装してみよう。