我目前有一个用 Python 创建并导出(作为 protobuf)到 C++ 的 TensorFlow 图。我正在通过 session->Run(...)
调用运行入队/出队操作 (FIFOQueue
),并且在无法运行后需要调用超时运行一段时间。我可以通过将 RunOptions
提供给 sess.run(...)
在 Python 中执行此操作。在 C++ 中是否有类似的方法来执行此操作?
最佳答案
如果您查看 tensorflow::Session
的当前版本(v1.3.0,尽管自从为 v0.8.0 创建 RunOptions
以来它似乎一直相同)的 header |和 tensorflow::ClientSession
,有标记为“实验性”的函数签名,允许您传递 RunOptions
对象。如果您以这种方式调用 Run
并查看 the implementation,指针 RunMetadata
参数似乎也是必需的您似乎无法传递 nullptr
。所以你可以这样做:
#include <vector>
#include <tensorflow/core/public/session.h>
int main(int argc, char *argv[])
{
const int64_t TIMEOUT_MS = ...; // Timeout in milliseconds
tensorflow::GraphDef graph = ...; // Load you graph definition
tensorflow::Session *newSession;
auto status = tensorflow::NewSession(tensorflow::SessionOptions(), &newSession);
if (!status.ok()) { /* Handle error. */ }
status = session->Create(graph);
if (!status.ok()) { /* Handle error. */ }
tensorflow::RunOptions run_options;
run_options.set_timeout_in_ms(TIMEOUT_MS);
tensorflow::RunMetadata run_metadata;
// Run parameters
std::vector<std::pair<string, Tensor> > inputs = ...;
std::vector<string> output_tensor_names = ...;
std::vector<string> target_node_names = ...;
std::vector<Tensor> outputs;
// Run
status = sess.Run(run_options, inputs, output_tensor_names,
target_node_names, &outputs, &run_metadata);
if (!status.ok()) { /* Handle error. */ }
// Use output
// ...
return 0;
}
看起来这个界面已经存在很长时间了,但由于它被标记为实验性的,它可能会显示一些错误或者它只是在即将到来的版本中发生变化。
关于c++ - 在 TensorFlow C++ 中传递 RunOptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45719598/