我计算了两个句子的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。
答案 0 :(得分: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));