예제 #1
0
def run_testset(kernel_type='cosine',
                log_2gs=-12,
                log_2gt=-12,
                log_2w=-2,
                log_2p=-1,
                complete_flag=1,
                zero_diag_flag=True):
    dc = DataClass(valid_flag=False)
    dc.kernel_type = kernel_type
    dc.source_gamma = 2**log_2gs
    dc.target_gamma = 2**log_2gt
    dc.zero_diag_flag = zero_diag_flag
    y, I, K, offset = dc.get_TL_Kernel()

    w_2 = 2**log_2w
    p = 2**log_2p

    if complete_flag == 1:
        K = DataClass.complete_TL_Kernel(K, offset)

    # log_2p == -1 means that using full kernel w/o sparsify
    if log_2p != -1:
        K = DataClass.sym_sparsify_K(K, p)

    auc, ap, rl = solve_and_eval(y, I, K, offset, w_2)
    print('tst test: auc %6f ap %6f rl %6f' % (auc, ap, rl))
예제 #2
0
def grid_search(gList,
                wList,
                pList,
                complete_flag=False,
                zero_diag_flag=False):

    dc = DataClass(valid_flag=True,
                   zero_diag_flag=zero_diag_flag,
                   kernel_normal=False)
    y, I, K, offset = dc.get_TL_Kernel()

    n = len(y)
    f = np.zeros(n)
    best_auc = 0.0
    best_gs = 0.0
    best_gt = 0.0
    best_w = 0.0
    best_p = -1
    # pList padding
    if pList[0] != -1:
        pList = np.insert(pList, 0, -1)

    if kernel_type == 'cosine' and len(gList) != 1:
        raise Warning('For cosine kernel, no need to tune gamma!')

    for g_s in gList:
        for g_t in gList:
            dc.source_gamma = 2.0**g_s
            dc.target_gamma = 2.0**g_t
            y, I, K, offset = dc.get_TL_Kernel()
            if complete_flag == 1:
                K = DataClass.complete_TL_Kernel(K, offset)

            for w in wList:
                w_2 = 2.0**w

                for p in pList:
                    _p = 2**p
                    if p == -1:
                        K_sp = K
                    else:
                        K_sp = DataClass.sym_sparsify_K(K, _p)
                    auc, ap, rl = solve_and_eval(y, I, K_sp, offset, w_2)
                    print('log_2gs %3d log_2gt %3d log_2w %3d log_2p %3d auc %6f ap %6f rl %6f' \
                            % (g_s, g_t, w, p, auc, ap, rl))
                    if auc > best_auc:
                        best_auc = auc
                        best_gs = g_s
                        best_gt = g_t
                        best_w = w
                        best_p = p

    print('best parameters: log_2gs %3d log_2gt %3d log_2w %3d log_2p %3d auc %6f' \
            % (best_gs, best_gt, best_w, best_p, best_auc))