mazzzystar / Queryable

Run OpenAI's CLIP and Apple's MobileCLIP model on iOS to search photos.
https://queryable.app
MIT License
2.66k stars 421 forks source link

PhotoSearchModel.swift: "cosine_similarity" function #26

Closed HKdAlex closed 11 months ago

HKdAlex commented 12 months ago

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 12 months ago

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 11 months ago

Yes, submitted a pull request, though I never did it before. Hoping I did it correctly.

mazzzystar commented 11 months ago

@HKdAlex Merged, your implementation is correct, thank you for pointing it out !