Beispiel #1
0
# Folds Index
Folds_index = np.loadtxt('{}/{}/folds_py.dat'.format(root_path, data_name),
                         delimiter=',')
folds_index = cp.asarray(Folds_index)

types = cp.unique(dataY)
n_types = types.size
n_CV = folds_index.shape[1]
# One hot coding for the target
dataY_tmp = cp.zeros((dataY.size, n_types))
for i in range(n_types):
    for j in range(dataY.size):  # remove this loop
        if dataY[j] == types[i]:
            dataY_tmp[j, i] = 1

option = op(N=256, L=32, C=2**-6, scale=1, seed=1, nCV=0)
N_range = [256, 512, 1024]
# N_range = [64, 128, 256, 512]
# N_range = [16, 32, 64]
L = 32
option.scale = 1
# C_range = np.append(0,2.**np.arange(-6, 12, 2))
C_range = 2.**np.arange(-6, 12, 2)

Models_tmp = []
Models = []
# dataX = rescale(dataX) #####delete

train_acc_result = cp.zeros((n_CV, 1))
test_acc_result = cp.zeros((n_CV, 1))
train_time_result = cp.zeros((n_CV, 1))
Beispiel #2
0
U_dataY = np.unique(dataY)
nclass = U_dataY.size
dataY_temp = np.zeros((dataY.size, nclass))

# 0-1 coding for the target
for i in range(nclass):
    for j in range(dataY.size):  # remove this loop
        if dataY[j] == U_dataY[i]:
            dataY_temp[j, i] = 1

dataX = rescale(dataX)

trainX = dataX[train_indx, :]
trainY = dataY_temp[train_indx, :]
testX = dataX[test_indx, :]
testY = dataY_temp[test_indx, :]

# default values, you need to tune them for best results
option = op(100, 10, pow(2, -2), pow(2, 0.5), 0)
option.N = 100
option.L = 4
option.C = 2**6
option.scale = 1
option.seed = 0

[model, train_acc, test_acc, training_time,
 testing_time] = MRVFL(trainX, trainY, testX, testY, option)
