การเตรียมข้อมูล

Recurrent Neural Networks (RNNs) สำหรับ Language Modeling ด้วย Keras

David Cecchini

Data Scientist

การจำแนกข้อความ

การประยุกต์ใช้การจำแนกข้อความ:

  • การจำแนกข่าวอัตโนมัติ
  • การจำแนกเอกสารสำหรับธุรกิจ
  • การแบ่งคิวสำหรับฝ่ายสนับสนุนลูกค้า
  • และอีกมากมาย!
Recurrent Neural Networks (RNNs) สำหรับ Language Modeling ด้วย Keras

การเปลี่ยนแปลงจาก binary classification

สิ่งที่เปลี่ยนจาก binary เป็น multi-class:

  • รูปร่างของตัวแปรเอาต์พุต y
  • จำนวนยูนิตบน output layer
  • Activation function บน output layer
  • Loss function
Recurrent Neural Networks (RNNs) สำหรับ Language Modeling ด้วย Keras

การเปลี่ยนแปลงจาก binary classification

รูปร่างของตัวแปรเอาต์พุต y:

  • เข้ารหัสแบบ one-hot สำหรับแต่ละคลาส
# Example: num_classes = 3
y[0] = [0, 1, 0]
y.shape = (N, num_classes)

จำนวนยูนิตบน output layer:

# Output layer
model.add(Dense(num_classes))
Recurrent Neural Networks (RNNs) สำหรับ Language Modeling ด้วย Keras

การเปลี่ยนแปลงจาก binary classification

ความแตกต่างระหว่างตัวเลขในเส้นตรงและในปริภูมิ แสดงให้เห็นการใช้งาน one-hot encoding

Recurrent Neural Networks (RNNs) สำหรับ Language Modeling ด้วย Keras

การเปลี่ยนแปลงจาก binary classification

Activation function บน output layer:

  • softmax ให้ค่าความน่าจะเป็นของทุกคลาส
# Output layer
model.add(Dense(num_classes, activation="softmax"))

Loss function:

  • แทนที่จะใช้แบบ binary ให้ใช้ categorical cross-entropy
# Compile the model
model.compile(loss='categorical_crossentropy')
Recurrent Neural Networks (RNNs) สำหรับ Language Modeling ด้วย Keras

การเตรียมหมวดหมู่ข้อความสำหรับ 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
Recurrent Neural Networks (RNNs) สำหรับ Language Modeling ด้วย Keras

การเตรียมข้อมูล 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.]]
Recurrent Neural Networks (RNNs) สำหรับ Language Modeling ด้วย Keras

มาฝึกกันเถอะ!

Recurrent Neural Networks (RNNs) สำหรับ Language Modeling ด้วย Keras

Preparing Video For Download...