From 34b3ee31fafae7d0b0ad2977b1ab14094018e2b1 Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Sun, 26 Mar 2017 14:06:38 +0800 Subject: [PATCH] Add sequence exampleAdd sequence exampleAdd sequence exampleAdd sequence exampleAdd sequence exampleAdd sequence exampleAdd sequence exampleAdd sequence exampleAdd sequence example --- paddle/capi/examples/sequence/.gitignore | 73 +++++++++++++++++++ paddle/capi/examples/sequence/CMakeLists.txt | 6 ++ .../examples/sequence/convert_protobin.sh | 1 + paddle/capi/examples/sequence/main.c | 63 ++++++++++++++++ .../capi/examples/sequence/trainer_config.py | 13 ++++ 5 files changed, 156 insertions(+) create mode 100644 paddle/capi/examples/sequence/.gitignore create mode 100644 paddle/capi/examples/sequence/CMakeLists.txt create mode 120000 paddle/capi/examples/sequence/convert_protobin.sh create mode 100644 paddle/capi/examples/sequence/main.c create mode 100644 paddle/capi/examples/sequence/trainer_config.py diff --git a/paddle/capi/examples/sequence/.gitignore b/paddle/capi/examples/sequence/.gitignore new file mode 100644 index 00000000000..fab7372d796 --- /dev/null +++ b/paddle/capi/examples/sequence/.gitignore @@ -0,0 +1,73 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/paddle/capi/examples/sequence/CMakeLists.txt b/paddle/capi/examples/sequence/CMakeLists.txt new file mode 100644 index 00000000000..71b73acba7c --- /dev/null +++ b/paddle/capi/examples/sequence/CMakeLists.txt @@ -0,0 +1,6 @@ +project(sequence) +cmake_minimum_required(VERSION 2.8) +aux_source_directory(. SRC_LIST) +add_executable(${PROJECT_NAME} ${SRC_LIST}) +set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 99) +target_link_libraries(${PROJECT_NAME} -lpaddle_capi_shared) diff --git a/paddle/capi/examples/sequence/convert_protobin.sh b/paddle/capi/examples/sequence/convert_protobin.sh new file mode 120000 index 00000000000..3c1b3533523 --- /dev/null +++ b/paddle/capi/examples/sequence/convert_protobin.sh @@ -0,0 +1 @@ +../dense/convert_protobin.sh \ No newline at end of file diff --git a/paddle/capi/examples/sequence/main.c b/paddle/capi/examples/sequence/main.c new file mode 100644 index 00000000000..7e71bb8b8af --- /dev/null +++ b/paddle/capi/examples/sequence/main.c @@ -0,0 +1,63 @@ +#include +#include +#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 ids. + int sentence_ids[] = {83, 48, 20, 84, 394, 853, 64, 53, 64}; + + paddle_ivector sentence = paddle_ivector_create( + sentence_ids, sizeof(sentence_ids) / sizeof(int), false, false); + CHECK(paddle_arguments_set_ids(in_args, 0, sentence)); + + int seq_pos_array[] = {0, sizeof(sentence_ids) / sizeof(int)}; + + paddle_ivector seq_pos = paddle_ivector_create( + seq_pos_array, sizeof(seq_pos_array) / sizeof(int), false, false); + + CHECK(paddle_arguments_set_sequence_start_pos(in_args, 0, 0, seq_pos)); + + 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(); + + CHECK(paddle_arguments_value(out_args, 0, prob)); + + paddle_real* array; + + CHECK(paddle_matrix_get_row(prob, 0, &array)); + + printf("Prob: "); + for (int i = 0; i < 2; ++i) { + printf("%.2f ", array[i]); + } + printf("\n"); + + return 0; +} diff --git a/paddle/capi/examples/sequence/trainer_config.py b/paddle/capi/examples/sequence/trainer_config.py new file mode 100644 index 00000000000..6bbc7a909aa --- /dev/null +++ b/paddle/capi/examples/sequence/trainer_config.py @@ -0,0 +1,13 @@ +from paddle.trainer_config_helpers import * + +WORD_DIM = 3000 + +sentence = data_layer(name='sentence', size=WORD_DIM) +sentence_embedding = embedding_layer( + input=sentence, + size=64, + param_attr=ParameterAttribute( + initial_max=1.0, initial_min=0.5)) +lstm = simple_lstm(input=sentence_embedding, size=64) +lstm_last = last_seq(input=lstm) +outputs(fc_layer(input=lstm_last, size=2, act=SoftmaxActivation())) -- GitLab