File size: 3,300 Bytes
fc7d689 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | import matplotlib.pyplot as plt
import numpy as np
def moving_average(data, window_size):
"""
Calculate the moving average of a data series.
Parameters
----------
data: `numpy.ndarray`
The data series to average.
window_size: `int`
The size of the window to average over.
Returns
-------
moving_average: `numpy.ndarray`
The moving average of the data series.
"""
weights = np.ones(window_size) / window_size
return np.convolve(data, weights, mode='valid')
def plot_losses(loss_history, labels):
"""
Plot the convergence curve of a list of loss terms.
Parameters
----------
loss_history: `list` of `dict` of `float`
The loss histories during training.
The keys are the plot labels.
The values are the loss values.
labels: `list` of `str`
The labels of the losses to plot.
"""
# Plotting
plt.figure(figsize=(10, 6))
for label in labels:
loss_values = [float(vals[label]) for vals in loss_history]
plt.plot(loss_values, label=label)
plt.title('Loss')
plt.xlabel('Step')
plt.ylabel('Loss')
plt.yscale('log')
plt.grid()
plt.legend()
plt.show()
def plot_smoothed_losses(loss_history, window_size, labels):
"""
Plot the convergence curve of a list of loss terms with a moving average.
Parameters
----------
loss_history: `list` of `dict` of `float`
The loss histories during training.
The keys are the plot labels.
The values are the loss values.
window_size: `int`
The size of the window to average over.
labels: `list` of `str`
The labels of the losses to plot.
"""
# Plotting
plt.figure(figsize=(10, 6))
for label in labels:
# Loss values
loss_values = [float(vals[label]) for vals in loss_history]
# Calculate the moving average
smooth_loss = moving_average(loss_values, window_size)
# Adjust the length of original loss values to match the smoothed array
adjusted_loss_values = loss_values[:len(smooth_loss)]
# Plot
lines = plt.plot(adjusted_loss_values, alpha=0.5, label=label)
color = lines[-1].get_color()
plt.plot(smooth_loss, color=color)
plt.title('Loss')
plt.xlabel('Step')
plt.ylabel('Loss')
plt.yscale('log')
plt.grid()
plt.legend()
plt.show()
def plot_smoothed_loss(loss_history, window_size):
"""
Plot the convergence curve of a loss term with a moving average.
Parameters
----------
loss_history: `list` of `float`
The loss values during training.
window_size: `int`
The size of the window to average over.
"""
# Plotting
plt.figure(figsize=(10, 6))
# Calculate the moving average
smooth_loss = moving_average(loss_history, window_size)
# Adjust the length of original loss values to match the smoothed array
adjusted_loss_values = loss_history[:len(smooth_loss)]
# Plot
color = "tab:blue"
plt.plot(adjusted_loss_values, alpha=0.5, color=color)
plt.plot(smooth_loss, color=color)
plt.title('Loss')
plt.xlabel('Step')
plt.ylabel('Loss')
plt.yscale('log')
plt.grid()
plt.show()
|