Skip to content

Commit 34b3ee3

Browse files
committed
Add sequence exampleAdd sequence exampleAdd sequence exampleAdd sequence
exampleAdd sequence exampleAdd sequence exampleAdd sequence exampleAdd sequence exampleAdd sequence example
1 parent 470bbcf commit 34b3ee3

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# This file is used to ignore files which are generated
2+
# ----------------------------------------------------------------------------
3+
4+
*~
5+
*.autosave
6+
*.a
7+
*.core
8+
*.moc
9+
*.o
10+
*.obj
11+
*.orig
12+
*.rej
13+
*.so
14+
*.so.*
15+
*_pch.h.cpp
16+
*_resource.rc
17+
*.qm
18+
.#*
19+
*.*#
20+
core
21+
!core/
22+
tags
23+
.DS_Store
24+
.directory
25+
*.debug
26+
Makefile*
27+
*.prl
28+
*.app
29+
moc_*.cpp
30+
ui_*.h
31+
qrc_*.cpp
32+
Thumbs.db
33+
*.res
34+
*.rc
35+
/.qmake.cache
36+
/.qmake.stash
37+
38+
# qtcreator generated files
39+
*.pro.user*
40+
41+
# xemacs temporary files
42+
*.flc
43+
44+
# Vim temporary files
45+
.*.swp
46+
47+
# Visual Studio generated files
48+
*.ib_pdb_index
49+
*.idb
50+
*.ilk
51+
*.pdb
52+
*.sln
53+
*.suo
54+
*.vcproj
55+
*vcproj.*.*.user
56+
*.ncb
57+
*.sdf
58+
*.opensdf
59+
*.vcxproj
60+
*vcxproj.*
61+
62+
# MinGW generated files
63+
*.Debug
64+
*.Release
65+
66+
# Python byte code
67+
*.pyc
68+
69+
# Binaries
70+
# --------
71+
*.dll
72+
*.exe
73+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
project(sequence)
2+
cmake_minimum_required(VERSION 2.8)
3+
aux_source_directory(. SRC_LIST)
4+
add_executable(${PROJECT_NAME} ${SRC_LIST})
5+
set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 99)
6+
target_link_libraries(${PROJECT_NAME} -lpaddle_capi_shared)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../dense/convert_protobin.sh
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <paddle/capi.h>
2+
#include <time.h>
3+
#include "../common/common.h"
4+
5+
#define CONFIG_BIN "./trainer_config.bin"
6+
7+
int main() {
8+
// Initalize Paddle
9+
char* argv[] = {"--use_gpu=False"};
10+
CHECK(paddle_init(1, (char**)argv));
11+
12+
// Reading config binary file. It is generated by `convert_protobin.sh`
13+
long size;
14+
void* buf = read_config(CONFIG_BIN, &size);
15+
16+
// Create a gradient machine for inference.
17+
paddle_gradient_machine machine;
18+
CHECK(paddle_gradient_machine_create_for_inference(&machine, buf, (int)size));
19+
CHECK(paddle_gradient_machine_randomize_param(machine));
20+
21+
// Loading parameter. Uncomment the following line and change the directory.
22+
// CHECK(paddle_gradient_machine_load_parameter_from_disk(machine,
23+
// "./some_where_to_params"));
24+
paddle_arguments in_args = paddle_arguments_create_none();
25+
26+
// There is only one input of this network.
27+
CHECK(paddle_arguments_resize(in_args, 1));
28+
29+
// Create input ids.
30+
int sentence_ids[] = {83, 48, 20, 84, 394, 853, 64, 53, 64};
31+
32+
paddle_ivector sentence = paddle_ivector_create(
33+
sentence_ids, sizeof(sentence_ids) / sizeof(int), false, false);
34+
CHECK(paddle_arguments_set_ids(in_args, 0, sentence));
35+
36+
int seq_pos_array[] = {0, sizeof(sentence_ids) / sizeof(int)};
37+
38+
paddle_ivector seq_pos = paddle_ivector_create(
39+
seq_pos_array, sizeof(seq_pos_array) / sizeof(int), false, false);
40+
41+
CHECK(paddle_arguments_set_sequence_start_pos(in_args, 0, 0, seq_pos));
42+
43+
paddle_arguments out_args = paddle_arguments_create_none();
44+
CHECK(paddle_gradient_machine_forward(machine,
45+
in_args,
46+
out_args,
47+
/* isTrain */ false));
48+
paddle_matrix prob = paddle_matrix_create_none();
49+
50+
CHECK(paddle_arguments_value(out_args, 0, prob));
51+
52+
paddle_real* array;
53+
54+
CHECK(paddle_matrix_get_row(prob, 0, &array));
55+
56+
printf("Prob: ");
57+
for (int i = 0; i < 2; ++i) {
58+
printf("%.2f ", array[i]);
59+
}
60+
printf("\n");
61+
62+
return 0;
63+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from paddle.trainer_config_helpers import *
2+
3+
WORD_DIM = 3000
4+
5+
sentence = data_layer(name='sentence', size=WORD_DIM)
6+
sentence_embedding = embedding_layer(
7+
input=sentence,
8+
size=64,
9+
param_attr=ParameterAttribute(
10+
initial_max=1.0, initial_min=0.5))
11+
lstm = simple_lstm(input=sentence_embedding, size=64)
12+
lstm_last = last_seq(input=lstm)
13+
outputs(fc_layer(input=lstm_last, size=2, act=SoftmaxActivation()))

0 commit comments

Comments
 (0)