print(train_acc)
print(test_acc)
Beispiel #3
0
def main(dataset, device_number):
    root_path = '/home/hu/eRVFL/UCIdata'
    # data_name = 'cardiotocography-10clases'
    data_name = dataset
    # n_device = 6
    n_device = device_number
    print('Dataset Name:{}\nDevice Number:{}'.format(data_name, n_device))
    logging.debug('Dataset Name:{}\tDevice Number:{}'.format(
        data_name, n_device))
    cp.cuda.Device(n_device).use()
    cp.cuda.Device(n_device).use()
    # load dataset
    # dataX
    datax = np.loadtxt('{0}/{1}/{1}_py.dat'.format(root_path, data_name),
                       delimiter=',')
    dataX = cp.asarray(datax)
    # dataY
    datay = np.loadtxt('{}/{}/labels_py.dat'.format(root_path, data_name),
                       delimiter=',')
    dataY = cp.asarray(datay)

    # Validation Index
    Validation = np.loadtxt('{}/{}/validation_folds_py.dat'.format(
        root_path, data_name),
                            delimiter=',')
    validation = cp.asarray(Validation)

    # Folds Index
    Folds_index = np.loadtxt('{}/{}/folds_py.dat'.format(root_path, data_name),
                             delimiter=',')
    folds_index = cp.asarray(Folds_index)

    types = cp.unique(dataY)
    n_types = types.size
    n_CV = folds_index.shape[1]
    # One hot coding for the target
    dataY_tmp = cp.zeros((dataY.size, n_types))
    for i in range(n_types):
        for j in range(dataY.size):  # remove this loop
            if dataY[j] == types[i]:
                dataY_tmp[j, i] = 1

    option = op(N=256,
                L=16,
                C=2**-6,
                scale=1,
                seed=1,
                nCV=0,
                ratio=0,
                mode='merged',
                drop=0)
    if dataX.shape[1] <= 16:
        N_range = [16, 32, 64]
        #N_range = [1024, 2048, 4096]
    elif dataX.shape[1] <= 64:
        N_range = [64, 128, 256, 512]
    else:
        N_range = [128, 256, 512, 1024]
    option.L = 16
    option.scale = 1
    C_range = np.append(0, 2.**np.arange(-6, 12, 2))
    # C_range = 2.**np.arange(-6, 12, 2)

    Models = []
    # dataX = rescale(dataX) #####delete

    train_acc_result = cp.zeros((n_CV, 1))
    test_acc_result = cp.zeros((n_CV, 1))
    train_time_result = cp.zeros((n_CV, 1))
    test_time_result = cp.zeros((n_CV, 1))

    option_best = op(N=256,
                     L=32,
                     C=2**-6,
                     scale=1,
                     seed=0,
                     nCV=0,
                     ratio=0,
                     mode='merged',
                     drop=0)
    option_sbest = op(N=256,
                      L=32,
                      C=2**-6,
                      scale=1,
                      seed=0,
                      nCV=0,
                      ratio=0,
                      mode='merged',
                      drop=0)
    option_tbest = op(N=256,
                      L=32,
                      C=2**-6,
                      scale=1,
                      seed=0,
                      nCV=0,
                      ratio=0,
                      mode='merged',
                      drop=0)
    for i in range(n_CV):
        MAX_acc = 0
        sMAX_acc = 0
        tMAX_acc = 0
        train_idx = cp.where(folds_index[:, i] == 0)[0]
        test_idx = cp.where(folds_index[:, i] == 1)[0]
        trainX = dataX[train_idx, :]
        trainY = dataY_tmp[train_idx, :]
        testX = dataX[test_idx, :]
        testY = dataY_tmp[test_idx, :]
        st = time.time()
        for n in N_range:
            option.N = n
            for j in C_range:
                option.C = j
                for r in cp.arange(0, 0.6, 0.1):
                    option.ratio = r
                    # for d in [0]:
                    for d in cp.arange(0, 0.6, 0.2):
                        option.drop = d
                        train_idx_val = cp.where(validation[:, i] == 0)[0]
                        test_idx_val = cp.where(validation[:, i] == 1)[0]
                        trainX_val = dataX[train_idx_val, :]
                        trainY_val = dataY_tmp[train_idx_val, :]
                        testX_val = dataX[test_idx_val, :]
                        testY_val = dataY_tmp[test_idx_val, :]
                        [
                            model_tmp, train_acc_temp, test_acc_temp,
                            training_time_temp, testing_time_temp
                        ] = MRVFL(trainX_val, trainY_val, testX_val, testY_val,
                                  option)
                        del model_tmp
                        cp._default_memory_pool.free_all_blocks()
                        while (test_acc_temp > tMAX_acc).any():
                            if (test_acc_temp > MAX_acc).any():
                                tMAX_acc = sMAX_acc
                                sMAX_acc = MAX_acc
                                MAX_acc = test_acc_temp.max()
                                option_best.acc_test = test_acc_temp.max()
                                option_best.acc_train = train_acc_temp.max()
                                option_best.C = option.C
                                option_best.N = option.N
                                option_best.L = cp.int(test_acc_temp.argmax() +
                                                       1)
                                option_best.scale = option.scale
                                option_best.nCV = i
                                option_best.ratio = r
                                option_best.drop = d
                                test_acc_temp[test_acc_temp.argmax()] = 0
                                print('Temp Best Option:{}'.format(
                                    option_best.__dict__))
                                logging.debug('Temp Best Option:{}'.format(
                                    option_best.__dict__))
                            elif (test_acc_temp > sMAX_acc).any():
                                tMAX_acc = sMAX_acc
                                sMAX_acc = test_acc_temp.max()
                                option_sbest.acc_test = test_acc_temp.max()
                                option_sbest.acc_train = train_acc_temp.max()
                                option_sbest.C = option.C
                                option_sbest.N = option.N
                                option_sbest.L = cp.int(
                                    test_acc_temp.argmax() + 1)
                                option_sbest.scale = option.scale
                                option_sbest.nCV = i
                                option_sbest.ratio = r
                                option_sbest.drop = d
                                test_acc_temp[test_acc_temp.argmax()] = 0
                                print('Temp Second Best Option:{}'.format(
                                    option_sbest.__dict__))
                                logging.debug(
                                    'Temp Second Best Option:{}'.format(
                                        option_sbest.__dict__))
                            elif (test_acc_temp > tMAX_acc).any():
                                tMAX_acc = test_acc_temp.max()
                                option_tbest.acc_test = test_acc_temp.max()
                                option_tbest.acc_train = train_acc_temp.max()
                                option_tbest.C = option.C
                                option_tbest.N = option.N
                                option_tbest.L = cp.int(
                                    test_acc_temp.argmax() + 1)
                                option_tbest.scale = option.scale
                                option_tbest.nCV = i
                                option_tbest.ratio = r
                                option_tbest.drop = d
                                test_acc_temp[test_acc_temp.argmax()] = 0
                                print('Temp Third Best Option:{}'.format(
                                    option_tbest.__dict__))
                                logging.debug(
                                    'Temp Third Best Option:{}'.format(
                                        option_tbest.__dict__))
                        #print('Training Time for one option set:{:.2f}'.format(time.time() - st))
                        #logging.debug('Training Time for one option set:{:.2f}'.format(time.time() - st))
        [model_RVFL0, train_acc0, test_acc0, train_time0,
         test_time0] = MRVFL(trainX, trainY, testX, testY, option_best)
        [model_RVFL1, train_acc1, test_acc1, train_time1,
         test_time1] = MRVFL(trainX, trainY, testX, testY, option_sbest)
        [model_RVFL2, train_acc2, test_acc2, train_time2,
         test_time2] = MRVFL(trainX, trainY, testX, testY, option_tbest)
        best_index = cp.argmax(
            cp.array([test_acc0.max(),
                      test_acc1.max(),
                      test_acc2.max()]))
        print('Best Index:{}'.format(best_index))
        print('Training Time for one fold set:{:.2f}'.format(time.time() - st))
        logging.debug(
            'Best Index:{}\nTraining Time for one fold set:{:.2f}'.format(
                best_index,
                time.time() - st))

        model_RVFL = eval('model_RVFL{}'.format(best_index))
        Models.append(model_RVFL)
        train_acc_result[i] = eval('train_acc{}.max()'.format(best_index))
        test_acc_result[i] = eval('test_acc{}.max()'.format(best_index))
        train_time_result[i] = eval('train_time{}'.format(best_index))
        test_time_result[i] = eval('test_time{}'.format(best_index))
        del model_RVFL
        cp._default_memory_pool.free_all_blocks()
        print(
            'Best Train accuracy in fold{}:{}\nBest Test accuracy in fold{}:{}'
            .format(i, train_acc_result[i], i, test_acc_result[i]))
        logging.debug(
            'Best Train accuracy in fold{}:{}\nBest Test accuracy in fold{}:{}'
            .format(i, train_acc_result[i], i, test_acc_result[i]))
    mean_train_acc = train_acc_result.mean()
    mean_test_acc = test_acc_result.mean()
    print('Train accuracy:{}\nTest accuracy:{}'.format(train_acc_result,
                                                       test_acc_result))
    logging.debug('Train accuracy:{}\nTest accuracy:{}'.format(
        train_acc_result, test_acc_result))
    print('Mean train accuracy:{}\nMean test accuracy:{}'.format(
        mean_train_acc, mean_test_acc))
    logging.debug('Mean train accuracy:{}\tMean test accuracy:{}'.format(
        mean_train_acc, mean_test_acc))
    save_result = open('Model_{}'.format(data_name), 'wb')
    pickle.dump(Models, save_result)
    save_result.close()
