-
Notifications
You must be signed in to change notification settings - Fork 92
Decouple batch size and number of negatives #263
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
base: main
Are you sure you want to change the base?
Conversation
|
@cla-bot check |
|
Thanks for tagging me. I looked for a signed form under your signature again, and updated the status on this PR. If the check was successful, no further action is needed. If the check was unsuccessful, please see the instructions in my first comment. |
There was a problem hiding this comment.
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 the ability to decouple batch size from the number of negative samples in contrastive learning by adding a new num_negatives parameter to both the Loader and CEBRA classes. This allows for more stable training behavior by providing additional negative examples to the InfoNCE loss independent of the batch size.
- Adds
num_negativesparameter toLoaderbase class andCEBRAclass APIs - Updates all loader implementations to use
num_negativesinstead of duplicatingbatch_size - Modifies goodness of fit computation to use
num_negativesinstead ofbatch_size
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
cebra/data/base.py |
Adds num_negatives field to base Loader class with validation and deprecates num_samples parameter |
cebra/data/single_session.py |
Updates single session loaders to use num_negatives and removes num_samples parameter |
cebra/data/multi_session.py |
Updates multi-session loaders to use num_negatives and adds validation for unified loader |
cebra/data/multiobjective.py |
Updates multiobjective loaders to use num_negatives instead of batch_size |
cebra/integrations/sklearn/cebra.py |
Adds num_negatives parameter to CEBRA class and passes it to loaders |
cebra/integrations/sklearn/metrics.py |
Updates goodness of fit computation to use num_negatives_ instead of batch_size |
tests/test_loader.py |
Comprehensive test updates to validate num_negatives functionality across all loader types |
tests/test_sklearn.py |
Adds basic test for num_negatives parameter in CEBRA class |
tests/test_sklearn_metrics.py |
Updates goodness of fit tests to validate num_negatives behavior |
|
hey @stes is any of the copilot reviews useful? If not, we can merge, thanks for this!! |
1412484 to
0f7ac51
Compare
|
@MMathisLab fine to merge from my end. Went through the copilot comments and made some further edits. |
This PR adds a new argument,
num_negatives, to both theLoaderandCEBRAclasses (torch and sklearn API). This allows to stabilize training behavior by providing additional negative examples to the InfoNCE loss independent of the batch size. We leveraged this logic for the models trained in DCL.Behavior
If the
num_negatives = None(the default), the previous behavior is obtained for backwards compatibility andLoader.batch_sizenegative examples are drawn. If a different value is set, then the number of negative examples in all loaders will be set tonum_negativesinstead ofLoader.batch_size.The goodness of fit computation was also adapted to use the
num_negatives.API modification
While implementing this functionality, I noticed an inconsistency between
single_sessionandmulti_sessionsolvers. In the single session, we passedself.batch_sizethrough theget_indicesfunction, while inmulti_sessionwe useself.batch_sizedirectly. The 2nd behavior makes more sense in the context of the general class design. I deprecated passing thenum_samplesparameter and adapted the samplers accordingly.