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

NTXentLoss, normalize issue. #696

Closed
jzhanghzau opened this issue May 5, 2024 · 3 comments
Closed

NTXentLoss, normalize issue. #696

jzhanghzau opened this issue May 5, 2024 · 3 comments

Comments

@jzhanghzau
Copy link

Do we have to normalize the feature vector by ourself before sending these feature vectors into NTXentLoss. Because I checked the code in metric-learning, it seems that you don't include the normalize step.

Thanks in advance!

JJ

@jzhanghzau
Copy link
Author

ah, I found you did the normalization step in calculating the similarity matrix.

another question is, assume I have a dataset, feature vector shape is [6, 100], label vector is [0,1,0,3,3,1]. Is it similar to supervised contrasitive learning if I use this NTXentloss in this case?

Thanks!

@stompsjo
Copy link
Contributor

stompsjo commented May 7, 2024

NTXentLoss() compares embeddings to a reference embedding, with associated labels. So if the labels for both the embeddings and reference are known, you can pass those into the loss function and they will be treated like supervised contrastive learning. In your example, feature embeddings that share the same label as a reference embedding would form positive pairs. Your call to the loss function would look something like loss = loss_func(embeddings1, labels, ref_emb=embeddings2, ref_label=labels).

Check out the blue drop-down box in the documentation for NTXentLoss for more information. Issue #6 or this comment are also a good places to look for more details.

@KevinMusgrave
Copy link
Owner

KevinMusgrave commented May 9, 2024

Thanks @stompsjo!

Also if you're looking specifically for Supervised Contrastive Learning there is a loss function for that: https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#supconloss

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

3 participants