Skip to content

Commit 212f6ea

Browse files
committed
modify the test config for test_CompareTwoNets.cpp
1 parent 3654e1e commit 212f6ea

File tree

7 files changed

+142
-346
lines changed

7 files changed

+142
-346
lines changed

paddle/gserver/tests/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,12 @@ if(NOT ON_TRAVIS)
111111
${CMAKE_CURRENT_BINARY_DIR}/test_CompareSparse
112112
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle/)
113113
endif()
114+
115+
################ test_CompareTwoNets ######################
116+
add_unittest_without_exec(test_CompareTwoNets
117+
test_CompareTwoNets.cpp)
118+
add_test(NAME test_CompareTwoNets
119+
COMMAND ${PADDLE_SOURCE_DIR}/paddle/.set_python_path.sh -d
120+
${PADDLE_SOURCE_DIR}/python:${PADDLE_SOURCE_DIR}/paddle/gserver/tests
121+
${CMAKE_CURRENT_BINARY_DIR}/test_CompareTwoNets
122+
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle/)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from paddle.trainer_config_helpers import *
17+
18+
######################## data source ################################
19+
dict_path = 'gserver/tests/Sequence/tour_dict_phrase.dict'
20+
dict_file = dict()
21+
for line_count, line in enumerate(open(dict_path, "r")):
22+
dict_file[line.strip()] = line_count
23+
24+
define_py_data_sources2(
25+
train_list='gserver/tests/Sequence/train.list',
26+
test_list=None,
27+
module='sequenceGen',
28+
obj='process',
29+
args={"dict_file": dict_file})
30+
31+
settings(batch_size=5)
32+
######################## network configure ################################
33+
dict_dim = len(open(dict_path, 'r').readlines())
34+
word_dim = 128
35+
hidden_dim = 128
36+
label_dim = 3
37+
38+
# This config is designed to be equivalent with sequence_recurrent_group.py
39+
40+
data = data_layer(name="word", size=dict_dim)
41+
42+
emb = embedding_layer(
43+
input=data, size=word_dim, param_attr=ParamAttr(name="emb"))
44+
45+
recurrent = recurrent_layer(input=emb, bias_attr=False, act=SoftmaxActivation())
46+
47+
recurrent_last = last_seq(input=recurrent)
48+
49+
with mixed_layer(
50+
size=label_dim, act=SoftmaxActivation(), bias_attr=True) as output:
51+
output += full_matrix_projection(input=recurrent_last)
52+
53+
outputs(
54+
classification_cost(
55+
input=output, label=data_layer(
56+
name="label", size=1)))
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python
2+
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from paddle.trainer_config_helpers import *
17+
18+
######################## data source ################################
19+
dict_path = 'gserver/tests/Sequence/tour_dict_phrase.dict'
20+
dict_file = dict()
21+
for line_count, line in enumerate(open(dict_path, "r")):
22+
dict_file[line.strip()] = line_count
23+
24+
define_py_data_sources2(
25+
train_list='gserver/tests/Sequence/train.list',
26+
test_list=None,
27+
module='sequenceGen',
28+
obj='process',
29+
args={"dict_file": dict_file})
30+
31+
settings(batch_size=5)
32+
######################## network configure ################################
33+
dict_dim = len(open(dict_path, 'r').readlines())
34+
word_dim = 128
35+
hidden_dim = 128
36+
label_dim = 3
37+
38+
# This config is designed to be equivalent with sequence_recurrent.py
39+
40+
data = data_layer(name="word", size=dict_dim)
41+
42+
emb = embedding_layer(
43+
input=data, size=word_dim, param_attr=ParamAttr(name="emb"))
44+
45+
46+
def step(y):
47+
mem = memory(name="rnn_state", size=hidden_dim)
48+
with mixed_layer(
49+
name="rnn_state",
50+
size=hidden_dim,
51+
bias_attr=False,
52+
act=SoftmaxActivation()) as out:
53+
out += identity_projection(input=y)
54+
out += full_matrix_projection(
55+
input=mem, param_attr=ParamAttr(name="___recurrent_layer_0__"))
56+
return out
57+
58+
59+
recurrent = recurrent_group(name="rnn", step=step, input=emb)
60+
61+
recurrent_last = last_seq(input=recurrent)
62+
63+
with mixed_layer(
64+
size=label_dim, act=SoftmaxActivation(), bias_attr=True) as output:
65+
output += full_matrix_projection(input=recurrent_last)
66+
67+
outputs(
68+
classification_cost(
69+
input=output, label=data_layer(
70+
name="label", size=1)))

