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

fix(train): in case of last batch <=2, move to validation if possible #3036

Merged
merged 21 commits into from
Nov 19, 2024

Conversation

ori-kron-wis
Copy link
Collaborator

@ori-kron-wis ori-kron-wis commented Oct 31, 2024

In case that train_size is None and the size of the last batch during training is <=2 , we adaptively move those samples from training to validation if possible. If train_size is set by user we do no fix this error and let the user change its train_size, selected indices or use drop last batch option.
close #3035

Copy link

codecov bot commented Oct 31, 2024

Codecov Report

Attention: Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.

Project coverage is 84.53%. Comparing base (54ba452) to head (19bb8bf).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/scvi/dataloaders/_data_splitting.py 94.44% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3036      +/-   ##
==========================================
- Coverage   84.96%   84.53%   -0.44%     
==========================================
  Files         178      178              
  Lines       15048    15061      +13     
==========================================
- Hits        12786    12732      -54     
- Misses       2262     2329      +67     
Files with missing lines Coverage Δ
src/scvi/external/cellassign/_model.py 92.30% <ø> (ø)
...ernal/contrastivevi/_contrastive_data_splitting.py 89.28% <ø> (ø)
src/scvi/external/contrastivevi/_model.py 79.50% <ø> (ø)
src/scvi/external/gimvi/_model.py 91.70% <ø> (ø)
src/scvi/external/mrvi/_model.py 7.85% <ø> (ø)
src/scvi/external/scbasset/_model.py 48.43% <ø> (ø)
src/scvi/external/solo/_model.py 94.80% <ø> (ø)
src/scvi/external/velovi/_model.py 75.47% <ø> (ø)
src/scvi/model/_multivi.py 72.26% <ø> (ø)
src/scvi/model/_peakvi.py 87.09% <ø> (ø)
... and 6 more

... and 1 file with indirect coverage changes

---- 🚨 Try these New Features:

@ori-kron-wis ori-kron-wis changed the title fix: in case of last batch ==1 during train, move one sample if possible fix(train): in case of last batch <=2, move to validation if possible Nov 5, 2024
if batch_size is not None:
num_of_cells = n_train % batch_size
if (num_of_cells < 3 and num_of_cells > 0) and not (
num_of_cells == 1 and drop_last is True
Copy link
Member

Choose a reason for hiding this comment

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

This one if confusing if drop_last it will drop the last batch no matter how many cells it contains.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True. this part is not about the drop_last logic.

this part of code is about when to show the warning. and a warning will be shown if we do have 1-2 cells in last batch but also when user didnt select drop_last with cell==1. whoever did it, doesnt need to see this warning, because it will not fail for him

only if also train_size_is_none there will be adaptive cell transferring to the validation set, if exists

Copy link
Member

Choose a reason for hiding this comment

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

Again the check doesn't need to be num_of_cells == 1 and drop_last is True. It should be: (num_of_cells < 3 and num_of_cells > 0) and drop_last is False

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok

if n_val > 0:
n_val += num_of_cells
warnings.warn(
f"{num_of_cells} cells moved from training set to validation set",
Copy link
Member

Choose a reason for hiding this comment

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

to avoid error during training. Set train_size to a fixed size to avoid this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it is 0.9 if given as None during init.
but as we discussed, we adapt the last batch only if it was in fact None, this is why we have the flag train_size_is_none

did you mean something else?

Copy link
Member

Choose a reason for hiding this comment

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

I think we want a message how to avound moving cells from training to validation. The user avoids it when stating train_size=0.9

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok

f"Last batch will have a small size of {num_of_cells} "
f"samples. Consider changing settings.batch_size or batch_size in model.train "
f"from currently {batch_size} to avoid errors during model training, "
f"or use drop_last parameter if there is 1 cell left",
Copy link
Member

Choose a reason for hiding this comment

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

this one is wrong

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

whats wrong?

f"samples. Consider changing settings.batch_size or batch_size in model.train "
f"from currently {settings.batch_size} to avoid errors during model training "
f"or change the given external indices accordingly or use drop_last parameter if "
f"there is 1 cell left",
Copy link
Member

Choose a reason for hiding this comment

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

this one to

Copy link
Member

Choose a reason for hiding this comment

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

This is confusing. See comment above - line 179 should be gone.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes

@@ -132,6 +164,20 @@ def validate_data_split_with_external_indexing(
n_train = len(external_indexing[0])
n_val = len(external_indexing[1])

if batch_size is not None:
num_of_cells = n_train % batch_size
if (num_of_cells < 3 and num_of_cells > 0) and not (
Copy link
Member

Choose a reason for hiding this comment

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

Update drop_last here.

@canergen canergen merged commit b08e5df into main Nov 19, 2024
12 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/scvi-tools that referenced this pull request Nov 19, 2024
canergen pushed a commit that referenced this pull request Nov 19, 2024
…<=2, move to validation if possible) (#3048)

Backport PR #3036: fix(train): in case of last batch <=2, move to
validation if possible

Co-authored-by: Ori Kronfeld <ori.kronfeld@weizmann.ac.il>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
on-merge: backport to 1.2.x on-merge: backport to 1.2.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Training fails with mini-batch size of one sample
2 participants