如何使用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:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\ops.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:\tensorflowJava\Builder.py", line 7, in <module>
tf.saved_model.load(
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\saved_model\load.py", line 900, in load
result = load_internal(export_dir, tags, options)["root"]
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\saved_model\load.py", line 958, in load_internal
root = load_v1_in_v2.load(export_dir, tags)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\saved_model\load_v1_in_v2.py", line 286, in load
result = loader.load(tags=tags)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\saved_model\load_v1_in_v2.py", line 268, in load
signature_functions = self._extract_signatures(wrapped, meta_graph_def)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\saved_model\load_v1_in_v2.py", line 176, in _extract_signatures
signature_fn = wrapped.prune(feeds=feeds, fetches=fetches)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\wrap_function.py", line 325, in prune
fetches = nest.map_structure(_fetch_preprocessing_callback, fetches)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\util\nest.py", line 869, in map_structure
structure[0], [func(*x) for x in entries],
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\util\nest.py", line 869, in <listcomp>
structure[0], [func(*x) for x in entries],
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\wrap_function.py", line 311, in _fetch_preprocessing_callback
decoded = _get_element_from_tensor_info(fetch, self._func_graph)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\wrap_function.py", line 104, in _get_element_from_tensor_info
return graph.as_graph_element(tensor_info.name)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\ops.py", line 3895, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\ops.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."
1条答案
按热度按时间1sbrub3j1#
我不明白为什么你要用Java训练你的模型,然后尝试用Python。基本上,Python是创建模型的首选,而不是Java。而且在Python中加载和使用模型更容易。检查如何使用keras和tensorflow keras.io构建模型