-
Notifications
You must be signed in to change notification settings - Fork 42
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
CORAL loss is defined differently from the original paper #17
Comments
I agree with you. @SSARCandy This is my implementation, any advice? def coral_loss(source, target):
d = source.size(1)
ns, nt = source.size(0), target.size(0)
# source covariance
tmp_s = torch.ones((1, ns)) @ source
cs = (source.t() @ source - (tmp_s.t() @ tmp_s) / ns) / (ns - 1)
# target covariance
tmp_t = torch.ones((1, nt)) @ target
ct = (target.t() @ target - (tmp_t.t() @ tmp_t) / nt) / (nt - 1)
# frobenius norm
loss = (cs - ct).pow(2).sum().sqrt()
loss = loss / (4 * d * d)
return loss |
@yaox12 I used to run your code but got the following error. Traceback (most recent call last): |
@redhat12345 My code is based on |
@yaox12 Even I use Pytorch=0.4 but got the error: Traceback (most recent call last): |
@redhat12345 if the |
@yaox12, I agree with you. But I think line 14 is: |
Why you think writer's code is also right? |
In my opinion, the main problem is the calculation of the covariance, in paper, the covariance is get by dividing by (n-1), but in the code , it is get by dividing by (n), that is " torch.mean(torch.mul((xc - xct), (xc - xct)))" . however, I'm actually not sure which one is the right one. |
tldr, no error, this code is "correct" but the magnitude of the loss is not scaled correctly.
|
I noticed that both the covariance and the Frobenius norm are computed differently in your implementation.
You compute the Frobenius norm as below:
# frobenius norm between source and target
loss = torch.mean(torch.mul((xc - xct), (xc - xct)))
However as stated here http://mathworld.wolfram.com/FrobeniusNorm.html , after squaring each element and summing them, should be computed the square root of the sum not the mean of the squared elements.
In the original paper the covariances are computed as below :
https://arxiv.org/abs/1607.01719
While in your implementation:
The text was updated successfully, but these errors were encountered: