def test_grad_noreg(self): X, y = data1() np.testing.assert_allclose( solution.grad(np.array([0, 0, 0, 0]), X, y, 0.0), [-0.2313, 0.1625, -0.6625, -0.2563], rtol=1e-3) np.testing.assert_allclose( solution.grad(np.array([0.1, -0.1, 0.1, 0.1]), X, y, 0.0), [0.5610, 0.5969, -0.1870, -0.1151], rtol=1e-3) np.testing.assert_allclose( solution.grad(np.array([-1.06, -5.39, 7.64, 3.79]), X, y, 0.0), [0., 0., 0., 0.], atol=1e-3) X, y = data2() np.testing.assert_allclose( solution.grad(np.array([0.1, -0.1, 0.1, 0.1]), X, y, 0.0), [1.3211, 1.0822, 0.1827, -0.0282], rtol=1e-3)
def test_z_grad_cost_compatible(self): X, y = data1() thetas = [[-0.231, 0.162, -0.662, -0.256], [0.561, 0.597, -0.187, -0.115], [-1.06, -5.39, 7.64, 3.79]] for theta in thetas: for lambda_ in [0.0, 1.0, 100.0, 0.0001]: theta = np.array(theta) an_grad = grad(theta, X, y, lambda_) n_grad = num_grad(theta, X, y, lambda_) np.testing.assert_almost_equal( an_grad, n_grad, decimal=5, err_msg="lambvda: %d theta: %s" % (lambda_, theta))
def test_z_grad_cost_compatible(self): X, y = data1() def diff_numberic_analitic_grad(theta, lambda_, analitic_grad): vec = solution.num_grad(theta, X, y, lambda_) return np.max(np.abs(analitic_grad-vec)) thetas = [[-0.231, 0.162, -0.662, -0.256], [0.561, 0.597, -0.187, -0.115], [-1.06, -5.39, 7.64, 3.79]] for theta in thetas: for lambda_ in [0.0, 1.0, 100.0, 0.0001]: theta = np.array(theta) an_grad = solution.grad(theta, X, y, lambda_) self.assertAlmostEqual( diff_numberic_analitic_grad(theta, lambda_, an_grad), 0.0, places=6)