Plots the learning rate range test. Args: skip_start: number of batches to trim from the start. skip_end: number of batches to trim from the start. log_lr: True to plot the learning rate in a logarithmic scale; otherwise, plotted in a line
(
self,
skip_start: int = 0,
skip_end: int = 0,
log_lr: bool = True,
ax: Any | None = None,
steepest_lr: bool = True,
)
| 485 | return None, None |
| 486 | |
| 487 | def plot( |
| 488 | self, |
| 489 | skip_start: int = 0, |
| 490 | skip_end: int = 0, |
| 491 | log_lr: bool = True, |
| 492 | ax: Any | None = None, |
| 493 | steepest_lr: bool = True, |
| 494 | ) -> Any | None: |
| 495 | """Plots the learning rate range test. |
| 496 | |
| 497 | Args: |
| 498 | skip_start: number of batches to trim from the start. |
| 499 | skip_end: number of batches to trim from the start. |
| 500 | log_lr: True to plot the learning rate in a logarithmic |
| 501 | scale; otherwise, plotted in a linear scale. |
| 502 | ax: the plot is created in the specified matplotlib axes object and the |
| 503 | figure is not be shown. If `None`, then the figure and axes object are |
| 504 | created in this method and the figure is shown. |
| 505 | steepest_lr: plot the learning rate which had the steepest gradient. |
| 506 | |
| 507 | Returns: |
| 508 | The `matplotlib.axes.Axes` object that contains the plot. Returns `None` if |
| 509 | `matplotlib` is not installed. |
| 510 | """ |
| 511 | if not has_matplotlib: |
| 512 | warnings.warn("Matplotlib is missing, can't plot result") |
| 513 | return None |
| 514 | |
| 515 | lrs, losses = self.get_lrs_and_losses(skip_start, skip_end) |
| 516 | |
| 517 | # Create the figure and axes object if axes was not already given |
| 518 | fig = None |
| 519 | if ax is None: |
| 520 | fig, ax = plt.subplots() |
| 521 | |
| 522 | # Plot loss as a function of the learning rate |
| 523 | ax.plot(lrs, losses) |
| 524 | |
| 525 | # Plot the LR with steepest gradient |
| 526 | if steepest_lr: |
| 527 | lr_at_steepest_grad, loss_at_steepest_grad = self.get_steepest_gradient(skip_start, skip_end) |
| 528 | if lr_at_steepest_grad is not None and loss_at_steepest_grad is not None: |
| 529 | ax.scatter( |
| 530 | lr_at_steepest_grad, |
| 531 | loss_at_steepest_grad, |
| 532 | s=75, |
| 533 | marker="o", |
| 534 | color="red", |
| 535 | zorder=3, |
| 536 | label="steepest gradient", |
| 537 | ) |
| 538 | ax.legend() |
| 539 | |
| 540 | if log_lr: |
| 541 | ax.set_xscale("log") |
| 542 | ax.set_xlabel("Learning rate") |
| 543 | ax.set_ylabel("Loss") |
| 544 |