การฝึกโมเดลด้วย Keras

TensorFlow เบื้องต้นใน Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

ภาพรวมของการฝึกและการประเมิน

  1. โหลดและทำความสะอาดข้อมูล
  2. กำหนดโมเดล
  3. ฝึกและตรวจสอบโมเดล
  4. ประเมินโมเดล
TensorFlow เบื้องต้นใน Python

วิธีฝึกโมเดล

# Import tensorflow
import tensorflow as tf

# Define a sequential model
model = tf.keras.Sequential()
# Define the hidden layer
model.add(tf.keras.layers.Dense(16, activation='relu', input_shape=(784,)))
# Define the output layer
model.add(tf.keras.layers.Dense(4, activation='softmax'))
TensorFlow เบื้องต้นใน Python

วิธีฝึกโมเดล

# Compile model
model.compile('adam', loss='categorical_crossentropy')
# Train model
model.fit(image_features, image_labels)
TensorFlow เบื้องต้นใน Python

การทำงานของ fit()

  • อาร์กิวเมนต์ที่จำเป็น
    • features
    • labels
  • อาร์กิวเมนต์เพิ่มเติม
    • batch_size
    • epochs
    • validation_split
TensorFlow เบื้องต้นใน Python

Batch size และ epoch

ภาพแสดงการแบ่งชุดข้อมูลออกเป็น batch และการรวม batch ทั้งหมดเข้าด้วยกันเป็นหนึ่ง epoch

TensorFlow เบื้องต้นใน Python

การตรวจสอบความถูกต้อง

ภาพแสดงการแบ่งชุดข้อมูลออกเป็นชุดฝึกและชุดตรวจสอบ

TensorFlow เบื้องต้นใน Python

การตรวจสอบความถูกต้อง

# Train model with validation split
model.fit(features, labels, epochs=10, validation_split=0.20)
TensorFlow เบื้องต้นใน Python

การตรวจสอบความถูกต้อง

ภาพแสดงผลการฝึกและการตรวจสอบ 10 epoch

TensorFlow เบื้องต้นใน Python

การเปลี่ยน metric

# Recomile the model with the accuracy metric
model.compile('adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train model with validation split
model.fit(features, labels, epochs=10, validation_split=0.20)
TensorFlow เบื้องต้นใน Python

การเปลี่ยน metric

ภาพแสดงผลการฝึกและการตรวจสอบ 10 epoch

TensorFlow เบื้องต้นใน Python

การทำงานของ evaluate()

ภาพแสดงการแบ่งชุดข้อมูลออกเป็นชุดฝึก ชุดตรวจสอบ และชุดทดสอบ

# Evaluate the test set
model.evaluate(test)
TensorFlow เบื้องต้นใน Python

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

TensorFlow เบื้องต้นใน Python

Preparing Video For Download...