Beispiel #1
0
def test_val_grad(model):
    criterion = HeldOutLogistic(idx_val, idx_val)
    algo = Forward()
    val_fwd, grad_fwd = criterion.get_val_grad(model,
                                               X,
                                               y,
                                               log_alpha,
                                               algo.get_beta_jac_v,
                                               tol=tol)

    criterion = HeldOutLogistic(idx_val, idx_val)
    algo = ImplicitForward(tol_jac=1e-8, n_iter_jac=5000)
    val_imp_fwd, grad_imp_fwd = criterion.get_val_grad(model,
                                                       X,
                                                       y,
                                                       log_alpha,
                                                       algo.get_beta_jac_v,
                                                       tol=tol)

    criterion = HeldOutLogistic(idx_val, idx_val)
    algo = Implicit()
    val_imp, grad_imp = criterion.get_val_grad(model,
                                               X,
                                               y,
                                               log_alpha,
                                               algo.get_beta_jac_v,
                                               tol=tol)

    assert np.allclose(val_fwd, val_imp_fwd, atol=1e-4)
    assert np.allclose(grad_fwd, grad_imp_fwd, atol=1e-4)
    assert np.allclose(val_imp_fwd, val_imp, atol=1e-4)

    # for the implcit the conjugate grad does not converge
    # hence the rtol=1e-2
    assert np.allclose(grad_imp_fwd, grad_imp, rtol=1e-2)
Beispiel #2
0
def test_val_grad(model):
    #######################################################################
    # Not all methods computes the full Jacobian, but all
    # compute the gradients
    # check that the gradient returned by all methods are the same

    criterion = HeldOutLogistic(X_val, y_val, model)
    algo = Forward()
    val_fwd, grad_fwd = criterion.get_val_grad(log_C,
                                               algo.get_beta_jac_v,
                                               tol=tol)

    criterion = HeldOutLogistic(X_val, y_val, model)
    algo = ImplicitForward(tol_jac=1e-8, n_iter_jac=100)
    val_imp_fwd, grad_imp_fwd = criterion.get_val_grad(log_C,
                                                       algo.get_beta_jac_v,
                                                       tol=tol)

    criterion = HeldOutLogistic(X_val, y_val, model)
    algo = Implicit()
    val_imp, grad_imp = criterion.get_val_grad(log_C,
                                               algo.get_beta_jac_v,
                                               tol=tol)

    assert np.allclose(val_fwd, val_imp_fwd)
    assert np.allclose(grad_fwd, grad_imp_fwd)
    assert np.allclose(val_imp_fwd, val_imp)
    assert np.allclose(grad_imp_fwd, grad_imp, atol=1e-5)
Beispiel #3
0
def test_val_grad_custom(model, model_custom):
    criterion = HeldOutLogistic(X_val, y_val, model)
    algo = ImplicitForward(tol_jac=1e-8, n_iter_jac=5000)
    val, grad = criterion.get_val_grad(
        log_alpha, algo.get_beta_jac_v, tol=tol)

    criterion = HeldOutLogistic(X_val, y_val, model_custom)
    algo = ImplicitForward(tol_jac=1e-8, n_iter_jac=5000)
    val_custom, grad_custom = criterion.get_val_grad(
        log_alpha, algo.get_beta_jac_v, tol=tol)

    assert np.allclose(val, val_custom)
    assert np.allclose(grad, grad_custom)