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

Cosine distance calculation problem #8

Open
ouc-lq opened this issue Apr 11, 2021 · 2 comments
Open

Cosine distance calculation problem #8

ouc-lq opened this issue Apr 11, 2021 · 2 comments

Comments

@ouc-lq
Copy link

ouc-lq commented Apr 11, 2021

In the source code, the author calculates the cosine distance as follows.

        sum_support = torch.sum(torch.pow(support_image, 2), 1) 
        support_manitude = sum_support.clamp(eps, float("inf")).rsqrt() 
        dot_product = input_image.unsqueeze(1).bmm(support_image.unsqueeze(2)).squeeze()
        cosine_similarity = dot_product * support_manitude * input_manitude
        similarities.append(cosine_similarity)

But in my opinion, the right the cosine distance should be calculated as follows.

        sum_support = torch.sum(torch.pow(support_image, 2), 1) 
        support_manitude = sum_support.clamp(eps, float("inf")).rsqrt() 
        sum_input = torch.sum(torch.pow(input_image, 2), 1)
        input_manitude = sum_input.clamp(eps, float("inf")).rsqrt()
        dot_product = input_image.unsqueeze(1).bmm(support_image.unsqueeze(2)).squeeze()
        cosine_similarity = dot_product * support_manitude * input_manitude
        similarities.append(cosine_similarity)

Am i right? If not, what is the mistake?

@ouc-lq
Copy link
Author

ouc-lq commented Apr 11, 2021

@502dxceit
Copy link

we could calculate cosine similarity with following succinct code:
cosine_similarity = F.cosine_similarity(support_image, target_set)
where F is imported as torch.nn.functional

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

2 participants