예제 #1
0
    def test_conditional_ranking(self):

        regparam = 0.001

        K_train1, K_train2, Y_train, K_test1, K_test2, Y_test, X_train1, X_train2, X_test1, X_test2 = (
            self.generate_xortask()
        )
        train_rows, train_columns = Y_train.shape
        trainlabelcount = train_rows * train_columns

        K_Kron_train_x = np.kron(K_train2, K_train1)

        # Train linear Conditional Ranking Kronecker RLS
        params = {}
        params["X1"] = X_train1
        params["X2"] = X_train2
        params["Y"] = Y_train
        params["regparam"] = regparam
        linear_kron_condrank_learner = KronRLS(**params)
        linear_kron_condrank_learner.solve_linear_conditional_ranking(regparam)

        # Train an ordinary RankRLS for reference
        params = {}
        params["X"] = K_Kron_train_x
        params["kernel"] = "PrecomputedKernel"
        params["Y"] = Y_train.reshape((trainlabelcount, 1), order="F")
        qids = []
        for j in range(Y_train.shape[1]):
            for i in range(Y_train.shape[0]):
                qids.append(i)
        params["qids"] = qids
        rankrls_learner = QueryRankRLS(**params)
        rankrls_learner.solve(regparam)
        K_test_x = np.kron(K_test2, K_test1)
        ordrankrls_testpred = rankrls_learner.predict(K_test_x)
        condrank_testpred = linear_kron_condrank_learner.predict(X_test1, X_test2)
        # print('')
        print(
            "\n\nMeanabsdiff: conditional ranking vs rankrls "
            + str(np.mean(np.abs(condrank_testpred - ordrankrls_testpred)))
            + "\n"
        )
        np.testing.assert_almost_equal(condrank_testpred, ordrankrls_testpred)
예제 #2
0
 def test_conditional_ranking(self):
     
     regparam = 0.001
     
     K_train1, K_train2, Y_train, K_test1, K_test2, Y_test, X_train1, X_train2, X_test1, X_test2 = self.generate_xortask()
     train_rows, train_columns = Y_train.shape
     trainlabelcount = train_rows * train_columns
     
     K_Kron_train_x = np.kron(K_train2, K_train1)
     
     
     #Train linear Conditional Ranking Kronecker RLS
     params = {}
     params["X1"] = X_train1
     params["X2"] = X_train2
     params["Y"] = Y_train
     params["regparam"] = regparam
     linear_kron_condrank_learner = KronRLS(**params)
     linear_kron_condrank_learner.solve_linear_conditional_ranking(regparam)
     
     #Train an ordinary RankRLS for reference
     params = {}
     params["X"] = K_Kron_train_x
     params["kernel"] = "PrecomputedKernel"
     params["Y"] = Y_train.reshape((trainlabelcount, 1), order = 'F')
     qids = []
     for j in range(Y_train.shape[1]):
         for i in range(Y_train.shape[0]):
             qids.append(i)
     params["qids"] = qids
     rankrls_learner = QueryRankRLS(**params)
     rankrls_learner.solve(regparam)
     K_test_x = np.kron(K_test2, K_test1)
     ordrankrls_testpred = rankrls_learner.predict(K_test_x)
     condrank_testpred = linear_kron_condrank_learner.predict(X_test1, X_test2)
     #print('')
     print('\n\nMeanabsdiff: conditional ranking vs rankrls ' + str(np.mean(np.abs(condrank_testpred - ordrankrls_testpred))) + '\n')
     np.testing.assert_almost_equal(condrank_testpred, ordrankrls_testpred)
