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

Early stopping for customized training loop #2645

Open
wangjiawen2013 opened this issue Dec 28, 2024 · 1 comment
Open

Early stopping for customized training loop #2645

wangjiawen2013 opened this issue Dec 28, 2024 · 1 comment

Comments

@wangjiawen2013
Copy link
Contributor

wangjiawen2013 commented Dec 28, 2024

Hi,
This is a snippet from burn's guide example, which we can set early_stopping:

    // Model
    let learner = LearnerBuilder::new(artifact_dir)
        .metric_train_numeric(AccuracyMetric::new())
        .metric_valid_numeric(AccuracyMetric::new())
        .with_file_checkpointer(CompactRecorder::new())
        .early_stopping(MetricEarlyStoppingStrategy::new::<LossMetric<B>>(
            Aggregate::Mean,
            Direction::Lowest,
            Split::Valid,
            StoppingCondition::NoImprovementSince { n_epochs: 1 },
        ))
        .devices(vec![device.clone()])
        .num_epochs(config.num_epochs)
        .summary()
        .build(
            config.model.init::<B>(&device),
            config.optimizer.init(),
            config.learning_rate);

But for customized training loop, such as in https://github.com/tracel-ai/burn/blob/main/examples/custom-training-loop/src/lib.rs, how to set early_stopping?

@laggui
Copy link
Member

laggui commented Jan 2, 2025

The LearnerBuilder takes an EarlyStoppingStrategy. This is implemented for the MetricEarlyStoppingStrategy.

Then, the Learner simply checks at the end of every epoch if the training should stop based on the strategy.

So if you want to use the same strategy, it should be fairly simple to create the early stopping instance and then check at the end of every epoch in your training loop to break from the loop.

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

No branches or pull requests

2 participants