如何使用TensorFlow的SavedModelBundle。Exporter,用于导出在Python中使用的模型。
我创建了这个测试脚本:
try(Graph graph = new Graph()) {
Ops tf = Ops.create(graph);
Operand x = tf.constant(1);
Operand y = tf.placeholder(TInt32.class);
Operand sum = tf.math.add(x, y);
try(Session session = new Session(graph)){
Signature signature = Signature.builder().input("Input1", y).output("Output1", sum).build();
SessionFunction function = SessionFunction.create(signature, session);
SavedModelBundle.exporter("C:\TFModel")
.withSession(session)
.withFunction(function)
.export();
}
}
System.out.println("Done");
当在java中创建模型时,这不会引发任何错误,但当将模型加载到Python中时,它会引发以下错误:
Traceback (most recent call last):
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythonframeworkops.py", line 3939, in _as_graph_element_locked
return op.outputs[out_n]
IndexError: list index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:tensorflowJavaBuilder.py", line 7, in <module>
tf.saved_model.load(
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythonsaved_modelload.py", line 900, in load
result = load_internal(export_dir, tags, options)["root"]
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythonsaved_modelload.py", line 958, in load_internal
root = load_v1_in_v2.load(export_dir, tags)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythonsaved_modelload_v1_in_v2.py", line 286, in load
result = loader.load(tags=tags)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythonsaved_modelload_v1_in_v2.py", line 268, in load
signature_functions = self._extract_signatures(wrapped, meta_graph_def)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythonsaved_modelload_v1_in_v2.py", line 176, in _extract_signatures
signature_fn = wrapped.prune(feeds=feeds, fetches=fetches)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythoneagerwrap_function.py", line 325, in prune
fetches = nest.map_structure(_fetch_preprocessing_callback, fetches)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythonutilnest.py", line 869, in map_structure
structure[0], [func(*x) for x in entries],
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythonutilnest.py", line 869, in <listcomp>
structure[0], [func(*x) for x in entries],
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythoneagerwrap_function.py", line 311, in _fetch_preprocessing_callback
decoded = _get_element_from_tensor_info(fetch, self._func_graph)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythoneagerwrap_function.py", line 104, in _get_element_from_tensor_info
return graph.as_graph_element(tensor_info.name)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythonframeworkops.py", line 3895, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythonframeworkops.py", line 3941, in _as_graph_element_locked
raise KeyError("The name %s refers to a Tensor which does not "
KeyError: "The name 'Init:0' refers to a Tensor which does not exist. The operation, 'Init', exists but only has 0 outputs."
我不明白为什么要用Java训练模型,然后尝试在Python中使用。基本上,Python是创建模型的首选,而不是java。此外,在Python中加载和使用模型更容易。查看如何使用keras和tensorflow keras.io 构建模型