1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #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; }
|