การประเมินประสิทธิภาพโมเดล

Deep Learning เบื้องต้นด้วย PyTorch

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

Training, validation และ testing

$$

  • โดยทั่วไปชุดข้อมูลจะถูกแบ่งออกเป็นสามส่วน:
สัดส่วนข้อมูล บทบาท
Training 80-90% ปรับพารามิเตอร์ของโมเดล
Validation 10-20% ปรับ hyperparameter
Test 5-10% ประเมินประสิทธิภาพโมเดลขั้นสุดท้าย

$$

  • ติดตาม loss และ accuracy ระหว่างการ training และ validation
Deep Learning เบื้องต้นด้วย PyTorch

การคำนวณ training loss

$$

สำหรับแต่ละ epoch:

  • รวม loss ของทุก batch ใน dataloader
  • คำนวณ mean training loss เมื่อสิ้นสุด epoch
training_loss = 0.0

for inputs, labels in trainloader: # Run the forward pass outputs = model(inputs) # Compute the loss loss = criterion(outputs, labels)
# Backpropagation loss.backward() # Compute gradients optimizer.step() # Update weights optimizer.zero_grad() # Reset gradients
# Calculate and sum the loss training_loss += loss.item()
epoch_loss = training_loss / len(trainloader)
Deep Learning เบื้องต้นด้วย PyTorch

การคำนวณ validation loss

validation_loss = 0.0
model.eval() # Put model in evaluation mode


with torch.no_grad(): # Disable gradients for efficiency
for inputs, labels in validationloader: # Run the forward pass outputs = model(inputs) # Calculate the loss loss = criterion(outputs, labels) validation_loss += loss.item() epoch_loss = validation_loss / len(validationloader) # Compute mean loss
model.train() # Switch back to training mode
Deep Learning เบื้องต้นด้วย PyTorch

Overfitting

ตัวอย่างของ overfitting

Deep Learning เบื้องต้นด้วย PyTorch

การคำนวณ accuracy ด้วย torchmetrics

import torchmetrics


# Create accuracy metric metric = torchmetrics.Accuracy(task="multiclass", num_classes=3)
for features, labels in dataloader: outputs = model(features) # Forward pass # Compute batch accuracy (keeping argmax for one-hot labels) metric.update(outputs, labels.argmax(dim=-1))
# Compute accuracy over the whole epoch accuracy = metric.compute()
# Reset metric for the next epoch metric.reset()
Deep Learning เบื้องต้นด้วย PyTorch

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

Deep Learning เบื้องต้นด้วย PyTorch

Preparing Video For Download...