Kerasで学ぶ言語モデリングのためのRecurrent Neural Networks (RNNs)
David Cecchini
Data Scientist
テキスト分類の用途:
二値から多クラスへの変更点:
y の形状出力変数 y の形状:
# Example: num_classes = 3
y[0] = [0, 1, 0]
y.shape = (N, num_classes)
出力層のユニット数:
# Output layer
model.add(Dense(num_classes))

出力層の活性化関数:
softmax は各クラスの確率を出力# Output layer
model.add(Dense(num_classes, activation="softmax"))
損失関数:
# Compile the model
model.compile(loss='categorical_crossentropy')
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
from tensorflow.keras.utils import to_categoricaly = 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)