Skip to content
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

[Feature] Add additional callbacks #633

Merged
merged 7 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions docs/tutorials/qml/ml_tools/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,85 @@ config = TrainConfig(
)
```

### 1.9. `LRSchedulerStepDecay`

Reduces the learning rate by a factor at regular intervals.

```python exec="on" source="material-block" html="1"
from qadence.ml_tools import TrainConfig
from qadence.ml_tools.callbacks import LRSchedulerStepDecay

lr_step_decay = LRSchedulerStepDecay(on="train_epoch_end", called_every=100, gamma=0.5)

config = TrainConfig(
max_iter=10000,
callbacks=[lr_step_decay]
)
```

### 1.10. `LRSchedulerCyclic`

Applies a cyclic learning rate schedule during training.

```python exec="on" source="material-block" html="1"
from qadence.ml_tools import TrainConfig
from qadence.ml_tools.callbacks import LRSchedulerCyclic

lr_cyclic = LRSchedulerCyclic(on="train_batch_end", called_every=1, base_lr=0.001, max_lr=0.01, step_size=2000)

config = TrainConfig(
max_iter=10000,
callbacks=[lr_cyclic]
)
```

### 1.11. `LRSchedulerCosineAnnealing`

Applies cosine annealing to the learning rate during training.

```python exec="on" source="material-block" html="1"
from qadence.ml_tools import TrainConfig
from qadence.ml_tools.callbacks import LRSchedulerCosineAnnealing

lr_cosine = LRSchedulerCosineAnnealing(on="train_batch_end", called_every=1, t_max=5000, min_lr=1e-6)

config = TrainConfig(
max_iter=10000,
callbacks=[lr_cosine]
)
```

### 1.12. `EarlyStopping`

Stops training when a monitored metric has not improved for a specified number of epochs.

```python exec="on" source="material-block" html="1"
from qadence.ml_tools import TrainConfig
from qadence.ml_tools.callbacks import EarlyStopping

early_stopping = EarlyStopping(on="val_epoch_end", called_every=1, monitor="val_loss", patience=5, mode="min")

config = TrainConfig(
max_iter=10000,
callbacks=[early_stopping]
)
```

### 1.13. `GradientMonitoring`

Logs gradient statistics (e.g., mean, standard deviation, max) during training.

```python exec="on" source="material-block" html="1"
from qadence.ml_tools import TrainConfig
from qadence.ml_tools.callbacks import GradientMonitoring

gradient_monitoring = GradientMonitoring(on="train_batch_end", called_every=10)

config = TrainConfig(
max_iter=10000,
callbacks=[gradient_monitoring]
)
```

## 2. Custom Callbacks

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/qml/ml_tools/trainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ def train(
writer.print_metrics(OptimizeResult(iteration, model, optimizer, loss, metrics))

if iteration % config.write_every == 0:
writer.write(OptimizeResult(iteration, model, optimizer, loss, metrics))
writer.write(iteration, metrics)

if config.log_folder:
if iteration % config.checkpoint_every == 0:
Expand All @@ -540,7 +540,7 @@ def train(
# Final writing and checkpointing
if config.log_folder:
write_checkpoint(config.log_folder, model, optimizer, iteration)
writer.write(OptimizeResult(iteration, model, optimizer, loss, metrics))
writer.write(iteration,metrics)
writer.close()

return model, optimizer
Expand Down
10 changes: 10 additions & 0 deletions qadence/ml_tools/callbacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

from .callback import (
Callback,
EarlyStopping,
GradientMonitoring,
LoadCheckpoint,
LogHyperparameters,
LogModelTracker,
LRSchedulerCosineAnnealing,
LRSchedulerCyclic,
LRSchedulerStepDecay,
PlotMetrics,
PrintMetrics,
SaveBestCheckpoint,
Expand All @@ -26,5 +31,10 @@
"SaveBestCheckpoint",
"SaveCheckpoint",
"WriteMetrics",
"GradientMonitoring",
"LRSchedulerStepDecay",
"LRSchedulerCyclic",
"LRSchedulerCosineAnnealing",
"EarlyStopping",
"get_writer",
]
Loading
Loading