PhotoSearchModel.swift: "cosine_similarity" function
HKdAlex opened this issue · 3 comments
HKdAlex commented
Is this function actually correct?
func cosine_similarity(A: MLShapedArray<Float32>, B: MLShapedArray<Float32>) -> Float {
let magnitude = vDSP.rootMeanSquare(A.scalars) * vDSP.rootMeanSquare(B.scalars)
let dotarray = vDSP.dot(A.scalars, B.scalars)
return dotarray / magnitude
}
This line does not seem to be calculating the magnitude of a vector which is supposed to the square root of the sum of squares (RSS) but is actually calculating the RMS (the square root of the mean sum of squares):
let magnitude = vDSP.rootMeanSquare(A.scalars) * vDSP.rootMeanSquare(B.scalars)
My suggested correction would be:
let magnitude = vDSP.sumOfSquares(A.scalars).squareRoot() * vDSP.sumOfSquares(B.scalars).squareRoot()
I am not good at this stuff but trying t figure it out.
mazzzystar commented
Thank you for the correction, I indeed should not have divided by 1/n when calculating the sum, you are right. Can you submit a PR (Pull Request)?
HKdAlex commented
Yes, submitted a pull request, though I never did it before. Hoping I did it correctly.
mazzzystar commented
@HKdAlex Merged, your implementation is correct, thank you for pointing it out !