“ Hello TensorFlow!”使用C API [英] "Hello TensorFlow!" using the C API
问题描述
出于学习目的,如何使用TensorFlow C API编写此Python示例?
For learning purposes, how to code this Python example using the TensorFlow C API ?
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;
}
我正在Windows上使用 TensorFlow对其进行测试。 dll
来自:
http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow-windows/lastSuccessfulBuild/artifact/lib_package/libtensorflow-cpu-windows-x86_64.zip
I am testing it on Windows using the TensorFlow.dll
from:
http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow-windows/lastSuccessfulBuild/artifact/lib_package/libtensorflow-cpu-windows-x86_64.zip
上面的代码在 TF_SessionRun()
调用中具有GPF。找到解决方案后,如何检索输出?
输出应使用不同的张量吗?上面的代码在输出和操作中都重复使用了。
The above code GPFs on the TF_SessionRun()
call. Once we find a solution for that, how to retrieve the output ? Should a different tensor be used for the
output ? The above code reuses it in both the output and the operation.
非常感谢
推荐答案
在偏移量初始化旁边还有一个错误需要解决。此版本似乎可以正常工作:
There was a bug to solve beside the offset initialization. This version seems to work fine:
#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: %i\n", TF_GetCode( status ) );
printf( "%s\n", ( ( char * ) TF_TensorData( tensorOutput ) ) + 9 );
TF_CloseSession( session, status );
TF_DeleteSession( session, status );
TF_DeleteStatus( status );
TF_DeleteSessionOptions( options );
return 0;
}
我们必须删除tensorOutput吗?不知道为什么我们必须加9(而不是8)才能得到字符串的开头。
Do we have to delete the tensorOutput ? Not sure why we have to add 9 (instead of 8) to get the beginning of the string.
这篇关于“ Hello TensorFlow!”使用C API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!