Recurrent Neural Networks (RNNs) สำหรับ Language Modeling ด้วย Keras
David Cecchini
Data Scientist
การประยุกต์ใช้การจำแนกข้อความ:
สิ่งที่เปลี่ยนจาก binary เป็น multi-class:
yรูปร่างของตัวแปรเอาต์พุต y:
# Example: num_classes = 3
y[0] = [0, 1, 0]
y.shape = (N, num_classes)
จำนวนยูนิตบน output layer:
# Output layer
model.add(Dense(num_classes))

Activation function บน output layer:
softmax ให้ค่าความน่าจะเป็นของทุกคลาส# Output layer
model.add(Dense(num_classes, activation="softmax"))
Loss function:
# 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.]]
Recurrent Neural Networks (RNNs) สำหรับ Language Modeling ด้วย Keras