Skip to content

[TO_REVIEW] Fix deep coral #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions skada/deep/losses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Author: Theo Gnassounou <[email protected]>
# Remi Flamary <[email protected]>
# Yanis Lalou <[email protected]>
# Antoine Collas <[email protected]>
#
# License: BSD 3-Clause

Expand All @@ -9,6 +10,7 @@
import ot
import skorch # noqa: F401
import torch # noqa: F401
from torch.nn.functional import mse_loss

from skada.deep.base import BaseDALoss

Expand Down Expand Up @@ -40,10 +42,11 @@ def deepcoral_loss(features, features_target, assume_centered=False):
if not assume_centered:
features = features - features.mean(0)
features_target = features_target - features_target.mean(0)
cov = torch.cov(features)
cov_target = torch.cov(features_target)
diff = cov - cov_target
loss = (diff * diff).sum() / (4 * len(cov) ** 2)
cov = torch.cov(features.T)
cov_target = torch.cov(features_target.T)
divergence = mse_loss(cov, cov_target, reduction="sum")
dim = features.shape[1]
loss = (1 / (4 * (dim**2))) * divergence
return loss


Expand Down
2 changes: 1 addition & 1 deletion skada/deep/tests/test_deep_divergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_deepcoral(assume_centered):
ToyModule2D(),
reg=1,
layer_name="dropout",
batch_size=10,
batch_size=15,
max_epochs=10,
train_split=None,
assume_centered=assume_centered,
Expand Down
Loading