main.c 3.0 KB
Newer Older
1
//   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

Y
Yu Yang 已提交
15 16
#include <paddle/capi.h>
#include <time.h>
Y
ying 已提交
17

Y
Yu Yang 已提交
18 19 20 21 22 23 24 25 26
#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));

Y
ying 已提交
27 28
  // Read the binary configuration file which is generated by
  // `convert_protobin.sh`
Y
Yu Yang 已提交
29 30 31
  long size;
  void* buf = read_config(CONFIG_BIN, &size);

Y
ying 已提交
32
  // Create the gradient machine for inference.
Y
Yu Yang 已提交
33 34 35 36
  paddle_gradient_machine machine;
  CHECK(paddle_gradient_machine_create_for_inference(&machine, buf, (int)size));
  CHECK(paddle_gradient_machine_randomize_param(machine));

Y
ying 已提交
37 38
  // Load the trained parameters. Uncomment the following line and change the
  // directory as needed.
Y
Yu Yang 已提交
39 40 41 42 43 44 45
  // 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));

Y
ying 已提交
46
  // Create the input matrix.
Y
Yu Yang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  paddle_matrix mat = paddle_matrix_create_sparse(1, 784, 3, true, false);
  srand(time(0));
  paddle_real* array;
  int colBuf[] = {9, 93, 109};
  int rowBuf[] = {0, sizeof(colBuf) / sizeof(int)};

  CHECK(paddle_matrix_sparse_copy_from(mat,
                                       rowBuf,
                                       sizeof(rowBuf) / sizeof(int),
                                       colBuf,
                                       sizeof(colBuf) / sizeof(int),
                                       NULL,
                                       0));

  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 已提交
70
  CHECK(paddle_arguments_get_value(out_args, 0, prob));
Y
Yu Yang 已提交
71 72 73 74 75 76 77 78 79

  CHECK(paddle_matrix_get_row(prob, 0, &array));

  printf("Prob: ");
  for (int i = 0; i < 10; ++i) {
    printf("%.2f ", array[i]);
  }
  printf("\n");

Y
Yu Yang 已提交
80 81 82 83 84 85
  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 已提交
86 87
  return 0;
}