Skip to content

Commit 452fe1e

Browse files
committed
init checkin for distributed book chapter 6
1 parent 43dab72 commit 452fe1e

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
from __future__ import print_function
2+
import numpy as np
3+
import paddle.v2 as paddle
4+
import paddle.v2.fluid as fluid
5+
6+
7+
def convolution_net(data, label, input_dim, class_dim=2, emb_dim=32,
8+
hid_dim=32):
9+
emb = fluid.layers.embedding(input=data, size=[input_dim, emb_dim])
10+
conv_3 = fluid.nets.sequence_conv_pool(
11+
input=emb,
12+
num_filters=hid_dim,
13+
filter_size=3,
14+
act="tanh",
15+
pool_type="sqrt")
16+
conv_4 = fluid.nets.sequence_conv_pool(
17+
input=emb,
18+
num_filters=hid_dim,
19+
filter_size=4,
20+
act="tanh",
21+
pool_type="sqrt")
22+
prediction = fluid.layers.fc(input=[conv_3, conv_4],
23+
size=class_dim,
24+
act="softmax")
25+
cost = fluid.layers.cross_entropy(input=prediction, label=label)
26+
avg_cost = fluid.layers.mean(x=cost)
27+
adam_optimizer = fluid.optimizer.Adam(learning_rate=0.002)
28+
optimize_ops, params_grads = adam_optimizer.minimize(avg_cost)
29+
accuracy = fluid.evaluator.Accuracy(input=prediction, label=label)
30+
return avg_cost, accuracy, accuracy.metrics[0], optimize_ops, params_grads
31+
32+
33+
def to_lodtensor(data, place):
34+
seq_lens = [len(seq) for seq in data]
35+
cur_len = 0
36+
lod = [cur_len]
37+
for l in seq_lens:
38+
cur_len += l
39+
lod.append(cur_len)
40+
flattened_data = np.concatenate(data, axis=0).astype("int64")
41+
flattened_data = flattened_data.reshape([len(flattened_data), 1])
42+
res = fluid.LoDTensor()
43+
res.set(flattened_data, place)
44+
res.set_lod([lod])
45+
return res
46+
47+
48+
def main():
49+
BATCH_SIZE = 100
50+
PASS_NUM = 5
51+
52+
word_dict = paddle.dataset.imdb.word_dict()
53+
dict_dim = len(word_dict)
54+
class_dim = 2
55+
56+
data = fluid.layers.data(
57+
name="words", shape=[1], dtype="int64", lod_level=1)
58+
label = fluid.layers.data(name="label", shape=[1], dtype="int64")
59+
cost, accuracy, acc_out, optimize_ops, params_grads = convolution_net(
60+
data, label, input_dim=dict_dim, class_dim=class_dim)
61+
62+
train_data = paddle.batch(
63+
paddle.reader.shuffle(
64+
paddle.dataset.imdb.train(word_dict), buf_size=1000),
65+
batch_size=BATCH_SIZE)
66+
place = fluid.CPUPlace()
67+
exe = fluid.Executor(place)
68+
69+
t = fluid.DistributeTranspiler()
70+
71+
# all parameter server endpoints list for spliting parameters
72+
pserver_endpoints = os.getenv("PSERVERS")
73+
# server endpoint for current node
74+
current_endpoint = os.getenv("SERVER_ENDPOINT")
75+
# run as trainer or parameter server
76+
training_role = os.getenv(
77+
"TRAINING_ROLE", "TRAINER") # get the training role: trainer/pserver
78+
t.transpile(
79+
optimize_ops, params_grads, pservers=pserver_endpoints, trainers=2)
80+
81+
exe.run(fluid.default_startup_program())
82+
83+
if training_role == "PSERVER":
84+
if not current_endpoint:
85+
print("need env SERVER_ENDPOINT")
86+
exit(1)
87+
pserver_prog = t.get_pserver_program(current_endpoint, optimize_ops)
88+
exe.run(pserver_prog)
89+
elif training_role == "TRAINER":
90+
trainer_prog = t.get_trainer_program()
91+
feeder = fluid.DataFeeder(feed_list=[data, label], place=place)
92+
93+
for pass_id in xrange(PASS_NUM):
94+
accuracy.reset(exe)
95+
for data in train_data():
96+
cost_val, acc_val = exe.run(trainer_prog,
97+
feed=feeder.feed(data),
98+
fetch_list=[cost, acc_out])
99+
pass_acc = accuracy.eval(exe)
100+
print("cost=" + str(cost_val) + " acc=" + str(acc_val) +
101+
" pass_acc=" + str(pass_acc))
102+
if cost_val < 1.0 and pass_acc > 0.8:
103+
exit(0)
104+
else:
105+
print("environment var TRAINER_ROLE should be TRAINER os PSERVER")
106+
107+
108+
if __name__ == '__main__':
109+
main()

0 commit comments

Comments
 (0)