0

我计算了两个句子的 VectorMean,如下所示:

String demoString1 = "Enter first label";
String demoString2 = "Enter first name";
        Collection<String> label1 = Splitter.on(' ').splitToList(demoString1);
        Collection<String> label2 = Splitter.on(' ').splitToList(demoString2);

        System.out.println("label1:==>"+label1);
        System.out.println("getWordVectorMatrix->INDArray------------------"+vectors.getWordVectorsMean(label1));

        System.out.println("label2:==>"+label2);
        System.out.println("getWordVectorMatrix->INDArray------------------"+vectors.getWordVectorsMean(label2));

输出:

label1:==>[Enter, first, label]
getWordVectorMatrix->INDArray------------------[0.02,  -0.14,  0.07,  -0.10,.............100 dimension vector]
label2:==>[Enter, first, name]
getWordVectorMatrix->INDArray------------------[-0.00,  -0.15,  0.07,  -0.13,............100 dimension vector]

现在我如何使用它们的均值计算两个句子之间的相似度(余弦相似度)?我进行了搜索,但在 DL4J 中找不到任何可用的 API。

4

1 回答 1

0

方法:

public static double cosineSimForSentence(Word2Vec vector, String sentence1, String sentence2){
        Collection<String> label1 = Splitter.on(' ').splitToList(sentence1);
        Collection<String> label2 = Splitter.on(' ').splitToList(sentence2);
        try{
            return Transforms.cosineSim(vector.getWordVectorsMean(label1), vector.getWordVectorsMean(label2));
        }catch(Exception e){
            exceptionMessage = e.getMessage();
        }
        return Transforms.cosineSim(vector.getWordVectorsMean(label1), vector.getWordVectorsMean(label2));

    }

方法调用:

System.out.println("Similarity Score between: "+demoString1+" --vs-- "+ demoString2 +":==>"+ cosineSimForSentence(vectors, demoString1, demoString2));
于 2018-04-04T06:40:31.167 回答