如何在C语言中使用tensorflow(四)实例

通过TF_SessionRun的方式输出字符串内容:

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<tensorflow/c/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;
}

将代码替换到test.c当中,编译生成可执行文件test。

./test
2020-11-16 19:58:37.850140: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-11-16 19:58:37.874392: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3000000000 Hz
2020-11-16 19:58:37.874637: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x559021926b60 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-11-16 19:58:37.874654: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
status code: 0
Hello TensorFlow!

从可执行文件的输出结果发现,输出包含LOG信息,LOG信息通过宏TF_CPP_MIN_LOG_LEVEL确定,具体取值及意义如下表所示。

0默认值,输出所有信息
1屏蔽通知信息
2屏蔽通知信息和警告信息
3屏蔽通知信息、警告信息和报错信息

因此可以根据要求将TF_CPP_MIN_LOG_LEVEL设置为合理值。

$ export TF_CPP_MIN_LOG_LEVEL='2'
$ ./test
status code: 0
Hello TensorFlow!

参考资料:

https://stackoverflow.com/questions/44378764/hello-tensorflow-using-the-c-api

https://blog.csdn.net/mid_Faker/article/details/105141301

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值