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

Refactor cosine similarity and add cosine distance #240

Merged
merged 11 commits into from
Jun 16, 2019

Conversation

Shashi456
Copy link
Contributor

@Shashi456 Shashi456 commented Jun 14, 2019

Refer to this

cc: @rxwei.
Actually if we are removing the argument labels, i think we should do away with predicted and expected and have separate variable names.

Copy link
Contributor

@rxwei rxwei left a comment

Choose a reason for hiding this comment

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

Thanks for the patch. However, what you did in this patch is not removing argument labels, but instead merely deleting comments. Here are the definitions of argument labels.

In Swift, functions names do not just include the base name, but also the argument labels. For the existing API, cosineSimilarity(predicted:expected:) is the function name, which includes argument labels predicted: and expected:. By suggesting changing the name to cosineSimilarity(_:_:), I included the suggested name change which is removing argument labels, which should manifest in code as:

public func cosineSimilarity<Scalar: TensorFlowFloatingPoint>(
    _ predicted: Tensor<Scalar>, _ expected: Tensor<Scalar>
) -> Tensor<Scalar>

Hope that clarifies things. Comments should not be changed.

@Shashi456
Copy link
Contributor Author

@rxwei Ah, I'll fix this immediately. Thanks for taking time and explaining this to me.

@Shashi456
Copy link
Contributor Author

@rxwei well the build passes locally. I cant build tests because i keep getting error since #234. Can you trigger a build?

@Shashi456
Copy link
Contributor Author

@rxwei Why do some kokoro labels get blocked?

@eaplatanios
Copy link
Contributor

This is not computing cosine similarity but rather the negative cosine similarity. I would suggest you remove the minus sign and also introduce a dual function called cosineDistance which is computed as 1 - cosineSimilarity.

@eaplatanios eaplatanios reopened this Jun 14, 2019
@Shashi456 Shashi456 changed the title Removing argument labels from documentation of cosine similarity Refactoring Cosine similarity and adding Cosine distance Jun 14, 2019
@Shashi456
Copy link
Contributor Author

Also @rxwei any idea on passing #31, one of the last losses to be added.

@Shashi456
Copy link
Contributor Author

@rxwei could you retrigger a build?

@rxwei rxwei changed the title Refactoring Cosine similarity and adding Cosine distance Refactor cosine similarity and add cosine distance Jun 15, 2019
Copy link
Contributor

@saeta saeta left a comment

Choose a reason for hiding this comment

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

Thanks for adding this @Shashi456 ! Looks almost there.

let output: Float = 1.0
XCTAssertEqual(z, Tensor(output))
}

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 also add a test for cosine distance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@saeta, actually cosine distance just returns 1 - cosineSimilarity, so in effect we would be testing the same function. To avoid redundancy I didn't add a test

Copy link
Contributor

Choose a reason for hiding this comment

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

Even though the function is implemented as a trivial wrapper, it's good practice to assume the implementation can change in the future for whatever reasons and have tests be implementation-agnostic.

@rxwei
Copy link
Contributor

rxwei commented Jun 16, 2019

Merging this to keep things rolling. It is fine to add a test for cosineDistance(_:_:) later.

@rxwei rxwei merged commit c108a10 into tensorflow:master Jun 16, 2019
@Shashi456 Shashi456 deleted the doc2 branch June 16, 2019 18:38
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.

5 participants