Example #1
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()
     rows, columns = Y_train.shape
     #print K_train1.shape, K_train2.shape, K_test1.shape, K_test2.shape, rows, columns
     trainlabelcount = rows * columns
     indmatrix = np.mat(range(trainlabelcount)).T.reshape(rows, columns)
     
     #Train linear Kronecker RLS with data-matrices
     params = {}
     params["regparam"] = regparam
     params["xmatrix1"] = X_train1
     params["xmatrix2"] = X_train2
     params["train_labels"] = Y_train
     linear_kron_learner = KronRLS.createLearner(**params)
     linear_kron_learner.train()
     linear_kron_model = linear_kron_learner.getModel()
     linear_kron_testpred = linear_kron_model.predictWithDataMatrices(X_test1, X_test2)
     
     #Train kernel Kronecker RLS with pre-computed kernel matrices
     params = {}
     params["regparam"] = regparam
     params["kmatrix1"] = K_train1
     params["kmatrix2"] = K_train2
     params["train_labels"] = Y_train
     kernel_kron_learner = KronRLS.createLearner(**params)
     kernel_kron_learner.train()
     kernel_kron_model = kernel_kron_learner.getModel()
     kernel_kron_testpred = kernel_kron_model.predictWithKernelMatrices(K_test1, K_test2)
     
     #Train an ordinary RLS regressor for reference
     K_Kron_train_x = np.kron(K_train1, K_train2)
     params = {}
     params["kernel_matrix"] = K_Kron_train_x
     params["train_labels"] = Y_train.reshape(trainlabelcount, 1)
     ordrls_learner = RLS.createLearner(**params)
     ordrls_learner.solve(regparam)
     ordrls_model = ordrls_learner.getModel()
     K_Kron_test_x = np.kron(K_test1, K_test2)
     ordrls_testpred = ordrls_model.predict(K_Kron_test_x)
     ordrls_testpred = ordrls_testpred.reshape(Y_test.shape[0], Y_test.shape[1])
     
     print
     print type(linear_kron_testpred), type(kernel_kron_testpred), type(ordrls_testpred)
     print linear_kron_testpred[0, 0], kernel_kron_testpred[0, 0], ordrls_testpred[0, 0]
     print linear_kron_testpred[0, 1], kernel_kron_testpred[0, 1], ordrls_testpred[0, 1]
     print linear_kron_testpred[1, 0], kernel_kron_testpred[1, 0], ordrls_testpred[1, 0]
     
     print np.mean(np.abs(linear_kron_testpred - ordrls_testpred)), np.mean(np.abs(kernel_kron_testpred - ordrls_testpred))
Example #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)
Example #3
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()
     rows, columns = Y_train.shape
     trainlabelcount = rows * columns
     indmatrix = np.mat(range(trainlabelcount)).T.reshape(rows, columns)
     
     K_Kron_train_x = np.kron(K_train1, K_train2)
     K_test_x = np.kron(K_test1, K_test2)
     
     
     #Train linear Conditional Ranking Kronecker RLS
     params = {}
     params["xmatrix1"] = X_train1
     params["xmatrix2"] = X_train2
     params["train_labels"] = Y_train
     params["regparam"] = regparam
     linear_kron_condrank_learner = KronRLS.createLearner(**params)
     linear_kron_condrank_learner.solve_linear_conditional_ranking(regparam)
     condrank_model = linear_kron_condrank_learner.getModel()
     
     #Train an ordinary RankRLS for reference
     params = {}
     params["kernel_matrix"] = K_Kron_train_x
     params["train_labels"] = Y_train.reshape(trainlabelcount, 1)
     params["train_qids"] = [range(i*Y_train.shape[1], (i+1)*Y_train.shape[1]) for i in range(Y_train.shape[0])]
     rankrls_learner = LabelRankRLS.createLearner(**params)
     rankrls_learner.solve(regparam)
     rankrls_model = rankrls_learner.getModel()
     K_test_x = np.kron(K_test1, K_test2)
     ordrankrls_testpred = rankrls_model.predict(K_test_x).reshape(Y_test.shape[0], Y_test.shape[1])
     condrank_testpred = condrank_model.predictWithDataMatrices(X_test1, X_test2)
     print
     #print condrank_testpred.ravel().shape, Y_test.ravel().shape, ordrankrls_testpred.ravel().shape, Y_test.ravel().shape
     
     print 'TEST cond vs rankrls', np.mean(np.abs(condrank_testpred - ordrankrls_testpred))
Example #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)