What is the difference between cosine similarity functions torch.nn.CosineSimilarity and torch.nn.functional.cosine_similarity ? Where can we use and where we shouldn’t ? If we are using it inside a model with back-propagation flow, which one can we use ?
Hi Ramraj!
The two are effectively the same and they can be used essentially
interchangeably. In particular, they both support backpropagation
in the same way.
CosineSimilarity
is the class / function-object version (and almost
certainly merely wraps a call to cosine_similarity()
), while
cosine_similarity()
is the function version.
It’s primarily a matter of style which you choose to use. However,
CosineSimilarity
is a Module
, so you could, for example, place
it in a Sequential
, if that were to make sense for your use case.
Best.
K. Frank
1 Like