ivanhk / fastText_java

Java port of c++ version of facebook fasttext
Other
122 stars 80 forks source link

Cosine similarity of two vectors #16

Closed gembin closed 7 years ago

gembin commented 7 years ago

I used sup training, andfasttext.printVectors() to print the vectors of two sentences and then calculate the cosine similarity between the vectors, but got result some thing like: 3.250774993991685, its seems not correct, I think the result should be in range of [-1, 1], any ideas?

private static double cosineSimilarity(float[] vectorA, float[] vectorB) {
        double dotProduct = 0.0d; // the numerator of the cosine similarity
        double normA = 0.0d; // the first part of the denominator of the cosine similarity
        double normB = 0.0d; // the second part of the denominator of the cosine similarity
        int length = vectorA.length;
        for (int i = 0; i < length; i++) {
            dotProduct += vectorA[i] * vectorB[i];
            normA += Math.pow(vectorA[i], 2);
            normB += Math.pow(vectorB[i], 2);
        }
        return dotProduct / Math.sqrt(normA) * Math.sqrt(normB);
    }
ivanhk commented 7 years ago

try return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));

gembin commented 7 years ago

Oh, yes, good catch! 👍