-
Notifications
You must be signed in to change notification settings - Fork 81
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
Add xCEBRA implementation (AISTATS 2025) #225
Open
gonlairo
wants to merge
20
commits into
AdaptiveMotorControlLab:main
Choose a base branch
from
gonlairo:aistats2025
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
20 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 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 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 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 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 |
---|---|---|
|
@@ -35,3 +35,83 @@ | |
- 'tests/**/*.py' | ||
- 'docs/**/*.py' | ||
- 'conda/**/*.yml' | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think best just in the related files, not here? |
||
- header: | | ||
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. | ||
|
||
include: | ||
- 'cebra/attribution/jacobian.py' | ||
|
||
|
||
- header: | | ||
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 | ||
|
||
This file contains the PyTorch implementation of Jacobian regularization described in [1]. | ||
Judy Hoffman, Daniel A. Roberts, and Sho Yaida, | ||
"Robust Learning with Jacobian Regularization," 2019. | ||
[arxiv:1908.02729](https://arxiv.org/abs/1908.02729) | ||
|
||
Adapted from https://github.com/facebookresearch/jacobian_regularizer/blob/main/jacobian/jacobian.py | ||
licensed under the following MIT License: | ||
|
||
MIT License | ||
|
||
Copyright (c) Facebook, Inc. and its affiliates. | ||
|
||
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. | ||
|
||
include: | ||
- 'cebra/models/jacobian_regularizer.py' |
This file contains 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,27 @@ | ||
# | ||
# 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. | ||
# | ||
import cebra.registry | ||
|
||
cebra.registry.add_helper_functions(__name__) | ||
|
||
from cebra.attribution.attribution_models import * | ||
from cebra.attribution.jacobian_attribution import * |
This file contains 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.
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.
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.
pragmatic workaround until i moved the demo files. will remove once fixed