余弦相似度始终为 1.0
Cosine similarity is constantly 1.0
我正在尝试构建段落向量并使用 Java 中的 DeepLearning4J 框架对它们执行一些推理。当我将段落向量构建到 ZIP 文件夹中时,我可以通过使用行号来获得相似性,如下所示:
SentenceIterator sentenceIterator = new BasicLineIterator(new File(inputFilePath));
AbstractCache<VocabWord> abstractCache = new AbstractCache<VocabWord>();
TokenizerFactory tokenizerFactory = new DefaultTokenizerFactory();
tokenizerFactory.setTokenPreProcessor(new CommonPreprocessor());
LabelsSource labelsSource = new LabelsSource("LINE_");
ParagraphVectors paragraphVectors = new ParagraphVectors.Builder()
.minWordFrequency(1)
.iterations(5)
.epochs(1)
.layerSize(100)
.learningRate(0.025)
.labelsSource(labelsSource)
.windowSize(5)
.iterate(sentenceIterator)
.trainWordVectors(false)
.vocabCache(abstractCache)
.tokenizerFactory(tokenizerFactory)
.sampling(0)
.build();
paragraphVectors.fit();
double similarity1 = paragraphVectors.similarity("LINE_9835", "LINE_100");
System.out.println("Similarity: " + similarity1);
WordVectorSerializer.writeParagraphVectors(paragraphVectors, outputParagraphVectorsFilePath);
变量inputFilePath
指的是包含一些信息的文本文件。变量 outputParagraphVectorsFilePath
指的是磁盘上要存储向量的位置。这个功能有效,相似之处是准确的。出现以下问题:
TokenizerFactory tokenizerFactory = new DefaultTokenizerFactory();
tokenizerFactory.setTokenPreProcessor(new CommonPreprocessor());
ParagraphVectors paragraphVectors = WordVectorSerializer.readParagraphVectors(new File(inputFilePath));
paragraphVectors.setTokenizerFactory(tokenizerFactory);
paragraphVectors.getConfiguration().setIterations(1);
INDArray inferredVectorA = paragraphVectors.inferVector("This is my world .");
INDArray inferredVectorA2 = paragraphVectors.inferVector("This is my world .");
INDArray inferredVectorB = paragraphVectors.inferVector("This is my way .");
System.out.println("Cosine similarity A/B:" + Transforms.cosineSim(inferredVectorA, inferredVectorB));
System.out.println("Cosine similarity A/B2:" + Transforms.cosineSim(inferredVectorA, inferredVectorA2));
inputFilePath
变量指的是包含矢量的 ZIP 文件夹在磁盘上的位置。当我 运行 这个函数时,我得到以下信息:
Cosine similarity A/B:1.0
Cosine similarity A/B2:1.0
即使我改变周围的矢量并将它们与其他矢量进行比较,我也得到相同的 1.0。难道我做错了什么?任何帮助将不胜感激。
我正在尝试构建段落向量并使用 Java 中的 DeepLearning4J 框架对它们执行一些推理。当我将段落向量构建到 ZIP 文件夹中时,我可以通过使用行号来获得相似性,如下所示:
SentenceIterator sentenceIterator = new BasicLineIterator(new File(inputFilePath));
AbstractCache<VocabWord> abstractCache = new AbstractCache<VocabWord>();
TokenizerFactory tokenizerFactory = new DefaultTokenizerFactory();
tokenizerFactory.setTokenPreProcessor(new CommonPreprocessor());
LabelsSource labelsSource = new LabelsSource("LINE_");
ParagraphVectors paragraphVectors = new ParagraphVectors.Builder()
.minWordFrequency(1)
.iterations(5)
.epochs(1)
.layerSize(100)
.learningRate(0.025)
.labelsSource(labelsSource)
.windowSize(5)
.iterate(sentenceIterator)
.trainWordVectors(false)
.vocabCache(abstractCache)
.tokenizerFactory(tokenizerFactory)
.sampling(0)
.build();
paragraphVectors.fit();
double similarity1 = paragraphVectors.similarity("LINE_9835", "LINE_100");
System.out.println("Similarity: " + similarity1);
WordVectorSerializer.writeParagraphVectors(paragraphVectors, outputParagraphVectorsFilePath);
变量inputFilePath
指的是包含一些信息的文本文件。变量 outputParagraphVectorsFilePath
指的是磁盘上要存储向量的位置。这个功能有效,相似之处是准确的。出现以下问题:
TokenizerFactory tokenizerFactory = new DefaultTokenizerFactory();
tokenizerFactory.setTokenPreProcessor(new CommonPreprocessor());
ParagraphVectors paragraphVectors = WordVectorSerializer.readParagraphVectors(new File(inputFilePath));
paragraphVectors.setTokenizerFactory(tokenizerFactory);
paragraphVectors.getConfiguration().setIterations(1);
INDArray inferredVectorA = paragraphVectors.inferVector("This is my world .");
INDArray inferredVectorA2 = paragraphVectors.inferVector("This is my world .");
INDArray inferredVectorB = paragraphVectors.inferVector("This is my way .");
System.out.println("Cosine similarity A/B:" + Transforms.cosineSim(inferredVectorA, inferredVectorB));
System.out.println("Cosine similarity A/B2:" + Transforms.cosineSim(inferredVectorA, inferredVectorA2));
inputFilePath
变量指的是包含矢量的 ZIP 文件夹在磁盘上的位置。当我 运行 这个函数时,我得到以下信息:
Cosine similarity A/B:1.0
Cosine similarity A/B2:1.0
即使我改变周围的矢量并将它们与其他矢量进行比较,我也得到相同的 1.0。难道我做错了什么?任何帮助将不胜感激。