Example #1
0
    def test_step(self):
        # test for value
        param = OrderedDict()
        param['test_1'] = Parameter(np.ones(4))
        param['test_1'].grad = np.ones(4)

        optimizer = SGD(param)
        optimizer.step()

        self.assertTrue(
            (param['test_1'].val == np.ones(4) *
             (1. - 0.0001)).all())  # checking with hand calculation
Example #2
0
    def test_step(self):
        # test for value
        param = OrderedDict()
        param['test_1'] = Parameter(np.ones(4))
        param['test_1'].grad = np.ones(4)

        optimizer = MomentumSGD(param)
        optimizer.step()  # first time

        self.assertTrue(
            (param['test_1'].val == np.ones(4) *
             (1. - 0.0001)).all())  # checking with hand calculation

        pre_grad = -np.ones(4) * 0.0001  # omega
        grad = -np.ones(4) * 0.0001 + pre_grad * 0.9  # omega

        val = param['test_1'].val + grad

        optimizer.step()  # second time

        self.assertTrue((param['test_1'].val == val
                         ).all())  # checking with hand calculation