-
Notifications
You must be signed in to change notification settings - Fork 137
Refactor cosine similarity and add cosine distance #240
Conversation
There was a problem hiding this 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.
@rxwei Ah, I'll fix this immediately. Thanks for taking time and explaining this to me. |
@rxwei Why do some kokoro labels get blocked? |
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 |
@rxwei could you retrigger a build? |
There was a problem hiding this 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)) | ||
} | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Merging this to keep things rolling. It is fine to add a test for |
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.