コード例 #1
0
ファイル: test_graph_net.py プロジェクト: yogeshmj/nilearn
def test__squared_loss_derivative_lipschitz_constant():
    # Tests Lipschitz-continuity of the derivative of _squared_loss loss
    # function
    rng = check_random_state(42)
    grad_weight = 2.08e-1
    lipschitz_constant = _squared_loss_derivative_lipschitz_constant(
        X, mask, grad_weight)
    for _ in range(20):
        x_1 = rng.rand(*w.shape) * rng.randint(1000)
        x_2 = rng.rand(*w.shape) * rng.randint(1000)
        gradient_difference = linalg.norm(
            _squared_loss_and_spatial_grad_derivative(X, y, x_1, mask,
                                                      grad_weight) -
            _squared_loss_and_spatial_grad_derivative(X, y, x_2, mask,
                                                      grad_weight))
        point_difference = linalg.norm(x_1 - x_2)
        assert gradient_difference <= lipschitz_constant * point_difference
コード例 #2
0
def test__squared_loss_derivative_lipschitz_constant():
    # Tests Lipschitz-continuity of the derivative of _squared_loss loss
    # function
    rng = check_random_state(42)
    grad_weight = 2.08e-1
    lipschitz_constant = _squared_loss_derivative_lipschitz_constant(
        X, mask, grad_weight)
    for _ in range(20):
        x_1 = rng.rand(*w.shape) * rng.randint(1000)
        x_2 = rng.rand(*w.shape) * rng.randint(1000)
        gradient_difference = extmath.norm(
            _squared_loss_and_spatial_grad_derivative(X, y, x_1, mask,
                                                      grad_weight)
            - _squared_loss_and_spatial_grad_derivative(X, y, x_2, mask,
                                                        grad_weight))
        point_difference = extmath.norm(x_1 - x_2)
        assert_true(
            gradient_difference <= lipschitz_constant * point_difference)
コード例 #3
0
ファイル: test_same_api.py プロジェクト: rob-luke/nilearn
def test_same_energy_calculus_pure_lasso():
    rng = check_random_state(42)
    X, y, w, mask = _make_data(rng=rng, masked=True)

    # check funcvals
    f1 = _squared_loss(X, y, w)
    f2 = _squared_loss_and_spatial_grad(X, y, w.ravel(), mask, 0.)
    assert f1 == f2

    # check derivatives
    g1 = _squared_loss_grad(X, y, w)
    g2 = _squared_loss_and_spatial_grad_derivative(X, y, w.ravel(), mask, 0.)
    np.testing.assert_array_equal(g1, g2)
コード例 #4
0
def test_same_energy_calculus_pure_lasso():
    rng = check_random_state(42)
    X, y, w, mask = _make_data(rng=rng, masked=True)

    # check funcvals
    f1 = _squared_loss(X, y, w)
    f2 = _squared_loss_and_spatial_grad(X, y, w.ravel(), mask, 0.)
    assert_equal(f1, f2)

    # check derivatives
    g1 = _squared_loss_grad(X, y, w)
    g2 = _squared_loss_and_spatial_grad_derivative(X, y, w.ravel(), mask, 0.)
    np.testing.assert_array_equal(g1, g2)
コード例 #5
0
def test__squared_loss_gradient_at_simple_points():
    """Tests gradient of data loss function in points near to zero. This is
    a not so hard test, just for detecting big errors"""
    X, y, w, mask = create_graph_net_simulation_data(n_samples=10, size=4)
    grad_weight = 1
    func = lambda w: _squared_loss_and_spatial_grad(X, y, w, mask,
                                                    grad_weight)
    func_grad = lambda w: _squared_loss_and_spatial_grad_derivative(
        X, y, w, mask, grad_weight)
    for i in range(0, w.size, 2):
        point = np.zeros(*w.shape)
        point[i] = 1
        assert_almost_equal(sp.optimize.check_grad(func, func_grad, point),
                            0, decimal=3)
コード例 #6
0
ファイル: test_graph_net.py プロジェクト: spanlab/spantoolbox
def test__squared_loss_gradient_at_simple_points():
    """Tests gradient of data loss function in points near to zero. This is
    a not so hard test, just for detecting big errors"""
    X, y, w, mask = create_graph_net_simulation_data(n_samples=10, size=4)
    grad_weight = 1
    func = lambda w: _squared_loss_and_spatial_grad(X, y, w, mask, grad_weight)
    func_grad = lambda w: _squared_loss_and_spatial_grad_derivative(
        X, y, w, mask, grad_weight)
    for i in range(0, w.size, 2):
        point = np.zeros(*w.shape)
        point[i] = 1
        assert_almost_equal(sp.optimize.check_grad(func, func_grad, point),
                            0,
                            decimal=3)