モデル入力のためのテキスト前処理
これまでに、単語からインデックス、インデックスから単語への辞書を作成する方法を学びました。この演習では、テキストを文字単位に分割し、教師あり学習用にデータの準備を進めます。
テキストを文字に分割するのは不思議に思えるかもしれませんが、テキスト生成ではよく行われます。データ準備の手順自体は同じで、変わるのはテキストの分割方法だけです。
固定長のテキストと、そのラベルである直後の文字の対応からなる学習用データを作成します。
Sheldon(The Big Bang Theory)の引用が入ったデータセットを、変数 sheldon_quotes として引き続き使用します。
print_examples() 関数は変換後のペアを表示します。詳細は help() を参照してください。
この演習はコースの一部です
Kerasで学ぶ言語モデリングのためのRecurrent Neural Networks (RNNs)
演習の手順
stepを2、chars_windowを10に設定します。- 次の文を変数
sentencesに追加します。 - テキスト
sheldonの正しい位置を変数next_charsに追加します。 print_examples()関数を使って、10個の文と次の文字を表示します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Create lists to keep the sentences and the next character
sentences = [] # ~ Training data
next_chars = [] # ~ Training labels
# Define hyperparameters
step = ____ # ~ Step to take when reading the texts in characters
chars_window = ____ # ~ Number of characters to use to predict the next one
# Loop over the text: length `chars_window` per time with step equal to `step`
for i in range(0, len(sheldon_quotes) - chars_window, step):
sentences.____(sheldon_quotes[i:i + chars_window])
next_chars.append(sheldon_quotes[____])
# Print 10 pairs
print_examples(____, ____, 10)