未找到:FeedInputs:无法找到Feed输出TensorFlow

Mos*_*san 5 c++ model tensorflow

我在这个网站上尝试使用Tensorflow在c ++中保存模型的例子:https://medium.com/jim-fleming/loading-a-tensorflow-graph-with-the-c-api-4caaff88463f#.ji310n4zo

它运作良好.但它不保存变量ab的值,因为它只保存图形而不保存变量.我试图替换以下行:

tf.train.write_graph(sess.graph_def, 'models/', 'graph.pb', as_text=False)
Run Code Online (Sandbox Code Playgroud)

saver.save(sess, 'models/graph', global_step=0)
Run Code Online (Sandbox Code Playgroud)

当然在创建了保护对象之后.它不起作用,它输出:

未找到:FeedInputs:无法找到Feed输出a

我检查了节点加载的节点,它们只是:

_资源

_下沉

在write_graph函数中,然后在C++中加载模型,我加载了以下节点:

_资源

_下沉

保存/ restore_slice_1/shape_and_slice

保存/ restore_slice_1/tensor_name

保存/ restore_slice/shape_and_slice

保存/ restore_slice/tensor_name

保存/保存/ shapes_and_slices

保存/保存/ tensor_names

保存/常量

保存/ restore_slice_1

保存/ restore_slice

b

保存/ Assign_1

面包

B/initial_value

B /分配

一个

保存/分配

保存/ RESTORE_ALL

保存/保存

保存/ control_dependency

A /读

C

A/initial_value

A /分配

在里面

张量

甚至saver.save()创建的图形文件比write_graph创建的图形文件小得多165B(1.9KB).

Mos*_*san 3

我不确定这是否是解决问题的最佳方法,但至少它解决了问题。

由于 write_graph 还可以存储常量的值,因此在使用 write_graph 函数写入图形之前,我将以下代码添加到 python 中:

for v in tf.trainable_variables():
    vc = tf.constant(v.eval())
    tf.assign(v, vc, name="assign_variables")
Run Code Online (Sandbox Code Playgroud)

这将创建在训练后存储变量值的常量,然后创建张量“ allocate_variables ”以将它们分配给变量。现在,当您调用 write_graph 时,它将把变量的值存储在文件中。

唯一剩下的部分是在 C 代码中调用这些张量“ allocate_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);
      }
Run Code Online (Sandbox Code Playgroud)

  • 回答您自己的问题是值得赞赏的,因为其他人可以从您的经验中学习。 (3认同)