プリンキピア

第4章分類のための線形ニューラルネットワーク

4.2.2. Reading a Minibatch4.2.2 ミニバッチを読む

To make our life easier when reading from the training and test sets, we use the built-in data iterator rather than creating one from scratch. Recall that at each iteration, a data iterator reads a minibatch of data with size batch_size. We also randomly shuffle the examples for the training data iterator.
英語のヒント
訓練集合とテスト集合から読み出すときに楽ができるよう、データのイテレータをゼロから作るのではなく、組み込みのものを使う。データのイテレータは、反復のたびに大きさbatch_sizeのデータのミニバッチを読み出すことを思い出してほしい。また、訓練用のデータのイテレータでは、事例を無作為にシャッフルする。
@d2l.add_to_class(FashionMNIST)  #@save
def get_dataloader(self, train):
    data = self.train if train else self.val
    return torch.utils.data.DataLoader(data, self.batch_size, shuffle=train,
                                       num_workers=self.num_workers)
To see how this works, let’s load a minibatch of images by invoking the train_dataloader method. It contains 64 images.
英語のヒント
これがどう動くかを見るため、train_dataloaderメソッドを呼び出して画像のミニバッチを読み込もう。これには64枚の画像が含まれる。
X, y = next(iter(data.train_dataloader()))
print(X.shape, X.dtype, y.shape, y.dtype)
torch.Size([64, 1, 32, 32]) torch.float32 torch.Size([64]) torch.int64
Let’s look at the time it takes to read the images. Even though it is a built-in loader, it is not blazingly fast. Nonetheless, this is sufficient since processing images with a deep network takes quite a bit longer. Hence it is good enough that training a network will not be I/O constrained.
英語のヒント
画像を読むのにかかる時間を見てみよう。組み込みのローダーではあるが、猛烈に速いわけではない。それでも、深いネットワークで画像を処理するほうがかなり長くかかるので、これで十分である。したがって、ネットワークの訓練がI/Oに制約されない程度には十分に速い。
tic = time.time()
for X, y in data.train_dataloader():
    continue
f'{time.time() - tic:.2f} sec'
'4.69 sec'