第9章リカレントニューラルネットワーク
9.3.3. Partitioning Sequences9.3.3 系列を分割する
We will design language models using neural networks and use perplexity to evaluate how good the model is at predicting the next token given the current set of tokens in text sequences. Before introducing the model, let’s assume that it processes a minibatch of sequences with predefined length at a time. Now the question is how to read minibatches of input sequences and target sequences at random.
英語のヒント
ニューラルネットワークを使って言語モデルを設計し、パープレキシティを使って、テキスト系列の現在のトークンの集合が与えられたときに次のトークンを予測するのがモデルがどれくらいうまいかを評価する。モデルを紹介する前に、モデルがあらかじめ決めた長さの系列のミニバッチを一度に処理すると仮定しよう。ここでの問題は、入力系列と目標系列のミニバッチをどうやってランダムに読み込むかである。
Suppose that the dataset takes the form of a sequence of token indices in
corpus. We will partition it into subsequences, where each subsequence has tokens (time steps). To iterate over (almost) all the tokens of the entire dataset for each epoch and obtain all possible length- subsequences, we can introduce randomness. More concretely, at the beginning of each epoch, discard the first tokens, where is uniformly sampled at random. The rest of the sequence is then partitioned into subsequences. Denote by the length- subsequence starting from token at time step . The resulting partitioned subsequences are Each subsequence will be used as an input sequence into the language model.英語のヒント
データセットが、
corpusの中の個のトークンのインデックスの系列という形をしているとしよう。これを部分系列に分割し、各部分系列が個のトークン(時間ステップ)をもつようにする。各エポックでデータセット全体の(ほぼ)すべてのトークンをたどり、ありうるすべての長さの部分系列を得るために、ランダム性を導入できる。もっと具体的には、各エポックの初めに、最初の個のトークンを捨てる。ここでは一様にランダムにサンプリングされる。そして、系列の残りを個の部分系列に分割する。時間ステップのトークンから始まる長さの部分系列をと表す。各部分系列は言語モデルへの入力系列として使われる。分割して得られる個の部分系列は、次のとおりである:For language modeling, the goal is to predict the next token based on the tokens we have seen so far; hence the targets (labels) are the original sequence, shifted by one token. The target sequence for any input sequence is with length .
英語のヒント
言語のモデリングでは、これまでに見たトークンにもとづいて次のトークンを予測することが目標である。したがって、目標(ラベル)は元の系列を1トークンずらしたものである。どの入力系列についても、目標系列は長さのである。
Fig. 9.3.1図9.3.1
Obtaining five pairs of input sequences and target sequences from partitioned length-5 subsequences.
英語のヒント
分割した長さ5の部分系列から、入力系列と目標系列の五つの組を得る。
Fig. 9.3.1 shows an example of obtaining five pairs of input sequences and target sequences with and .
英語のヒント
図9.3.1は、とのときに、入力系列と目標系列の五つの組を得る例を示している。
@d2l.add_to_class(d2l.TimeMachine) #@save
def __init__(self, batch_size, num_steps, num_train=10000, num_val=5000):
super(d2l.TimeMachine, self).__init__()
self.save_hyperparameters()
corpus, self.vocab = self.build(self._download())
array = torch.tensor([corpus[i:i+num_steps+1]
for i in range(len(corpus)-num_steps)])
self.X, self.Y = array[:,:-1], array[:,1:]To train language models, we will randomly sample pairs of input sequences and target sequences in minibatches. The following data loader randomly generates a minibatch from the dataset each time. The argument
batch_size specifies the number of subsequence examples in each minibatch and num_steps is the subsequence length in tokens.英語のヒント
言語モデルを訓練するには、入力系列と目標系列の組をミニバッチでランダムにサンプリングする。次のデータローダーは、毎回データセットからランダムにミニバッチを生成する。引数
batch_sizeは各ミニバッチの部分系列の事例の数を指定し、num_stepsはトークン単位での部分系列の長さである。@d2l.add_to_class(d2l.TimeMachine) #@save
def get_dataloader(self, train):
idx = slice(0, self.num_train) if train else slice(
self.num_train, self.num_train + self.num_val)
return self.get_tensorloader([self.X, self.Y], train, idx)As we can see in the following, a minibatch of target sequences can be obtained by shifting the input sequences by one token.
英語のヒント
以下に見るように、目標系列のミニバッチは、入力系列を1トークンずらすことで得られる。
data = d2l.TimeMachine(batch_size=2, num_steps=10)
for X, Y in data.train_dataloader():
print('X:', X, '\nY:', Y)
breakDownloading ../data/timemachine.txt from http://d2l-data.s3-accelerate.amazonaws.com/timemachine.txt...
X: tensor([[10, 4, 2, 21, 10, 16, 15, 0, 20, 2],
[21, 9, 6, 19, 0, 24, 2, 26, 0, 16]])
Y: tensor([[ 4, 2, 21, 10, 16, 15, 0, 20, 2, 10],
[ 9, 6, 19, 0, 24, 2, 26, 0, 16, 9]])