用于Tensorflow模型的c ++中的dict

这个问题与这个问题有关:从Python导出Tensorflow图以用于C ++

我试图从Python导出一个Tensorflow模型到C ++。 问题是,我的神经网络以一个占位符开始接收输入,这需要一个feed_dict。 我无法找到任何c ++ API为我的模型提供feed_dict。 我能做什么?

如果没有提供feed_dicts的API,我应该如何更改我的模型,以便可以在没有占位符的情况下为c ++目的而训练和导出它?


tensorflow::Session::Run()方法是Python tf.Session.run()方法的C ++等价物,它支持使用inputs参数提供张量。 像C ++和Python中的很多事情一样,使用它只是更加棘手(在这种情况下,它看起来像文档有点穷)。

inputs参数的类型为const std::vector<std::pair<string, Tensor>>& 。 我们来分解一下:

  • inputs每个元素都对应于您想要在Run()调用中inputs的单张量(例如占位符)。 一个元素的类型为std::pair<string, Tensor>

  • std::pair<string, Tensor>的第一个元素是图中要std::pair<string, Tensor>的名称。 例如,让我们说在Python中你有:

    p = tf.placeholder(..., name="placeholder")
    # ...
    sess.run(..., feed_dict={p: ...})
    

    ...然后在C ++中,这一对中的第一个元素将是p.name的值,在这种情况下,它将是"placeholder:0"

  • 作为tensorflow::Tensor对象, std::pair<string, Tensor>的第二个元素std::pair<string, Tensor>是要馈入的值。 你必须用C ++自己构建它,定义一个Numpy数组或Python对象要复杂一点,但下面是一个如何指定2 x 2矩阵的例子:

    using tensorflow::Tensor;
    using tensorflow::TensorShape;
    
    Tensor t(DT_FLOAT, TensorShape({2, 2}));
    auto t_matrix = t.matrix<float>();
    t_matrix(0, 0) = 1.0;
    t_matrix(0, 1) = 0.0;
    t_matrix(1, 0) = 0.0;
    t_matrix(1, 1) = 1.0;
    

    ...然后你可以传递t作为这一对中的第二个元素。

  • 链接地址: http://www.djcxy.com/p/32099.html

    上一篇: dict in c++ for Tensorflow models

    下一篇: Set weight and bias tensors of tensorflow conv2d operation