cca_zoo.deep¶
Deep CCA variants. Requires pip install cca-zoo[deep].
Base class¶
BaseDeep ¶
BaseDeep(
latent_dimensions: int,
encoders: list[Module],
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: LightningModule
Base class for deep multiview CCA models using PyTorch Lightning.
Subclasses override :meth:loss to implement the specific objective
function. Training is handled by a :class:lightning.Trainer.
The sklearn-compatible interface (fit, transform, score) is
provided for convenience, wrapping the Lightning training loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of the latent space. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
lr
|
float
|
Learning rate for the Adam optimiser. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Small constant for numerical stability. Default is 1e-6. |
1e-06
|
Source code in cca_zoo/deep/_base.py
forward ¶
Encode all views into latent representations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[Tensor]
|
List of tensors, each (batch_size, n_features_i). |
required |
Returns:
| Type | Description |
|---|---|
list[Tensor]
|
List of tensors, each (batch_size, latent_dimensions). |
Source code in cca_zoo/deep/_base.py
transform ¶
Project all samples in a DataLoader into the latent space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
DataLoader
|
DataLoader yielding batches with a |
required |
Returns:
| Type | Description |
|---|---|
list[ndarray]
|
List of numpy arrays, each (n_samples, latent_dimensions). |
Source code in cca_zoo/deep/_base.py
score ¶
Return average pairwise canonical correlations after linear CCA.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
DataLoader
|
DataLoader with a |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape |
Source code in cca_zoo/deep/_base.py
training_step ¶
Compute the training loss for one mini-batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
dict[str, Any]
|
Dictionary with key |
required |
batch_idx
|
int
|
Batch index (unused). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar loss tensor. |
Source code in cca_zoo/deep/_base.py
validation_step ¶
Compute the validation loss for one mini-batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
dict[str, Any]
|
Dictionary with |
required |
batch_idx
|
int
|
Batch index (unused). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar loss tensor. |
Source code in cca_zoo/deep/_base.py
configure_optimizers ¶
Create the Adam optimiser.
Returns:
| Type | Description |
|---|---|
Optimizer
|
Adam optimiser with the configured learning rate. |
Data¶
MultiviewDataset ¶
Bases: Dataset[dict[str, list[Tensor]]]
Wraps per-view arrays into the batch format :class:BaseDeep expects.
:meth:~cca_zoo.deep.BaseDeep.training_step and validation_step
read each batch as a dict with a "views" key holding a list of
per-view tensors — plain :class:torch.utils.data.TensorDataset
yields tuples instead, which does not match that contract. This class
is a small, optional convenience for the common case of already having
each view as a single in-memory array.
For anything more custom (lazy loading, augmentation, lists of file
paths, lists of paired NOI-style samples, ...), write your own
:class:~torch.utils.data.Dataset whose __getitem__ returns a dict
of that same shape — that is the actual contract, not this class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of array-likes, each of shape (n_samples, n_features_i). All must have the same number of samples. |
required |
Example
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") loader = DataLoader(MultiviewDataset([X1, X2]), batch_size=32) batch = next(iter(loader)) list(batch.keys()) ['views']
Source code in cca_zoo/deep/_data.py
__len__ ¶
__getitem__ ¶
Return the index-th sample as a {"views": [...]} dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Sample index. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[Tensor]]
|
Dictionary with key |
dict[str, list[Tensor]]
|
each of shape (n_features_i,). |
Source code in cca_zoo/deep/_data.py
Models¶
DCCA ¶
DCCA(
latent_dimensions: int,
encoders: list[Module],
objective: Module | None = None,
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: BaseDeep
Deep Canonical Correlation Analysis with a pluggable objective.
Trains two (or more) neural network encoders to maximise canonical correlation between their outputs, by minimising, per mini-batch:
where \(\Sigma_{11}, \Sigma_{22}\) are the (ridge-regularised)
within-view covariances of the two encoder outputs and
\(\Sigma_{12}\) their cross-covariance (see
:class:~cca_zoo.deep.objectives.CCALoss). The objective is pluggable
via the objective parameter, which defaults to the above.
The model is a :class:lightning.pytorch.LightningModule and is
trained via a :class:lightning.Trainer.
References
Andrew, G., et al. "Deep canonical correlation analysis." ICML 2013.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of the shared latent space. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
objective
|
Module | None
|
Differentiable loss module operating on a list of
latent tensors. If |
None
|
lr
|
float
|
Learning rate for the Adam optimiser. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Regularisation parameter passed to the default CCALoss when
|
1e-06
|
Example
import torch import torch.nn as nn enc1 = nn.Linear(10, 4) enc2 = nn.Linear(8, 4) model = DCCA(latent_dimensions=4, encoders=[enc1, enc2])
Source code in cca_zoo/deep/_dcca.py
loss ¶
loss(
representations: list[Tensor],
independent_representations: list[Tensor] | None = None,
) -> dict[str, torch.Tensor]
Compute the DCCA training objective.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
Encoded views from the current batch, each of shape (batch_size, latent_dimensions). |
required |
independent_representations
|
list[Tensor] | None
|
Optional second set of encodings (unused in the base DCCA formulation). |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with key |
dict[str, Tensor]
|
loss tensor to minimise. |
Source code in cca_zoo/deep/_dcca.py
DCCA_EY ¶
DCCA_EY(
latent_dimensions: int,
encoders: list[Module],
objective: Module | None = None,
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: DCCA
DCCA using the EigenGame / Eckart-Young (EY) objective.
For \(M\) views with embeddings \(Z_1, \dots, Z_M\), define the
mean pairwise cross-covariance \(C\) and mean auto-covariance
\(V\) (see :mod:cca_zoo._utils._ey for the full definitions).
The EY loss minimised is:
This is an unconstrained stand-in for CCA — unlike the exact
eigendecomposition solution, it requires no manifold projection and is
a stationary point exactly at the canonical directions, making it
suitable for mini-batch gradient descent. When
independent_representations are provided, the \(\operatorname{tr}(VV)\)
penalty becomes \(\operatorname{tr}(V V_{\text{ind}})\) to decouple
estimation of the two quantities (as in the EigenGame formulation).
References
Chapman, J., Wells, L., & Lawry Aguila, A. (2024). Unconstrained Stochastic CCA: Unifying Multiview and Self-Supervised Learning. arXiv:2310.01012.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of the shared latent space. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
objective
|
Module | None
|
Ignored; the EY objective is fixed for this class. Accepted for API compatibility but overridden internally. |
None
|
lr
|
float
|
Learning rate. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Regularisation for numerical stability. Default is 1e-6. |
1e-06
|
Example
import torch import torch.nn as nn enc1 = nn.Linear(10, 4) enc2 = nn.Linear(8, 4) model = DCCA_EY(latent_dimensions=4, encoders=[enc1, enc2])
Source code in cca_zoo/deep/_dcca.py
loss ¶
loss(
representations: list[Tensor],
independent_representations: list[Tensor] | None = None,
) -> dict[str, torch.Tensor]
Compute the EigenGame / Eckart-Young CCA loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
Encoded views from the current batch. |
required |
independent_representations
|
list[Tensor] | None
|
Optional second set of encodings
for the EigenGame penalty term. When provided the penalty
is |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with keys |
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_dcca_ey.py
DCCA_NOI ¶
DCCA_NOI(
latent_dimensions: int,
encoders: list[Module],
rho: float = 0.1,
objective: Module | None = None,
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: DCCA
Deep CCA via Non-linear Orthogonal Iterations.
Uses batch whitening to approximate the CCA whitening step stochastically. The loss pushes each view's representations towards a stop-gradient copy of the other views' whitened representations:
where \(W_j\) is an exponential-moving-average batch-whitening
transform for view \(j\) (see :class:_BatchWhiten) and
\(\operatorname{sg}(\cdot)\) denotes stop-gradient.
References
Wang, W., et al. "Stochastic optimization for deep CCA via nonlinear orthogonal iterations." Allerton 2015. IEEE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of the shared latent space. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
rho
|
float
|
Exponential moving average momentum for the batch whitening layers. Must be in [0, 1]. Default is 0.1. |
0.1
|
objective
|
Module | None
|
Ignored; the NOI loss is fixed. Accepted for API compatibility. |
None
|
lr
|
float
|
Learning rate. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Regularisation for the whitening layers. Default is 1e-6. |
1e-06
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
import torch import torch.nn as nn enc1 = nn.Linear(10, 4) enc2 = nn.Linear(8, 4) model = DCCA_NOI(latent_dimensions=4, encoders=[enc1, enc2], rho=0.1)
Source code in cca_zoo/deep/_dcca_noi.py
loss ¶
loss(
representations: list[Tensor],
independent_representations: list[Tensor] | None = None,
) -> dict[str, torch.Tensor]
Compute the NOI loss.
Each view's representations are pushed towards the whitened representation of the other view (stop-gradient on the target).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
Encoded views from the current batch, each of shape (batch_size, latent_dimensions). |
required |
independent_representations
|
list[Tensor] | None
|
Unused; present for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with key |
Source code in cca_zoo/deep/_dcca_noi.py
DCCA_SDL ¶
DCCA_SDL(
latent_dimensions: int,
encoders: list[Module],
lam: float = 0.5,
objective: Module | None = None,
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: DCCA
Deep CCA via Stochastic Decorrelation Loss.
Combines an MSE alignment loss between views with a within-view decorrelation penalty. Batch normalisation is applied to each encoder output before the loss is computed.
References
Chang, X., Xiang, T., & Hospedales, T. M. "Scalable and effective deep CCA via soft decorrelation." CVPR 2018.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of the shared latent space. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
lam
|
float
|
Weight of the SDL decorrelation penalty. Default is 0.5. |
0.5
|
objective
|
Module | None
|
Ignored; the SDL loss is fixed. Accepted for API compatibility. |
None
|
lr
|
float
|
Learning rate. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Regularisation for numerical stability. Default is 1e-6. |
1e-06
|
Example
import torch import torch.nn as nn enc1 = nn.Linear(10, 4) enc2 = nn.Linear(8, 4) model = DCCA_SDL(latent_dimensions=4, encoders=[enc1, enc2], lam=0.5)
Source code in cca_zoo/deep/_dcca_sdl.py
forward ¶
Encode views and apply batch normalisation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[Tensor]
|
List of input tensors, one per view. |
required |
Returns:
| Type | Description |
|---|---|
list[Tensor]
|
List of batch-normalised latent tensors. |
Source code in cca_zoo/deep/_dcca_sdl.py
loss ¶
loss(
representations: list[Tensor],
independent_representations: list[Tensor] | None = None,
) -> dict[str, torch.Tensor]
Compute the SDL loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
Encoded and batch-normalised views from the current batch, each of shape (batch_size, latent_dimensions). |
required |
independent_representations
|
list[Tensor] | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with keys |
Source code in cca_zoo/deep/_dcca_sdl.py
DCCAE ¶
DCCAE(
latent_dimensions: int,
encoders: list[Module],
decoders: list[Module],
lam: float = 0.5,
objective: Module | None = None,
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: BaseDeep
Deep CCA with Autoencoders.
Extends DCCA by adding per-view reconstruction losses. The total objective is a convex combination of the CCA loss and the summed MSE reconstruction losses:
where \(\mathcal{L}_{\text{CCA}}\) defaults to
:class:~cca_zoo.deep.objectives.CCALoss. When \(\lambda = 0\) the
model reduces to :class:~cca_zoo.deep.DCCA; when \(\lambda = 1\)
it is a pure autoencoder.
References
Wang, W., et al. "On deep multi-view representation learning." ICML 2015.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of the shared latent space. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
decoders
|
list[Module]
|
List of :class: |
required |
lam
|
float
|
Weight for the reconstruction term. Must be in [0, 1]. When 0 the model reduces to DCCA; when 1 it is a pure autoencoder. Default is 0.5. |
0.5
|
objective
|
Module | None
|
Differentiable CCA loss operating on a list of latent
tensors. Defaults to :class: |
None
|
lr
|
float
|
Learning rate. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Ridge regularisation for the CCA loss. Default is 1e-6. |
1e-06
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
import torch import torch.nn as nn enc1, enc2 = nn.Linear(10, 4), nn.Linear(8, 4) dec1, dec2 = nn.Linear(4, 10), nn.Linear(4, 8) model = DCCAE( ... latent_dimensions=4, ... encoders=[enc1, enc2], ... decoders=[dec1, dec2], ... )
Source code in cca_zoo/deep/_dccae.py
loss ¶
loss(
representations: list[Tensor],
independent_representations: list[Tensor] | None = None,
) -> dict[str, torch.Tensor]
Compute the DCCAE objective (CCA + reconstruction).
This method does not have access to the original views for
reconstruction. Override training_step or call
:meth:_full_loss if reconstruction targets are needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
Encoded views from the current batch. |
required |
independent_representations
|
list[Tensor] | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with key |
dict[str, Tensor]
|
(reconstruction is not computed here without raw views). |
Source code in cca_zoo/deep/_dccae.py
training_step ¶
Training step that includes reconstruction loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
dict[str, Any]
|
Dictionary with key |
required |
batch_idx
|
int
|
Batch index (unused). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar loss tensor. |
Source code in cca_zoo/deep/_dccae.py
DVCCA ¶
DVCCA(
latent_dimensions: int,
encoders: list[Module],
decoders: list[Module],
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: BaseDeep
Deep Variational Canonical Correlation Analysis.
A variational autoencoder framework for multiview data. Each encoder maps a view to a 2 * latent_dimensions output, which is split into a posterior mean mu and log-variance log_var. A shared latent code z is sampled via the reparameterisation trick and then decoded to reconstruct all views.
The training objective is the negative ELBO:
where the approximate posterior aggregates all views' encoders and the prior is a standard normal:
References
Wang, W., et al. "Deep variational canonical correlation analysis." arXiv:1610.03454 (2016).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of the latent space. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
decoders
|
list[Module]
|
List of :class: |
required |
lr
|
float
|
Learning rate. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Regularisation for numerical stability. Default is 1e-6. |
1e-06
|
Example
import torch import torch.nn as nn
Encoders output 2 * latent_dimensions¶
enc1 = nn.Linear(10, 8) enc2 = nn.Linear(10, 8) dec1 = nn.Linear(4, 10) dec2 = nn.Linear(4, 10) model = DVCCA( ... latent_dimensions=4, ... encoders=[enc1, enc2], ... decoders=[dec1, dec2], ... )
Source code in cca_zoo/deep/_dvcca.py
forward ¶
Encode views and return a list of latent representations (mu).
At inference time this returns the posterior mean as the point estimate of the latent code for each view independently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[Tensor]
|
List of input tensors, one per view. |
required |
Returns:
| Type | Description |
|---|---|
list[Tensor]
|
List with one tensor of shape (batch_size, latent_dimensions) |
list[Tensor]
|
representing the shared posterior mean. |
Source code in cca_zoo/deep/_dvcca.py
loss ¶
loss(
representations: list[Tensor],
independent_representations: list[Tensor] | None = None,
) -> dict[str, torch.Tensor]
Compute the ELBO loss (used during validation via BaseDeep).
Note: Reconstruction requires access to original views; for
training the full ELBO is computed in :meth:training_step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
Unused here; the method returns zero so that the validation step in BaseDeep does not crash. |
required |
independent_representations
|
list[Tensor] | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with |
Source code in cca_zoo/deep/_dvcca.py
training_step ¶
Training step computing the full negative ELBO.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
dict[str, Any]
|
Dictionary with key |
required |
batch_idx
|
int
|
Batch index (unused). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar loss tensor (negative ELBO). |
Source code in cca_zoo/deep/_dvcca.py
transform ¶
Project all samples to the shared latent space via posterior mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
DataLoader
|
DataLoader yielding batches with a |
required |
Returns:
| Type | Description |
|---|---|
list[ndarray]
|
List with one numpy array of shape (n_samples, latent_dimensions). |
Source code in cca_zoo/deep/_dvcca.py
DTCCA ¶
DTCCA(
latent_dimensions: int,
encoders: list[Module],
objective: Module | None = None,
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: DCCA
Deep Tensor CCA.
Applies the tensor CCA loss (:class:~cca_zoo.deep.objectives.TCCALoss)
to neural representations. The cross-moment tensor is formed from
whitened latent codes, and the objective is the negative Frobenius norm
of that tensor — a differentiable proxy for the tensor CCA criterion:
where \(\otimes\) denotes the outer product and \(H_i\) is the
ridge-whitened representation of view \(i\). This is the deep
analogue of the higher-order cross-moment maximised in closed form by
the linear :class:~cca_zoo.linear.TCCA.
References
Wong, H. S., et al. "Deep Tensor CCA for Multi-view Learning." IEEE Transactions on Big Data (2021).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of the shared latent space. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
objective
|
Module | None
|
Ignored; the TCCA loss is always used. Accepted for API compatibility. |
None
|
lr
|
float
|
Learning rate. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Ridge regularisation for whitening. Default is 1e-6. |
1e-06
|
Example
import torch import torch.nn as nn enc1 = nn.Linear(10, 4) enc2 = nn.Linear(8, 4) enc3 = nn.Linear(6, 4) model = DTCCA(latent_dimensions=4, encoders=[enc1, enc2, enc3])
Source code in cca_zoo/deep/_dtcca.py
loss ¶
loss(
representations: list[Tensor],
independent_representations: list[Tensor] | None = None,
) -> dict[str, torch.Tensor]
Compute the DTCCA loss via the tensor cross-moment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
Encoded views from the current batch, each of shape (batch_size, latent_dimensions). |
required |
independent_representations
|
list[Tensor] | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with key |
Source code in cca_zoo/deep/_dtcca.py
DMCCA ¶
DMCCA(
latent_dimensions: int,
encoders: list[Module],
objective: Module | None = None,
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: DCCA
Deep Multiset CCA.
Applies the multiview pairwise-sum CCA loss
(:class:~cca_zoo.deep.objectives.MCCALoss) to neural representations,
encouraging every pair of views to be mutually correlated in the shared
latent space:
Unlike the base :class:DCCA, this supports more than two
encoders/views out of the box. This is the same SUMCOR multiset
objective used by the linear :class:~cca_zoo.linear.MCCA, here
optimised over neural encoder outputs by gradient descent rather than
via eigendecomposition.
References
Kettenring, J. R. (1971). Canonical analysis of several sets of variables. Biometrika, 58(3), 433-451.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of the shared latent space. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
objective
|
Module | None
|
Ignored; the MCCA loss is always used. Accepted for API compatibility. |
None
|
lr
|
float
|
Learning rate. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Ridge regularisation passed to each pairwise CCA loss. Default is 1e-6. |
1e-06
|
Example
import torch.nn as nn enc1 = nn.Linear(10, 4) enc2 = nn.Linear(8, 4) enc3 = nn.Linear(6, 4) model = DMCCA(latent_dimensions=4, encoders=[enc1, enc2, enc3])
Source code in cca_zoo/deep/_dmcca.py
loss ¶
loss(
representations: list[Tensor],
independent_representations: list[Tensor] | None = None,
) -> dict[str, torch.Tensor]
Compute the DMCCA loss via the summed pairwise CCA objective.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
Encoded views from the current batch, each of shape (batch_size, latent_dimensions). |
required |
independent_representations
|
list[Tensor] | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with key |
Source code in cca_zoo/deep/_dmcca.py
DGCCA ¶
DGCCA(
latent_dimensions: int,
encoders: list[Module],
objective: Module | None = None,
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: DCCA
Deep Generalised CCA.
Applies the generalised CCA (MAX-VAR) loss
(:class:~cca_zoo.deep.objectives.GCCALoss) to neural representations,
maximising correlation of each view with a shared latent target rather
than with every other view pairwise:
where \(H_i\) is the ridge-whitened representation of view
\(i\) and \(\lambda_d(\cdot)\) is the \(d\)-th largest
eigenvalue. Unlike the base :class:DCCA, this supports more than two
encoders/views out of the box, mirroring the linear
:class:~cca_zoo.linear.GCCA.
References
Benton, A., et al. "Deep Generalized Canonical Correlation Analysis." RepL4NLP 2019.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of the shared latent space. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
objective
|
Module | None
|
Ignored; the GCCA loss is always used. Accepted for API compatibility. |
None
|
lr
|
float
|
Learning rate. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Ridge regularisation for within-view whitening. Default is 1e-6. |
1e-06
|
Example
import torch.nn as nn enc1 = nn.Linear(10, 4) enc2 = nn.Linear(8, 4) enc3 = nn.Linear(6, 4) model = DGCCA(latent_dimensions=4, encoders=[enc1, enc2, enc3])
Source code in cca_zoo/deep/_dgcca.py
loss ¶
loss(
representations: list[Tensor],
independent_representations: list[Tensor] | None = None,
) -> dict[str, torch.Tensor]
Compute the DGCCA loss via the generalised CCA objective.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
Encoded views from the current batch, each of shape (batch_size, latent_dimensions). |
required |
independent_representations
|
list[Tensor] | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with key |
Source code in cca_zoo/deep/_dgcca.py
SplitAE ¶
SplitAE(
latent_dimensions: int,
encoders: list[Module],
decoders: list[Module],
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: BaseDeep
Split Autoencoder baseline for multiview learning.
All views are encoded individually into a shared latent space. The concatenated representations are used to reconstruct each view via dedicated decoders:
where \(\|\) denotes concatenation. This model serves as a reconstruction-based baseline that does not explicitly maximise correlation between views — "split autoencoder" architectures of this kind are a common baseline in the deep multiview representation learning literature (e.g. compared against by Wang, W., et al. in "On deep multi-view representation learning." ICML 2015), rather than a single-paper reproduction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of each encoder's output. Decoders receive the concatenation of all encoder outputs, so their input size is n_views * latent_dimensions. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
decoders
|
list[Module]
|
List of :class: |
required |
lr
|
float
|
Learning rate. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Unused; present for API consistency. Default is 1e-6. |
1e-06
|
Example
import torch import torch.nn as nn enc1 = nn.Linear(10, 4) enc2 = nn.Linear(8, 4)
Decoders receive 2 * 4 = 8 dimensional input¶
dec1 = nn.Linear(8, 10) dec2 = nn.Linear(8, 8) model = SplitAE( ... latent_dimensions=4, ... encoders=[enc1, enc2], ... decoders=[dec1, dec2], ... )
Source code in cca_zoo/deep/_splitae.py
loss ¶
loss(
representations: list[Tensor],
independent_representations: list[Tensor] | None = None,
) -> dict[str, torch.Tensor]
Return a zero loss placeholder.
Reconstruction requires the original views; use the full training step for proper loss computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
Unused. |
required |
independent_representations
|
list[Tensor] | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with |
Source code in cca_zoo/deep/_splitae.py
training_step ¶
Training step computing the reconstruction loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
dict[str, Any]
|
Dictionary with key |
required |
batch_idx
|
int
|
Batch index (unused). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar loss tensor. |
Source code in cca_zoo/deep/_splitae.py
BarlowTwins ¶
BarlowTwins(
latent_dimensions: int,
encoders: list[Module],
lam: float = 0.005,
objective: Module | None = None,
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: DCCA
Barlow Twins self-supervised learning model.
Learns representations by encouraging the cross-correlation matrix between two views to be close to the identity:
where \(Z_1, Z_2\) are the batch-normalised representations of the two views. Batch normalisation is applied per-view before computing \(C\).
References
Zbontar, J., et al. "Barlow twins: Self-supervised learning via redundancy reduction." ICML 2021.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of the shared latent space. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
lam
|
float
|
Weight for the off-diagonal redundancy penalty. Default is 5e-3. |
0.005
|
objective
|
Module | None
|
Ignored; the Barlow Twins loss is fixed. Accepted for API compatibility. |
None
|
lr
|
float
|
Learning rate. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Unused. Present for API compatibility. Default is 1e-6. |
1e-06
|
Example
import torch import torch.nn as nn enc1 = nn.Linear(10, 4) enc2 = nn.Linear(8, 4) model = BarlowTwins(latent_dimensions=4, encoders=[enc1, enc2], lam=5e-3)
Source code in cca_zoo/deep/_barlowtwins.py
forward ¶
Encode views and apply batch normalisation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[Tensor]
|
List of input tensors, one per view. |
required |
Returns:
| Type | Description |
|---|---|
list[Tensor]
|
List of batch-normalised latent tensors. |
Source code in cca_zoo/deep/_barlowtwins.py
loss ¶
loss(
representations: list[Tensor],
independent_representations: list[Tensor] | None = None,
) -> dict[str, torch.Tensor]
Compute the Barlow Twins loss for two batch-normalised views.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
List containing exactly two batch-normalised tensors, each of shape (batch_size, latent_dimensions). |
required |
independent_representations
|
list[Tensor] | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with keys |
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_barlowtwins.py
VICReg ¶
VICReg(
latent_dimensions: int,
encoders: list[Module],
sim_coeff: float = 25.0,
std_coeff: float = 25.0,
cov_coeff: float = 1.0,
objective: Module | None = None,
lr: float = 0.001,
max_epochs: int = 100,
eps: float = 1e-06,
)
Bases: DCCA
Variance-Invariance-Covariance Regularization.
Three-term self-supervised objective that jointly encourages invariance (MSE similarity between the two views' representations), variance (per-feature standard deviation at least 1, preventing collapse), and covariance (off-diagonal covariance close to zero, reducing redundancy between feature dimensions):
where \(\gamma, \mu, \nu\) are sim_coeff, std_coeff, and
cov_coeff respectively, \(\sigma(z_k)\) is the per-dimension
standard deviation of view \(k\)'s representation, and \(d\)
is the latent dimensionality.
References
Bardes, A., Ponce, J., & LeCun, Y. "VICReg: Variance-Invariance- Covariance Regularization for Self-Supervised Learning." arXiv:2105.04906 (2022).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Dimensionality of the shared latent space. |
required |
encoders
|
list[Module]
|
List of :class: |
required |
sim_coeff
|
float
|
Weight for the invariance (MSE) term. Default is 25.0. |
25.0
|
std_coeff
|
float
|
Weight for the variance hinge term. Default is 25.0. |
25.0
|
cov_coeff
|
float
|
Weight for the covariance penalty term. Default is 1.0. |
1.0
|
objective
|
Module | None
|
Ignored; the VICReg loss is fixed. Accepted for API compatibility. |
None
|
lr
|
float
|
Learning rate. Default is 1e-3. |
0.001
|
max_epochs
|
int
|
Maximum training epochs. Default is 100. |
100
|
eps
|
float
|
Unused. Present for API compatibility. Default is 1e-6. |
1e-06
|
Example
import torch import torch.nn as nn enc1 = nn.Linear(10, 4) enc2 = nn.Linear(8, 4) model = VICReg(latent_dimensions=4, encoders=[enc1, enc2])
Source code in cca_zoo/deep/_vicreg.py
loss ¶
loss(
representations: list[Tensor],
independent_representations: list[Tensor] | None = None,
) -> dict[str, torch.Tensor]
Compute the VICReg three-term loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
List of tensors, each of shape (batch_size, latent_dimensions). Currently only the first two views are used. |
required |
independent_representations
|
list[Tensor] | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary with keys |
dict[str, Tensor]
|
|
Source code in cca_zoo/deep/_vicreg.py
Objectives¶
objectives ¶
Differentiable CCA loss functions for use with deep models.
CCALoss ¶
Bases: Module
Andrew 2013 deep CCA correlation loss for two views.
Computes the negative sum of squared singular values of the whitened cross-covariance:
where \(\Sigma_{11}, \Sigma_{22}\) are the (ridge-regularised) empirical within-view covariances of the two encoder outputs over the mini-batch and \(\Sigma_{12}\) their cross-covariance. Minimising this loss maximises the sum of squared canonical correlations.
References
Andrew, G., et al. "Deep canonical correlation analysis." ICML 2013.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eps
|
float
|
Ridge regularisation added to within-view covariance matrices for numerical stability. Default is 1e-5. |
1e-05
|
Example
import torch loss_fn = CCALoss(eps=1e-4) z1 = torch.randn(32, 4) z2 = torch.randn(32, 4) loss = loss_fn([z1, z2])
Source code in cca_zoo/deep/objectives.py
forward ¶
Compute the CCA loss for a list containing exactly two views.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
List of two tensors, each of shape (batch_size, latent_dimensions). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar tensor: negative sum of squared canonical correlations. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of representations is not exactly 2. |
Source code in cca_zoo/deep/objectives.py
MCCALoss ¶
Bases: Module
Multiview extension of CCALoss that sums pairwise CCA losses.
where \(\mathcal{L}_{\text{CCA}}\) is :class:CCALoss applied to
each pair of views. This is the deep, gradient-descent analogue of the
SUMCOR multiset objective (Kettenring 1971) also used by the linear
:class:~cca_zoo.linear.MCCA, and encourages every pair of views to be
mutually correlated in the shared latent space.
References
Kettenring, J. R. (1971). Canonical analysis of several sets of variables. Biometrika, 58(3), 433-451.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eps
|
float
|
Ridge regularisation passed to each pairwise CCALoss. Default is 1e-5. |
1e-05
|
Example
import torch loss_fn = MCCALoss(eps=1e-4) views = [torch.randn(32, 4) for _ in range(3)] loss = loss_fn(views)
Source code in cca_zoo/deep/objectives.py
forward ¶
Compute the sum of pairwise CCA losses across all view pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
List of tensors, each of shape (batch_size, latent_dimensions). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar tensor: sum of pairwise negative canonical correlations. |
Source code in cca_zoo/deep/objectives.py
GCCALoss ¶
Bases: Module
Generalised CCA loss for multiple views (MAX-VAR GCCA objective).
Maximises the sum of squared correlations between each whitened view and a shared latent target. Equivalently, minimises the negative sum of the top \(k\) eigenvalues of the summed whitened Gram matrix:
where \(H_i = \tilde{Z}_i (\tilde{Z}_i^\top \tilde{Z}_i +
\epsilon I)^{-1/2}\) is the (mean-centred, ridge-whitened) representation
of view \(i\), and \(\lambda_d(\cdot)\) denotes the \(d\)-th
largest eigenvalue. This mirrors the generalised eigenvalue problem
solved in closed form by the linear :class:~cca_zoo.linear.GCCA.
References
Benton, A., et al. "Deep Generalized Canonical Correlation Analysis." RepL4NLP 2019.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eps
|
float
|
Ridge regularisation for within-view covariance inversion. Default is 1e-5. |
1e-05
|
Example
import torch loss_fn = GCCALoss(eps=1e-4) views = [torch.randn(32, 4) for _ in range(3)] loss = loss_fn(views)
Source code in cca_zoo/deep/objectives.py
forward ¶
Compute the GCCA loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
List of tensors, each of shape (batch_size, latent_dimensions). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar tensor: negative total GCCA objective. |
Source code in cca_zoo/deep/objectives.py
TCCALoss ¶
Bases: Module
Tensor CCA loss (proxy via Frobenius norm of cross-moment tensor).
Forms the higher-order cross-moment tensor of the whitened representations and returns the negative Frobenius norm as a differentiable proxy for the tensor CCA objective:
where \(\otimes\) denotes the outer product and \(H_i =
\tilde{Z}_i (\tilde{Z}_i^\top \tilde{Z}_i + \epsilon I)^{-1/2}\) is the
whitened representation of view \(i\). This is the deep,
gradient-descent analogue of the higher-order cross-moment maximised
in closed form by the linear :class:~cca_zoo.linear.TCCA.
References
Kim, T.-K., Wong, S.-F., & Cipolla, R. (2007). Tensor canonical correlation analysis for action classification. CVPR 2007. IEEE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eps
|
float
|
Ridge regularisation for whitening. Default is 1e-5. |
1e-05
|
Example
import torch loss_fn = TCCALoss(eps=1e-4) views = [torch.randn(32, 4) for _ in range(3)] loss = loss_fn(views)
Source code in cca_zoo/deep/objectives.py
forward ¶
Compute the tensor CCA loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representations
|
list[Tensor]
|
List of tensors, each of shape (batch_size, latent_dimensions). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar tensor: negative Frobenius norm of the cross-moment tensor. |