加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

dll – 使用C API的“Hello TensorFlow!”

发布时间:2020-12-14 01:59:43 所属栏目:Windows 来源:网络整理
导读:出于学习目的,如何使用TensorFlow C API对此 Python示例进行编码? import tensorflow as tfhello = tf.constant("hello TensorFlow!")sess=tf.Session()print(sess.run(hello)) 我这样试过: #include string.h#include iostream.h#include "c_api.h"int ma
出于学习目的,如何使用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,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,// Inputs
                 output,&tensor,1,// Outputs
                 &operation,// 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

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

非常感谢

解决方法

偏移初始化旁边有一个要解决的错误.这个版本似乎工作正常:

#include <iostream.h>
#include "c_api.h"

int main( int argc,8 + TF_StringEncodedSize( strlen( hello ) ) );
  TF_Tensor * tensorOutput;
  TF_OperationDescription * operationDescription = TF_NewOperation( graph,"hello" );
  TF_Operation * operation; 
  struct TF_Output output;

  TF_StringEncode( hello,status );
  memset( TF_TensorData( tensor ),8 );
  TF_SetAttrTensor( operationDescription,status );

  output.oper = operation;
  output.index = 0;

  TF_SessionRun( session,// Inputs
                 &output,&tensorOutput,status );

  printf( "status code: %in",TF_GetCode( status ) );
  printf( "%sn",( ( char * ) TF_TensorData( tensorOutput ) ) + 9 );

  TF_CloseSession( session,status );
  TF_DeleteStatus( status );
  TF_DeleteSessionOptions( options );  

  return 0;
}

我们必须删除tensorOutput吗?不知道为什么我们必须添加9(而不是8)来获得字符串的开头.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读