|
19 | 19 | TRAIN_FILES = ['train.recordio']
|
20 | 20 | TEST_FILES = ['test.recordio']
|
21 | 21 |
|
22 |
| -DICT_DIM = 89528 |
| 22 | +DICT_DIM = 5147 |
23 | 23 |
|
24 | 24 | # embedding dim
|
25 | 25 | emb_dim = 128
|
|
33 | 33 | # class num
|
34 | 34 | class_dim = 2
|
35 | 35 |
|
| 36 | +# epoch num |
| 37 | +epoch_num = 10 |
36 | 38 |
|
37 |
| -def network_cfg(is_train, pass_num=100): |
38 |
| - with fluid.unique_name.guard(): |
39 |
| - train_file_obj = fluid.layers.open_files( |
40 |
| - filenames=TRAIN_FILES, |
41 |
| - pass_num=pass_num, |
42 |
| - shapes=[[-1, 1], [-1, 1]], |
43 |
| - lod_levels=[1, 0], |
44 |
| - dtypes=['int64', 'int64'], |
45 |
| - thread_num=1) |
46 |
| - |
47 |
| - test_file_obj = fluid.layers.open_files( |
48 |
| - filenames=TEST_FILES, |
49 |
| - pass_num=1, |
50 |
| - shapes=[[-1, 1], [-1, 1]], |
51 |
| - lod_levels=[1, 0], |
52 |
| - dtypes=['int64', 'int64'], |
53 |
| - thread_num=1) |
54 | 39 |
|
55 |
| - if is_train: |
56 |
| - file_obj = fluid.layers.shuffle(train_file_obj, buffer_size=1000) |
57 |
| - else: |
58 |
| - file_obj = test_file_obj |
| 40 | +def build_program(is_train): |
| 41 | + file_obj_handle = fluid.layers.io.open_files( |
| 42 | + filenames=TRAIN_FILES if is_train else TEST_FILES, |
| 43 | + shapes=[[-1, 1], [-1, 1]], |
| 44 | + lod_levels=[1, 0], |
| 45 | + dtypes=['int64', 'int64'], |
| 46 | + thread_num=1) |
| 47 | + if is_train: |
| 48 | + file_obj = fluid.layers.io.shuffle(file_obj_handle, buffer_size=1000) |
| 49 | + else: |
| 50 | + file_obj = file_obj_handle |
| 51 | + file_obj = fluid.layers.io.double_buffer(file_obj) |
59 | 52 |
|
60 |
| - file_obj = fluid.layers.double_buffer( |
61 |
| - file_obj, |
62 |
| - name="train_double_buffer" if is_train else 'test_double_buffer') |
| 53 | + with fluid.unique_name.guard(): |
63 | 54 |
|
64 | 55 | data, label = fluid.layers.read_file(file_obj)
|
65 | 56 |
|
@@ -90,58 +81,64 @@ def network_cfg(is_train, pass_num=100):
|
90 | 81 |
|
91 | 82 | if is_train:
|
92 | 83 | # SGD optimizer
|
93 |
| - sgd_optimizer = fluid.optimizer.Adagrad(learning_rate=0.01) |
| 84 | + sgd_optimizer = fluid.optimizer.Adagrad(learning_rate=0.001) |
94 | 85 | sgd_optimizer.minimize(avg_cost)
|
95 | 86 |
|
96 |
| - return { |
97 |
| - 'loss': avg_cost, |
98 |
| - 'log': [avg_cost, acc], |
99 |
| - 'file': train_file_obj if is_train else test_file_obj |
100 |
| - } |
| 87 | + return {'loss': avg_cost, 'log': [avg_cost, acc], 'file': file_obj_handle} |
101 | 88 |
|
102 | 89 |
|
103 | 90 | def main():
|
104 | 91 | train = fluid.Program()
|
105 | 92 | startup = fluid.Program()
|
| 93 | + test = fluid.Program() |
106 | 94 |
|
107 | 95 | with fluid.program_guard(train, startup):
|
108 |
| - train_args = network_cfg(is_train=True) |
109 |
| - |
110 |
| - test = fluid.Program() |
| 96 | + train_args = build_program(is_train=True) |
111 | 97 |
|
112 |
| - with fluid.program_guard(test, fluid.Program()): |
113 |
| - test_args = network_cfg(is_train=False) |
| 98 | + with fluid.program_guard(test, startup): |
| 99 | + test_args = build_program(is_train=False) |
114 | 100 |
|
| 101 | + use_cuda = fluid.core.is_compiled_with_cuda() |
115 | 102 | # startup
|
116 |
| - place = fluid.CUDAPlace(0) |
| 103 | + place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() |
117 | 104 | exe = fluid.Executor(place=place)
|
118 | 105 | exe.run(startup)
|
119 | 106 |
|
120 | 107 | train_exe = fluid.ParallelExecutor(
|
121 |
| - use_cuda=True, loss_name=train_args['loss'].name, main_program=train) |
| 108 | + use_cuda=use_cuda, |
| 109 | + loss_name=train_args['loss'].name, |
| 110 | + main_program=train) |
| 111 | + test_exe = fluid.ParallelExecutor( |
| 112 | + use_cuda=use_cuda, main_program=test, share_vars_from=train_exe) |
122 | 113 |
|
123 | 114 | fetch_var_list = [var.name for var in train_args['log']]
|
124 |
| - for i in xrange(sys.maxint): |
125 |
| - result = map(numpy.array, |
126 |
| - train_exe.run(fetch_list=fetch_var_list |
127 |
| - if i % 1000 == 0 else [])) |
128 |
| - if len(result) != 0: |
129 |
| - print 'Train: ', result |
130 |
| - |
131 |
| - if i % 1000 == 0: |
132 |
| - test_exe = fluid.ParallelExecutor( |
133 |
| - use_cuda=True, main_program=test, share_vars_from=train_exe) |
134 |
| - loss = [] |
135 |
| - acc = [] |
136 |
| - try: |
137 |
| - while True: |
138 |
| - loss_np, acc_np = map( |
139 |
| - numpy.array, test_exe.run(fetch_list=fetch_var_list)) |
140 |
| - loss.append(loss_np[0]) |
141 |
| - acc.append(acc_np[0]) |
142 |
| - except: |
143 |
| - test_args['file'].reset() |
144 |
| - print 'TEST: ', numpy.mean(loss), numpy.mean(acc) |
| 115 | + for epoch_id in range(epoch_num): |
| 116 | + # train |
| 117 | + try: |
| 118 | + batch_id = 0 |
| 119 | + while True: |
| 120 | + result = map(numpy.array, |
| 121 | + train_exe.run(fetch_list=fetch_var_list |
| 122 | + if batch_id % 10 == 0 else [])) |
| 123 | + if len(result) != 0: |
| 124 | + print 'Train loss: ', result |
| 125 | + batch_id += 1 |
| 126 | + except fluid.core.EOFException: |
| 127 | + print 'End of epoch', epoch_id |
| 128 | + train_args['file'].reset() |
| 129 | + |
| 130 | + # test |
| 131 | + loss = [] |
| 132 | + acc = [] |
| 133 | + try: |
| 134 | + while True: |
| 135 | + loss_np, acc_np = map(numpy.array, |
| 136 | + test_exe.run(fetch_list=fetch_var_list)) |
| 137 | + loss.append(loss_np[0]) |
| 138 | + acc.append(acc_np[0]) |
| 139 | + except: |
| 140 | + test_args['file'].reset() |
| 141 | + print 'TEST: ', numpy.mean(loss), numpy.mean(acc) |
145 | 142 |
|
146 | 143 |
|
147 | 144 | if __name__ == '__main__':
|
|
0 commit comments