gpt4 book ai didi

nlp - 使用 Mallet 加载模型和分类输入

转载 作者:行者123 更新时间:2023-12-04 10:15:42 26 4
gpt4 key购买 nike

我已经有一个使用 SimpleTagger 训练过的 CRF 训练模型。

        SimpleTagger.main(new String[] {
"--train", "true",
"--model-file", "/Desktop/crfmodel",
"--threads", "8",
"--training-proportion", "0.8",
"--weights", "dense",
"--test", "lab",
// "--orders", "2",
"/Desktop/annotations.txt"
});

我计划加载此模型并将其用于标记。我正在使用此代码。
    public static void main(String[] args) throws Exception {

//DOCS http://mallet.cs.umass.edu/classifier-devel.php

Instance instance = getMyInstance();

Classifier classifier = loadClassifier(Paths.get("/Desktop/crfmodel").toFile());

Labeling labeling = classifier.classify(instance).getLabeling();
Label l = labeling.getBestLabel();
System.out.print(instance);
System.out.println(l);
}


private static Classifier loadClassifier(File serializedFile)
throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream (new FileInputStream(serializedFile));
Classifier crf = (Classifier) ois.readObject();
ois.close();

return crf;
}

当我尝试执行上述操作时,出现以下错误
Exception in thread "main" java.lang.ClassCastException: cc.mallet.fst.CRF cannot be cast to cc.mallet.classify.Classifier
at TagClassifier.loadClassifier(TagClassifier.java:77)
at TagClassifier.main(TagClassifier.java:64)

错误正在发生
Classifier crf = (Classifier) ois.readObject();

我可以知道为什么会这样。另外,如果有正确的记录方式来使用训练有素的模型标记输入,您能否分享任何链接/文档?非常感谢您提前!

最佳答案

我想我是通过查看 SimpleTagger 代码弄清楚的。

        crfModel = loadClassifier(Paths.get("/Desktop/crfmodel").toFile());
pipe = crfModel.getInputPipe();
pipe.setTargetProcessing(false);
String formatted = getFormattedQuery(q);

Instance instance = pipe.pipe(new Instance(formatted, null, null, null));
Sequence sequence = (Sequence) instance.getData();
Sequence[] tags = tag(sequence, 3);
    private static Sequence[] tag(Sequence input, int bestK) {
Sequence[] answers;
if (bestK == 1) {
answers = new Sequence[1];
answers[0] = crfModel.transduce(input);
} else {
MaxLatticeDefault lattice = new MaxLatticeDefault(crfModel, input, null);
answers = lattice.bestOutputSequences(bestK).toArray(new Sequence[0]);
}
return answers;
}

关于nlp - 使用 Mallet 加载模型和分类输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61071307/

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