Skip to content

Trainer v2#4

Merged
Arvindhbabu merged 4 commits into
mainfrom
trainer-v2
Jul 20, 2026
Merged

Trainer v2#4
Arvindhbabu merged 4 commits into
mainfrom
trainer-v2

Conversation

@Arvindhbabu

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings July 20, 2026 05:46
@Arvindhbabu
Arvindhbabu merged commit 324785f into main Jul 20, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 BaseTrainer abstraction with shared trainer plumbing (state/history/metrics/callbacks, AMP scaffolding).
  • Add new training utilities: TrainingState, Metrics, History, and callback system.
  • Refactor Trainer to use BaseTrainer, accumulate metrics via Metrics, and persist history via History.

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 thread src/training/metrics.py
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 thread src/training/history.py
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 thread src/training/history.py
Comment on lines +72 to +74
path = Path(path)

with open(path, "w", encoding="utf-8") as f:
Comment thread src/training/history.py
Comment on lines +90 to +94
if len(self.records) == 0:
return

with open(
path,
Comment thread src/training/trainer.py
best_loss = float("inf")

history = []
self.state.best_val_loss = float("inf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants