Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Add Cosine Similarity #233

Merged
merged 5 commits into from
Jun 13, 2019
Merged

Add Cosine Similarity #233

merged 5 commits into from
Jun 13, 2019

Conversation

Descartess
Copy link
Contributor

@Descartess Descartess commented Jun 13, 2019

Reference #127

public func cosineProximityLoss<Scalar: TensorFlowFloatingPoint>(
predicted: Tensor<Scalar>, expected: Tensor<Scalar>
) -> Tensor<Scalar> {
return -(expected * predicted).sum() / (sqrt(expected.squared().sum()) * sqrt(predicted.squared().sum()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please break this line into two so that it fits within 100 columns? The second line should indent by 4 from the first line.

/// - predicted: Predicted outputs from a neural network.
/// - expected: Expected values, i.e. targets, that correspond to the correct output.
@differentiable(wrt: predicted)
public func cosineProximityLoss<Scalar: TensorFlowFloatingPoint>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think cosineDistance might be a better option.

Copy link
Contributor

@Shashi456 Shashi456 Jun 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest calling this cosineSimilarity(_:_:) and make it differentiable w.r.t. both parameters.

Rationale:

  1. It is a standard measure of similarity and is the canonical name of this measure (it also has a Uncyclopedia page).
  2. It does not have to be a loss function, so it can be used in other contexts. Hence arguments labels differentiating between predicted and expected do not seem to be useful.
  3. Because of (2), we should make it differentiable with respect to both parameters.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve just noticed that the argument labels were not removed as suggested. Could you please start a PR to resolve it when you get a chance? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rxwei if it's okay I can fix this in #239

Copy link
Contributor

@rxwei rxwei Jun 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on! It'd be better if you fix it in a separate PR instead.

@Descartess Descartess changed the title Add Cosine proximity loss Add Cosine Similarity Jun 13, 2019
@rxwei rxwei self-assigned this Jun 13, 2019
@rxwei rxwei merged commit c4a7194 into tensorflow:master Jun 13, 2019
dan12411 pushed a commit to dan12411/swift-apis that referenced this pull request Jun 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants