Exemple #1
0
 def testCGRLS(self):
     m, n = 100, 300
     for regparam in [0.00000001, 1, 100000000]:
         Xtrain = np.mat(np.random.rand(m, n))
         Y = np.mat(np.random.rand(m, 1))
         rpool = {}
         rpool["train_features"] = Xtrain
         rpool["train_labels"] = Y
         rpool["regparam"] = regparam
         rpool["bias"] = 1.0
         rls = RLS.createLearner(**rpool)
         rls.solve(regparam)
         model = rls.getModel()
         W = model.W
         b = model.b
         rls = CGRLS.createLearner(**rpool)
         rls.train()
         model = rls.getModel()
         W2 = model.W
         b2 = model.b
         for i in range(W.shape[0]):
             # for j in range(W.shape[1]):
             #    self.assertAlmostEqual(W[i,j],W2[i,j],places=5)
             self.assertAlmostEqual(W[i], W2[i], places=5)
         self.assertAlmostEqual(b, b2, places=5)
Exemple #2
0
def nfoldRLS(X, Y, fcount):
    kwargs = {}
    kwargs['train_features'] = X
    kwargs['train_labels'] = Y
    rls = RLS.createLearner(**kwargs)
    rls.train()
    bestperf = -1. 
    for logrp in range(5, 25):
        rp = 2. ** logrp
        rls.solve(rp)
        perfs = []
        kf = KFold(len(Y), n_folds=fcount, indices=True, shuffle=True, random_state=77)
        for train, test in kf:
            P = rls.computeHO(test)
            perf = cindex(Y[test], P)
            perfs.append(perf)
        perf = np.mean(perfs)
        print "N-fold CV %f for lambda 2^%d" %(perf, logrp)
        if perf > bestperf:
            bestperf = perf
            bestlogrp = logrp
    rp = 2. ** bestlogrp
    print "Best N-fold CV %f for lambda 2^%d" %(bestperf, bestlogrp)
    rls.solve(rp)
    model = rls.getModel()
    return model
Exemple #3
0
def looRLS(XPath, yPath, metaPath):
    X, Y = readAuto(XPath, yPath)
    meta = {}
    if metaPath != None:
        print "Loading metadata from", metaPath
        meta = result.getMeta(metaPath)
    X_train, X_hidden, Y_train, Y_hidden = hidden.split(X, Y, meta=meta) 
    kwargs = {}
    kwargs['train_features'] = X_train
    kwargs['train_labels'] = Y_train
    kwargs['regparam'] = 1.0
    rls = RLS.createLearner(**kwargs)
    rls.train()
    bestperf = -1. 
    for logrp in range(5, 25):
        rp = 2. ** logrp
        rls.solve(rp)
        Ploo = rls.computeLOO()
        perf = cindex(Y_train, Ploo)
        print "Leave-one-out %f for lambda 2^%d" %(perf, logrp)
        if perf > bestperf:
            bestperf = perf
            bestlogrp = logrp
    rp = 2. ** bestlogrp
    print "Best leave-one-out %f for lambda 2^%d" %(bestperf, bestlogrp)
    rls.solve(rp)
    model = rls.getModel()
    P = model.predict(X_hidden)
    perf = cindex(Y_hidden, P)
    print "final performance: %f" %perf
Exemple #4
0
def testRLS(input):
    X, Y = svmlight_format.load_svmlight_file(input)

    hoindices = range(int(0.1 * len(Y)))
    hocompl = list(set(range(len(Y))) - set(hoindices))
    trainX = X[hocompl]
    testX = X[hoindices]
    trainY = Y[hocompl]
    testY = Y[hoindices]
    print len(trainY), len(testY)

    kwargs = {}
    kwargs["train_features"] = trainX
    kwargs["train_labels"] = trainY

    rls = RLS.createLearner(**kwargs)
    rls.train()
    bestperf = -1.0
    for logrp in range(-5, 5):
        rp = 2.0 ** logrp
        rls.solve(rp)
        Ploo = rls.computeLOO()
        perf = cindex(trainY, Ploo)
        print logrp, perf
        if perf > bestperf:
            bestperf = perf
            bestlogrp = logrp
    rp = 2.0 ** bestlogrp
    rls.solve(rp)
    P = rls.getModel().predict(testX)
Exemple #5
0
 def testCGRLS(self):
     m, n = 100, 300
     for regparam in [0.00000001, 1, 100000000]:
         Xtrain = np.mat(np.random.rand(m, n))
         Y = np.mat(np.random.rand(m, 1))
         rpool = {}
         rpool['train_features'] = Xtrain
         rpool['train_labels'] = Y
         rpool['regparam'] = regparam
         rpool["bias"] = 1.0
         rls = RLS.createLearner(**rpool)
         rls.solve(regparam)
         model = rls.getModel()
         W = model.W
         b = model.b
         rls = CGRLS.createLearner(**rpool)
         rls.train()
         model = rls.getModel()
         W2 = model.W
         b2 = model.b
         for i in range(W.shape[0]):
             #for j in range(W.shape[1]):
             #    self.assertAlmostEqual(W[i,j],W2[i,j],places=5)
             self.assertAlmostEqual(W[i], W2[i], places=5)
         self.assertAlmostEqual(b, b2, places=5)
