def test_GD(self): print("Test GD") ig = ImageGeometry(12, 13, 14) initial = ig.allocate() # b = initial.copy() # fill with random numbers # b.fill(numpy.random.random(initial.shape)) b = ig.allocate('random') identity = IdentityOperator(ig) norm2sq = LeastSquares(identity, b) rate = norm2sq.L / 3. alg = GD(initial=initial, objective_function=norm2sq, rate=rate, atol=1e-9, rtol=1e-6) alg.max_iteration = 1000 alg.run(verbose=0) self.assertNumpyArrayAlmostEqual(alg.x.as_array(), b.as_array()) alg = GD(initial=initial, objective_function=norm2sq, rate=rate, max_iteration=20, update_objective_interval=2, atol=1e-9, rtol=1e-6) alg.max_iteration = 20 self.assertTrue(alg.max_iteration == 20) self.assertTrue(alg.update_objective_interval == 2) alg.run(20, verbose=0) self.assertNumpyArrayAlmostEqual(alg.x.as_array(), b.as_array())
def test_GDArmijo2(self): from cil.optimisation.functions import Rosenbrock from cil.framework import VectorData, VectorGeometry f = Rosenbrock(alpha=1., beta=100.) vg = VectorGeometry(2) x = vg.allocate('random_int', seed=2) # x = vg.allocate('random', seed=1) x.fill(numpy.asarray([10., -3.])) max_iter = 10000 update_interval = 1000 alg = GD(x, f, max_iteration=max_iter, update_objective_interval=update_interval, alpha=1e6) alg.run(verbose=0) print(alg.get_output().as_array(), alg.step_size, alg.kmax, alg.k) # this with 10k iterations numpy.testing.assert_array_almost_equal(alg.get_output().as_array(), [0.13463363, 0.01604593], decimal=5)