-
Notifications
You must be signed in to change notification settings - Fork 91
Add xCEBRA implementation (AISTATS 2025) #225
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
Merged
MMathisLab
merged 71 commits into
AdaptiveMotorControlLab:main
from
gonlairo:aistats2025
Apr 23, 2025
Merged
Changes from all commits
Commits
Show all changes
71 commits
Select commit
Hold shift + click to select a range
9c49c57
Add multiobjective solver and regularized training (#783)
gonlairo cbcb229
Fix tests
stes 97ad03f
Apply fixes to pass ruff tests
stes 36282d0
Fix typos
stes 5fa5b42
Update license headers, fix additional ruff errors
stes 9ef548d
remove unused comment
stes a9e7027
Merge branch 'main' into aistats2025
MMathisLab a2f7117
rename regcl in codebase
stes 761f373
change regcl name in dockerfile
stes 2052ec9
Improve attribution module
stes 5e483d0
Fix imports name naming
stes 4254173
add basic integration test
stes 1d69957
temp disable of binary check
stes 1d10668
Add legacy multiobjective model for backward compat
stes 5e30829
add synth import back in
stes 458958f
Fix docstrings and type annot in cebra/models/jacobian_regularizer.py
stes d906ad5
add xcebra to tests
stes 6f91018
add missing cvxpy dep
stes df4f661
fix docstrings
stes d81b93d
more docstrings to fix attr error
stes e54c0d1
Merge branch 'main' into aistats2025
MMathisLab 1f57b30
Merge branch 'main' into aistats2025
MMathisLab ea7ae46
Merge branch 'main' into aistats2025
MMathisLab 6e6915a
Merge branch 'main' into aistats2025
stes 73238ba
Improve build setup for docs
stes 7fb7393
update pydata theme options
stes 34836ee
Add README for docs folder
stes cc5f3ef
Fix demo notebook build
stes f4a08b5
Finish build setup
stes 92c5e9e
update git workflow
stes dc82226
Merge remote-tracking branch 'origin/stes/upgrade-docs-rebased' into …
stes 6b6af82
Move demo notebooks to CEBRA-demos repo
stes cf5f5a2
revert unneeded changes in solver
stes ff12a3d
formatting in solver
stes 2db1d22
further minimize solver diff
stes 9d982be
Revert unneeded updates to the solver
stes e20fda1
fix citation
stes 61cb9b7
fix docs build, missing refs
stes 74988ac
remove file dependency from xcebra int test
stes 1a8fd96
remove unneeded change in registry
stes d382c4c
update gitignore
stes 446cc67
update docs
stes 9123923
exclude some assets
stes e4faad6
include binary file check again
stes 8e3c83e
add timeout to workflow
stes e794ee1
add timeout also to docs build
stes 8f236d1
switch build back to sphinx for gh actions
stes 24d6402
pin sphinx version in setup.cfg
stes 1f64a12
attempt workflow fix
stes 69e22d7
attempt to fix build workflow
stes 1b8e1d7
update to sphinx-build
stes 8f903c8
fix build workflow
stes 1f924b2
fix indent error
stes f4cd549
fix build system
stes f2dd965
revert demos to main
stes 6e7104a
Merge remote-tracking branch 'origin/stes/upgrade-docs-rebased' into …
stes 691bb12
adapt workflow for testing
stes 49c7b10
bump version to 0.6.0rc1
stes 9462caf
format imports
stes e4d717d
docs writing
stes a7c9562
enable build on dev branch
stes df6679d
fix some review comments
stes f5dc743
extend multiobjective docs
stes 7435d2f
Set version to alpha
stes ea37d02
make tempdir platform independent
stes 90e9bbf
Merge branch 'main' into aistats2025
stes cadd612
Remove ratinabox and ephysiopy as deps
stes 7f278b1
Apply review comments
stes 3978687
Merge branch 'main' into aistats2025
MMathisLab e311a14
Merge branch 'main' into aistats2025
MMathisLab ec95857
Update Makefile
MMathisLab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # | ||
| # CEBRA: Consistent EmBeddings of high-dimensional Recordings using Auxiliary variables | ||
| # © Mackenzie W. Mathis & Steffen Schneider (v0.4.0+) | ||
| # Source code: | ||
| # https://github.com/AdaptiveMotorControlLab/CEBRA | ||
| # | ||
| # Please see LICENSE.md for the full license document: | ||
| # https://github.com/AdaptiveMotorControlLab/CEBRA/blob/main/LICENSE.md | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| """Attribution methods for CEBRA. | ||
|
|
||
| This module was added in v0.6.0 and contains attribution methods described and benchmarked | ||
| in [Schneider2025]_. | ||
|
|
||
|
|
||
| .. [Schneider2025] Schneider, S., González Laiz, R., Filippova, A., Frey, M., & Mathis, M. W. (2025). | ||
| Time-series attribution maps with regularized contrastive learning. | ||
| The 28th International Conference on Artificial Intelligence and Statistics. | ||
| https://openreview.net/forum?id=aGrCXoTB4P | ||
| """ | ||
| import cebra.registry | ||
|
|
||
| cebra.registry.add_helper_functions(__name__) | ||
|
|
||
| from cebra.attribution.attribution_models import * | ||
| from cebra.attribution.jacobian_attribution import * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| # | ||
| # CEBRA: Consistent EmBeddings of high-dimensional Recordings using Auxiliary variables | ||
| # © Mackenzie W. Mathis & Steffen Schneider (v0.4.0+) | ||
| # Source code: | ||
| # https://github.com/AdaptiveMotorControlLab/CEBRA | ||
| # | ||
| # Please see LICENSE.md for the full license document: | ||
| # https://github.com/AdaptiveMotorControlLab/CEBRA/blob/main/LICENSE.md | ||
| # | ||
| # Adapted from https://github.com/rpatrik96/nl-causal-representations/blob/master/care_nl_ica/dep_mat.py, | ||
| # licensed under the following MIT License: | ||
| # | ||
| # MIT License | ||
| # | ||
| # Copyright (c) 2022 Patrik Reizinger | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the "Software"), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in all | ||
| # copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| # SOFTWARE. | ||
| # | ||
|
|
||
| from typing import Union | ||
|
|
||
| import numpy as np | ||
| import torch | ||
|
|
||
|
|
||
| def tensors_to_cpu_and_double(vars_: list[torch.Tensor]) -> list[torch.Tensor]: | ||
| """Convert a list of tensors to CPU and double precision. | ||
|
|
||
| Args: | ||
| vars_: List of PyTorch tensors to convert | ||
|
|
||
| Returns: | ||
| List of tensors converted to CPU and double precision | ||
| """ | ||
| cpu_vars = [] | ||
| for v in vars_: | ||
| if v.is_cuda: | ||
| v = v.to("cpu") | ||
| cpu_vars.append(v.double()) | ||
| return cpu_vars | ||
|
|
||
|
|
||
| def tensors_to_cuda(vars_: list[torch.Tensor], | ||
| cuda_device: str) -> list[torch.Tensor]: | ||
| """Convert a list of tensors to CUDA device. | ||
|
|
||
| Args: | ||
| vars_: List of PyTorch tensors to convert | ||
| cuda_device: CUDA device to move tensors to | ||
|
|
||
| Returns: | ||
| List of tensors moved to specified CUDA device | ||
| """ | ||
| cpu_vars = [] | ||
| for v in vars_: | ||
| if not v.is_cuda: | ||
| v = v.to(cuda_device) | ||
| cpu_vars.append(v) | ||
| return cpu_vars | ||
|
|
||
|
|
||
| def compute_jacobian( | ||
| model: torch.nn.Module, | ||
| input_vars: list[torch.Tensor], | ||
| mode: str = "autograd", | ||
| cuda_device: str = "cuda", | ||
| double_precision: bool = False, | ||
| convert_to_numpy: bool = True, | ||
| hybrid_solver: bool = False, | ||
| ) -> Union[torch.Tensor, np.ndarray]: | ||
| """Compute the Jacobian matrix for a given model and input. | ||
|
|
||
| This function computes the Jacobian matrix using PyTorch's autograd functionality. | ||
| It supports both CPU and CUDA computation, as well as single and double precision. | ||
|
|
||
| Args: | ||
| model: PyTorch model to compute Jacobian for | ||
| input_vars: List of input tensors | ||
| mode: Computation mode, currently only "autograd" is supported | ||
| cuda_device: Device to use for CUDA computation | ||
| double_precision: If True, use double precision | ||
| convert_to_numpy: If True, convert output to numpy array | ||
| hybrid_solver: If True, concatenate multiple outputs along dimension 1 | ||
|
|
||
| Returns: | ||
| Jacobian matrix as either PyTorch tensor or numpy array | ||
| """ | ||
| if double_precision: | ||
| model = model.to("cpu").double() | ||
| input_vars = tensors_to_cpu_and_double(input_vars) | ||
| if hybrid_solver: | ||
| output = model(*input_vars) | ||
| output_vars = torch.cat(output, dim=1).to("cpu").double() | ||
| else: | ||
| output_vars = model(*input_vars).to("cpu").double() | ||
| else: | ||
| model = model.to(cuda_device).float() | ||
| input_vars = tensors_to_cuda(input_vars, cuda_device=cuda_device) | ||
|
|
||
| if hybrid_solver: | ||
| output = model(*input_vars) | ||
| output_vars = torch.cat(output, dim=1) | ||
| else: | ||
| output_vars = model(*input_vars) | ||
|
|
||
| if mode == "autograd": | ||
| jacob = [] | ||
| for i in range(output_vars.shape[1]): | ||
| grads = torch.autograd.grad( | ||
| output_vars[:, i:i + 1], | ||
| input_vars, | ||
| retain_graph=True, | ||
| create_graph=False, | ||
| grad_outputs=torch.ones(output_vars[:, i:i + 1].shape).to( | ||
| output_vars.device), | ||
| ) | ||
| jacob.append(torch.cat(grads, dim=1)) | ||
|
|
||
| jacobian = torch.stack(jacob, dim=1) | ||
|
|
||
| jacobian = jacobian.detach().cpu() | ||
|
|
||
| if convert_to_numpy: | ||
| jacobian = jacobian.numpy() | ||
|
|
||
| return jacobian |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.