Exemple #6
0
def search_params_linear(trainX, trainY, group_ids, rprange):
    """Search best parameters for kernel and regularization."""
    kwargs = {
        'train_features': trainX,
        'train_labels': trainY,
        'kernel_obj': LinearKernel(trainX),
    }
    rls = RLS.createLearner(**kwargs)
    rls.train()
    perf, perf_groups, rp = search_rp(rls, trainY, group_ids, rprange)
    return perf, perf_groups, rp
Exemple #7
0
def search_params_nonlinear(trainX, trainY, group_ids, rprange, gammarange):
    """Search best parameters for kernel and regularization."""
    bestperf = -1.
    for loggamma in range(*gammarange):
        gamma = 2. ** loggamma
        kwargs = {
            'train_features': trainX,
            'train_labels': trainY,
            #'kernel_obj': LinearKernel(trainX),
            'kernel_obj': GaussianKernel(trainX, gamma=gamma),
            #'kernel_obj': PolynomialKernel(trainX, gamma=gamma, coef0=1, degree=2),
        }
        rls = RLS.createLearner(**kwargs)
        rls.train()
        perf, perf_groups, rp = search_rp(rls, trainY, group_ids, rprange)
        if perf > bestperf:
            bestperf = perf
            bestperf_groups = perf_groups
            bestrp = rp
            bestgamma = gamma
    return bestperf, bestperf_groups, bestrp, bestgamma
Exemple #8
0
 def testRLS(self):
     
     print
     print
     print
     print
     print "Testing the cross-validation routines of the RLS module."
     print
     print
     floattype = float64
     
     m, n = 400, 100
     Xtrain = mat(random.rand(m, n))
     K = Xtrain * Xtrain.T
     ylen = 2
     Y = mat(zeros((m, ylen), dtype=floattype))
     Y = mat(random.rand(m, ylen))
     
     #hoindices = [45, 50, 55]
     hoindices = [45]
     hocompl = list(set(range(m)) - set(hoindices))
     
     Kho = K[ix_(hocompl, hocompl)]
     Yho = Y[hocompl]
     
     kwargs = {}
     kwargs['train_labels'] = Y
     kwargs['kernel_matrix'] = K
     dualrls = RLS.createLearner(**kwargs)
     
     kwargs = {}
     kwargs["train_features"] = Xtrain
     kwargs["train_labels"] = Y
     primalrls = RLS.createLearner(**kwargs)
     
     kwargs = {}
     kwargs['train_labels'] = Yho
     kwargs['kernel_matrix'] = Kho
     dualrls_naive = RLS.createLearner(**kwargs)
     
     testkm = K[ix_(hocompl, hoindices)]
     trainX = Xtrain[hocompl]
     testX = Xtrain[hoindices]
     kwargs = {}
     kwargs['train_labels'] = Yho
     kwargs['train_features'] = trainX
     primalrls_naive = RLS.createLearner(**kwargs)
     
     loglambdas = range(-5, 5)
     for j in range(0, len(loglambdas)):
         regparam = 2. ** loglambdas[j]
         print
         print "Regparam 2^%1d" % loglambdas[j]
         
         dumbho = testkm.T * la.inv(Kho + regparam * eye(Kho.shape[0])) * Yho
         print dumbho, 'Dumb HO (dual)'
         
         dualrls_naive.solve(regparam)
         predho1 = dualrls_naive.getModel().predict(testkm.T)
         print predho1, 'Naive HO (dual)'
         
         dualrls.solve(regparam)
         predho2 = dualrls.computeHO(hoindices)
         print predho2, 'Fast HO (dual)'
         
         dualrls.solve(regparam)
         predho = dualrls.computeLOO()[hoindices[0]]
         print predho, 'Fast LOO (dual)'
         
         primalrls_naive.solve(regparam)
         predho3 = primalrls_naive.getModel().predict(testX)
         print predho3, 'Naive HO (primal)'
         
         primalrls.solve(regparam)
         predho4 = primalrls.computeHO(hoindices)
         print predho4, 'Fast HO (primal)'
         for predho in [predho1, predho2, predho3, predho4]:
             self.assertEqual(dumbho.shape, predho.shape)
             for row in range(predho.shape[0]):
                 for col in range(predho.shape[1]):
                     self.assertAlmostEqual(dumbho[row,col],predho[row,col])
         primalrls.solve(regparam)
         predho = primalrls.computeLOO()[hoindices[0]]
         print predho, 'Fast LOO (primal)'
     print
     hoindices = range(100, 300)
     hocompl = list(set(range(m)) - set(hoindices))
     
     Kho = K[ix_(hocompl, hocompl)]
     Yho = Y[hocompl]
     testkm = K[ix_(hocompl, hoindices)]
     
     dumbho = testkm.T * la.inv(Kho + regparam * eye(Kho.shape[0])) * Yho
     
     kwargs = {}
     kwargs['train_labels'] = Yho
     kwargs['kernel_matrix'] = Kho
     dualrls_naive = RLS.createLearner(**kwargs)
     dualrls_naive.solve(regparam)
     predho1 = dualrls_naive.getModel().predict(testkm.T)
     print sum(abs(predho1-dumbho)), 'Naive HO (dual)'
     
     dualrls.solve(regparam)
     predho2 = dualrls.computeHO(hoindices)
     print sum(abs(predho2-dumbho)), 'Fast HO (dual)'
