GithubHelp home page GithubHelp logo

Comments (6)

SkafteNicki avatar SkafteNicki commented on June 20, 2024 1

It should still be two [N, d] tensors meaning N tensors each with dimension d.
First the function should calculate the cosine similarity between the two tensors and then it should have a reduction argument that determines if we should take the sum or the mean over the batch dimension

from torchmetrics.

avinashsai avatar avinashsai commented on June 20, 2024

Hi,
https://pytorch.org/docs/stable/generated/torch.nn.CosineSimilarity.html. There is already cosine similarity in torch. Can I use it?

from torchmetrics.

SkafteNicki avatar SkafteNicki commented on June 20, 2024

@avinashsai depends on your purpose.
If you want the calculate the mean score on a single batch, no problem.
But lets say that you want to accumulate over a complete dataset, then you need some kind of custom accumulation:

preds = [torch.randn(5, 2), torch.randn(10, 2)]
target = [torch.randn(5, 2), torch.randn(10, 2)]

individual_scores = []
for p,t in zip(preds, target):
    individual_scores.append(torch.nn.CosineSimilarity()(p, t).mean())
# total score and correct score is different
total_score = torch.mean(torch.stack(individual_scores))
correct_score = torch.nn.CosineSimilarity()(torch.cat(preds, dim=0), torch.cat(target, dim=0)).mean()

the global mean does not equal the mean of the score on the individual batches.
This is basically the reason why we also have custom implementation of mean squared error, mean absolute error ect even though these are also a part of pytorch, because when you want to accumulate over multiple batches you need to be careful about the order of operations.

from torchmetrics.

avinashsai avatar avinashsai commented on June 20, 2024

@SkafteNicki Thanks for the clarification. So, this cosine similarity metric will compute between 2 n-dim tensors??

from torchmetrics.

N-georgakopoulos avatar N-georgakopoulos commented on June 20, 2024

Hey, I would like to work on this, could you assign it to me?

from torchmetrics.

stale avatar stale commented on June 20, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from torchmetrics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.