问题描述
我在这个网站上尝试了这个在 C++ 中使用 Tensorflow 保存模型的例子:https://medium.com/jim-fleming/loading-a-tensorflow-graph-with-the-c-api-4caaff88463f#.ji310n4zo
I was trying this example of using Tensorflow saved model in c++ in this website:https://medium.com/jim-fleming/loading-a-tensorflow-graph-with-the-c-api-4caaff88463f#.ji310n4zo
效果很好.但它不保存变量 a 和 b 的值,因为它只保存图形而不保存变量.我试图替换以下行:
It works well. But it does not save the values of the variables a and b as it only saves the graph not the variables. I tried to replace the following line:
tf.train.write_graph(sess.graph_def, 'models/', 'graph.pb', as_text=False)
与
saver.save(sess, 'models/graph', global_step=0)
当然是在创建保护程序对象之后.它不起作用,它输出:
of course after creating the saver object. It does not work and it outputs:
未找到:FeedInputs:无法找到提要输出
我检查了加载的节点,它们只是:
I checked the nodes the Nodes that are loaded and they are only:
_源
_SINK
在 write_graph 函数中,然后在 C++ 中加载模型时,我加载了以下节点:
while in the write_graph function and then load the model in C++, I got the following nodes loaded:
_源
_SINK
save/restore_slice_1/shape_and_slice
save/restore_slice_1/shape_and_slice
save/restore_slice_1/tensor_name
save/restore_slice_1/tensor_name
save/restore_slice/shape_and_slice
save/restore_slice/shape_and_slice
save/restore_slice/tensor_name
save/restore_slice/tensor_name
save/save/shapes_and_slices
save/save/shapes_and_slices
save/save/tensor_names
save/save/tensor_names
保存/常量
save/restore_slice_1
save/restore_slice_1
save/restore_slice
save/restore_slice
b
保存/分配_1
读/读
b/initial_value
b/initial_value
b/分配
一个
保存/分配
保存/恢复所有
保存/保存
save/control_dependency
save/control_dependency
读/读
c
a/initial_value
a/initial_value
a/分配
初始化
张量
甚至saver.save()创建的图形文件比write_graph创建的1.9KB小得多,165B.
and even the graph file that is created by saver.save() is much smaller, 165B, compared to the one created by write_graph, 1.9KB.
推荐答案
我不确定这是否是解决问题的最佳方式,但至少可以解决问题.
I'm not sure if that is the best way of solving the problem but at least it solves it.
由于 write_graph 也可以存储常量的值,所以在使用 write_graph 函数编写图形之前,我在 python 中添加了以下代码:
As write_graph can also store the values of the constants, I added the following code to the python just before writing the graph with write_graph function:
for v in tf.trainable_variables():
vc = tf.constant(v.eval())
tf.assign(v, vc, name="assign_variables")
这会创建在训练后存储变量值的常量,然后创建张量assign_variables"以将它们分配给变量.现在,当您调用 write_graph 时,它会将变量的值存储在文件中.
This creates constants that store variables' values after being trained and then create tensors "assign_variables" to assign them to the variables. Now, when you call write_graph, it will store the variables' values in the file.
剩下的唯一部分是在 c 代码中调用这些张量assign_variables",以确保为您的变量分配了存储在文件.这是一种方法:
The only remaining part is to call these tensors "assign_variables" in the c code to make sure that your variables are assigned with the constants values that are stored in the file. Here is a one way to do it:
Status status = NewSession(SessionOptions(), &session);
std::vector<tensorflow::Tensor> outputs;
for(int i = 0;status.ok(); i++) {
char name[100];
if (i==0)
sprintf(name, "assign_variables");
else
sprintf(name, "assign_variables_%d", i);
status = session->Run({}, {name}, {}, &outputs);
}
这篇关于未找到:FeedInputs:无法找到提要输出 TensorFlow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!