读取weka训练模型文件

时间:2016-05-29 08:42:12

标签: java weka eofexception

我正在尝试加载我的weka训练模型文件以生成预测。但是在尝试执行此操作时,我收到了java.io.eofexception的错误。我确定这与我的模型文件没有正确形成有关。但是我使用了weka工具来创建模型文件并且不明白什么是错误的。

代码

public Classifier loadModel() throws Exception {
    this.readConfFile();
    Classifier classifier;
    FileInputStream fis = new FileInputStream(
            prop.getProperty("Output_Model_Dir") + "/best3.model");
    ObjectInputStream ois = new ObjectInputStream(fis);

    Log.write.info("Load Classifier Successfully => "
            + prop.getProperty("Output_Model_Dir") + "/best3.model");
    classifier = (Classifier) weka.core.SerializationHelper.read(ois);
    Log.write.info("1");
    ois.close();
    Log.write.fine("Read Classifier Successfully");
    return classifier;
}

日志

INFO: Load Classifier Successfully => C:/Users/CRY$TAL/workspace/flysafews/system_files/best.model
java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2325)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2794)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at weka.core.SerializationHelper.read(SerializationHelper.java:285)
    at flysafe.predict.support.FileHandler.loadModel(FileHandler.java:104)
    at flysafe.predict.core.PredictionManager.predict(PredictionManager.java:189)
    at flysafe.webservice.WebServiceHandler.predictQuery(WebServiceHandler.java:62)

PS:我使用了不同的模型文件。当使用该文件时,代码工作除了arff结构的错误不同。

1 个答案:

答案 0 :(得分:0)

您将ObjectInputStream传递给weka.core.SerializationHelper.read,该InputStream只需要ObjectInputStream,并将ObjectInputStream包裹在另一个FileInputStream fis中,然后失败。

因此,只需将SerializationHelper.read传递给FileInputStream fis = new FileInputStream(...); classifier = (Classifier) weka.core.SerializationHelper.read(fis);

即可
"B2:B3"