gpt4 book ai didi

java - 将keras模型加载到java程序以预测新输入

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:58 28 4
gpt4 key购买 nike

我有这段适用于 python 的代码

X = numpy.loadtxt("compiledFeatures.csv", delimiter=",")
model = load_model("kerasnaive.h5")
predictions = model.predict(X)

print(predictions);

我正在尝试用 java 编写具有相同功能的代码,

我已经编写了这段代码,但它不起作用,任何人都知道我做错了什么,或者是否有另一种更简单的方法来做到这一点?

代码进入catch block ,在调试代码的过程中,似乎从模型文件中获得的所有信息都是空的

path = String.format("%s\\kerasnaive.h5", System.getProperty("user.dir"), 
pAgents[i]);
try {
network = KerasModelImport.importKerasModelAndWeights(path, false);
}
catch (Exception e){
System.out.println("cannot build keras layers");
}

INDArray input = Nd4j.create(1);
input.add(featuresInput); //an NDarray that i got in the method

INDArray output = network[i].outputSingle(input);

好像模型没有建好(网络还是null)python 的代码加载模型并且它工作,

在 Java 中我收到错误:“无法确定层的输出数量:未找到 output_dim 或 nb_filter 字段。有关详细信息,请参阅 http://deeplearning4j.org/model-import-keras。”

虽然两种情况下使用的是同一个文件

谢谢,奥里

最佳答案

您当前正在使用 importKerasModelAndWeights 导入经过训练的 keras 模型。我不确定您是如何训练模型的,但在 Keras 中有两种类型的模型可用:顺序模型 和使用函数式 API 的模型类。您可以阅读更多here .

如果您在创建网络时使用了Sequential model,则需要使用importKerasSequentialModel函数。 Keras Sequential models .

关于java - 将keras模型加载到java程序以预测新输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50721628/

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