def test_optim_rosenbrock(self): x0 = Variable(np.array(0.0)) x1 = Variable(np.array(2.0)) lr = 0.001 iters = 50000 for i in range(iters): y = rosenbrock(x0, x1) x0.cleargrad() x1.cleargrad() y.backward() x0.data = x0.data - lr * x0.grad.data x1.data = x1.data - lr * x1.grad.data # 50000回回すと、誤差は10の-8乗以下になる self.assertEqual((round(x0.data, 8), round(x1.data, 8)), (1.0, 1.0))
def test_newton_method(self): def f(x): y = x**4 - 2 * x**2 return y x = Variable(np.array(2.0)) iters = 10 for i in range(iters): y = f(x) x.cleargrad() y.backward(create_graph=True) gx = x.grad x.cleargrad() gx.backward() gx2 = x.grad x.data = x.data - gx.data / gx2.data self.assertEqual(x.data, 1.0)