|
|
|
@ -18,6 +18,7 @@ import numpy as np
|
|
|
|
|
import paddle
|
|
|
|
|
import paddle.dataset.mnist as mnist
|
|
|
|
|
import unittest
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
MNIST_RECORDIO_FILE = "./mnist_test_pe.recordio"
|
|
|
|
|
|
|
|
|
@ -85,6 +86,7 @@ def fc_with_batchnorm(use_feed):
|
|
|
|
|
class TestMNIST(TestParallelExecutorBase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
|
|
|
|
os.environ['CPU_NUM'] = str(4)
|
|
|
|
|
# Convert mnist to recordio file
|
|
|
|
|
with fluid.program_guard(fluid.Program(), fluid.Program()):
|
|
|
|
|
reader = paddle.batch(mnist.train(), batch_size=4)
|
|
|
|
@ -99,9 +101,12 @@ class TestMNIST(TestParallelExecutorBase):
|
|
|
|
|
fluid.recordio_writer.convert_reader_to_recordio_file(
|
|
|
|
|
MNIST_RECORDIO_FILE, reader, feeder)
|
|
|
|
|
|
|
|
|
|
def check_simple_fc_convergence(self, balance_parameter_opt_between_cards):
|
|
|
|
|
self.check_network_convergence(simple_fc_net)
|
|
|
|
|
self.check_network_convergence(simple_fc_net, allow_op_delay=True)
|
|
|
|
|
def check_simple_fc_convergence(self,
|
|
|
|
|
balance_parameter_opt_between_cards,
|
|
|
|
|
use_cuda=True):
|
|
|
|
|
self.check_network_convergence(simple_fc_net, use_cuda=use_cuda)
|
|
|
|
|
self.check_network_convergence(
|
|
|
|
|
simple_fc_net, use_cuda=use_cuda, allow_op_delay=True)
|
|
|
|
|
|
|
|
|
|
img = np.zeros(shape=[32, 784], dtype='float32')
|
|
|
|
|
label = np.ones(shape=[32, 1], dtype='int64')
|
|
|
|
@ -109,17 +114,21 @@ class TestMNIST(TestParallelExecutorBase):
|
|
|
|
|
simple_fc_net,
|
|
|
|
|
feed_dict={"image": img,
|
|
|
|
|
"label": label},
|
|
|
|
|
use_cuda=use_cuda,
|
|
|
|
|
balance_parameter_opt_between_cards=balance_parameter_opt_between_cards
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_simple_fc(self):
|
|
|
|
|
self.check_simple_fc_convergence(False)
|
|
|
|
|
self.check_simple_fc_convergence(False, use_cuda=True)
|
|
|
|
|
self.check_simple_fc_convergence(False, use_cuda=False)
|
|
|
|
|
|
|
|
|
|
def test_simple_fc_with_new_strategy(self):
|
|
|
|
|
self.check_simple_fc_convergence(True)
|
|
|
|
|
self.check_simple_fc_convergence(True, use_cuda=True)
|
|
|
|
|
self.check_simple_fc_convergence(True, use_cuda=False)
|
|
|
|
|
|
|
|
|
|
def check_simple_fc_parallel_accuracy(self,
|
|
|
|
|
balance_parameter_opt_between_cards):
|
|
|
|
|
balance_parameter_opt_between_cards,
|
|
|
|
|
use_cuda=True):
|
|
|
|
|
img = np.zeros(shape=[32, 784], dtype='float32')
|
|
|
|
|
label = np.ones(shape=[32, 1], dtype='int64')
|
|
|
|
|
single_first_loss, single_last_loss = self.check_network_convergence(
|
|
|
|
@ -127,12 +136,14 @@ class TestMNIST(TestParallelExecutorBase):
|
|
|
|
|
seed=1000,
|
|
|
|
|
feed_dict={"image": img,
|
|
|
|
|
"label": label},
|
|
|
|
|
use_cuda=use_cuda,
|
|
|
|
|
use_parallel_executor=False)
|
|
|
|
|
parallel_first_loss, parallel_last_loss = self.check_network_convergence(
|
|
|
|
|
method=simple_fc_net,
|
|
|
|
|
seed=1000,
|
|
|
|
|
feed_dict={"image": img,
|
|
|
|
|
"label": label},
|
|
|
|
|
use_cuda=use_cuda,
|
|
|
|
|
use_parallel_executor=True,
|
|
|
|
|
balance_parameter_opt_between_cards=balance_parameter_opt_between_cards
|
|
|
|
|
)
|
|
|
|
@ -143,28 +154,33 @@ class TestMNIST(TestParallelExecutorBase):
|
|
|
|
|
self.assertAlmostEquals(p_l, single_last_loss[0], delta=1e-6)
|
|
|
|
|
|
|
|
|
|
def test_simple_fc_parallel_accuracy(self):
|
|
|
|
|
self.check_simple_fc_parallel_accuracy(False)
|
|
|
|
|
self.check_simple_fc_parallel_accuracy(False, use_cuda=True)
|
|
|
|
|
self.check_simple_fc_parallel_accuracy(False, use_cuda=False)
|
|
|
|
|
|
|
|
|
|
def test_simple_fc_parallel_accuracy_with_new_strategy(self):
|
|
|
|
|
self.check_simple_fc_parallel_accuracy(True)
|
|
|
|
|
self.check_simple_fc_parallel_accuracy(True, use_cuda=True)
|
|
|
|
|
self.check_simple_fc_parallel_accuracy(True, use_cuda=False)
|
|
|
|
|
|
|
|
|
|
def check_batchnorm_fc_convergence(self,
|
|
|
|
|
balance_parameter_opt_between_cards):
|
|
|
|
|
self.check_network_convergence(fc_with_batchnorm)
|
|
|
|
|
def check_batchnorm_fc_convergence(
|
|
|
|
|
self, balance_parameter_opt_between_cards, use_cuda):
|
|
|
|
|
self.check_network_convergence(fc_with_batchnorm, use_cuda=use_cuda)
|
|
|
|
|
img = np.zeros(shape=[32, 784], dtype='float32')
|
|
|
|
|
label = np.ones(shape=[32, 1], dtype='int64')
|
|
|
|
|
self.check_network_convergence(
|
|
|
|
|
fc_with_batchnorm,
|
|
|
|
|
feed_dict={"image": img,
|
|
|
|
|
"label": label},
|
|
|
|
|
use_cuda=use_cuda,
|
|
|
|
|
balance_parameter_opt_between_cards=balance_parameter_opt_between_cards
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_batchnorm_fc(self):
|
|
|
|
|
self.check_batchnorm_fc_convergence(False)
|
|
|
|
|
self.check_batchnorm_fc_convergence(False, use_cuda=True)
|
|
|
|
|
self.check_batchnorm_fc_convergence(False, use_cuda=False)
|
|
|
|
|
|
|
|
|
|
def test_batchnorm_fc_with_new_strategy(self):
|
|
|
|
|
self.check_batchnorm_fc_convergence(True)
|
|
|
|
|
self.check_batchnorm_fc_convergence(True, use_cuda=True)
|
|
|
|
|
self.check_batchnorm_fc_convergence(True, use_cuda=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|