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