ปรับปรุงการเทรนโมเดลด้วย Lightning
การนำเทคนิคอัตโนมัติอย่าง ModelCheckpoint และ EarlyStopping มาใช้ จะช่วยให้โมเดลเลือกพารามิเตอร์ที่ให้ผลลัพธ์ดีที่สุด พร้อมหลีกเลี่ยงการคำนวณที่ไม่จำเป็น
ชุดข้อมูลที่ใช้เป็นส่วนหนึ่งของชุดข้อมูล Osmanya MNIST ซึ่งเป็นกรณีการใช้งานจริงที่เทคนิคการเทรน AI แบบ scalable สามารถเพิ่มประสิทธิภาพและความแม่นยำได้อย่างเห็นได้ชัด
OsmanyaDataModule และ ImageClassifier ได้ถูกกำหนดไว้ให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
AI Models แบบ Scalable ด้วย PyTorch Lightning
คำแนะนำการฝึกหัด
- Import callback ที่จะใช้สำหรับการทำ model checkpointing และ early stopping
- เทรนโมเดลโดยใช้ callback
ModelCheckpointและEarlyStopping
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Import relevant checkpoints
from lightning.pytorch.callbacks import ____, ____
class EvaluatedImageClassifier(ImageClassifier):
def validation_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
acc = (y_hat.argmax(dim=1) == y).float().mean()
self.log("val_acc", acc)
model = EvaluatedImageClassifier()
data_module = OsmanyaDataModule()
# Train the model with ModelCheckpoint and EarlyStopping checkpoints
trainer = Trainer(____=[____(monitor="val_acc", save_top_k=1), ____(monitor="val_acc", patience=3)])
trainer.fit(model, datamodule=data_module)