main.c 2.4 KB
Newer Older
Y
Yu Yang 已提交
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
#include <paddle/capi.h>
#include <time.h>
#include "../common/common.h"

#define CONFIG_BIN "./trainer_config.bin"

int main() {
  // Initalize Paddle
  char* argv[] = {"--use_gpu=False"};
  CHECK(paddle_init(1, (char**)argv));

  // Reading config binary file. It is generated by `convert_protobin.sh`
  long size;
  void* buf = read_config(CONFIG_BIN, &size);

  // Create a gradient machine for inference.
  paddle_gradient_machine machine;
  CHECK(paddle_gradient_machine_create_for_inference(&machine, buf, (int)size));
  CHECK(paddle_gradient_machine_randomize_param(machine));

  // Loading parameter. Uncomment the following line and change the directory.
  // CHECK(paddle_gradient_machine_load_parameter_from_disk(machine,
  //                                                "./some_where_to_params"));
  paddle_arguments in_args = paddle_arguments_create_none();

  // There is only one input of this network.
  CHECK(paddle_arguments_resize(in_args, 1));

  // Create input matrix.
30
  paddle_matrix mat = paddle_matrix_create(/* sample_num */ 10,
Y
Yu Yang 已提交
31 32 33 34
                                           /* size */ 784,
                                           /* useGPU */ false);
  srand(time(0));

35 36
  std::vector<paddle_real> input;
  input.resize(784 * 10);
Y
Yu Yang 已提交
37

38 39
  for (int i = 0; i < input.size(); ++i) {
    input[i] = rand() / ((float)RAND_MAX);
Y
Yu Yang 已提交
40
  }
41 42 43
  
  // Set value for the input matrix
  CHECK(paddle_matrix_set_value(mat, input.data()));
Y
Yu Yang 已提交
44 45 46 47 48 49 50 51 52 53

  CHECK(paddle_arguments_set_value(in_args, 0, mat));

  paddle_arguments out_args = paddle_arguments_create_none();
  CHECK(paddle_gradient_machine_forward(machine,
                                        in_args,
                                        out_args,
                                        /* isTrain */ false));
  paddle_matrix prob = paddle_matrix_create_none();

Y
Yu Yang 已提交
54
  CHECK(paddle_arguments_get_value(out_args, 0, prob));
Y
Yu Yang 已提交
55

56 57 58 59 60 61 62
  std::std::vector<paddle_real> result;
  int height;
  int width;

  CHECK(paddle_matrix_get_shape(prob, &height, &width);
  result.resize(height * width);
  CHECK(paddle_matrix_get_value(prob, result.data()));
Y
Yu Yang 已提交
63 64

  printf("Prob: ");
65 66
  for (int i = 0; i < height * width; ++i) {
    printf("%.2f ", result[i]);
Y
Yu Yang 已提交
67 68 69
  }
  printf("\n");

Y
Yu Yang 已提交
70 71 72 73 74 75
  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 已提交
76 77
  return 0;
}