def test_ridge_grad_cov(): """Test ovk.OVKRidgeRisk gradient with finite differences.""" K = ovk.DecomposableKernel(A=eye(2)) risk = ovk.OVKRidgeRisk(0.01) assert check_grad(lambda *args: risk.functional_grad_val(*args)[0], lambda *args: risk.functional_grad_val(*args)[1], randn(X.shape[0] * y.shape[1]), y.ravel(), K(X, X)) < 1e-3
def test_rff_ridge_grad_cov(): """Test ovk.ORFFRidgeRisk gradient with finite differences.""" K = ovk.DecomposableKernel(A=eye(2)) risk = ovk.ORFFRidgeRisk(0.01) D = 100 assert check_grad(lambda *args: risk.functional_grad_val(*args)[0], lambda *args: risk.functional_grad_val(*args)[1], randn(D * y.shape[1]), y.ravel(), K.get_orff_map( X, D), K) < 1e-3
def test_grad_val(): """Test whether ovk.OVKRidgeRisk gradient and val are consistents.""" K = ovk.DecomposableKernel(A=eye(2)) risk = ovk.OVKRidgeRisk(0.01) C = randn(X.shape[0] * y.shape[1]) v = risk(C, y.ravel(), K(X, X)) g = risk.functional_grad(C, y.ravel(), K(X, X)) vg = risk.functional_grad_val(C, y.ravel(), K(X, X)) assert_allclose(v, vg[0]) assert_allclose(g, vg[1])
def test_rff_ridge_hinge_grad(): """Test ovk.ORFFRidgeRisk gradient with finite differences.""" K = ovk.DecomposableKernel(A=eye(3)) risk = ovk.ORFFRidgeRisk(0.01, 'Hinge') D = 100 y = one_hot(randint(0, 3, X.shape[0]), 3) vl = check_grad(lambda *args: risk.functional_grad_val(*args)[0], lambda *args: risk.functional_grad_val(*args)[1], rand(D * y.shape[1]), y.ravel(), K.get_orff_map(X, D), K) assert vl < 1e-3
def test_rff_ridge_SCSVM(): K = ovk.DecomposableKernel(A=eye(2)) risk = ovk.ORFFRidgeRisk(0.01, 'SCSVM') D = 100 y = one_hot(randint(0, 3, X.shape[0]), 3) sc = ovk.preprocessing.SimplexCoding() y = sc.fit_transform(y) vl = check_grad(lambda *args: risk.functional_grad_val(*args)[0], lambda *args: risk.functional_grad_val(*args)[1], rand(D * y.shape[1]), y.ravel(), K.get_orff_map(X, D), K) assert vl < 1e-3
def test_rff_grad_val_least_squares(): """Test whether ovk.ORFFRidgeRisk least square gradient and val are consistents.""" K = ovk.DecomposableKernel(A=eye(2)) risk = ovk.ORFFRidgeRisk(0.01) D = 100 C = randn(D * y.shape[1]) v = risk(C, y.ravel(), K.get_orff_map(X, D), K) g = risk.functional_grad(C, y.ravel(), K.get_orff_map(X, D), K) vg = risk.functional_grad_val(C, y.ravel(), K.get_orff_map(X, D), K) assert_allclose(v, vg[0]) assert_allclose(g, vg[1])
def test_rff_grad_val_SCSVM(): K = ovk.DecomposableKernel(A=eye(2)) risk = ovk.ORFFRidgeRisk(0.01, 'SCSVM') y = one_hot(randint(0, 3, X.shape[0]), 3) sv = ovk.preprocessing.SimplexCoding() y = sv.fit_transform(y) D = 100 C = rand(D * y.shape[1]) v = risk(C, y.ravel(), K.get_orff_map(X, D), K) g = risk.functional_grad(C, y.ravel(), K.get_orff_map(X, D), K) vg = risk.functional_grad_val(C, y.ravel(), K.get_orff_map(X, D), K) assert_allclose(v, vg[0]) assert_allclose(g, vg[1])
def test_rff_grad_val_hinge(): """Test whether ovk.ORFFRidgeRisk hinge gradient and val are consistents.""" K = ovk.DecomposableKernel(A=eye(3)) risk = ovk.ORFFRidgeRisk(0.01, 'Hinge') y = one_hot(randint(0, 3, X.shape[0]), 3) D = 100 C = randn(D * y.shape[1]) v = risk(C, y.ravel(), K.get_orff_map(X, D), K) g = risk.functional_grad(C, y.ravel(), K.get_orff_map(X, D), K) vg = risk.functional_grad_val(C, y.ravel(), K.get_orff_map(X, D), K) assert_allclose(v, vg[0]) assert_allclose(g, vg[1])