データ前処理

Kerasで学ぶ言語モデリングのためのRecurrent Neural Networks (RNNs)

David Cecchini

Data Scientist

テキスト分類

テキスト分類の用途:

  • ニュースの自動分類
  • 企業向けドキュメント分類
  • カスタマーサポートのキュー分割
  • そのほか多数
Kerasで学ぶ言語モデリングのためのRecurrent Neural Networks (RNNs)

二値分類からの変更点

二値から多クラスへの変更点:

  • 出力変数 y の形状
  • 出力層のユニット数
  • 出力層の活性化関数
  • 損失関数
Kerasで学ぶ言語モデリングのためのRecurrent Neural Networks (RNNs)

二値分類からの変更点

出力変数 y の形状:

  • クラスのワンホットエンコード
# Example: num_classes = 3
y[0] = [0, 1, 0]
y.shape = (N, num_classes)

出力層のユニット数:

# Output layer
model.add(Dense(num_classes))
Kerasで学ぶ言語モデリングのためのRecurrent Neural Networks (RNNs)

二値分類からの変更点

一次元の数と空間内の数の違い。ワンホットエンコードの適用例を示す

Kerasで学ぶ言語モデリングのためのRecurrent Neural Networks (RNNs)

二値分類からの変更点

出力層の活性化関数:

  • softmax は各クラスの確率を出力
# Output layer
model.add(Dense(num_classes, activation="softmax"))

損失関数:

  • バイナリではなく categorical cross-entropy を使用
# Compile the model
model.compile(loss='categorical_crossentropy')
Kerasで学ぶ言語モデリングのためのRecurrent Neural Networks (RNNs)

Keras 用のテキストカテゴリ準備

y = ["sports", "economy", "data_science", "sports", "finance"]
# Transform to pandas series object
y_series = pd.Series(y, dtype="category")

# Print the category codes print(y_series.cat.codes)
0    3
1    1
2    0
3    3
4    2
Kerasで学ぶ言語モデリングのためのRecurrent Neural Networks (RNNs)

y の前処理

from tensorflow.keras.utils import to_categorical

y = np.array([0, 1, 2]) # Change to categorical y_prep = to_categorical(y) print(y_prep)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
Kerasで学ぶ言語モデリングのためのRecurrent Neural Networks (RNNs)

練習しましょう!

Kerasで学ぶ言語モデリングのためのRecurrent Neural Networks (RNNs)

Preparing Video For Download...