パート2: to_categorical() 関数を探る
パート1では、one-hot ベクトルの計算で num_classes 引数を使わない compute_onehot_length() 関数を実装しました。
num_classes 引数は、to_categorical() 関数が生成する one-hot エンコード済みベクトルの長さを制御します。語彙が異なる2つのコーパス(テキストの集合)を扱う場合、num_classes を指定しないままにすると、one-hot ベクトルの長さが異なることがあります。
この演習では、compute_onehot_length() 関数と word2index 辞書が用意されています。
この演習はコースの一部です
Kerasで学ぶMachine Translation
演習の手順
words_1に対してcompute_onehot_length()を呼び出します。words_2に対してcompute_onehot_length()を呼び出します。words_1とwords_2で得られた one-hot ベクトルの長さを出力します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
words_1 = ["I", "like", "cats", "We", "like", "dogs", "He", "hates", "rabbits"]
# Call compute_onehot_length on words_1
length_1 = ____(____, ____)
words_2 = ["I", "like", "cats", "We", "like", "dogs", "We", "like", "cats"]
# Call compute_onehot_length on words_2
length_2 = ____(____, ____)
# Print length_1 and length_2
print("length_1 =>", ____, " and length_2 => ", ____)