Exemple #1
0
    def test_simple_loss_multiple_args(self):
        # test that args and kwargs are both handled by optimizers
        x0 = np.zeros(2), np.zeros(3)
        ans = np.arange(2.), np.arange(3.)

        opt = fmin_l_bfgs_b(simple_loss_multiple_args, x0)
        opt = fmin_l_bfgs_b(simple_loss_multiple_args,
                            init_kwargs=dict(p=x0[0], q=x0[1]))
        opt = fmin_l_bfgs_b(simple_loss_multiple_args,
                            init_args=x0[0],
                            init_kwargs=dict(q=x0[1]))
        self.assertTrue(np.allclose(opt[0], ans[0]))
        self.assertTrue(np.allclose(opt[1], ans[1]))

        opt = fmin_cg(simple_loss_multiple_args, x0)
        opt = fmin_cg(simple_loss_multiple_args,
                      init_kwargs=dict(p=x0[0], q=x0[1]))
        opt = fmin_cg(simple_loss_multiple_args,
                      init_args=x0[0],
                      init_kwargs=dict(q=x0[1]))
        self.assertTrue(np.allclose(opt[0], ans[0]))
        self.assertTrue(np.allclose(opt[1], ans[1]))

        opt = fmin_ncg(simple_loss_multiple_args, x0)
        opt = fmin_ncg(simple_loss_multiple_args,
                       init_kwargs=dict(p=x0[0], q=x0[1]))
        opt = fmin_ncg(simple_loss_multiple_args,
                       init_args=x0[0],
                       init_kwargs=dict(q=x0[1]))
        self.assertTrue(np.allclose(opt[0], ans[0]))
        self.assertTrue(np.allclose(opt[1], ans[1]))
Exemple #2
0
    def test_subtensor(self):
        x0 = np.zeros(2)
        ans = [-3, 4]

        opt = fmin_l_bfgs_b(subtensor_loss, x0)
        self.assertTrue(np.allclose(opt, ans))

        opt = fmin_cg(subtensor_loss, x0)
        self.assertTrue(np.allclose(opt, ans))

        opt = fmin_ncg(subtensor_loss, x0)
        self.assertTrue(np.allclose(opt, ans))
    def test_L2(self):
        x0 = np.zeros((5, 3))
        ans = np.array([[+3.0, -1.0, -5.0], [+1.5, -0.5, -2.5], [
            +0.0, 0.0, 0.0
        ], [-1.5, 0.5, 2.5], [-3.0, 1.0, 5.0]]) / 10.0

        opt = fmin_l_bfgs_b(l2_loss, x0)
        self.assertTrue(np.allclose(opt, ans))

        opt = fmin_cg(l2_loss, x0)
        self.assertTrue(np.allclose(opt, ans))

        opt = fmin_ncg(l2_loss, x0)
        self.assertTrue(np.allclose(opt, ans))
Exemple #4
0
    def test_L2(self):
        x0 = np.zeros((5, 3))
        ans = np.array([[+3.0, -1.0, -5.0],
                        [+1.5, -0.5, -2.5],
                        [+0.0,  0.0,  0.0],
                        [-1.5,  0.5,  2.5],
                        [-3.0,  1.0,  5.0]]) / 10.0

        opt = fmin_l_bfgs_b(l2_loss, x0)
        self.assertTrue(np.allclose(opt, ans))

        opt = fmin_cg(l2_loss, x0)
        self.assertTrue(np.allclose(opt, ans))

        opt = fmin_ncg(l2_loss, x0)
        self.assertTrue(np.allclose(opt, ans))