如何通过一次运行将多个输入样本馈送到C++张量流模型
How to feed multiple input samples to a tensorflow model in C++ with a single Run
使用 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_in
和d_layer_out
std::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 倍。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何通过一次运行将多个输入样本馈送到C++张量流模型
- cvPreparePredictData中不支持的格式或格式组合(输入样本必须具有32fC1类型)
- 快速CV角检测样本 - 了解fcvMemAlloc输入
- 如何在libsvm训练代码中输入样本图像
- 在输入和输出之间使用不同数量的样本对音频ffmpegc++进行编码