Skip to content
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

Mask is always zero in Margin-based Contrastive Loss #5

Closed
OrigamiDream opened this issue Sep 26, 2022 · 1 comment
Closed

Mask is always zero in Margin-based Contrastive Loss #5

OrigamiDream opened this issue Sep 26, 2022 · 1 comment

Comments

@OrigamiDream
Copy link

Reference: https://github.com/wzhouad/Contra-OOD/blob/main/model.py#L40

I have question about the implementation of Margin-based Contrastive Loss

mask = (labels.unsqueeze(1) == labels.unsqueeze(0)).float()

If the batch size is 64, the labels variable have a shape of [64]
When the above code performs, ([64, 1] == [1, 64]).float()[64, 64], which is exact 2D diagonal matrix.

mask = mask - torch.diag(torch.diag(mask))

But the problem is on the second line of code.
If torch.diag(mask) performs, the result has a shape of [64] that is one-filled vector: $[1, 1, 1, ...]$
Therefore, the result of torch.diag(torch.diag(mask)) is exactly same with the mask, which is exact 2D diagonal matrix.
Furthermore, if you subtract the result from mask, eventually the mask is always zero-filled matrix.
Eventually, the mask variable have no power for gradient descending.

Is this really on your purpose?

I thought the mask variable is used for distinguishing $P(i)$ and $N(i)$ in equation.
Is this right? Or am I missing a point?

@OrigamiDream
Copy link
Author

That was my mistake, problem solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant