12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import numpy
15
+ import numpy as np
16
16
import unittest
17
17
18
18
import paddle .fluid as fluid
@@ -243,7 +243,7 @@ def run_executor(exe, feed, fetch_list, program=None):
243
243
begin = time .time ()
244
244
first_loss , = run_executor (
245
245
exe = exe , feed = feed_dict , fetch_list = [loss .name ])
246
- first_loss = numpy .array (first_loss )
246
+ first_loss = np .array (first_loss )
247
247
248
248
for i in xrange (iter ):
249
249
run_executor (exe = exe , feed = feed_dict , fetch_list = [])
@@ -256,7 +256,7 @@ def run_executor(exe, feed, fetch_list, program=None):
256
256
print "%.4f Instance per second" % (
257
257
(batch_size * iter + 2 ) / (end - begin ))
258
258
259
- last_loss = numpy .array (last_loss )
259
+ last_loss = np .array (last_loss )
260
260
261
261
print first_loss , last_loss
262
262
# self.assertGreater(first_loss[0], last_loss[0])
@@ -284,8 +284,8 @@ def check_simple_fc_convergence(self):
284
284
self .check_network_convergence (simple_fc_net )
285
285
self .check_network_convergence (simple_fc_net , allow_op_delay = True )
286
286
287
- img = numpy .zeros (shape = [32 , 784 ], dtype = 'float32' )
288
- label = numpy .ones (shape = [32 , 1 ], dtype = 'int64' )
287
+ img = np .zeros (shape = [32 , 784 ], dtype = 'float32' )
288
+ label = np .ones (shape = [32 , 1 ], dtype = 'int64' )
289
289
self .check_network_convergence (
290
290
simple_fc_net , feed_dict = {"image" : img ,
291
291
"label" : label })
@@ -294,8 +294,8 @@ def test_simple_fc(self):
294
294
self .check_simple_fc_convergence ()
295
295
296
296
def check_simple_fc_parallel_accuracy (self ):
297
- img = numpy .zeros (shape = [32 , 784 ], dtype = 'float32' )
298
- label = numpy .ones (shape = [32 , 1 ], dtype = 'int64' )
297
+ img = np .zeros (shape = [32 , 784 ], dtype = 'float32' )
298
+ label = np .ones (shape = [32 , 1 ], dtype = 'int64' )
299
299
single_first_loss , single_last_loss = self .check_network_convergence (
300
300
method = simple_fc_net ,
301
301
seed = 1000 ,
@@ -319,8 +319,8 @@ def test_simple_fc_parallel_accuracy(self):
319
319
320
320
def check_batchnorm_fc_convergence (self ):
321
321
self .check_network_convergence (fc_with_batchnorm )
322
- img = numpy .zeros (shape = [32 , 784 ], dtype = 'float32' )
323
- label = numpy .ones (shape = [32 , 1 ], dtype = 'int64' )
322
+ img = np .zeros (shape = [32 , 784 ], dtype = 'float32' )
323
+ label = np .ones (shape = [32 , 1 ], dtype = 'int64' )
324
324
self .check_network_convergence (
325
325
fc_with_batchnorm , feed_dict = {"image" : img ,
326
326
"label" : label })
@@ -404,9 +404,6 @@ class ModelHyperParams(object):
404
404
dropout = 0.1
405
405
406
406
407
- import numpy as np
408
-
409
-
410
407
def prepare_batch_input (insts , src_pad_idx , trg_pad_idx , n_head ):
411
408
"""
412
409
Pad the instances to the max sequence length in batch, and generate the
@@ -533,9 +530,8 @@ def check_network_convergence(self):
533
530
opt .minimize (loss )
534
531
535
532
batch_size = 32
536
- image = numpy .random .normal (size = (batch_size ,
537
- 784 )).astype ('float32' )
538
- label = numpy .random .randint (0 , 10 , (batch_size , 1 ), dtype = "int64" )
533
+ image = np .random .normal (size = (batch_size , 784 )).astype ('float32' )
534
+ label = np .random .randint (0 , 10 , (batch_size , 1 ), dtype = "int64" )
539
535
540
536
place = fluid .CUDAPlace (0 )
541
537
exe = fluid .Executor (place )
@@ -552,12 +548,12 @@ def check_network_convergence(self):
552
548
553
549
for i in xrange (5 ):
554
550
test_loss , = test_exe .run ([loss .name ], feed = feed_dict )
555
- test_loss = numpy .array (test_loss )
551
+ test_loss = np .array (test_loss )
556
552
557
553
train_loss , = train_exe .run ([loss .name ], feed = feed_dict )
558
- train_loss = numpy .array (train_loss )
554
+ train_loss = np .array (train_loss )
559
555
self .assertTrue (
560
- numpy .allclose (
556
+ np .allclose (
561
557
train_loss , test_loss , atol = 1e-8 ),
562
558
"Train loss: " + str (train_loss ) + "\n Test loss:" +
563
559
str (test_loss ))
@@ -712,7 +708,7 @@ def check_network_convergence(self, is_sparse):
712
708
data = train_data ()
713
709
for i in xrange (10 ):
714
710
cur_batch = next (data )
715
- print map (numpy .array ,
711
+ print map (np .array ,
716
712
pe .run (feed = feeder .feed (cur_batch ),
717
713
fetch_list = [avg_cost .name ]))[0 ]
718
714
@@ -723,7 +719,7 @@ def test_update_dense_parameter(self):
723
719
self .check_network_convergence (is_sparse = False )
724
720
725
721
726
- # test fetch op
722
+ # test fetch all the variables of global_block
727
723
728
724
import paddle .dataset .flowers as flowers
729
725
@@ -763,7 +759,8 @@ def parallel_exe(self, train_inputs, seed):
763
759
opt .minimize (loss )
764
760
765
761
# TODO(zcd): I found that onece the memory optimizer is open,
766
- # parallel_exe doesn't fetch some variable, such as conv2d_0.b_0@GRAD, conv2d_1.b_0@GRAD.
762
+ # parallel_exe doesn't fetch some variable, such as conv2d_0.b_0@GRAD,
763
+ # conv2d_1.b_0@GRAD. Those variables should not be pruned.
767
764
# fluid.memory_optimize(main)
768
765
769
766
place = fluid .CUDAPlace (0 )
@@ -775,16 +772,15 @@ def parallel_exe(self, train_inputs, seed):
775
772
use_cuda = True , loss_name = loss .name , main_program = main )
776
773
777
774
fetch_list = []
778
- for data in train_inputs :
779
- all_vars = main .global_block ().vars
780
- for k , v in all_vars .iteritems ():
781
- if v .persistable and 'velocity' not in k :
782
- fetch_list .append (k )
775
+ all_vars = main .global_block ().vars
776
+ for k , v in all_vars .iteritems ():
777
+ if 'velocity' not in k :
778
+ fetch_list .append (k )
783
779
780
+ for data in train_inputs :
784
781
ret = pe .run (fetch_list , feed = feeder .feed (data ))
785
- result = {}
786
782
for i in range (len (fetch_list )):
787
- result [ fetch_list [i ]] = np .sum (ret [i ])
783
+ print ( "%s - %s" % ( fetch_list [i ], np .sum (ret [i ])) )
788
784
789
785
def test_update_sparse_parameter (self ):
790
786
tst_reader = paddle .batch (flowers .test (use_xmap = False ), batch_size = 16 )
0 commit comments