Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C API加载TensorFlow图时出错_Tensorflow - Fatal编程技术网

用C API加载TensorFlow图时出错

用C API加载TensorFlow图时出错,tensorflow,Tensorflow,我正在尝试使用TensorFlow C API加载和执行一个图形。它一直在失败,我不知道为什么 我首先使用这个Python脚本创建一个非常简单的图形并将其保存到一个文件中 import tensorflow as tf graph = tf.Graph() with graph.as_default(): input = tf.placeholder(tf.float32, [10, 3], name='input') output = tf.reduce_sum(input**

我正在尝试使用TensorFlow C API加载和执行一个图形。它一直在失败,我不知道为什么

我首先使用这个Python脚本创建一个非常简单的图形并将其保存到一个文件中

import tensorflow as tf
graph = tf.Graph()
with graph.as_default():
    input = tf.placeholder(tf.float32, [10, 3], name='input')
    output = tf.reduce_sum(input**2, name='output')
tf.train.write_graph(graph, '.', 'test.pbtxt')

然后我用这个C++代码加载它。

#include <fstream>
#include <iostream>
#include <string>
#include <c_api.h>

using namespace std;

int main() {
    ifstream graphFile("test.pbtxt");
    string graphText((istreambuf_iterator<char>(graphFile)), istreambuf_iterator<char>());
    TF_Buffer* buffer = TF_NewBufferFromString(graphText.c_str(), graphText.size());
    TF_Graph* graph = TF_NewGraph();
    TF_ImportGraphDefOptions* importOptions = TF_NewImportGraphDefOptions();
    TF_Status* status = TF_NewStatus();
    TF_GraphImportGraphDef(graph, buffer, importOptions, status);
    cout<<TF_GetCode(status)<<endl;
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
ifstream图形文件(“test.pbtxt”);
字符串graphText((istreambuf_迭代器(graphFile)),istreambuf_迭代器();
TF_Buffer*Buffer=TF_NewBufferFromString(graphText.c_str(),graphText.size());
TF_Graph*Graph=TF_NewGraph();
TF_ImportGraphDefOptions*importOptions=TF_NewImportGraphDefOptions();
TF_Status*Status=TF_NewStatus();
TF_GraphImportGraphDef(图形、缓冲区、导入、状态);

cout首先,我认为您应该使用
作为\u Graph\u def()
来编写图形,在您的情况下:

以open('test.pb',wb')作为f的
:
f、 写入(graph.as_graph_def().SerializeToString())

除了它之外,我建议你不要直接使用C API,因为它容易出错,因为内存泄漏。相反,我已经尝试了你的代码使用,一个C++包装器,它工作得像个符咒。我已经使用了下面的代码:

#负载模型
模型(“../test.pb”);
#按名称声明张量
自动输入=新张量(模型,“输入”);
自动输出=新张量(型号,“输出”);
#饲料数据
std::矢量数据(30,1);
输入->设置_数据(数据);
#跑来跑去
运行(输入、输出);
std::无法获取_数据()[0]