Trainer v2#4
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a “Trainer v2” refactor by extracting shared trainer functionality into a new BaseTrainer and adding supporting utilities (training state, metrics, history, callbacks). The existing Trainer is updated to inherit from BaseTrainer and to use the new history/metrics/state abstractions.
Changes:
- Add
BaseTrainerabstraction with shared trainer plumbing (state/history/metrics/callbacks, AMP scaffolding). - Add new training utilities:
TrainingState,Metrics,History, and callback system. - Refactor
Trainerto useBaseTrainer, accumulate metrics viaMetrics, and persist history viaHistory.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_state.py | Adds a smoke script demonstrating TrainingState update/serialization. |
| tests/test_metrics.py | Adds a smoke script demonstrating Metrics update/compute/print. |
| tests/test_history.py | Adds a smoke script demonstrating History add/best/save. |
| tests/test_callbacks.py | Adds a smoke script demonstrating callback manager invocation. |
| tests/test_base_trainer.py | Adds a smoke script verifying BaseTrainer import/subclassing. |
| src/training/trainer.py | Refactors Trainer to inherit from BaseTrainer and use History/Metrics/TrainingState. |
| src/training/state.py | Adds dataclass-based training state container with update/reset helpers. |
| src/training/metrics.py | Adds metrics accumulator powered by scikit-learn. |
| src/training/history.py | Adds training history storage and JSON/CSV export. |
| src/training/callbacks.py | Adds callback interface and callback manager. |
| src/training/base_trainer.py | Adds shared trainer base class with common fields/utilities and AMP scaffolding. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+45
to
+46
| self.device = device | ||
|
|
Comment on lines
+62
to
+65
| self.use_amp = ( | ||
| torch.cuda.is_available() | ||
| and device.type == "cuda" | ||
| ) |
Comment on lines
+93
to
+117
| results = { | ||
|
|
||
| "accuracy": accuracy_score( | ||
| y_true, | ||
| y_pred, | ||
| ), | ||
|
|
||
| "precision": precision_score( | ||
| y_true, | ||
| y_pred, | ||
| zero_division=0, | ||
| ), | ||
|
|
||
| "recall": recall_score( | ||
| y_true, | ||
| y_pred, | ||
| zero_division=0, | ||
| ), | ||
|
|
||
| "f1": f1_score( | ||
| y_true, | ||
| y_pred, | ||
| zero_division=0, | ||
| ), | ||
| } |
Comment on lines
+28
to
+35
| Example: | ||
| history.add( | ||
| epoch=1, | ||
| train_loss=0.4, | ||
| val_loss=0.3, | ||
| train_accuracy=0.95, | ||
| val_accuracy=0.94, | ||
| ) |
Comment on lines
+72
to
+74
| path = Path(path) | ||
|
|
||
| with open(path, "w", encoding="utf-8") as f: |
Comment on lines
+90
to
+94
| if len(self.records) == 0: | ||
| return | ||
|
|
||
| with open( | ||
| path, |
| best_loss = float("inf") | ||
|
|
||
| history = [] | ||
| self.state.best_val_loss = float("inf") |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.