未找到:FeedInputs:无法找到提要输出 TensorFlow
Not found: FeedInputs: unable to find feed output TensorFlow
我在本网站尝试使用 C++ 中的 Tensorflow 保存模型的示例:
https://medium.com/jim-fleming/loading-a-tensorflow-graph-with-the-c-api-4caaff88463f#.ji310n4zo
效果很好。但是它不保存变量 a 和 b[= 的值38=] 因为它只保存图形而不保存变量。我尝试替换以下行:
tf.train.write_graph(sess.graph_def, 'models/', 'graph.pb', as_text=False)
和
saver.save(sess, 'models/graph', global_step=0)
当然是在创建保护程序对象之后。它不起作用并输出:
未找到:FeedInputs:无法找到 Feed 输出 a
我检查了加载的节点,它们只有:
_SOURCE
_SINK
在 write_graph 函数中然后用 C++ 加载模型时,我加载了以下节点:
_SOURCE
_SINK
save/restore_slice_1/shape_and_slice
save/restore_slice_1/tensor_name
save/restore_slice/shape_and_slice
save/restore_slice/tensor_name
save/save/shapes_and_slices
save/save/tensor_names
save/Const
save/restore_slice_1
save/restore_slice
b
save/Assign_1
b/read
b/initial_value
b/Assign
a
save/Assign
save/restore_all
save/save
save/control_dependency
a/read
c
a/initial_value
a/Assign
init
Tensor
甚至 saver.save() 创建的图形文件也比 write_graph 创建的图形文件小得多,只有 165B,只有 1.9KB。
我不确定这是否是解决问题的最佳方法,但至少它解决了问题。
由于write_graph也可以存储常量的值,所以我在python中添加了以下代码,就在使用write_graph函数编写图形之前:
for v in tf.trainable_variables():
vc = tf.constant(v.eval())
tf.assign(v, vc, name="assign_variables")
这会创建常量,在训练后存储变量的值,然后创建张量“assign_variables”来分配它们到变量。现在,当您调用 write_graph 时,它会将变量的值存储在文件中。
唯一剩下的部分是在 C 代码中调用这些张量“assign_variables”以确保您的变量被赋予存储在文件中的常量值。这是一种方法:
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);
}
还有另一种恢复变量的方法,通过调用 save/restore_all
操作,应该出现在图中:
std::vector<tensorflow::Tensor> outputs;
Tensor checkpoint_filepath(DT_STRING, TensorShape());
checkpoint_filepath.scalar<std::string>()() = "path to the checkpoint file";
status = session->Run( {{ "save/Const", checkpoint_filepath },},
{}, {"save/restore_all"}, &outputs);
我在本网站尝试使用 C++ 中的 Tensorflow 保存模型的示例: https://medium.com/jim-fleming/loading-a-tensorflow-graph-with-the-c-api-4caaff88463f#.ji310n4zo
效果很好。但是它不保存变量 a 和 b[= 的值38=] 因为它只保存图形而不保存变量。我尝试替换以下行:
tf.train.write_graph(sess.graph_def, 'models/', 'graph.pb', as_text=False)
和
saver.save(sess, 'models/graph', global_step=0)
当然是在创建保护程序对象之后。它不起作用并输出:
未找到:FeedInputs:无法找到 Feed 输出 a
我检查了加载的节点,它们只有:
_SOURCE
_SINK
在 write_graph 函数中然后用 C++ 加载模型时,我加载了以下节点:
_SOURCE
_SINK
save/restore_slice_1/shape_and_slice
save/restore_slice_1/tensor_name
save/restore_slice/shape_and_slice
save/restore_slice/tensor_name
save/save/shapes_and_slices
save/save/tensor_names
save/Const
save/restore_slice_1
save/restore_slice
b
save/Assign_1
b/read
b/initial_value
b/Assign
a
save/Assign
save/restore_all
save/save
save/control_dependency
a/read
c
a/initial_value
a/Assign
init
Tensor
甚至 saver.save() 创建的图形文件也比 write_graph 创建的图形文件小得多,只有 165B,只有 1.9KB。
我不确定这是否是解决问题的最佳方法,但至少它解决了问题。
由于write_graph也可以存储常量的值,所以我在python中添加了以下代码,就在使用write_graph函数编写图形之前:
for v in tf.trainable_variables():
vc = tf.constant(v.eval())
tf.assign(v, vc, name="assign_variables")
这会创建常量,在训练后存储变量的值,然后创建张量“assign_variables”来分配它们到变量。现在,当您调用 write_graph 时,它会将变量的值存储在文件中。
唯一剩下的部分是在 C 代码中调用这些张量“assign_variables”以确保您的变量被赋予存储在文件中的常量值。这是一种方法:
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);
}
还有另一种恢复变量的方法,通过调用 save/restore_all
操作,应该出现在图中:
std::vector<tensorflow::Tensor> outputs;
Tensor checkpoint_filepath(DT_STRING, TensorShape());
checkpoint_filepath.scalar<std::string>()() = "path to the checkpoint file";
status = session->Run( {{ "save/Const", checkpoint_filepath },},
{}, {"save/restore_all"}, &outputs);