2017-08-16 138 views
1

我目前有一個TensorFlow圖,我在Python中創建並導出(作爲protobuf)到C++中。我通過session->Run(...)調用運行入隊/出隊操作(FIFOQueue),並且需要呼叫在無法運行操作一段時間後超時。我可以通過將RunOptions填入sess.run(...)來完成Python的工作。在C++中有類似的方法嗎?在TensorFlow中傳遞RunOptions C++

回答

0

如果你看一下當前版本的標題(V1.3.0,但它似乎已經自從RunOptions是爲v0.8.0創建的相同)的tensorflow::Sessiontensorflow::ClientSession,有標註爲「實驗函數簽名「可以讓你通過一個RunOptions對象。一個指針RunMetadata參數似乎也需要,如果您以這種方式致電Run,並看看the implementation它似乎並不像你可以通過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; 
} 

貌似接口已經存在了很長一段時間,但因爲它被標記爲實驗是可能的,它顯示了一些錯誤,或者只是改變即將到來的版本。

+0

謝謝!我問這個問題的原因是因爲我需要一種停止接收數據的方式來關閉程序。正在使用超時「優雅」選項? –

+0

@nc_我自己並沒有真正使用過這個選項,但至少對我來說聽起來合理,只要你的超時時間足夠長以確保你不會因爲延遲或評估中止而停下來。但是您也可以研究使用該選項的可能警告。 – jdehesa