如何通过一次运行将多个输入样本馈送到C++张量流模型

How to feed multiple input samples to a tensorflow model in C++ with a single Run

本文关键字:样本 输入 模型 张量流 C++ 何通过 运行 一次      更新时间:2023-10-16

使用 python tensorflow API,可以做这样的事情:

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b
sess.run(adder_node, {a: [1,3], b: [2, 4]})

结果: [ 3. 7.]

C++有什么方法可以通过对Run方法的单个调用将多个输入提供给模型?我尝试使用一个 std::vector of feed_dicts

// prepare tensorflow inputs
std::vector<std::pair<std::string, tensorflow::Tensor>> feed_dict;
for(size_t i = 0; i < noutput_items; i++) {
tensorflow::TensorShape data_shape({1, d_vlen_in});
tensorflow::Tensor n_tensor(tensorflow::DT_FLOAT, data_shape);
auto n_data = n_tensor.flat<float>().data();
for(int j = 0 ; j < d_vlen_in ; j++) {
n_data[j] = in[j];
}
feed_dict.push_back(std::make_pair(d_layer_in, n_tensor));
in += d_vlen_in;
}
// prepare tensorflow outputs
std::vector<tensorflow::Tensor> outputs;
TF_CHECK_OK(d_session->Run(feed_dict, {d_layer_out}, {}, &outputs));

d_layer_ind_layer_outstd::strings,"输入"是我的输入层/占位符。

但是,它失败并显示:

Non-OK-status: d_session->Run(feed_dict, {d_layer_out}, {}, &outputs) status: Invalid argument: Endpoint "input" fed more than once.

有人知道这样做的方法吗?我在这里的主要目标是提高吞吐量。

所以我找到了答案,这很简单。 提要字典的几个元素用于设置多个输入变量,与 python 中的相同。然而,输入张量的第一个(或零)维数是批量维数,在某些情况下可以用作输入信号的时间维数。

// prepare tensorflow inputs
// dimension 0 is the batch dimension, i.e., time dimension
tensorflow::TensorShape data_shape({noutput_items, d_vlen_in});
tensorflow::Tensor in_tensor(tensorflow::DT_FLOAT, data_shape);
auto in_tensor_data = in_tensor.flat<float>().data();
for(size_t i = 0; i < noutput_items; i++) {
for(int j = 0 ; j < d_vlen_in ; j++) {
in_tensor_data[(i*d_vlen_in)+j] = in[(i*d_vlen_in)+j];
}
}
std::vector<std::pair<std::string, tensorflow::Tensor>> feed_dict = {
{ d_layer_in, in_tensor },
};
// prepare tensorflow outputs
std::vector<tensorflow::Tensor> outputs;
TF_CHECK_OK(d_session->Run(feed_dict, {d_layer_out}, {}, &outputs));

这当然会引入一些缓冲/延迟,但它使我的吞吐量增加了大约 1000 倍。