第16章自然言語処理:応用
16.1.2. Preprocessing the Dataset16.1.2 データセットの前処理
Treating each word as a token and filtering out words that appear less than 5 times, we create a vocabulary out of the training dataset.
英語のヒント
各単語をトークンとして扱い、5回未満しか現れない単語を取り除いて、訓練データセットから語彙を作る。
train_tokens = d2l.tokenize(train_data[0], token='word')
vocab = d2l.Vocab(train_tokens, min_freq=5, reserved_tokens=['<pad>'])After tokenization, let’s plot the histogram of review lengths in tokens.
英語のヒント
トークン化の後、トークン数で測ったレビューの長さのヒストグラムを描こう。
d2l.set_figsize()
d2l.plt.xlabel('# tokens per review')
d2l.plt.ylabel('count')
d2l.plt.hist([len(line) for line in train_tokens], bins=range(0, 1000, 50));As we expected, the reviews have varying lengths. To process a minibatch of such reviews at each time, we set the length of each review to 500 with truncation and padding, which is similar to the preprocessing step for the machine translation dataset in Section 10.5.
英語のヒント
予想どおり、レビューの長さはまちまちである。そうしたレビューのミニバッチを一度に処理するため、切り詰めとパディングによって各レビューの長さを500にそろえる。これは、10.5節の機械翻訳のデータセットの前処理の手順と似ている。
num_steps = 500 # sequence length
train_features = torch.tensor([d2l.truncate_pad(
vocab[line], num_steps, vocab['<pad>']) for line in train_tokens])
print(train_features.shape)torch.Size([25000, 500])