เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การ 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
แก้ไขและรันโค้ด