예제 #3
0
 def test_kron_rls(self):
     
     regparam = 0.001
     
     K_train1, K_train2, Y_train, K_test1, K_test2, Y_test, X_train1, X_train2, X_test1, X_test2 = self.generate_xortask()
     Y_train = Y_train.ravel(order = 'F')
     Y_test = Y_test.ravel(order = 'F')
     train_rows, train_columns = K_train1.shape[0], K_train2.shape[0]
     test_rows, test_columns = K_test1.shape[0], K_test2.shape[0]
     trainlabelcount = train_rows * train_columns
     
     #Train linear Kronecker RLS with data-matrices
     params = {}
     params["regparam"] = regparam
     params["X1"] = X_train1
     params["X2"] = X_train2
     params["Y"] = Y_train
     linear_kron_learner = KronRLS(**params)
     linear_kron_testpred = linear_kron_learner.predict(X_test1, X_test2).reshape((test_rows, test_columns), order = 'F')
     
     #Train kernel Kronecker RLS with pre-computed kernel matrices
     params = {}
     params["regparam"] = regparam
     params["K1"] = K_train1
     params["K2"] = K_train2
     params["Y"] = Y_train
     kernel_kron_learner = KronRLS(**params)
     kernel_kron_testpred = kernel_kron_learner.predict(K_test1, K_test2).reshape((test_rows, test_columns), order = 'F')
     
     #Train an ordinary RLS regressor for reference
     K_Kron_train_x = np.kron(K_train2, K_train1)
     params = {}
     params["X"] = K_Kron_train_x
     params["kernel"] = "PrecomputedKernel"
     params["Y"] = Y_train.reshape(trainlabelcount, 1, order = 'F')
     ordrls_learner = RLS(**params)
     ordrls_learner.solve(regparam)
     K_Kron_test_x = np.kron(K_test2, K_test1)
     ordrls_testpred = ordrls_learner.predict(K_Kron_test_x)
     ordrls_testpred = ordrls_testpred.reshape((test_rows, test_columns), order = 'F')
     
     print('')
     print('Prediction: linear KronRLS, kernel KronRLS, ordinary RLS')
     print('[0, 0] ' + str(linear_kron_testpred[0, 0]) + ' ' + str(kernel_kron_testpred[0, 0]) + ' ' + str(ordrls_testpred[0, 0]))
     print('[0, 1] ' + str(linear_kron_testpred[0, 1]) + ' ' + str(kernel_kron_testpred[0, 1]) + ' ' + str(ordrls_testpred[0, 1]))
     print('[1, 0] ' + str(linear_kron_testpred[1, 0]) + ' ' + str(kernel_kron_testpred[1, 0]) + ' ' + str(ordrls_testpred[1, 0]))
     print('Meanabsdiff: linear KronRLS - ordinary RLS, kernel KronRLS - ordinary RLS')
     print(str(np.mean(np.abs(linear_kron_testpred - ordrls_testpred))) + ' ' + str(np.mean(np.abs(kernel_kron_testpred - ordrls_testpred))))
     np.testing.assert_almost_equal(linear_kron_testpred, ordrls_testpred)
     np.testing.assert_almost_equal(kernel_kron_testpred, ordrls_testpred)
     
     print('')
     ordrls_loopred = ordrls_learner.leave_one_out().reshape((train_rows, train_columns), order = 'F')
     linear_kron_loopred = linear_kron_learner.in_sample_loo().reshape((train_rows, train_columns), order = 'F')
     kernel_kron_loopred = kernel_kron_learner.in_sample_loo().reshape((train_rows, train_columns), order = 'F')
     print('In-sample LOO: linear KronRLS, kernel KronRLS, ordinary RLS')
     print('[0, 0] ' + str(linear_kron_loopred[0, 0]) + ' ' + str(kernel_kron_loopred[0, 0]) + ' ' + str(ordrls_loopred[0, 0]))
     print('[0, 1] ' + str(linear_kron_loopred[0, 1]) + ' ' + str(kernel_kron_loopred[0, 1]) + ' ' + str(ordrls_loopred[0, 1]))
     print('[1, 0] ' + str(linear_kron_loopred[1, 0]) + ' ' + str(kernel_kron_loopred[1, 0]) + ' ' + str(ordrls_loopred[1, 0]))
     print('Meanabsdiff: linear KronRLS - ordinary RLS, kernel KronRLS - ordinary RLS')
     print(str(np.mean(np.abs(linear_kron_loopred - ordrls_loopred))) + ' ' + str(np.mean(np.abs(kernel_kron_loopred - ordrls_loopred))))
     np.testing.assert_almost_equal(linear_kron_loopred, ordrls_loopred)
     np.testing.assert_almost_equal(kernel_kron_loopred, ordrls_loopred)
