c++ - 指定输入/输出节点以在加载了 C++ API 的模型上运行 TensorFlow 1.0+ 中的推理

标签 c++ machine-learning tensorflow

我正在使用 TensorFlow 1.4 C++ API 加载 V2 检查点,这个答案非常简单:https://stackoverflow.com/a/43639305/9015277 .但是,这个答案没有指定如何将输入馈送到加载的网络。

在 TF 1.4 中,ClientSession::Run() 的输入可以使用 FeedType 对象指定,定义如下:

std::unordered_map< Output, Input::Initializer, OutputHash > FeedType

这里每个 Output 键代表一个由 OP 产生的张量值。对于在 C++ API 中构建的图形,我想只传递输入占位符相当简单,但我如何才能对从 V2 检查点加载的图形执行相同的操作?

在这个例子中(我相信它使用 r0.12 api)https://github.com/tensorflow/tensorflow/blob/ab0fcaceda001825654424bf18e8a8e0f8d39df2/tensorflow/examples/label_image/main.cc#L346这又很简单,图层只是用它们的名字给出。但是我怎样才能使用新的 API 做同样的事情呢?

最佳答案

好吧,我没有得到任何有用的答案,所以最后我只使用了旧的 C++ API(顺便说一句,它在 r1.4 中仍然有效)。我仍在寻找如何使用新 API 完成此操作的答案。

在旧的TF API Session::Run中是这样的:

virtual Status Run(
  const std::vector< std::pair< string, Tensor > > & inputs,
  const std::vector< string > & output_tensor_names,
  const std::vector< string > & target_node_names,
  std::vector< Tensor > *outputs
)=0

inputs vector 中的字符串允许使用 python 图定义中的名称指定网络中的输入,类似于 feed_dict 在 python 上的使用方式。这是我在 python 中输入占位符的图形定义:

with tf.variable_scope('global'):
    velocity_state = tf.placeholder(shape=[None, 1],
                                    dtype=tf.float32,
                                    name='velocity_state')

在 C++ 中为这个特定的占位符提供一些虚拟数据,并运行推理:

using namespace tensorflow;

// specifying input node name and creating tensor to feed it
string velocity_state_placeholder = "global/velocity_state";
Tensor velocity_state = Input::Initializer((float)0.0, TensorShape({1, 1})).tensor;

// collecting all inputs
std::vector<std::pair<string, Tensor>> input_feed;
input_feed.push_back(std::make_pair(velocity_state_placeholder, velocity_state));

// output node name
string action_distribution = "global/fully_connected_1/Softmax";

// tensor for results
std::vector<Tensor> output_tensors;

// running inference
Status run_status = session->Run(input_feed,
                                {action_distribution},
                                {},
                                &output_tensors);

关于c++ - 指定输入/输出节点以在加载了 C++ API 的模型上运行 TensorFlow 1.0+ 中的推理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47514329/

相关文章:

c++ - sizeof(int()) 是合法的表达式吗?

c++ - 如何在 C++ 中打印/检查 vector 的值

python - Tensorflow - 准确度从 1.0 开始,随着损失而降低

python - 使用 hyperopt 进行评估和预测的不同结果指标

python-3.x - 将注意力层添加到 Seq2Seq 模型

c++ - 强制 gsl::as_span 返回一个 gsl::span<const T>?

c++ - 如何使用 QProcess 运行 vim 终端

machine-learning - 如何从 Keras 中经过训练的模型中获取偏差?

python - 为什么我的 CUDA 突然适用于 Pytorch 但不适用于 Tensorflow?

tensorflow - 在张量板可视化中调整垂直轴范围