Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี Train โมเดลต่อเนื่องกับ Flower Dataset ด้วย TensorFlow

บทนำ

หลังสร้างและ compile โมเดลสำหรับ Flower classification (5 คลาส) ใช้ fit() เทรนต่อ พร้อมระบุ epochs และ validation data

โค้ดตัวอย่าง

print("The data is fit to the model")
model.fit(
    train_ds,
    validation_data=val_ds,
    epochs=3
)

ผลลัพธ์

The data is fit to the model
Epoch 1/3 92/92 [==============================] - 102s 1s/step - loss: 0.7615 - accuracy: 0.7146 - val_loss: 0.7673 - val_accuracy: 0.7180
Epoch 2/3 92/92 [==============================] - 95s 1s/step - loss: 0.5864 - accuracy: 0.7786 - val_loss: 0.6814 - val_accuracy: 0.7629
Epoch 3/3 92/92 [==============================] - 95s 1s/step - loss: 0.4180 - accuracy: 0.8478 - val_loss: 0.7040 - val_accuracy: 0.7575

(พร้อมรูปภาพตัวอย่าง predictions)

คำอธิบาย

  • train_ds, val_ds: tf.data.Dataset ที่เตรียมไว้ (shuffle, batch, prefetch)
  • Epochs=3: เทรน 3 รอบ (demo เร็ว) production ควรมากกว่า + callbacks
  • Accuracy เพิ่มขึ้น: 71% → 85% | Val accuracy: 72% → 76% (overfitting เริ่มขึ้น)
  • Dataset สร้างด้วย tf.keras.preprocessing.image_dataset_from_directory แล้ว customize ด้วย tf.data

ข้อสังเกต

Validation accuracy ต่ำกว่า training → overfitting ควรเพิ่ม regularization (dropout, data augmentation, weight decay) หรือลด complexity