Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用tensorflow对java与python中加载的模型进行错误预测;“保存的模型”;应用程序编程接口_Tensorflow_Tensorflow Serving - Fatal编程技术网

使用tensorflow对java与python中加载的模型进行错误预测;“保存的模型”;应用程序编程接口

使用tensorflow对java与python中加载的模型进行错误预测;“保存的模型”;应用程序编程接口,tensorflow,tensorflow-serving,Tensorflow,Tensorflow Serving,我正在尝试加载一个Java模型,该模型是用python训练的,并使用保存的模型api保存的(来自tensorflow.python.saved_model) 我可以在单独的Python脚本和Java中加载它,但是Java版本中的预测是错误的 我用一个简单的模型编写了一个快速的示例项目,该模型演示了“bug”(我希望是我的误解) Python:OrTraining.py 使用保存的模型Api在培训后保存模型 builders = saved_model_builder.SavedModelBuil

我正在尝试加载一个Java模型,该模型是用python训练的,并使用保存的模型api保存的(
来自tensorflow.python.saved_model

我可以在单独的Python脚本和Java中加载它,但是Java版本中的预测是错误的

我用一个简单的模型编写了一个快速的示例项目,该模型演示了“bug”(我希望是我的误解)

Python:OrTraining.py

使用保存的模型Api在培训后保存模型

builders = saved_model_builder.SavedModelBuilder(export_path)
builders.add_meta_graph_and_variables(sess, ["or"], signature_def_map={
    "predict": tf.saved_model.signature_def_utils.predict_signature_def(
        inputs= {"images": x_placeholder},
        outputs= {"scores": hypothesis_function})
    })
builders.save()
with tf.Session(graph=tf.Graph()) as sess:
tf.saved_model.loader.load(sess, ["or"], "orTrainingModels")
graph = tf.get_default_graph()
print(graph.get_operations())
x_placeholder = graph.get_tensor_by_name("or_inputs:0")
hypothesis_function = graph.get_tensor_by_name("hypothesis_output:0")
# sess.run("init")
print(sess.run(hypothesis_function, feed_dict={x_placeholder: np.array([
    np.array([1, 0]),
    np.array([0, 1]),
    np.array([0, 0]),
    np.array([1, 1]),
])}))

Python:OrLoadSavedModel.py

使用保存的模型Api在单独的脚本中加载模型

builders = saved_model_builder.SavedModelBuilder(export_path)
builders.add_meta_graph_and_variables(sess, ["or"], signature_def_map={
    "predict": tf.saved_model.signature_def_utils.predict_signature_def(
        inputs= {"images": x_placeholder},
        outputs= {"scores": hypothesis_function})
    })
builders.save()
with tf.Session(graph=tf.Graph()) as sess:
tf.saved_model.loader.load(sess, ["or"], "orTrainingModels")
graph = tf.get_default_graph()
print(graph.get_operations())
x_placeholder = graph.get_tensor_by_name("or_inputs:0")
hypothesis_function = graph.get_tensor_by_name("hypothesis_output:0")
# sess.run("init")
print(sess.run(hypothesis_function, feed_dict={x_placeholder: np.array([
    np.array([1, 0]),
    np.array([0, 1]),
    np.array([0, 0]),
    np.array([1, 1]),
])}))

Java:OrLoadSavedModel.Java

装载

java版本和python版本都可以毫无问题地加载和运行图形,但java版本不会输出正确的预测

起初我认为这是因为没有加载权重/偏差,但我能够在java版本中“运行”权重/偏差操作,并在培训后看到它具有python脚本中显示的正确权重

检查java()中的权重


这是我输入数据的方式的一个问题。Tensorflow不喜欢创建装箱类型的张量(整数vs int/Float vs Float),有一些检查可以查看您是否试图传入装箱类型,但检查似乎不够全面

*测试自*

Tensor result = session.runner()
            .fetch("da_weights")
            .run().get(0);
@Test
public void testCreateFromArrayOfBoxed() {
    Integer[] vector = new Integer[] {1, 2, 3, 4};
    try (Tensor<Integer> t = Tensor.create(vector, Integer.class)) {
        fail("Tensor.create() should fail because it was given an array of boxed values"); 
    } catch (IllegalArgumentException e) {
     // The expected exception
   }
}
    Float[] input = new Float[]{0f, 1f};
    Tensor tensorOutput = Tensor.create(input);
    float[] floatOutput= new float[2];
    tensorOutput.copyTo(floatOutput);
    println(Arrays.toString(floatOutput)); // -7.377E30, -7.377E30


    float[] input = new float[]{0f, 1f};
    Tensor tensorOutput = Tensor.create(input);

    float[] floatOutput= new float[2];
    tensorOutput.copyTo(floatOutput);
    println(Arrays.toString(floatOutput)); // 0, 1