我正在尝试使用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
{
{1}}
即使我改变向量并将它们与其他向量进行比较,我也得到相同的1.0。难道我做错了什么?任何帮助将不胜感激。