プリンキピア

第9章リカレントニューラルネットワーク

9.2.4. Putting It All Together9.2.4 すべてをまとめる

Using the above classes and methods, we package everything into the following build method of the TimeMachine class, which returns corpus, a list of token indices, and vocab, the vocabulary of The Time Machine corpus. The modifications we did here are: (i) we tokenize text into characters, not words, to simplify the training in later sections; (ii) corpus is a single list, not a list of token lists, since each text line in The Time Machine dataset is not necessarily a sentence or paragraph.
英語のヒント
上のクラスとメソッドを使って、すべてをTimeMachineクラスの次のbuildメソッドにまとめる。これは、トークンのインデックスのリストであるcorpusと、タイム・マシンのコーパスの語彙であるvocabを返す。ここで行った変更は次のとおりである。(i)後の節での訓練を単純にするため、テキストを単語ではなく文字にトークン化する。(ii)タイム・マシンのデータセットのテキストの各行は必ずしも文や段落ではないので、corpusはトークンのリストのリストではなく、一つのリストである。
@d2l.add_to_class(TimeMachine)  #@save
def build(self, raw_text, vocab=None):
    tokens = self._tokenize(self._preprocess(raw_text))
    if vocab is None: vocab = Vocab(tokens)
    corpus = [vocab[token] for token in tokens]
    return corpus, vocab

corpus, vocab = data.build(raw_text)
len(corpus), len(vocab)
(173428, 28)