Exemple #9
0
 def testRLS(self):
     
     print
     print
     print
     print
     print "Testing the cross-validation routines of the RLS module."
     print
     print
     floattype = float64
     
     m, n = 100, 300
     Xtrain = random.rand(m, n)
     ylen = 1
     Y = mat(zeros((m, ylen), dtype=floattype))
     Y = mat(random.rand(m, 1))
     basis_vectors = [0,3,7,8]
     
     def complement(indices, m):
         compl = range(m)
         for ind in indices:
             compl.remove(ind)
         return compl
     
     #hoindices = [45, 50, 55]
     hoindices = [0, 1, 2]
     hocompl = complement(hoindices, m)
     
     #bk = LinearKernel.Kernel()
     #bk = GaussianKernel.Kernel()
     bk = GaussianKernel.createKernel(**{'train_features':Xtrain[basis_vectors], 'gamma':'0.001'})
     rk = RsetKernel.createKernel(**{'base_kernel':bk, 'basis_features':Xtrain[basis_vectors], 'train_features':Xtrain})
     
     rpool = {}
     rpool['train_features'] = Xtrain
     bk2 = GaussianKernel.createKernel(**{'train_features':Xtrain, 'gamma':'0.001'})
     K = np.mat(bk2.getKM(Xtrain))
     
     Kho = K[ix_(hocompl, hocompl)]
     Yho = Y[hocompl]
     
     #rpool = {}
     #rpool['train_labels'] = Y
     #rpool['kernel_matrix'] = K[basis_vectors]
     #rpool['basis_vectors'] = basis_vectors
     #dualrls = RLS.createLearner(**rpool)
     
     rpool = {}
     rpool['train_labels'] = Y
     rpool['train_features'] = Xtrain
     rpool['basis_vectors'] = Xtrain[basis_vectors]
     primalrls = RLS.createLearner(**rpool)
     
     testkm = K[ix_(hocompl, hoindices)]
     Xhocompl = Xtrain[hocompl]
     testX = Xtrain[hoindices]
     
     rpool = {}
     rpool['train_labels'] = Yho
     rpool['train_features'] = Xhocompl
     rk = RsetKernel.createKernel(**{'base_kernel':bk, 'basis_features':Xtrain[basis_vectors], 'train_features':Xhocompl})
     rpool['kernel_obj'] = rk
     dualrls_naive = RLS.createLearner(**rpool)
     
     rpool = {}
     rpool['train_labels'] = Yho
     rpool['train_features'] = Xhocompl
     primalrls_naive = RLS.createLearner(**rpool)
     
     rsaK = K[:, basis_vectors] * la.inv(K[ix_(basis_vectors, basis_vectors)]) * K[basis_vectors]
     rsaKho = rsaK[ix_(hocompl, hocompl)]
     rsa_testkm = rsaK[ix_(hocompl, hoindices)]
     loglambdas = range(-5, 5)
     for j in range(0, len(loglambdas)):
         regparam = 2. ** loglambdas[j]
         print
         print "Regparam 2^%1d" % loglambdas[j]
         
         print (rsa_testkm.T * la.inv(rsaKho + regparam * eye(rsaKho.shape[0])) * Yho).T, 'Dumb HO (dual)'
         dumbho = np.squeeze(np.array(rsa_testkm.T * la.inv(rsaKho + regparam * eye(rsaKho.shape[0])) * Yho))
         
         dualrls_naive.solve(regparam)
         predho1 = np.squeeze(dualrls_naive.getModel().predict(testX))
         print predho1.T, 'Naive HO (dual)'
         
         #dualrls.solve(regparam)
         #predho2 = np.squeeze(dualrls.computeHO(hoindices))
         #print predho2.T, 'Fast HO (dual)'
         
         for predho in [dumbho, predho1]:#, predho2]:
             self.assertEqual(dumbho.shape, predho.shape)
             for row in range(predho.shape[0]):
                 #for col in range(predho.shape[1]):
                 #    self.assertAlmostEqual(dumbho[row,col],predho[row,col])
                     self.assertAlmostEqual(dumbho[row],predho[row])