main.c 3.7 KB
Newer Older
Y
Yu Yang 已提交
1 2
#include <paddle/capi.h>
#include <time.h>
P
peterzhang2029 已提交
3

Y
Yu Yang 已提交
4 5
#include "../common/common.h"

Y
ying 已提交
6
// Modify this path as needed.
Y
Yu Yang 已提交
7
#define CONFIG_BIN "./trainer_config.bin"
Y
ying 已提交
8 9 10 11 12 13
// Modify this path as needed.
// This demo assumes that merged model is not used, then this path is the
// directory storing all the trained parameters.
// If the model is trained by PaddlePaddle V2 API, the model is saved as
// a compressed file. You need to uncompress the compressed file first.
#define MODEL_PATH "models/pass_4"
Y
Yu Yang 已提交
14 15

int main() {
Y
ying 已提交
16
  // Initalize the PaddlePaddle runtime environment.
P
peterzhang2029 已提交
17 18
  char* argv[] = {"--use_gpu=False"};
  CHECK(paddle_init(1, (char**)argv));
Y
Yu Yang 已提交
19

Y
ying 已提交
20
  // Read the binary configuration file generated by `convert_protobin.sh`
Y
Yu Yang 已提交
21 22 23
  long size;
  void* buf = read_config(CONFIG_BIN, &size);

Y
ying 已提交
24
  // Create the gradient machine for inference.
Y
Yu Yang 已提交
25 26 27
  paddle_gradient_machine machine;
  CHECK(paddle_gradient_machine_create_for_inference(&machine, buf, (int)size));

Y
ying 已提交
28 29 30 31 32 33 34
  // Load the trained model. Modify the parameter MODEL_PATH to set the correct
  // path of the trained model.
  CHECK(paddle_gradient_machine_load_parameter_from_disk(machine, MODEL_PATH));

  // Inputs and outputs of the network are organized as paddle_arguments object
  // in C-API. In the comments below, "argument" specifically means one input of
  // the neural network in PaddlePaddle C-API.
Y
Yu Yang 已提交
35 36
  paddle_arguments in_args = paddle_arguments_create_none();

Y
ying 已提交
37 38
  // There is only one data layer in this demo MNIST network, invoke this
  // function to create one argument.
Y
Yu Yang 已提交
39 40
  CHECK(paddle_arguments_resize(in_args, 1));

Y
ying 已提交
41 42 43 44 45 46 47 48 49
  // Each argument needs one matrix or one ivector (integer vector, for sparse
  // index input, usually used in NLP task) to holds the real input data.
  // In the comments below, "matrix" specifically means the object needed by
  // argument to hold the data. Here we create the matrix for the above created
  // agument to store the testing samples.
  paddle_matrix mat =
      paddle_matrix_create(/* height = batch size */ 1,
                           /* width = dimensionality of the data layer */ 784,
                           /* whether to use GPU */ false);
Y
Yu Yang 已提交
50

P
peterzhang2029 已提交
51
  paddle_real* array;
Y
ying 已提交
52 53
  // Get the pointer pointing to the start address of the first row of the
  // created matrix.
P
peterzhang2029 已提交
54
  CHECK(paddle_matrix_get_row(mat, 0, &array));
P
peterzhang2029 已提交
55

Y
ying 已提交
56 57
  // Fill the matrix with a randomly generated test sample.
  srand(time(0));
P
peterzhang2029 已提交
58 59 60
  for (int i = 0; i < 784; ++i) {
    array[i] = rand() / ((float)RAND_MAX);
  }
Y
Yu Yang 已提交
61

Y
ying 已提交
62
  // Assign the matrix to the argument.
Y
Yu Yang 已提交
63 64
  CHECK(paddle_arguments_set_value(in_args, 0, mat));

Y
ying 已提交
65
  // Create the output argument.
Y
Yu Yang 已提交
66
  paddle_arguments out_args = paddle_arguments_create_none();
Y
ying 已提交
67 68

  // Invoke the forward computation.
Y
Yu Yang 已提交
69 70 71
  CHECK(paddle_gradient_machine_forward(machine,
                                        in_args,
                                        out_args,
Y
ying 已提交
72
                                        /* is train taks or not */ false));
Y
Yu Yang 已提交
73

Y
ying 已提交
74 75 76 77
  // Create the matrix to hold the forward result of the neural network.
  paddle_matrix prob = paddle_matrix_create_none();
  // Access the matrix of the output argument, the predicted result is stored in
  // which.
Y
Yu Yang 已提交
78
  CHECK(paddle_arguments_get_value(out_args, 0, prob));
Y
Yu Yang 已提交
79

P
peterzhang2029 已提交
80 81 82
  uint64_t height;
  uint64_t width;
  CHECK(paddle_matrix_get_shape(prob, &height, &width));
P
peterzhang2029 已提交
83
  CHECK(paddle_matrix_get_row(prob, 0, &array));
Y
Yu Yang 已提交
84

P
peterzhang2029 已提交
85
  printf("Prob: \n");
86
  for (int i = 0; i < height * width; ++i) {
P
peterzhang2029 已提交
87
    printf("%.4f ", array[i]);
P
peterzhang2029 已提交
88
    if ((i + 1) % width == 0) {
P
peterzhang2029 已提交
89 90
      printf("\n");
    }
Y
Yu Yang 已提交
91 92 93
  }
  printf("\n");

Y
ying 已提交
94
  // The cleaning up.
Y
Yu Yang 已提交
95 96 97 98 99 100
  CHECK(paddle_matrix_destroy(prob));
  CHECK(paddle_arguments_destroy(out_args));
  CHECK(paddle_matrix_destroy(mat));
  CHECK(paddle_arguments_destroy(in_args));
  CHECK(paddle_gradient_machine_destroy(machine));

Y
Yu Yang 已提交
101 102
  return 0;
}