Beispiel #4
0
# Folds Index
Folds_index = np.loadtxt('{}/{}/folds_py.dat'.format(root_path, data_name), delimiter=',')
folds_index = cp.asarray(Folds_index)

types = cp.unique(dataY)
n_types = types.size
n_CV = folds_index.shape[1]
# One hot coding for the target
dataY_tmp = cp.zeros((dataY.size, n_types))
for i in range(n_types):
    for j in range(dataY.size):  # remove this loop
        if dataY[j] == types[i]:
            dataY_tmp[j, i] = 1

option = op(N=256, L=32, C=2 ** -6, scale=1, seed=1, nCV=0, ratio=0, mode='merged', drop=0)
# N_range = [256, 512, 1024]
N_range = [64, 128, 256, 512]
# N_range = [16, 32, 64]
L = 32
option.scale = 1
C_range = np.append(0,2.**np.arange(-6, 12, 2))

Models_tmp = []
Models = []
# dataX = rescale(dataX) #####delete

train_acc_result = cp.zeros((n_CV, 1))
test_acc_result = cp.zeros((n_CV, 1))
train_time_result = cp.zeros((n_CV, 1))
test_time_result = cp.zeros((n_CV, 1))
Beispiel #5
0
dataY = data[:,[-1]]

#fix the random seed for these type of experiments where it involves randomization


# do normalization for each feature
dataX_mean=np.mean(dataX,axis=0)
dataX_std=np.std(dataX,axis=0)
dataX=(dataX-dataX_mean)/dataX_std

ACC_CV = np.zeros((1,4))
train_accuracy = np.zeros((1,4))


#Look at the documentation of RVFL_train_val function file 
option=op(100,False,True,'radbas',0,1,'Uniform',1,1)
option.N = 20
option.C = 1
option.bias = 1
option.link = 1
option.mode = 2
option.ActivationFunction='sig'
option.Scalemode=1


for i in range(0,4):

    trainX = dataX[index[2*i-2],:]
    trainY = dataY[index[2*i-2],:]