cca_zoo.deep¶
Deep CCA variants. Requires pip install cca-zoo[deep].
Base class¶
BaseDeep ¶
Bases: LightningModule
Base class for deep multiview models, as Lightning modules.
Train with a :class:lightning.pytorch.Trainer. Calling the model, or
trainer.predict, returns each view's encoding; for canonical variates,
fit :class:~cca_zoo.linear.CCA or :class:~cca_zoo.linear.MCCA to the
training encodings. Subclasses implement :meth:loss.
The covariance-based losses are estimated from each process's batch, so under multi-GPU (DDP) training they see the per-GPU batch, not the global one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view, each mapping it to |
required |
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
Source code in cca_zoo/deep/_base.py
forward ¶
Encode each view; one tensor of shape (batch_size, n_components) per view.
loss ¶
The loss of a batch and its terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
A dictionary whose |
Source code in cca_zoo/deep/_base.py
predict_step ¶
configure_optimizers ¶
Data¶
MultiviewDataset ¶
Bases: Dataset[dict[str, Any]]
Dataset of in-memory views, yielding {"views": [...]} samples.
The deep models read batches as dictionaries with a "views" list, and
:class:DPCCA also a "partials" tensor; any dataset returning that
shape works.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
Arrays of shape (n_samples, n_features_i), one per view. |
required |
partials
|
ArrayLike | None
|
Conditioning variable for :class: |
None
|
Examples:
>>> import numpy as np
>>> from torch.utils.data import DataLoader
>>> from cca_zoo.deep import MultiviewDataset
>>> rng = np.random.default_rng(0)
>>> X1 = rng.standard_normal((100, 10)).astype("float32")
>>> X2 = rng.standard_normal((100, 8)).astype("float32")
>>> batch = next(iter(DataLoader(MultiviewDataset([X1, X2]), batch_size=32)))
>>> [v.shape for v in batch["views"]]
[torch.Size([32, 10]), torch.Size([32, 8])]
Source code in cca_zoo/deep/_data.py
Models¶
DCCA ¶
DCCA(
n_components: int,
encoders: list[Module],
learning_rate: float = 0.001,
reg_covar: float = 1e-06,
)
Bases: _ObjectiveModel
Deep CCA of two views: encoders trained to maximise canonical correlation.
Minimises, per mini-batch,
(:class:~cca_zoo.deep.objectives.CCALoss). As linear CCA generalises to
MCCA, GCCA and TCCA, it generalises to more views as :class:DMCCA,
:class:DGCCA and :class:DTCCA.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
reg_covar
|
float
|
Non-negative regularisation added to the diagonal of each
covariance, as scikit-learn's |
1e-06
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If there are not two encoders. |
References
Andrew, G., Arora, R., Bilmes, J., & Livescu, K. (2013). Deep canonical correlation analysis. ICML.
Examples:
>>> import lightning.pytorch as pl
>>> import numpy as np
>>> import torch
>>> import torch.nn as nn
>>> from torch.utils.data import DataLoader
>>> from cca_zoo.deep import DCCA, MultiviewDataset
>>> rng = np.random.default_rng(0)
>>> X1 = rng.standard_normal((64, 10)).astype("float32")
>>> X2 = rng.standard_normal((64, 8)).astype("float32")
>>> loader = DataLoader(MultiviewDataset([X1, X2]), batch_size=32)
>>> model = DCCA(n_components=2, encoders=[nn.Linear(10, 2), nn.Linear(8, 2)])
>>> trainer = pl.Trainer(max_epochs=2, logger=False, enable_progress_bar=False,
... enable_checkpointing=False, enable_model_summary=False)
>>> trainer.fit(model, loader)
>>> batches = trainer.predict(model, loader)
>>> Z1, Z2 = (torch.cat(z).numpy() for z in zip(*batches))
>>> from cca_zoo.linear import CCA
>>> U1, U2 = CCA(n_components=2).fit([Z1, Z2]).transform([Z1, Z2])
>>> U1.shape
(64, 2)
Source code in cca_zoo/deep/_dcca.py
DCCAEY ¶
Bases: BaseDeep
Deep CCA by minimising the Eckart-Young loss.
(:mod:cca_zoo._utils._ey), which is unconstrained and so suits
mini-batches. With independent representations the penalty is
\(\operatorname{tr}(V V_{\text{ind}})\), an unbiased estimate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
References
Chapman, J., Wells, L., & Lawry Aguila, A. (2024). Unconstrained Stochastic CCA: Unifying Multiview and Self-Supervised Learning. arXiv:2310.01012.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import DCCAEY
>>> model = DCCAEY(n_components=4, encoders=[nn.Linear(10, 4), nn.Linear(8, 4)])
Source code in cca_zoo/deep/_base.py
loss ¶
The EY loss of a batch and its terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_dcca_ey.py
DCCANOI ¶
DCCANOI(
n_components: int,
encoders: list[Module],
rho: float = 0.9,
learning_rate: float = 0.001,
reg_covar: float = 1e-06,
)
Bases: BaseDeep
Deep CCA by nonlinear orthogonal iterations.
Wang et al.'s Algorithm 2: regresses each view's encoding on the others' whitened, centred encodings, held fixed,
with sg a stop-gradient, and the running moments updated by each training batch as \(\Sigma_j \leftarrow \rho \Sigma_j + (1 - \rho)\, \widehat{\Sigma}_j\), and \(\mu_j\) likewise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
rho
|
float
|
Weight of the previous running moments in |
0.9
|
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
reg_covar
|
float
|
Non-negative regularisation added to the diagonal of each
covariance, as scikit-learn's |
1e-06
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
References
Wang, W., Arora, R., Livescu, K., & Srebro, N. (2015). Stochastic optimization for deep CCA via nonlinear orthogonal iterations. Allerton.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import DCCANOI
>>> encoders = [nn.Linear(10, 4), nn.Linear(8, 4)]
>>> model = DCCANOI(n_components=4, encoders=encoders)
Source code in cca_zoo/deep/_dcca_noi.py
loss ¶
The NOI loss of a batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_dcca_noi.py
DCCASDL ¶
DCCASDL(
n_components: int,
encoders: list[Module],
lam: float = 0.5,
learning_rate: float = 0.001,
)
Bases: BaseDeep
Deep CCA with a stochastic decorrelation loss.
Batch-normalised encodings are aligned by mean squared error, summed over pairs of views, and decorrelated within each view:
With two views this is the original loss, with one difference: Chang et
al. estimate the covariance by a running average across batches, while
here each batch's covariance is used, so no state is carried between
batches, as in :class:DPCCA. Use batches well above n_components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
lam
|
float
|
Weight of the decorrelation term. Default is 0.5. |
0.5
|
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
References
Chang, X., Xiang, T., & Hospedales, T. M. (2018). Scalable and effective deep CCA via soft decorrelation. CVPR.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import DCCASDL
>>> encoders = [nn.Linear(10, 4), nn.Linear(8, 4)]
>>> model = DCCASDL(n_components=4, encoders=encoders)
Source code in cca_zoo/deep/_dcca_sdl.py
forward ¶
loss ¶
The SDL loss of a batch and its terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_dcca_sdl.py
DCCAE ¶
DCCAE(
n_components: int,
encoders: list[Module],
decoders: list[Module],
lam: float = 0.5,
objective: Module | None = None,
learning_rate: float = 0.001,
reg_covar: float = 1e-06,
)
Bases: _ObjectiveModel
Deep CCA with per-view autoencoder reconstruction.
lam=0 is :class:DMCCA (:class:DCCA with two views) and lam=1
independent autoencoders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
decoders
|
list[Module]
|
One module per view mapping its encoding back. |
required |
lam
|
float
|
Reconstruction weight in |
0.5
|
objective
|
Module | None
|
Correlation loss; None uses |
None
|
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
reg_covar
|
float
|
Non-negative regularisation added to the diagonal of each
covariance, as scikit-learn's |
1e-06
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
References
Wang, W., Arora, R., Livescu, K., & Bilmes, J. (2015). On deep multi-view representation learning. ICML.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import DCCAE
>>> model = DCCAE(
... n_components=4,
... encoders=[nn.Linear(10, 4), nn.Linear(8, 4)],
... decoders=[nn.Linear(4, 10), nn.Linear(4, 8)],
... )
Source code in cca_zoo/deep/_dccae.py
loss ¶
The DCCAE loss of a batch and its terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_dccae.py
DVCCA ¶
Bases: BaseDeep
Deep variational CCA: a latent inferred from one view generates all of them.
With prior \(z \sim \mathcal{N}(0, I)\), decoders \(p(x_i \mid z)\) for every view and an encoder \(q(z \mid x_1) = \mathcal{N}(\mu, \sigma^2)\) of the first view, training minimises the negative ELBO
Unlike the other deep models there is one encoding, so trainer.predict
returns a single array, the posterior mean \(\mu\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoder
|
Module
|
Module mapping the first view to |
required |
decoders
|
list[Module]
|
One module per view mapping the latent back to that view. |
required |
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
References
Wang, W., Yan, X., Lee, H., & Livescu, K. (2016). Deep variational canonical correlation analysis. arXiv:1610.03454.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import DVCCA
>>> model = DVCCA(
... n_components=4,
... encoder=nn.Linear(10, 8),
... decoders=[nn.Linear(4, 10), nn.Linear(4, 6)],
... )
Source code in cca_zoo/deep/_dvcca.py
forward ¶
loss ¶
The negative ELBO of a batch and its terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_dvcca.py
predict_step ¶
DVCCAPrivate ¶
DVCCAPrivate(
n_components: int,
encoder: Module,
private_encoders: list[Module],
decoders: list[Module],
n_private: int,
learning_rate: float = 0.001,
)
Bases: DVCCA
DVCCA with a private latent variable per view as well as the shared one.
The shared \(z\) is inferred from the first view, \(q(z \mid x_1)\), and each view's private \(h_i\) from that view, \(q(h_i \mid x_i)\); view \(i\) is decoded from \([z, h_i]\). All latents have standard normal priors, and training minimises the negative ELBO
The private variables take up view-specific variation, leaving \(z\) the
shared part. trainer.predict returns the posterior mean of \(z\);
:meth:private_means gives each view's private posterior mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Dimension of the shared latent. |
required |
encoder
|
Module
|
Module mapping the first view to |
required |
private_encoders
|
list[Module]
|
One module per view mapping it to |
required |
decoders
|
list[Module]
|
One module per view mapping |
required |
n_private
|
int
|
Dimension of each private latent. |
required |
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
References
Wang, W., Yan, X., Lee, H., & Livescu, K. (2016). Deep variational canonical correlation analysis. arXiv:1610.03454.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import DVCCAPrivate
>>> model = DVCCAPrivate(
... n_components=4,
... encoder=nn.Linear(10, 8),
... private_encoders=[nn.Linear(10, 4), nn.Linear(6, 4)],
... decoders=[nn.Linear(6, 10), nn.Linear(6, 6)],
... n_private=2,
... )
Source code in cca_zoo/deep/_dvcca.py
private_means ¶
Each view's private posterior mean, shape (batch_size, n_private).
loss ¶
The negative ELBO of a batch and its terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_dvcca.py
DTCCA ¶
DTCCA(
n_components: int,
encoders: list[Module],
learning_rate: float = 0.001,
reg_covar: float = 1e-06,
)
Bases: _ObjectiveModel
Deep tensor CCA: maximise the cross-moment tensor of whitened encodings.
Minimises \(-\|M\|_F\) with
\(M = \frac{1}{n} \sum_s H_1[s] \otimes \cdots \otimes H_M[s]\) for
ridge-whitened encodings \(H_i\)
(:class:~cca_zoo.deep.objectives.TCCALoss).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
reg_covar
|
float
|
Non-negative regularisation added to the diagonal of each
covariance, as scikit-learn's |
1e-06
|
References
Wong, H. S., Wang, L., Chan, R., & Zeng, T. (2021). Deep tensor CCA for multi-view learning. IEEE Transactions on Big Data.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import DTCCA
>>> encoders = [nn.Linear(10, 4), nn.Linear(8, 4), nn.Linear(6, 4)]
>>> model = DTCCA(n_components=4, encoders=encoders)
Source code in cca_zoo/deep/_dtcca.py
DMCCA ¶
DMCCA(
n_components: int,
encoders: list[Module],
learning_rate: float = 0.001,
reg_covar: float = 1e-06,
)
Bases: _ObjectiveModel
Deep multiset CCA: the sum of pairwise deep CCA losses.
(:class:~cca_zoo.deep.objectives.MCCALoss), for any number of views.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
reg_covar
|
float
|
Non-negative regularisation added to the diagonal of each
covariance, as scikit-learn's |
1e-06
|
References
Somandepalli, K., Kumar, N., Travadi, R., & Narayanan, S. (2019). Multimodal representation learning using deep multiset canonical correlation analysis. arXiv:1904.01775.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import DMCCA
>>> encoders = [nn.Linear(10, 4), nn.Linear(8, 4), nn.Linear(6, 4)]
>>> model = DMCCA(n_components=4, encoders=encoders)
Source code in cca_zoo/deep/_dmcca.py
DGCCA ¶
DGCCA(
n_components: int,
encoders: list[Module],
learning_rate: float = 0.001,
reg_covar: float = 1e-06,
)
Bases: _ObjectiveModel
Deep generalized CCA: correlate every view with a shared target.
for ridge-whitened encodings \(H_i\)
(:class:~cca_zoo.deep.objectives.GCCALoss), for any number of views.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
reg_covar
|
float
|
Non-negative regularisation added to the diagonal of each
covariance, as scikit-learn's |
1e-06
|
References
Benton, A., Khayrallah, H., Gujral, B., Reisinger, D. A., Zhang, S., & Arora, R. (2019). Deep generalized canonical correlation analysis. RepL4NLP.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import DGCCA
>>> encoders = [nn.Linear(10, 4), nn.Linear(8, 4), nn.Linear(6, 4)]
>>> model = DGCCA(n_components=4, encoders=encoders)
Source code in cca_zoo/deep/_dgcca.py
DPCCA ¶
DPCCA(
n_components: int,
encoders: list[Module],
partial_encoder: Module | None = None,
learning_rate: float = 0.001,
reg_covar: float = 1e-06,
)
Bases: BaseDeep
Deep partial CCA: correlate the views after conditioning on a shared variable.
The Eckart-Young loss of :class:DCCAEY on the views' partial covariances
given a conditioning variable \(Z\) (batch["partials"]). For the
stacked encodings \(F\) of a batch,
with \(C_{|Z}\) the mean over views of the sum of each view's blocks of
\(\Sigma_{FF|Z}\), and \(V_{|Z}\) the mean of its diagonal blocks, as
:class:DCCAEY forms them from the covariances.
partial_encoder=None uses \(Z\) as given; a module encodes it, trained to
explain the encodings by least squares, so that partialling removes all
the encoded \(Z\) can explain. \(Z\) is needed only for training: prediction
encodes the views alone.
Rotman et al. train by nonlinear orthogonal iterations, regressing each
view onto the other's whitened partialled encoding, with running
estimates of the covariances across batches; the EY loss needs neither
whitening nor running estimates, and suits mini-batch training (Chapman et
al., 2024). Their encoded variant also trains the partial encoder on the
correlation loss, which rewards it for failing to explain the confound;
here it minimises the partialling residual instead. With two views the
model is theirs; more views are handled as in :class:DCCAEY.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
partial_encoder
|
Module | None
|
Module encoding the conditioning variable, or None to use it as given. Default is None. |
None
|
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
reg_covar
|
float
|
Non-negative regularisation added to the diagonal of each
covariance, as scikit-learn's |
1e-06
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If a training batch has no |
References
Rotman, G., Vulić, I., & Reichart, R. (2018). Bridging languages through images with deep partial canonical correlation analysis. ACL.
Chapman, J., Wells, L., & Lawry Aguila, A. (2024). Unconstrained Stochastic CCA: Unifying Multiview and Self-Supervised Learning. arXiv:2310.01012.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import DPCCA
>>> encoders = [nn.Linear(10, 4), nn.Linear(8, 4)]
>>> model = DPCCA(n_components=4, encoders=encoders)
Source code in cca_zoo/deep/_dpcca.py
loss ¶
The EY loss of the encodings' partial covariances and its terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
|
dict[str, Tensor]
|
partial encoder's loss, when there is one. |
Source code in cca_zoo/deep/_dpcca.py
SplitAE ¶
SplitAE(
n_components: int,
encoders: list[Module],
decoders: list[Module],
learning_rate: float = 0.001,
)
Bases: BaseDeep
Split autoencoder: every view reconstructed from all views' encodings.
A reconstruction baseline that does not maximise correlation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Dimension of each encoding. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
decoders
|
list[Module]
|
One module per view, taking |
required |
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
References
Wang, W., Arora, R., Livescu, K., & Bilmes, J. (2015). On deep multi-view representation learning. ICML.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import SplitAE
>>> model = SplitAE(
... n_components=4,
... encoders=[nn.Linear(10, 4), nn.Linear(8, 4)],
... decoders=[nn.Linear(8, 10), nn.Linear(8, 8)],
... )
Source code in cca_zoo/deep/_splitae.py
loss ¶
The reconstruction loss of a batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_splitae.py
BarlowTwins ¶
BarlowTwins(
n_components: int,
encoders: list[Module],
lam: float = 0.005,
learning_rate: float = 0.001,
)
Bases: BaseDeep
Barlow Twins: drive the cross-correlation between views to the identity.
for batch-normalised encodings \(Z_a\), summed over pairs of views; with two views this is the original loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
lam
|
float
|
Weight of the off-diagonal term. Default is 5e-3. |
0.005
|
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
References
Zbontar, J., Jing, L., Misra, I., LeCun, Y., & Deny, S. (2021). Barlow twins: Self-supervised learning via redundancy reduction. ICML.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import BarlowTwins
>>> encoders = [nn.Linear(10, 4), nn.Linear(8, 4)]
>>> model = BarlowTwins(n_components=4, encoders=encoders)
Source code in cca_zoo/deep/_barlowtwins.py
forward ¶
loss ¶
The Barlow Twins loss of a batch and its terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_barlowtwins.py
VICReg ¶
VICReg(
n_components: int,
encoders: list[Module],
sim_coeff: float = 25.0,
std_coeff: float = 25.0,
cov_coeff: float = 1.0,
learning_rate: float = 0.001,
)
Bases: BaseDeep
Variance-invariance-covariance regularisation.
with \(\gamma, \mu, \nu\) = sim_coeff, std_coeff, cov_coeff. The
invariance term is summed over pairs of views; with two views this is the
original loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
sim_coeff
|
float
|
Weight of the invariance term. Default is 25.0. |
25.0
|
std_coeff
|
float
|
Weight of the variance term. Default is 25.0. |
25.0
|
cov_coeff
|
float
|
Weight of the covariance term. Default is 1.0. |
1.0
|
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
References
Bardes, A., Ponce, J., & LeCun, Y. (2022). VICReg: Variance-Invariance-Covariance Regularization for Self-Supervised Learning. ICLR.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import VICReg
>>> model = VICReg(n_components=4, encoders=[nn.Linear(10, 4), nn.Linear(8, 4)])
Source code in cca_zoo/deep/_vicreg.py
loss ¶
The VICReg loss of a batch and its terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_vicreg.py
LeJEPA ¶
LeJEPA(
n_components: int,
encoders: list[Module],
lam: float = 0.05,
n_slices: int = 256,
n_points: int = 17,
t_max: float = 5.0,
learning_rate: float = 0.001,
unbiased: bool = False,
)
Bases: BaseDeep
LeJEPA: every view predicts the views' centre, and SIGReg prevents collapse.
where SIGReg is the Epps-Pulley test statistic of \(z_k\)'s projections on
n_slices random unit directions against a standard normal, averaged
over directions, which are resampled at every training step. Isotropic
Gaussian embeddings, which SIGReg favours, cannot collapse, so no
stop-gradient, predictor or teacher network is needed. Every view is a
global view, so the centre is the mean over all views.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view; two or more. |
required |
lam
|
float
|
Weight \(\lambda \in [0, 1]\) of SIGReg against the predictive term. Default is 0.05. |
0.05
|
n_slices
|
int
|
Random directions per step. Default is 256. |
256
|
n_points
|
int
|
Quadrature points of the Epps-Pulley integral. Default is 17. |
17
|
t_max
|
float
|
Half-width of the integration interval. Default is 5.0. |
5.0
|
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
unbiased
|
bool
|
Whether SIGReg uses the unbiased estimate of the squared
characteristic-function error (see :func: |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
References
Balestriero, R., & LeCun, Y. (2025). LeJEPA: Provable and Scalable Self-Supervised Learning Without the Heuristics. arXiv:2511.08544.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import LeJEPA
>>> model = LeJEPA(n_components=4, encoders=[nn.Linear(10, 4), nn.Linear(8, 4)])
Source code in cca_zoo/deep/_lejepa.py
loss ¶
The LeJEPA loss of a batch and its terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_lejepa.py
NRDCCA ¶
NRDCCA(
n_components: int,
encoders: list[Module],
lam: float = 1.0,
learning_rate: float = 0.001,
reg_covar: float = 1e-06,
)
Bases: BaseDeep
Deep CCA with noise regularisation, against model collapse.
Linear CCA's correlation of a view with independent noise does not change under an invertible linear map, \(\operatorname{Corr}(X_k, A_k) = \operatorname{Corr}(W_k X_k, W_k A_k)\); an overfitted network's does. Each batch draws Gaussian noise \(A_k\) of each view's shape, and the loss
holds the encoders to that property. \(\mathcal{L}_{DCCA}\) is
:class:~cca_zoo.deep.objectives.MCCALoss, :class:DCCA's loss summed
over pairs of views, and \(\operatorname{Corr}\) is the mean canonical
correlation, which compares a view's \(d_k\) canonical correlations with its
encoding's. With lam=0 this is :class:DMCCA.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. |
required |
encoders
|
list[Module]
|
One module per view. |
required |
lam
|
float
|
Weight of the noise regularisation. Default is 1.0. |
1.0
|
learning_rate
|
float
|
Adam learning rate. Default is 1e-3. |
0.001
|
reg_covar
|
float
|
Non-negative regularisation added to the diagonal of each
covariance, as scikit-learn's |
1e-06
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
References
He, J., Du, J., Xu, S., & Ma, W. (2024). Preventing model collapse in deep canonical correlation analysis by noise regularization. NeurIPS.
Examples:
>>> import torch.nn as nn
>>> from cca_zoo.deep import NRDCCA
>>> model = NRDCCA(n_components=4, encoders=[nn.Linear(10, 4), nn.Linear(8, 4)])
Source code in cca_zoo/deep/_nrdcca.py
loss ¶
The DCCA loss and the noise regularisation of a batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_nrdcca.py
Objectives¶
objectives ¶
Differentiable CCA losses for the deep models.
CCALoss ¶
Bases: Module
Two-view deep CCA loss (Andrew et al., 2013).
minus the sum of squared canonical correlations of the batch, with ridge-regularised within-view covariances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reg_covar
|
float
|
Non-negative regularisation added to the diagonal of each
covariance, as scikit-learn's |
1e-06
|
References
Andrew, G., Arora, R., Bilmes, J., & Livescu, K. (2013). Deep canonical correlation analysis. ICML.
Examples:
>>> import torch
>>> from cca_zoo.deep.objectives import CCALoss
>>> loss = CCALoss(reg_covar=1e-4)([torch.randn(32, 4), torch.randn(32, 4)])
Source code in cca_zoo/deep/objectives.py
forward ¶
The loss of two views.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
One tensor of shape (batch_size, n_components) per view. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The loss. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If there are not exactly two views. |
Source code in cca_zoo/deep/objectives.py
MCCALoss ¶
Bases: Module
Sum of :class:CCALoss over every pair of views.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reg_covar
|
float
|
Non-negative regularisation added to the diagonal of each
covariance, as scikit-learn's |
1e-06
|
References
Kettenring, J. R. (1971). Canonical analysis of several sets of variables. Biometrika, 58(3), 433-451.
Examples:
>>> import torch
>>> from cca_zoo.deep.objectives import MCCALoss
>>> loss = MCCALoss(reg_covar=1e-4)([torch.randn(32, 4) for _ in range(3)])
Source code in cca_zoo/deep/objectives.py
forward ¶
The summed pairwise loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
One tensor of shape (batch_size, n_components) per view. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The loss. |
Source code in cca_zoo/deep/objectives.py
GCCALoss ¶
Bases: Module
Generalized CCA loss: minus the top eigenvalues of the summed projections.
for ridge-whitened, centred encodings \(H_i\), so that \(P_i\) projects onto view \(i\)'s encodings. The loss lies in \([-kM, 0]\) for \(M\) views at any batch size, and is \(-kM\) when every view's encodings agree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reg_covar
|
float
|
Non-negative regularisation added to the diagonal of each
covariance, as scikit-learn's |
1e-06
|
References
Benton, A., Khayrallah, H., Gujral, B., Reisinger, D. A., Zhang, S., & Arora, R. (2019). Deep generalized canonical correlation analysis. RepL4NLP.
Examples:
>>> import torch
>>> from cca_zoo.deep.objectives import GCCALoss
>>> loss = GCCALoss(reg_covar=1e-4)([torch.randn(32, 4) for _ in range(3)])
Source code in cca_zoo/deep/objectives.py
forward ¶
The generalized CCA loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
One tensor of shape (batch_size, n_components) per view. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The loss. |
Source code in cca_zoo/deep/objectives.py
TCCALoss ¶
Bases: Module
Tensor CCA loss: minus the norm of the whitened cross-moment tensor.
for ridge-whitened, centred encodings \(H_i\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reg_covar
|
float
|
Non-negative regularisation added to the diagonal of each
covariance, as scikit-learn's |
1e-06
|
References
Luo, Y., Tao, D., Ramamohanarao, K., Xu, C., & Wen, Y. (2015). Tensor canonical correlation analysis for multi-view dimension reduction. IEEE Transactions on Knowledge and Data Engineering, 27(11), 3111-3124.
Examples:
>>> import torch
>>> from cca_zoo.deep.objectives import TCCALoss
>>> loss = TCCALoss(reg_covar=1e-4)([torch.randn(32, 4) for _ in range(3)])
Source code in cca_zoo/deep/objectives.py
forward ¶
The tensor CCA loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
One tensor of shape (batch_size, n_components) per view. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The loss. |