|
|
|
|
@ -6,7 +6,7 @@ import paddle.v2.framework.optimizer as optimizer
|
|
|
|
|
|
|
|
|
|
class TestOptimizer(unittest.TestCase):
|
|
|
|
|
def test_sgd_optimizer(self):
|
|
|
|
|
program = framework.g_program
|
|
|
|
|
program = framework.Program()
|
|
|
|
|
block = program.global_block()
|
|
|
|
|
mul_x = block.create_parameter(
|
|
|
|
|
dtype="float32", shape=[5, 10], lod_level=0, name="mul.x")
|
|
|
|
|
@ -14,7 +14,7 @@ class TestOptimizer(unittest.TestCase):
|
|
|
|
|
dtype="float32", shape=[10, 8], lod_level=0, name="mul.y")
|
|
|
|
|
mul_out = block.create_var(
|
|
|
|
|
dtype="float32", shape=[5, 8], lod_level=0, name="mul.out")
|
|
|
|
|
mul_op = block.append_op(
|
|
|
|
|
block.append_op(
|
|
|
|
|
type="mul",
|
|
|
|
|
inputs={"X": mul_x,
|
|
|
|
|
"Y": mul_y},
|
|
|
|
|
@ -27,5 +27,47 @@ class TestOptimizer(unittest.TestCase):
|
|
|
|
|
self.assertEqual(sgd_op.type, "sgd")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMomentumOptimizer(unittest.TestCase):
|
|
|
|
|
class MockMomentum(optimizer.MomentumOptimizer):
|
|
|
|
|
def get_accumulators(self):
|
|
|
|
|
return self._accumulators
|
|
|
|
|
|
|
|
|
|
def get_velocity_str(self):
|
|
|
|
|
return self._velocity_acc_str
|
|
|
|
|
|
|
|
|
|
def test_momentum_optimizer(self):
|
|
|
|
|
program = framework.Program()
|
|
|
|
|
block = program.global_block()
|
|
|
|
|
mul_x = block.create_parameter(
|
|
|
|
|
dtype="float32", shape=[5, 10], lod_level=0, name="mul.x")
|
|
|
|
|
mul_y = block.create_var(
|
|
|
|
|
dtype="float32", shape=[10, 8], lod_level=0, name="mul.y")
|
|
|
|
|
mul_out = block.create_var(
|
|
|
|
|
dtype="float32", shape=[5, 8], lod_level=0, name="mul.out")
|
|
|
|
|
block.append_op(
|
|
|
|
|
type="mul",
|
|
|
|
|
inputs={"X": mul_x,
|
|
|
|
|
"Y": mul_y},
|
|
|
|
|
outputs={"Out": mul_out},
|
|
|
|
|
attrs={"x_num_col_dims": 1})
|
|
|
|
|
momentum_optimizer = self.MockMomentum(learning_rate=0.01, momentum=0.2)
|
|
|
|
|
params_grads = momentum_optimizer.create_backward_pass(mul_out)
|
|
|
|
|
self.assertEqual(len(params_grads), 1)
|
|
|
|
|
self.assertEqual(len(momentum_optimizer.get_accumulators()), 0)
|
|
|
|
|
opts = momentum_optimizer.create_optimization_pass(params_grads,
|
|
|
|
|
mul_out)
|
|
|
|
|
self.assertEqual(len(opts), 1)
|
|
|
|
|
sgd_op = opts[0]
|
|
|
|
|
self.assertEqual(sgd_op.type, "momentum")
|
|
|
|
|
|
|
|
|
|
# Check accumulators
|
|
|
|
|
accumulators = momentum_optimizer.get_accumulators()
|
|
|
|
|
self.assertEqual(len(accumulators), 1)
|
|
|
|
|
self.assertTrue(momentum_optimizer.get_velocity_str() in accumulators)
|
|
|
|
|
velocity_acc = accumulators[momentum_optimizer.get_velocity_str()]
|
|
|
|
|
self.assertEqual(len(velocity_acc), 1)
|
|
|
|
|
self.assertTrue(mul_x.name in velocity_acc)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|
|
|
|
|
|