出于学习目的,如何使用Tensorflow C API来编码此Python示例?
import tensorflow as tf
hello = tf.constant("hello TensorFlow!")
sess=tf.Session()
print(sess.run(hello))
我已经尝试过:
#include <string.h>
#include <iostream.h>
#include "c_api.h"
int main( int argc, char ** argv )
{
TF_Graph * graph = TF_NewGraph();
TF_SessionOptions * options = TF_NewSessionOptions();
TF_Status * status = TF_NewStatus();
TF_Session * session = TF_NewSession( graph, options, status );
char hello[] = "Hello TensorFlow!";
TF_Tensor * tensor = TF_AllocateTensor( TF_STRING, 0, 0, 8 + TF_StringEncodedSize( strlen( hello ) ) );
TF_OperationDescription * operationDescription = TF_NewOperation( graph, "Const", "hello" );
TF_Operation * operation;
struct TF_Output * output;
TF_StringEncode( hello, strlen( hello ), 8 + ( char * ) TF_TensorData( tensor ), TF_StringEncodedSize( strlen( hello ) ), status );
TF_SetAttrTensor( operationDescription, "value", tensor, status );
TF_SetAttrType( operationDescription, "dtype", TF_TensorType( tensor ) );
operation = TF_FinishOperation( operationDescription, status );
output->oper = operation;
output->index = 0;
TF_SessionRun( session, 0,
0, 0, 0, // Inputs
output, &tensor, 1, // Outputs
&operation, 1, // Operations
0, status );
printf( "%i", TF_GetCode( status ) );
TF_CloseSession( session, status );
TF_DeleteSession( session, status );
TF_DeleteStatus( status );
TF_DeleteSessionOptions( options );
return 0;
}
我使用TensorFlow.dll
在Windows上测试它:http://ci.tensorflow.org/view/nightly/job/nightly-libtensorflow-windows/lastsuccescesuccessfulbuild/artifact/lib_package/libtensorflow-cpu-windows-windows-windows-windows-x86_64.zip
TF_SessionRun()
调用上的上述代码GPF。一旦找到解决方案,如何检索输出?是否应该将其他张量用于输出 ?上面的代码将其重新在输出和操作中。
非常感谢
在偏移初始化旁边有一个错误。这个版本似乎很好:
#include <iostream.h>
#include "c_api.h"
int main( int argc, char ** argv )
{
TF_Graph * graph = TF_NewGraph();
TF_SessionOptions * options = TF_NewSessionOptions();
TF_Status * status = TF_NewStatus();
TF_Session * session = TF_NewSession( graph, options, status );
char hello[] = "Hello TensorFlow!";
TF_Tensor * tensor = TF_AllocateTensor( TF_STRING, 0, 0, 8 + TF_StringEncodedSize( strlen( hello ) ) );
TF_Tensor * tensorOutput;
TF_OperationDescription * operationDescription = TF_NewOperation( graph, "Const", "hello" );
TF_Operation * operation;
struct TF_Output output;
TF_StringEncode( hello, strlen( hello ), 8 + ( char * ) TF_TensorData( tensor ), TF_StringEncodedSize( strlen( hello ) ), status );
memset( TF_TensorData( tensor ), 0, 8 );
TF_SetAttrTensor( operationDescription, "value", tensor, status );
TF_SetAttrType( operationDescription, "dtype", TF_TensorType( tensor ) );
operation = TF_FinishOperation( operationDescription, status );
output.oper = operation;
output.index = 0;
TF_SessionRun( session, 0,
0, 0, 0, // Inputs
&output, &tensorOutput, 1, // Outputs
&operation, 1, // Operations
0, status );
printf( "status code: %in", TF_GetCode( status ) );
printf( "%sn", ( ( char * ) TF_TensorData( tensorOutput ) ) + 9 );
TF_CloseSession( session, status );
TF_DeleteSession( session, status );
TF_DeleteStatus( status );
TF_DeleteSessionOptions( options );
return 0;
}
我们必须删除Tensoroutput吗?不知道为什么我们必须添加9(而不是8(才能获得字符串的开始。
TF_STRING
张量使用此处描述的格式编码。在您的代码中,您对空间(8个字节(进行了解释以编码一个偏移量,但实际上并未初始化它。为此,您想添加类似的内容:
memset(tf_tensordata(张量(,0,8(;
在调用TF_SetAttrTensor
之前,这将将字符串元素的"偏移"设置为0(您要编码一个字符串值的位置(。
到您的第二个问题:您实际上并未重复使用相同的tensor
指针。TF_SessionRun
的评论表明TF_SessionRun
正在分配呼叫者所有权的新TF_Tensor
对象。因此,在您的代码段中,tensor
变量被覆盖以指向新分配的张量。
希望会有所帮助。
该示例将在Linux上使用TensorFlow安装,如下所述,https://www.tensorflow.org/install/lang_c具有更改的include
指令:
#include <stdio.h>
#include <string.h>
#include <tensorflow/c/c_api.h>
构建它足以执行
gcc hello_tf.c -ltensorflow -o hello_tf
在Tensorflow文档中所说的。