paddle/trainer/tests/test_CompareTwoNets.cpp renamed to paddle/gserver/tests/test_CompareTwoNets.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ DECLARE_bool(use_gpu);
3030
DECLARE_string(config);
3131
DECLARE_string(nics);
3232

33-
DEFINE_string(config_file_a, "", "config of one network to compare");
34-
DEFINE_string(config_file_b, "", "config of another network to compare");
3533
DEFINE_bool(need_high_accuracy,
3634
false,
3735
"whether need to run in double accuracy");
@@ -42,6 +40,10 @@ DEFINE_double(
4240
DECLARE_bool(thread_local_rand_use_global_seed);
4341
DECLARE_int32(seed);
4442

43+
static const string& config_file_a = "gserver/tests/sequence_recurrent.py";
44+
static const string& config_file_b =
45+
"gserver/tests/sequence_recurrent_group.py";
46+
4547
struct ComData {
4648
vector<Argument> outArgs;
4749
vector<ParameterPtr> parameters;
@@ -66,6 +68,7 @@ void calcGradient(ComData& data, const string configFile) {
6668
DataBatch dataBatch;
6769
int32_t batchSize = trainer.getConfig().opt_config().batch_size();
6870

71+
trainer.getDataProvider()->reset();
6972
trainer.getDataProvider()->setSkipShuffle();
7073
trainer.getDataProvider()->getNextBatch(batchSize, &dataBatch);
7174

@@ -167,11 +170,11 @@ void compareGradient(ComData& comDataA, ComData& comDataB) {
167170

168171
TEST(Trainer, create) {
169172
ComData dataA;
170-
calcGradient(dataA, FLAGS_config_file_a);
173+
calcGradient(dataA, config_file_a);
171174
LOG(INFO) << "\n\nforwardBackward of Network A is finished\n\n";
172175

173176
ComData dataB;
174-
calcGradient(dataB, FLAGS_config_file_b);
177+
calcGradient(dataB, config_file_b);
175178
LOG(INFO) << "\n\nforwardBackward of the Network B is finished\n\n";
176179

177180
compareGradient(dataA, dataB);

paddle/trainer/tests/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ if(WITH_PYTHON)
2828
${PADDLE_SOURCE_DIR}/paddle/.set_port.sh -p port ${CMAKE_CURRENT_BINARY_DIR}/test_TrainerOnePass
2929
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle/)
3030
endif()
31-
################ test_CompareTwoNets ######################
32-
add_unittest_without_exec(test_CompareTwoNets
33-
test_CompareTwoNets.cpp)
34-
add_test(NAME test_CompareTwoNets
35-
COMMAND ${PADDLE_SOURCE_DIR}/paddle/.set_python_path.sh -d ${PADDLE_SOURCE_DIR}/python/
36-
${CMAKE_CURRENT_BINARY_DIR}/test_CompareTwoNets
37-
--config_file_a=trainer/tests/sample_trainer_config_qb_rnn.conf --config_file_b=trainer/tests/sample_trainer_config_rnn.conf
38-
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle/)
3931

4032
############### test_CompareTwoOpts ###################
4133
add_unittest_without_exec(test_CompareTwoOpts

paddle/trainer/tests/sample_trainer_config_qb_rnn.conf

Lines changed: 0 additions & 154 deletions
This file was deleted.

0 commit comments

Comments
 (0)