-
Notifications
You must be signed in to change notification settings - Fork 3
Adding capability for plotting loss curves #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
namrathaurs
wants to merge
3
commits into
deepskies:main
Choose a base branch
from
namrathaurs:i138/loss-curves-plot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import matplotlib.pyplot as plt | ||
| from typing import Dict, List | ||
|
|
||
| from deepdiagnostics.plots.plot import Display | ||
|
|
||
| class LossPlot(Display): | ||
| def __init__(self, model=None, data=None): | ||
| ... | ||
|
|
||
| def _data_setup(self): | ||
| raise NotImplementedError | ||
|
|
||
| def plot_name(self): | ||
| return "loss_curves.png" | ||
|
|
||
| def plot( | ||
| self, | ||
| training_history: Dict[str, List[float]], | ||
| epochs: int = None, | ||
| best_val_loss: float = None | ||
namrathaurs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ): | ||
| try: | ||
| training_loss_data = training_history["train_loss"] | ||
namrathaurs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| validation_loss_data = training_history["val_loss"] | ||
| except Exception as e: | ||
| raise KeyError(f"Key {e} not found in supplied training history") | ||
|
|
||
| if epochs is None: | ||
| if len(training_loss_data) != len(validation_loss_data): | ||
| raise ValueError("Inconsistent training history data supplied [length of train and validation losses not equal]") | ||
| epochs = len(training_loss_data) | ||
| print(f"Number of epochs determined: {epochs}") | ||
| else: | ||
| if epochs != len(training_loss_data) or epochs != len(validation_loss_data): | ||
| raise ValueError("Epochs supplied inconsistent with training history data [epochs and loss history not equal]") | ||
|
|
||
| if best_val_loss is None: | ||
| best_val_loss = min(validation_loss_data) | ||
| print(f"Best validation loss found: {best_val_loss}") | ||
|
|
||
| epochs_trained = [ x for x in range(1, epochs+1) ] | ||
| plt.plot(epochs_trained, training_loss_data, label='Training Loss') | ||
| plt.plot(epochs_trained, validation_loss_data, label='Validation Loss') | ||
| plt.axhline(y=best_val_loss, color='m', linestyle='--', label="Best Val. Loss") | ||
|
|
||
| plt.xlabel('Epochs') | ||
| plt.ylabel('Loss') | ||
| plt.title('Training and Validation Loss Over Epochs') | ||
|
|
||
| plt.legend() | ||
| plt.grid() | ||
| plt.show() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things:
|
||
|
|
||
| def __call__(self, **kwargs): | ||
| raise NotImplementedError("Plotting loss is not supported in pipeline mode") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.