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

Example of K-fold Cross Validation with Fabric #16909

Merged
merged 18 commits into from
Mar 7, 2023
Merged

Example of K-fold Cross Validation with Fabric #16909

merged 18 commits into from
Mar 7, 2023

Conversation

shenoynikhil
Copy link
Contributor

@shenoynikhil shenoynikhil commented Feb 28, 2023

What does this PR do?

Resolves #16902

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

@github-actions github-actions bot added the fabric lightning.fabric.Fabric label Feb 28, 2023
@shenoynikhil shenoynikhil changed the title Added readme and empty python file Example of K-fold Cross Validation with Lightning Fabric Feb 28, 2023
@shenoynikhil shenoynikhil changed the title Example of K-fold Cross Validation with Lightning Fabric Example of K-fold Cross Validation with Fabric Feb 28, 2023
@shenoynikhil shenoynikhil marked this pull request as ready for review February 28, 2023 20:27
@peterchristofferholm
Copy link

Instead of having the k-folds in outer loop, it would be better to loop over the folds in each epoch. Otherwise you will not be able to things like early stopping or pruning doing hyperparameter sweeps.

for epoch in range(max_epochs):
    losses = []
    for k in range(k_folds):
        yhat = model[k](data[k])
        losses[k] = ...
    epoch_loss = mean(losses)

@awaelchli awaelchli self-assigned this Mar 1, 2023
@shenoynikhil
Copy link
Contributor Author

Instead of having the k-folds in outer loop, it would be better to loop over the folds in each epoch. Otherwise you will not be able to things like early stopping or pruning doing hyperparameter sweeps.

That makes sense. I'm looking into it.

@shenoynikhil
Copy link
Contributor Author

@peterchristofferholm I made the changes accordingly. I believe I saw K-Fold implemented in both ways, one where the k-fold loop is the outermost one and one where the epoch loop is. Not sure if there is one correct one, because eventually, you would want to aggregate validation score across the models.

@awaelchli Can you maybe also review how Fabric has been used?

@justusschock
Copy link
Member

Instead of having the k-folds in outer loop, it would be better to loop over the folds in each epoch. Otherwise you will not be able to things like early stopping or pruning doing hyperparameter sweeps.

The downside of this is, that with the current implementation you have K-times your model in GPU-memory (which often fits only a single model), or you have to do manual device management

@shenoynikhil
Copy link
Contributor Author

@awaelchli @justusschock @peterchristofferholm I guess I have both the implementations in the git history. Let me know which one is preferred, and I'll keep that.

Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

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

LGTM, minor comments.
Thanks for jumping on this so quickly. Impressive!

examples/fabric/kfold_cv/README.md Outdated Show resolved Hide resolved
examples/fabric/kfold_cv/README.md Outdated Show resolved Hide resolved
examples/fabric/kfold_cv/README.md Outdated Show resolved Hide resolved
@awaelchli awaelchli added this to the 2.0 milestone Mar 5, 2023
@Borda Borda merged commit 6bb3e99 into Lightning-AI:master Mar 7, 2023
@mergify mergify bot added the ready PRs ready to be merged label Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
example fabric lightning.fabric.Fabric ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

K-fold cross validation example in Fabric
6 participants