การ implement training step
ในแบบฝึกหัดนี้ คุณจะ implement เมธอด training_step() ใน PyTorch Lightning module ที่ออกแบบมาสำหรับงานจำแนกประเภทภาพ
การ implement ของคุณควรแยกข้อมูลจาก batch ที่มีทั้งรูปภาพและ label, คำนวณค่าพยากรณ์ของโมเดลผ่าน forward pass, คำนวณ cross entropy loss และบันทึก training loss
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
AI Models แบบ Scalable ด้วย PyTorch Lightning
คำแนะนำการฝึกหัด
- คำนวณค่าพยากรณ์โดยใช้ forward pass
- คำนวณ cross entropy loss
- บันทึก training loss
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
from torch.nn.functional import cross_entropy
def training_step(self, batch, batch_idx):
x, y = batch
# Ensure that you compute predictions using the forward pass
y_hat = ____
# Calculate the cross entropy loss
loss = ____
# Log the loss
self.____("train_loss", loss)
return loss