8

出于学习目的,如何使用 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/lastSuccessfulBuild/artifact/lib_package/libtensorflow-cpu-windows-x86_64.zip

上面的代码 GPFs 就TF_SessionRun()调用了。一旦我们找到解决方案,如何检索输出?是否应该为输出使用不同的张量?上面的代码在输出和操作中都重用了它。

非常感谢

4

3 回答 3

9

除了偏移初始化之外,还有一个错误需要解决。这个版本似乎工作正常:

#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)来获取字符串的开头。

于 2017-06-06T10:09:17.027 回答
1

TF_STRING张量使用此处描述的格式进行编码。在您的代码中,您占用了空间(8 个字节)来编码一个偏移量,但实际上并未对其进行初始化。为此,您需要添加以下内容:

memset(TF_TensorData(张量), 0, 8);

在调用 之前TF_SetAttrTensor,因为这会将字符串元素的“偏移量”设置为 0(您正在编码一个字符串值的位置)。

对于您的第二个问题:您实际上并没有重新使用相同的tensor指针。建议的注释TF_SessionRunTF_SessionRun正在分配TF_Tensor调用者拥有所有权的新对象。因此,在您的代码片段中,tensor变量被覆盖以指向新分配的张量。

希望有帮助。

于 2017-06-06T05:58:49.790 回答
0

该示例将在安装了 TensorFlow 的 Linux 上运行,如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 文档中所说的那样。

于 2020-06-11T07:44:14.830 回答