-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from jameschapman19/pytorchlightning
Pytorchlightning
- Loading branch information
Showing
50 changed files
with
1,799 additions
and
7,804 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import Iterable | ||
|
||
import torch | ||
|
||
from cca_zoo.deepmodels import DCCA | ||
from cca_zoo.deepmodels.architectures import BaseEncoder, Encoder | ||
|
||
|
||
class BarlowTwins(DCCA): | ||
""" | ||
A class used to fit a Barlow Twins model. | ||
:Citation: | ||
Zbontar, Jure, et al. "Barlow twins: Self-supervised learning via redundancy reduction." arXiv preprint arXiv:2103.03230 (2021). | ||
""" | ||
|
||
def __init__( | ||
self, | ||
latent_dims: int, | ||
encoders: Iterable[BaseEncoder] = [Encoder, Encoder], | ||
lam=1, | ||
): | ||
""" | ||
Constructor class for Barlow Twins | ||
:param latent_dims: # latent dimensions | ||
:param encoders: list of encoder networks | ||
:param lam: weighting of off diagonal loss terms | ||
""" | ||
super().__init__(latent_dims=latent_dims, encoders=encoders) | ||
self.lam = lam | ||
self.bns = torch.nn.ModuleList( | ||
[torch.nn.BatchNorm1d(latent_dims, affine=False) for _ in self.encoders] | ||
) | ||
|
||
def forward(self, *args): | ||
z = [] | ||
for i, (encoder, bn) in enumerate(zip(self.encoders, self.bns)): | ||
z.append(bn(encoder(args[i]))) | ||
return tuple(z) | ||
|
||
def loss(self, *args): | ||
z = self(*args) | ||
cross_cov = z[0].T @ z[1] / (z[0].shape[0] - 1) | ||
invariance = torch.mean(torch.pow(1 - torch.diag(cross_cov), 2)) | ||
covariance = torch.mean( | ||
torch.triu(torch.pow(cross_cov, 2), diagonal=1) | ||
) + torch.mean(torch.tril(torch.pow(cross_cov, 2), diagonal=-1)) | ||
return invariance + covariance |
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import torch | ||
import torch.nn.functional as F | ||
|
||
from cca_zoo.deepmodels import DCCA_NOI | ||
|
||
|
||
class DCCA_SDL(DCCA_NOI): | ||
""" | ||
A class used to fit a Deep CCA by Stochastic Decorrelation model. | ||
:Citation: | ||
Chang, Xiaobin, Tao Xiang, and Timothy M. Hospedales. "Scalable and effective deep CCA via soft decorrelation." Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition. 2018. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
latent_dims: int, | ||
N: int, | ||
encoders=None, | ||
r: float = 0, | ||
rho: float = 0.2, | ||
eps: float = 1e-3, | ||
shared_target: bool = False, | ||
lam=0.5, | ||
): | ||
""" | ||
Constructor class for DCCA | ||
:param latent_dims: # latent dimensions | ||
:param encoders: list of encoder networks | ||
:param r: regularisation parameter of tracenorm CCA like ridge CCA | ||
:param rho: covariance memory like DCCA non-linear orthogonal iterations paper | ||
:param eps: epsilon used throughout | ||
:param shared_target: not used | ||
""" | ||
super().__init__( | ||
latent_dims=latent_dims, | ||
N=N, | ||
encoders=encoders, | ||
r=r, | ||
rho=rho, | ||
eps=eps, | ||
shared_target=shared_target, | ||
) | ||
self.c = None | ||
self.cross_cov = None | ||
self.lam = lam | ||
self.bns = torch.nn.ModuleList( | ||
[ | ||
torch.nn.BatchNorm1d(latent_dims, affine=False) | ||
for _ in range(latent_dims) | ||
] | ||
) | ||
|
||
def forward(self, *args): | ||
z = [] | ||
for i, (encoder, bn) in enumerate(zip(self.encoders, self.bns)): | ||
z.append(bn(encoder(args[i]))) | ||
return tuple(z) | ||
|
||
def loss(self, *args): | ||
z = self(*args) | ||
self._update_covariances(*z, train=self.training) | ||
SDL_loss = self._sdl_loss(self.covs) | ||
l2_loss = F.mse_loss(z[0], z[1]) | ||
return l2_loss + self.lam * SDL_loss | ||
|
||
def _sdl_loss(self, covs): | ||
loss = 0 | ||
for cov in covs: | ||
cov = cov | ||
sgn = torch.sign(cov) | ||
sgn.fill_diagonal_(0) | ||
loss += torch.mean(cov * sgn) | ||
return loss | ||
|
||
def _update_covariances(self, *z, train=True): | ||
batch_covs = [z_.T @ z_ for z_ in z] | ||
if train: | ||
if self.c is not None: | ||
self.c = self.rho * self.c + 1 | ||
self.covs = [ | ||
self.rho * self.covs[i].detach() + (1 - self.rho) * batch_cov | ||
for i, batch_cov in enumerate(batch_covs) | ||
] | ||
else: | ||
self.c = 1 | ||
self.covs = batch_covs | ||
# pytorch-lightning runs validation once so this just fixes the bug | ||
elif self.covs is None: | ||
self.covs = batch_covs |
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
Oops, something went wrong.