|
|
@ -82,13 +82,14 @@ class MNIST(fluid.imperative.Layer):
|
|
|
|
self._simple_img_conv_pool_2 = SimpleImgConvPool(
|
|
|
|
self._simple_img_conv_pool_2 = SimpleImgConvPool(
|
|
|
|
20, 50, 5, 2, 2, act="relu")
|
|
|
|
20, 50, 5, 2, 2, act="relu")
|
|
|
|
|
|
|
|
|
|
|
|
pool_2_shape = 50 * 8 * 8
|
|
|
|
pool_2_shape = 50 * 4 * 4
|
|
|
|
SIZE = 10
|
|
|
|
SIZE = 10
|
|
|
|
scale = (2.0 / (pool_2_shape**2 * SIZE))**0.5
|
|
|
|
scale = (2.0 / (pool_2_shape**2 * SIZE))**0.5
|
|
|
|
self._fc = FC(10,
|
|
|
|
self._fc = FC(10,
|
|
|
|
param_attr=fluid.param_attr.ParamAttr(
|
|
|
|
param_attr=fluid.param_attr.ParamAttr(
|
|
|
|
initializer=fluid.initializer.NormalInitializer(
|
|
|
|
initializer=fluid.initializer.NormalInitializer(
|
|
|
|
loc=0.0, scale=scale)))
|
|
|
|
loc=0.0, scale=scale)),
|
|
|
|
|
|
|
|
act="softmax")
|
|
|
|
|
|
|
|
|
|
|
|
def forward(self, inputs):
|
|
|
|
def forward(self, inputs):
|
|
|
|
x = self._simple_img_conv_pool_1(inputs)
|
|
|
|
x = self._simple_img_conv_pool_1(inputs)
|
|
|
@ -98,9 +99,9 @@ class MNIST(fluid.imperative.Layer):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestImperativeMnist(unittest.TestCase):
|
|
|
|
class TestImperativeMnist(unittest.TestCase):
|
|
|
|
def test_mnist_cpu_float32(self):
|
|
|
|
def test_mnist_float32(self):
|
|
|
|
seed = 90
|
|
|
|
seed = 90
|
|
|
|
|
|
|
|
batch_num = 2
|
|
|
|
with fluid.imperative.guard():
|
|
|
|
with fluid.imperative.guard():
|
|
|
|
fluid.default_startup_program().random_seed = seed
|
|
|
|
fluid.default_startup_program().random_seed = seed
|
|
|
|
fluid.default_main_program().random_seed = seed
|
|
|
|
fluid.default_main_program().random_seed = seed
|
|
|
@ -112,15 +113,15 @@ class TestImperativeMnist(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
dy_param_init_value = {}
|
|
|
|
dy_param_init_value = {}
|
|
|
|
for batch_id, data in enumerate(train_reader()):
|
|
|
|
for batch_id, data in enumerate(train_reader()):
|
|
|
|
if batch_id >= 2:
|
|
|
|
if batch_id >= batch_num:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
x_data = np.array(
|
|
|
|
dy_x_data = np.array(
|
|
|
|
[x[0].reshape(1, 28, 28) for x in data]).astype('float32')
|
|
|
|
[x[0].reshape(1, 28, 28) for x in data]).astype('float32')
|
|
|
|
y_data = np.array([x[1] for x in data]).astype('int64').reshape(
|
|
|
|
y_data = np.array([x[1] for x in data]).astype('int64').reshape(
|
|
|
|
128, 1)
|
|
|
|
128, 1)
|
|
|
|
|
|
|
|
|
|
|
|
img = to_variable(x_data)
|
|
|
|
img = to_variable(dy_x_data)
|
|
|
|
label = to_variable(y_data)
|
|
|
|
label = to_variable(y_data)
|
|
|
|
label._stop_gradient = True
|
|
|
|
label._stop_gradient = True
|
|
|
|
|
|
|
|
|
|
|
@ -136,6 +137,7 @@ class TestImperativeMnist(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
avg_loss._backward()
|
|
|
|
avg_loss._backward()
|
|
|
|
sgd.minimize(avg_loss)
|
|
|
|
sgd.minimize(avg_loss)
|
|
|
|
|
|
|
|
mnist.clear_gradients()
|
|
|
|
dy_param_value = {}
|
|
|
|
dy_param_value = {}
|
|
|
|
for param in fluid.default_main_program().global_block(
|
|
|
|
for param in fluid.default_main_program().global_block(
|
|
|
|
).all_parameters():
|
|
|
|
).all_parameters():
|
|
|
@ -175,10 +177,10 @@ class TestImperativeMnist(unittest.TestCase):
|
|
|
|
static_param_init_value[static_param_name_list[i]] = out[i]
|
|
|
|
static_param_init_value[static_param_name_list[i]] = out[i]
|
|
|
|
|
|
|
|
|
|
|
|
for batch_id, data in enumerate(train_reader()):
|
|
|
|
for batch_id, data in enumerate(train_reader()):
|
|
|
|
if batch_id >= 2:
|
|
|
|
if batch_id >= batch_num:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
x_data = np.array(
|
|
|
|
static_x_data = np.array(
|
|
|
|
[x[0].reshape(1, 28, 28) for x in data]).astype('float32')
|
|
|
|
[x[0].reshape(1, 28, 28) for x in data]).astype('float32')
|
|
|
|
y_data = np.array([x[1] for x in data]).astype('int64').reshape(
|
|
|
|
y_data = np.array([x[1] for x in data]).astype('int64').reshape(
|
|
|
|
[128, 1])
|
|
|
|
[128, 1])
|
|
|
@ -186,7 +188,7 @@ class TestImperativeMnist(unittest.TestCase):
|
|
|
|
fetch_list = [avg_loss.name]
|
|
|
|
fetch_list = [avg_loss.name]
|
|
|
|
fetch_list.extend(static_param_name_list)
|
|
|
|
fetch_list.extend(static_param_name_list)
|
|
|
|
out = exe.run(fluid.default_main_program(),
|
|
|
|
out = exe.run(fluid.default_main_program(),
|
|
|
|
feed={"pixel": x_data,
|
|
|
|
feed={"pixel": static_x_data,
|
|
|
|
"label": y_data},
|
|
|
|
"label": y_data},
|
|
|
|
fetch_list=fetch_list)
|
|
|
|
fetch_list=fetch_list)
|
|
|
|
|
|
|
|
|
|
|
@ -196,11 +198,12 @@ class TestImperativeMnist(unittest.TestCase):
|
|
|
|
static_param_value[static_param_name_list[i - 1]] = out[i]
|
|
|
|
static_param_value[static_param_name_list[i - 1]] = out[i]
|
|
|
|
|
|
|
|
|
|
|
|
for key, value in six.iteritems(static_param_init_value):
|
|
|
|
for key, value in six.iteritems(static_param_init_value):
|
|
|
|
self.assertTrue(
|
|
|
|
self.assertTrue(np.allclose(value, dy_param_init_value[key]))
|
|
|
|
np.allclose(value.all(), dy_param_init_value[key].all()))
|
|
|
|
|
|
|
|
self.assertTrue(np.allclose(static_out.all(), dy_out.all()))
|
|
|
|
self.assertTrue(np.allclose(static_out, dy_out))
|
|
|
|
|
|
|
|
|
|
|
|
for key, value in six.iteritems(static_param_value):
|
|
|
|
for key, value in six.iteritems(static_param_value):
|
|
|
|
self.assertTrue(np.allclose(value.all(), dy_param_value[key].all()))
|
|
|
|
self.assertTrue(np.allclose(value, dy_param_value[key]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|