gpt4 book ai didi

java - 在 Java 中使用 Mallet 在 LDA 中折叠(估计新文档的主题)

转载 作者:行者123 更新时间:2023-12-02 06:21:19 25 4
gpt4 key购买 nike

我通过 Java 使用 Mallet,但我无法弄清楚如何根据我训练过的现有主题模型来评估新文档。

我生成模型的初始代码与 Mallett Developers Guide for Topic Modelling 中的代码非常相似。 ,之后我只需将模型保存为 Java 对象。在稍后的过程中,我从文件中重新加载该 Java 对象,通过 .addInstances() 添加新实例,然后仅根据原始训练集中找到的主题评估这些新实例。

This stats.SE thread提供了一些高级建议,但我不知道如何将它们应用到 Mallet 框架中。

非常感谢任何帮助。

最佳答案

推理实际上也列在 example link 中问题中提供(最后几行)。

对于对保存/加载训练模型的整个代码感兴趣的人,然后使用它来推断新文档的模型分布 - 这里有一些片段:

model.estimate()之后完成后,您就拥有了实际训练过的模型,因此您可以使用标准 Java ObjectOutputStream 对其进行序列化(因为 ParallelTopicModel 实现了 Serializable ):

try {
FileOutputStream outFile = new FileOutputStream("model.ser");
ObjectOutputStream oos = new ObjectOutputStream(outFile);
oos.writeObject(model);
oos.close();
} catch (FileNotFoundException ex) {
// handle this error
} catch (IOException ex) {
// handle this error
}

但请注意,当您推断时,您还需要通过同一管道传递新句子(如 Instance )以便对其进行预处理(tokenzie 等),因此,您还需要保存管道列表(因为我们使用SerialPipe何时可以创建实例然后序列化它):

// initialize the pipelist (using in model training)
SerialPipes pipes = new SerialPipes(pipeList);

try {
FileOutputStream outFile = new FileOutputStream("pipes.ser");
ObjectOutputStream oos = new ObjectOutputStream(outFile);
oos.writeObject(pipes);
oos.close();
} catch (FileNotFoundException ex) {
// handle error
} catch (IOException ex) {
// handle error
}

为了加载模型/管道并将其用于推理,我们需要反序列化:

private static void InferByModel(String sentence) {
// define model and pipeline
ParallelTopicModel model = null;
SerialPipes pipes = null;

// load the model
try {
FileInputStream outFile = new FileInputStream("model.ser");
ObjectInputStream oos = new ObjectInputStream(outFile);
model = (ParallelTopicModel) oos.readObject();
} catch (IOException ex) {
System.out.println("Could not read model from file: " + ex);
} catch (ClassNotFoundException ex) {
System.out.println("Could not load the model: " + ex);
}

// load the pipeline
try {
FileInputStream outFile = new FileInputStream("pipes.ser");
ObjectInputStream oos = new ObjectInputStream(outFile);
pipes = (SerialPipes) oos.readObject();
} catch (IOException ex) {
System.out.println("Could not read pipes from file: " + ex);
} catch (ClassNotFoundException ex) {
System.out.println("Could not load the pipes: " + ex);
}

// if both are properly loaded
if (model != null && pipes != null){

// Create a new instance named "test instance" with empty target
// and source fields note we are using the pipes list here
InstanceList testing = new InstanceList(pipes);
testing.addThruPipe(
new Instance(sentence, null, "test instance", null));

// here we get an inferencer from our loaded model and use it
TopicInferencer inferencer = model.getInferencer();
double[] testProbabilities = inferencer
.getSampledDistribution(testing.get(0), 10, 1, 5);
System.out.println("0\t" + testProbabilities[0]);
}
}

由于某种原因,我没有得到与原始模型完全相同的加载模型的推论 - 但这是另一个问题的问题(如果有人知道,我很高兴听到)

关于java - 在 Java 中使用 Mallet 在 LDA 中折叠(估计新文档的主题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14141195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com