mirror of
https://github.com/mrdbourke/pytorch-deep-learning.git
synced 2026-11-27 06:07:09 +00:00
add loss curve plotting function
This commit is contained in:
1 parent
e3553ad455
commit
e9d0d39b0f
1 file changed
+39
@@ -4,6 +4,7 @@ A series of helper functions used throughout the course.
|
||||
If a function gets defined once and could be used over and over, it'll go in here.
|
||||
"""
|
||||
import torch
|
||||
import matplotlib.pyplot as plt
|
||||
from torch import nn
|
||||
|
||||
# Calculate accuracy (a classification metric)
|
||||
@@ -36,3 +37,41 @@ def print_train_time(start, end, device=None):
|
||||
total_time = end - start
|
||||
print(f"\nTrain time on {device}: {total_time:.3f} seconds")
|
||||
return total_time
|
||||
|
||||
|
||||
# Plot loss curves of a model
|
||||
def plot_loss_curves(results):
|
||||
"""Plots training curves of a results dictionary.
|
||||
|
||||
Args:
|
||||
results (dict): dictionary containing list of values, e.g.
|
||||
{"train_loss": [...],
|
||||
"train_acc": [...],
|
||||
"test_loss": [...],
|
||||
"test_acc": [...]}
|
||||
"""
|
||||
loss = results["train_loss"]
|
||||
test_loss = results["test_loss"]
|
||||
|
||||
accuracy = results["train_acc"]
|
||||
test_accuracy = results["test_acc"]
|
||||
|
||||
epochs = range(len(results["train_loss"]))
|
||||
|
||||
plt.figure(figsize=(15, 7))
|
||||
|
||||
# Plot loss
|
||||
plt.subplot(1, 2, 1)
|
||||
plt.plot(epochs, loss, label="train_loss")
|
||||
plt.plot(epochs, test_loss, label="test_loss")
|
||||
plt.title("Loss")
|
||||
plt.xlabel("Epochs")
|
||||
plt.legend()
|
||||
|
||||
# Plot accuracy
|
||||
plt.subplot(1, 2, 2)
|
||||
plt.plot(epochs, accuracy, label="train_accuracy")
|
||||
plt.plot(epochs, test_accuracy, label="test_accuracy")
|
||||
plt.title("Accuracy")
|
||||
plt.xlabel("Epochs")
|
||||
plt.legend()
|
||||
Reference in new issue
Block a user