第10章現代のリカレントニューラルネットワーク
10.5.4. Reading the Dataset10.5.4 データセットを読み込む
Finally, we define the
get_dataloader method to return the data iterator.英語のヒント
最後に、データイテレータを返す
get_dataloaderメソッドを定義する。@d2l.add_to_class(MTFraEng) #@save
def get_dataloader(self, train):
idx = slice(0, self.num_train) if train else slice(self.num_train, None)
return self.get_tensorloader(self.arrays, train, idx)Let’s read the first minibatch from the English–French dataset.
英語のヒント
英語・フランス語のデータセットから最初のミニバッチを読み込もう。
data = MTFraEng(batch_size=3)
src, tgt, src_valid_len, label = next(iter(data.train_dataloader()))
print('source:', src.type(torch.int32))
print('decoder input:', tgt.type(torch.int32))
print('source len excluding pad:', src_valid_len.type(torch.int32))
print('label:', label.type(torch.int32))source: tensor([[117, 182, 0, 3, 4, 4, 4, 4, 4],
[ 62, 72, 2, 3, 4, 4, 4, 4, 4],
[ 57, 124, 0, 3, 4, 4, 4, 4, 4]], dtype=torch.int32)
decoder input: tensor([[ 3, 37, 100, 58, 160, 0, 4, 5, 5],
[ 3, 6, 2, 4, 5, 5, 5, 5, 5],
[ 3, 180, 0, 4, 5, 5, 5, 5, 5]], dtype=torch.int32)
source len excluding pad: tensor([4, 4, 4], dtype=torch.int32)
label: tensor([[ 37, 100, 58, 160, 0, 4, 5, 5, 5],
[ 6, 2, 4, 5, 5, 5, 5, 5, 5],
[180, 0, 4, 5, 5, 5, 5, 5, 5]], dtype=torch.int32)We show a pair of source and target sequences processed by the above
_build_arrays method (in the string format).英語のヒント
上の
_build_arraysメソッドで処理した原文と目標の系列の組を(文字列の形式で)示す。@d2l.add_to_class(MTFraEng) #@save
def build(self, src_sentences, tgt_sentences):
raw_text = '\n'.join([src + '\t' + tgt for src, tgt in zip(
src_sentences, tgt_sentences)])
arrays, _, _ = self._build_arrays(
raw_text, self.src_vocab, self.tgt_vocab)
return arrays
src, tgt, _, _ = data.build(['hi .'], ['salut .'])
print('source:', data.src_vocab.to_tokens(src[0].type(torch.int32)))
print('target:', data.tgt_vocab.to_tokens(tgt[0].type(torch.int32)))source: ['hi', '.', '<eos>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>']
target: ['<bos>', 'salut', '.', '<eos>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>']