@@ -102,37 +102,79 @@ def setUpClass(cls):
102
102
fluid .recordio_writer .convert_reader_to_recordio_file (
103
103
MNIST_RECORDIO_FILE , reader , feeder )
104
104
105
+ def _init_data (self , random = True ):
106
+ np .random .seed (5 )
107
+ if random :
108
+ img = np .random .random (size = [32 , 784 ]).astype (np .float32 )
109
+ else :
110
+ img = np .ones (shape = [32 , 784 ], dtype = 'float32' )
111
+ label = np .ones (shape = [32 , 1 ], dtype = 'int64' )
112
+ return img , label
113
+
114
+ # simple_fc
105
115
def check_simple_fc_convergence (self , use_cuda , use_reduce = False ):
106
116
if use_cuda and not core .is_compiled_with_cuda ():
107
117
return
108
118
self .check_network_convergence (simple_fc_net , use_cuda = use_cuda )
109
119
self .check_network_convergence (
110
120
simple_fc_net , use_cuda = use_cuda , allow_op_delay = True )
111
121
112
- img = np . zeros ( shape = [ 32 , 784 ], dtype = 'float32' )
113
- label = np . ones ( shape = [ 32 , 1 ], dtype = 'int64' )
122
+ img , label = self . _init_data ( )
123
+
114
124
self .check_network_convergence (
115
125
simple_fc_net ,
116
126
feed_dict = {"image" : img ,
117
127
"label" : label },
118
128
use_cuda = use_cuda ,
119
129
use_reduce = use_reduce )
120
130
131
+ def check_simple_fc_convergence_with_Reduce (self , use_cuda ):
132
+ if use_cuda and not core .is_compiled_with_cuda ():
133
+ return
134
+ self .check_network_convergence (
135
+ simple_fc_net , use_cuda = use_cuda , use_reduce = True )
136
+ self .check_network_convergence (
137
+ simple_fc_net ,
138
+ use_cuda = use_cuda ,
139
+ allow_op_delay = True ,
140
+ use_reduce = True )
141
+
142
+ img , label = self ._init_data ()
143
+
144
+ all_reduce_first_loss , all_reduce_last_loss = self .check_network_convergence (
145
+ simple_fc_net ,
146
+ feed_dict = {"image" : img ,
147
+ "label" : label },
148
+ use_cuda = use_cuda ,
149
+ use_reduce = False )
150
+ reduce_first_loss , reduce_last_loss = self .check_network_convergence (
151
+ simple_fc_net ,
152
+ feed_dict = {"image" : img ,
153
+ "label" : label },
154
+ use_cuda = use_cuda ,
155
+ use_reduce = True )
156
+
157
+ for loss in zip (all_reduce_first_loss , reduce_first_loss ):
158
+ self .assertAlmostEquals (loss [0 ], loss [1 ], delta = 1e-6 )
159
+ for loss in zip (all_reduce_last_loss , reduce_last_loss ):
160
+ self .assertAlmostEquals (loss [0 ], loss [1 ], delta = 1e-6 )
161
+
121
162
def test_simple_fc (self ):
122
163
# use_cuda
123
164
self .check_simple_fc_convergence (True )
124
165
self .check_simple_fc_convergence (False )
125
166
126
167
def test_simple_fc_with_new_strategy (self ):
127
168
# use_cuda, use_reduce
128
- self .check_simple_fc_convergence ( True , True )
129
- self .check_simple_fc_convergence (False , True )
169
+ self .check_simple_fc_convergence_with_Reduce ( True )
170
+ self .check_simple_fc_convergence_with_Reduce (False )
130
171
131
- def check_simple_fc_parallel_accuracy (self , use_cuda , use_reduce = False ):
172
+ def check_simple_fc_parallel_accuracy (self , use_cuda ):
132
173
if use_cuda and not core .is_compiled_with_cuda ():
133
174
return
134
- img = np .zeros (shape = [32 , 784 ], dtype = 'float32' )
135
- label = np .ones (shape = [32 , 1 ], dtype = 'int64' )
175
+
176
+ img , label = self ._init_data (random = False )
177
+
136
178
single_first_loss , single_last_loss = self .check_network_convergence (
137
179
method = simple_fc_net ,
138
180
seed = 1000 ,
@@ -146,8 +188,7 @@ def check_simple_fc_parallel_accuracy(self, use_cuda, use_reduce=False):
146
188
feed_dict = {"image" : img ,
147
189
"label" : label },
148
190
use_cuda = use_cuda ,
149
- use_parallel_executor = True ,
150
- use_reduce = use_reduce )
191
+ use_parallel_executor = True )
151
192
152
193
for p_f in parallel_first_loss :
153
194
self .assertAlmostEquals (p_f , single_first_loss [0 ], delta = 1e-6 )
@@ -158,32 +199,53 @@ def test_simple_fc_parallel_accuracy(self):
158
199
self .check_simple_fc_parallel_accuracy (True )
159
200
self .check_simple_fc_parallel_accuracy (False )
160
201
161
- def test_simple_fc_parallel_accuracy_with_new_strategy (self ):
162
- # use_cuda, use_reduce
163
- self .check_simple_fc_parallel_accuracy (True , True )
164
- self .check_simple_fc_parallel_accuracy (False , True )
165
-
166
- def check_batchnorm_fc_convergence (self , use_cuda , use_reduce = False ):
202
+ def check_batchnorm_fc_convergence (self , use_cuda ):
167
203
if use_cuda and not core .is_compiled_with_cuda ():
168
204
return
205
+
169
206
self .check_network_convergence (fc_with_batchnorm , use_cuda = use_cuda )
170
- img = np .zeros (shape = [32 , 784 ], dtype = 'float32' )
171
- label = np .ones (shape = [32 , 1 ], dtype = 'int64' )
207
+
208
+ img , label = self ._init_data ()
209
+
210
+ self .check_network_convergence (
211
+ fc_with_batchnorm ,
212
+ feed_dict = {"image" : img ,
213
+ "label" : label },
214
+ use_cuda = use_cuda )
215
+
216
+ def check_batchnorm_fc_convergence_use_reduce (self , use_cuda ):
217
+ if use_cuda and not core .is_compiled_with_cuda ():
218
+ return
172
219
self .check_network_convergence (
220
+ fc_with_batchnorm , use_cuda = use_cuda , use_reduce = True )
221
+
222
+ img , label = self ._init_data ()
223
+
224
+ all_reduce_first_loss , all_reduce_last_loss = self .check_network_convergence (
173
225
fc_with_batchnorm ,
174
226
feed_dict = {"image" : img ,
175
227
"label" : label },
176
228
use_cuda = use_cuda ,
177
- use_reduce = use_reduce )
229
+ use_reduce = False )
230
+ reduce_first_loss , reduce_last_loss = self .check_network_convergence (
231
+ fc_with_batchnorm ,
232
+ feed_dict = {"image" : img ,
233
+ "label" : label },
234
+ use_cuda = use_cuda ,
235
+ use_reduce = True )
236
+
237
+ for loss in zip (all_reduce_first_loss , reduce_first_loss ):
238
+ self .assertAlmostEquals (loss [0 ], loss [1 ], delta = 1e-6 )
239
+ for loss in zip (all_reduce_last_loss , reduce_last_loss ):
240
+ self .assertAlmostEquals (loss [0 ], loss [1 ], delta = 1e-4 )
178
241
179
242
def test_batchnorm_fc (self ):
180
243
self .check_batchnorm_fc_convergence (True )
181
244
self .check_batchnorm_fc_convergence (False )
182
245
183
246
def test_batchnorm_fc_with_new_strategy (self ):
184
- # use_cuda, use_reduce
185
- self .check_batchnorm_fc_convergence (True , True )
186
- self .check_batchnorm_fc_convergence (False , True )
247
+ self .check_batchnorm_fc_convergence_use_reduce (True )
248
+ self .check_batchnorm_fc_convergence_use_reduce (False )
187
249
188
250
189
251
if __name__ == '__main__' :
0 commit comments