예제 #4
0
    def test_kron_rls(self):

        regparam = 0.001

        K_train1, K_train2, Y_train, K_test1, K_test2, Y_test, X_train1, X_train2, X_test1, X_test2 = (
            self.generate_xortask()
        )
        Y_train = Y_train.ravel(order="F")
        Y_test = Y_test.ravel(order="F")
        train_rows, train_columns = K_train1.shape[0], K_train2.shape[0]
        test_rows, test_columns = K_test1.shape[0], K_test2.shape[0]
        trainlabelcount = train_rows * train_columns

        # Train linear Kronecker RLS with data-matrices
        params = {}
        params["regparam"] = regparam
        params["X1"] = X_train1
        params["X2"] = X_train2
        params["Y"] = Y_train
        linear_kron_learner = KronRLS(**params)
        linear_kron_testpred = linear_kron_learner.predict(X_test1, X_test2).reshape(
            (test_rows, test_columns), order="F"
        )

        # Train kernel Kronecker RLS with pre-computed kernel matrices
        params = {}
        params["regparam"] = regparam
        params["K1"] = K_train1
        params["K2"] = K_train2
        params["Y"] = Y_train
        kernel_kron_learner = KronRLS(**params)
        kernel_kron_testpred = kernel_kron_learner.predict(K_test1, K_test2).reshape(
            (test_rows, test_columns), order="F"
        )

        # Train an ordinary RLS regressor for reference
        K_Kron_train_x = np.kron(K_train2, K_train1)
        params = {}
        params["X"] = K_Kron_train_x
        params["kernel"] = "PrecomputedKernel"
        params["Y"] = Y_train.reshape(trainlabelcount, 1, order="F")
        ordrls_learner = RLS(**params)
        ordrls_learner.solve(regparam)
        K_Kron_test_x = np.kron(K_test2, K_test1)
        ordrls_testpred = ordrls_learner.predict(K_Kron_test_x)
        ordrls_testpred = ordrls_testpred.reshape((test_rows, test_columns), order="F")

        print("")
        print("Prediction: linear KronRLS, kernel KronRLS, ordinary RLS")
        print(
            "[0, 0] "
            + str(linear_kron_testpred[0, 0])
            + " "
            + str(kernel_kron_testpred[0, 0])
            + " "
            + str(ordrls_testpred[0, 0])
        )
        print(
            "[0, 1] "
            + str(linear_kron_testpred[0, 1])
            + " "
            + str(kernel_kron_testpred[0, 1])
            + " "
            + str(ordrls_testpred[0, 1])
        )
        print(
            "[1, 0] "
            + str(linear_kron_testpred[1, 0])
            + " "
            + str(kernel_kron_testpred[1, 0])
            + " "
            + str(ordrls_testpred[1, 0])
        )
        print("Meanabsdiff: linear KronRLS - ordinary RLS, kernel KronRLS - ordinary RLS")
        print(
            str(np.mean(np.abs(linear_kron_testpred - ordrls_testpred)))
            + " "
            + str(np.mean(np.abs(kernel_kron_testpred - ordrls_testpred)))
        )
        np.testing.assert_almost_equal(linear_kron_testpred, ordrls_testpred)
        np.testing.assert_almost_equal(kernel_kron_testpred, ordrls_testpred)

        print("")
        ordrls_loopred = ordrls_learner.leave_one_out().reshape((train_rows, train_columns), order="F")
        linear_kron_loopred = linear_kron_learner.in_sample_loo().reshape((train_rows, train_columns), order="F")
        kernel_kron_loopred = kernel_kron_learner.in_sample_loo().reshape((train_rows, train_columns), order="F")
        print("In-sample LOO: linear KronRLS, kernel KronRLS, ordinary RLS")
        print(
            "[0, 0] "
            + str(linear_kron_loopred[0, 0])
            + " "
            + str(kernel_kron_loopred[0, 0])
            + " "
            + str(ordrls_loopred[0, 0])
        )
        print(
            "[0, 1] "
            + str(linear_kron_loopred[0, 1])
            + " "
            + str(kernel_kron_loopred[0, 1])
            + " "
            + str(ordrls_loopred[0, 1])
        )
        print(
            "[1, 0] "
            + str(linear_kron_loopred[1, 0])
            + " "
            + str(kernel_kron_loopred[1, 0])
            + " "
            + str(ordrls_loopred[1, 0])
        )
        print("Meanabsdiff: linear KronRLS - ordinary RLS, kernel KronRLS - ordinary RLS")
        print(
            str(np.mean(np.abs(linear_kron_loopred - ordrls_loopred)))
            + " "
            + str(np.mean(np.abs(kernel_kron_loopred - ordrls_loopred)))
        )
        np.testing.assert_almost_equal(linear_kron_loopred, ordrls_loopred)
        np.testing.assert_almost_equal(kernel_kron_loopred, ordrls_loopred)