def forward_loss(self, input, target): self.input = input self.target = target self.logsoftmax_module = LogSoftmax() self.logsoftmax_out = self.logsoftmax_module.forward(self.input) self.batch_size, self.target_num = self.input.shape # print('target:', self.target) # print('batch_size:', self.batch_size) # print('target_num:', self.target_num) self.target_one_hot = get_one_hot(self.target, self.target_num) # print('target_one_hot:', self.target_one_hot) nll_log = -self.logsoftmax_out * self.target_one_hot # print('nll_log:', nll_log) return 1.0 / self.batch_size * np.sum(nll_log)
def main(): #set hyperparameter at here hiddenlayerlist = [[ 16, 32, 16 ]] #change the number of hidden layer, and nodes in the layer ss = 1e-2 #step Size numofiter = 300 #iterations size = 2500 #input size dim = 2 #input dimension margin = 0 #change Margin at here, change this value to 0 to make the data not linear separable output_unit = 1 algorithm = input('Select algorithm: (input ebp, r+, r-, ir+ or ir-)') # algorithm = 'r+' modeltype = input( 'Classification or Regression? (input c, r, mnist or bc)') modeltype = 'mnist' if modeltype == 'c': #generate the input and output for classification inputdata, outputdata = generatedata(size, dim, margin) #plot to viaualize if it is 1D print('Training Data Plot: ') plt.figure(1) if dim == 1: plt.scatter(inputdata[:size // 2, 0], np.ones((size // 2, 1)), color='r') plt.scatter(inputdata[size // 2:, 0], np.ones((size // 2, 1)), color='b') plt.legend(['Label 1', 'Label 0'], loc='upper right') elif dim == 2: plt.scatter(inputdata[:size // 2, 0], inputdata[:size // 2, 1], color='r') plt.scatter(inputdata[size // 2:, 0], inputdata[size // 2:, 1], color='b') plt.legend(['Label 1', 'Label 0'], loc='upper right') network = net(inputdata, outputdata, size, ss, numofiter, dim, hiddenlayerlist, modeltype, algorithm, output_unit, [], 0) network.backpropagation() output = network.forwardewithcomputedW(inputdata) #plot network computed result output = np.append(inputdata, output, axis=1) print('Network computed output: ') plt.figure(4) if dim == 1: output1 = output[output[:, -1] == 1] output2 = output[output[:, -1] == 0] plt.scatter(output1[:, 0], np.ones((np.shape(output1)[0], 1)), color='r') plt.scatter(output2[:, 0], np.ones((np.shape(output2)[0], 1)), color='b') plt.legend(['Label 1', 'Label 0'], loc='upper right') if dim == 2: output1 = output[output[:, -1] == 1] output2 = output[output[:, -1] == 0] plt.scatter(output1[:, 0], output1[:, 1], color='r') plt.scatter(output2[:, 0], output2[:, 1], color='b') plt.legend(['Label 1', 'Label 0'], loc='upper right') plt.show() elif modeltype == 'r': #generate the input and output for regression inputdata, outputdata = generatedataForRegression(size, dim) network = net(inputdata, outputdata, size, ss, numofiter, dim, hiddenlayerlist, modeltype, output_unit, [], 0) network.backpropagation() if dim == 2: fig = plt.figure(figsize=(10, 10)) ax = plt.axes(projection='3d') X = np.arange(-4, 4, 0.1) Y = np.arange(-4, 4, 0.1) X, Y = np.meshgrid(X, Y) a = X.flatten() b = Y.flatten() testx = np.append(np.reshape(a, (len(a), 1)), np.reshape(b, (len(b), 1)), axis=1) outputy = np.reshape(network.forwardewithcomputedW(testx), np.shape(X)) ax.plot_surface(X, Y, outputy, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False) elif modeltype == 'mnist': train_images, train_labels, test_images, test_labels = get_mnist() # size = train_images.shape[0] size = 60000 numofiter = 1000 dim = 28**2 hiddenlayerlist = [[1000]] # 2500, 2000, 1500, 1000, 500 output_unit = 10 ss = 5e-2 print('Algorithm: ' + algorithm + '\nModel type: ' + modeltype + '\nIterations: ' + str(numofiter) + '\nLearning rate: ' + str(ss)) # get_one_hot(train_labels[: size, :], 10) # train_labels[: size, :].flatten() network = net(train_images[:size, :], get_one_hot(train_labels[:size, :], 10), size, ss, numofiter, dim, hiddenlayerlist, modeltype, algorithm, output_unit, [], 5000) network.backpropagation() # load the saved model filename = 'wb_' + modeltype + '_' + algorithm + '_' + str( numofiter) + '.npz' wb_ini = np.load(filename)['arr_0'].tolist() network = net(train_images[:size, :], get_one_hot(train_labels[:size, :], 10), size, ss, numofiter, dim, hiddenlayerlist, modeltype, algorithm, output_unit, wb_ini, 0) # test the accuracy tst_size = 10000 tst_imgs = train_images[:tst_size] tst_lbls = train_labels[:tst_size].flatten() tst_out_raw = network.forwardewithcomputedW(tst_imgs) tst_out_cls = np.argmax(tst_out_raw, axis=1) accuracy = sum(tst_out_cls == tst_lbls) / tst_size print('test accuracy: ' + str(accuracy)) # set_trace() elif modeltype == 'bc': data = np.genfromtxt("breastCancerData.csv", delimiter=",") label = np.genfromtxt("breastCancerLabels.csv", delimiter=",") MinMaxscaler = sklearn.preprocessing.MinMaxScaler() data = np.float32(MinMaxscaler.fit_transform(data)) #Split Train and Test Data trainD, testD, trainT, testT = train_test_split(data, label, random_state=6) size = np.shape(trainD)[0] numofiter = 1000 dim = 9 hiddenlayerlist = [[80, 100, 50]] output_unit = 1 network = net(trainD, np.reshape(trainT, (len(trainT), 1)), size, ss, numofiter, dim, hiddenlayerlist, modeltype, algorithm, output_unit, [], 0) network.backpropagation() output = network.forwardewithcomputedW(testD) accuracy = sum( output == np.reshape(testT, (len(testT), 1))) / len(testT) print('test accuracy: ' + str(accuracy[0]))
print(info_list) return info_list def process_image(img): global id # resization img = misc.imresize(img, [aim_size, aim_size, 3]) print('{} finished.'.format(id)) id += 1 return img def dump_file(obj, dump_filename): with open(dump_filename, 'wb') as fout: pickle.dump(obj, fout) if __name__ == '__main__': info_list = get_info_list() result_list = [] for i, d in enumerate(info_list): if os.path.exists(d[3]): print(d[1], d[2]) if int(d[1]) < year: continue # one_hot = utils.get_one_hot([d[2]]) result_list.append( [utils.get_one_hot([d[2]]), process_image(misc.imread(d[3]))]) dump_file(result_list, dump_filename)
def al(dataset_name='seed4', FOIT_type='cross-all', rounds=10, batch_size=50): data, label = utils.load_source_data(dataset_name=dataset_name, FOIT_type=FOIT_type) _, number_label, _ = utils.get_number_of_label_n_trial(dataset_name) # data, label = utils.load_session_data_label(dataset_name, 0) # as unlabelled data cd_count = 16 if dataset_name == 'seed4' else 9 if dataset_name == 'seed3' else print( 'Wrong dataset_name') iteration_number = 3 if FOIT_type == 'cross-subject' else 15 accs = [([]) for i in range(iteration_number)] times = [([]) for i in range(iteration_number)] for ite in range(iteration_number): session_id = -1 sub_id = -1 if FOIT_type == 'cross-subject': session_id = ite sub_id = 14 elif FOIT_type == 'cross-session': session_id = 2 sub_id = ite elif FOIT_type == 'cross-all': session_id = 1 sub_id = ite else: print('Wrong FOIT type!') # print("Ite: ", ite) cd_data, cd_label, ud_data, ud_label = utils.pick_one_data( dataset_name, session_id=session_id, cd_count=cd_count, sub_id=sub_id) cd_data, cd_label = shuffle(cd_data, cd_label, random_state=0) ud_data, ud_label = shuffle(ud_data, ud_label, random_state=0) cd_data_min, cd_data_max = np.min(cd_data), np.max(cd_data) cd_data = utils.normalization(cd_data) # labelled data ud_data = utils.normalization(ud_data) # test data if FOIT_type == 'cross-all': data_ite, label_ite = data.copy(), label.copy() for i in range(len(data)): data_ite[i], label_ite[i] = shuffle(data_ite[i], label_ite[i], random_state=0) # data_ite, label_ite = shuffle(data, label, random_state=0) for i in range(len(data)): data_ite[i] = utils.norm_with_range(data_ite[i], cd_data_min, cd_data_max) # data_ite = utils.normalization(data_ite) elif FOIT_type == 'cross-session': data_ite, label_ite = data[ite], label[ite] for i in range(len(data_ite)): data_ite[i], label_ite[i] = shuffle(data_ite[i], label_ite[i], random_state=0) # data_ite[i] = utils.normalization(data_ite[i]) data_ite[i] = utils.norm_with_range(data_ite[i], cd_data_min, cd_data_max) # data_ite = utils.normalization(data_ite) else: data_ite, label_ite = data[ite], label[ite] for i in range(len(data_ite)): data_ite[i], label_ite[i] = shuffle(data_ite[i], label_ite[i], random_state=0) # data_ite, label_ite = shuffle(data_ite, label_ite, random_state=0) for i in range(len(data_ite)): # data_ite[i] = utils.normalization(data_ite[i]) data_ite[i] = utils.norm_with_range(data_ite[i], cd_data_min, cd_data_max) # data_ite, label_ite = data.copy(), label.copy() # for i in range(len(data)): # data_ite[i], label_ite[i] = shuffle(data_ite[i], label_ite[i], random_state=0) # for i in range(len(data)): # data_ite[i] = utils.norm_with_range(data_ite[i], cd_data_min, cd_data_max) # baseline clf = svm.LinearSVC(max_iter=30000) clf = CalibratedClassifierCV(clf, cv=5) since = time.time() clf.fit(cd_data, cd_label.squeeze()) time_baseline = time.time() - since scoreA = utils.test(clf, ud_data, ud_label.squeeze()) accs[ite].append(scoreA) times[ite].append(time_baseline) # select the data from the reservoir iteratively s_data_all, s_label_all = utils.stack_list(data_ite, label_ite) L_S_data = None L_S_label = None for i in range(rounds): # print("Rounds: ", i) # print(type(s_data_all)) # print(s_data_all.shape) s_data_all_predict_proba = clf.predict_proba(s_data_all) s_label_all_proba = utils.get_one_hot(s_label_all.squeeze(), number_label) confidence = np.zeros((s_label_all_proba.shape[0], 1)) for i in range(s_label_all_proba.shape[0]): confidence[i] = s_label_all_proba[i].dot( s_data_all_predict_proba[i].T) # confidence[i] = log_loss(s_label_all_proba[i], s_data_all_predict_proba[i]) indices = np.argsort(confidence, axis=0) # take the minimum topK indices topK_indices = indices[:batch_size] S_data = None S_label = None for i in topK_indices: one_data = s_data_all[i] one_label = s_label_all[i] if S_data is not None: S_data = np.vstack((S_data, one_data)) S_label = np.vstack((S_label, one_label)) else: S_data = one_data S_label = one_label for i in range(len(s_data_all) - 1, -1, -1): if i in topK_indices: s_data_all = np.delete(s_data_all, i, axis=0) s_label_all = np.delete(s_label_all, i, axis=0) if L_S_data is None: L_S_data = cd_data.copy() L_S_label = cd_label.copy() else: pass L_S_data = np.vstack((L_S_data, S_data)) L_S_label = np.vstack((L_S_label, S_label)) L_S_data, L_S_label = shuffle(L_S_data, L_S_label, random_state=0) clf.fit(L_S_data, L_S_label.squeeze()) time_updated_time = time.time() - since times[ite].append(time_updated_time) scoreTMP = utils.test(clf, ud_data, ud_label.squeeze()) accs[ite].append(scoreTMP) ResultTime = [] ResultAcc = [] ResultStd = [] for i in range(rounds + 1): tmpTime = [] tmpAcc = [] for j in range(iteration_number): tmpTime.append(times[j][i]) tmpAcc.append(accs[j][i]) ResultTime.append(np.mean(tmpTime)) ResultAcc.append(np.mean(tmpAcc)) ResultStd.append(np.std(tmpAcc)) print("Time: ", ResultTime) print("Accs: ", ResultAcc) print("Stds: ", ResultStd)
temp_clf = joblib.load(path) clf_sources.append(temp_clf) score = utils.test(temp_clf, utils.normalization(ud_data), ud_label.squeeze()) accs.append(score) # print('Accs of classifiers: {}'.format(accs)) accs = utils.normalization(accs) print('Accs of classifiers, normalized: {}'.format(accs)) ''' c) ''' rho = 0.5 s_data_all, s_label_all = utils.stack_list(sub_data_ses, sub_label_ses) s_data_all_predict_proba = clf.predict_proba( utils.normalization(s_data_all)) s_label_all_proba = utils.get_one_hot(s_label_all.squeeze(), number_label) confidence = np.zeros((s_label_all_proba.shape[0], 1)) for i in range(s_label_all_proba.shape[0]): confidence[i] = s_label_all_proba[i].dot(s_data_all_predict_proba[i].T) subs_data_0, subs_data_1, subs_data_2, subs_data_3 = [], [], [], [] conf_0, conf_1, conf_2, conf_3 = [], [], [], [] subs_label_0, subs_label_1, subs_label_2, subs_label_3 = [], [], [], [] for i in range(len(s_data_all)): temp_label = s_label_all[i][0] eval('subs_data_' + str(temp_label)).append(s_data_all[i]) eval('conf_' + str(temp_label)).append(confidence[i]) eval('subs_label_' + str(temp_label)).append(s_label_all[i]) indices = [] for i in range(4): indices.append(np.argsort(eval('conf_' + str(i)), axis=0)[::-1]) # indices.append(np.argsort(eval('conf_'+str(i)), axis=0))
import numpy as np from simple_rnn_model import SimpleRNNModel, params, SimpleRNNModel1 from utils import get_one_hot training_X = np.load("./AG/X_train.npy") training_y = np.load("./AG/y_train.npy") training_num = len(training_X) # shuffle_ids = np.array(np.arange(training_num)) # np.random.shuffle(shuffle_ids) # training_X = training_X[shuffle_ids] # training_y = training_y[shuffle_ids] test_X = np.load("./AG/X_test.npy") test_y = np.load("./AG/y_test.npy") nb_classes = 4 training_Y = get_one_hot(training_y, nb_classes) test_Y = get_one_hot(test_y, nb_classes) model = SimpleRNNModel1(params, 60, nb_classes) model.model.fit(x=training_X, y=training_Y, batch_size=64, epochs=30, callbacks=[model.early_stopping], verbose=2, validation_data=(test_X[:500], test_Y[:500]), shuffle=True) model.model.save_weights(filepath="./models/rnn") # batch_size = 64 # params["batch_size"] = batch_size # model = SimpleRNNModel(params, 60, 4)
def runModel(datagen, model, optimizer, class_wts, process, batch_size, n_batches, loss_wts): ''' process : 'trn', 'val' or 'tst' ''' running_loss = 0 pred_list = [] label_list = [] soft_pred_list = [] all_file_list = [] with trange(n_batches, desc=process, ncols=100) as t: for m in range(n_batches): data, labels, filenames = datagen.__next__() labels_one_hot = utils.get_one_hot(labels).cuda() if process == 'trn': optimizer.zero_grad() model.train() pred, aux_pred = model.forward(data) pred = F.softmax(pred, 1) aux_pred = F.softmax(aux_pred, 1) loss = 0 for i in range(2): loss += loss_wts[0] * utils.weightedBCE(class_wts[i], pred[:, i], (labels_one_hot [:, i]))\ + loss_wts[1] * utils.weightedBCE(class_wts[i], aux_pred[:, i], (labels_one_hot [:, i])) loss.backward() if torch.isnan(loss): pdb.set_trace() optimizer.step() elif process == 'val' or process == 'tst': model.eval() with torch.no_grad(): pred = F.softmax(model.forward(data), 1) loss = utils.weightedBCE(class_wts[0], pred[:, 0], labels_one_hot[:, 0])\ + utils.weightedBCE(class_wts[1], pred[:, 1], labels_one_hot[:, 1]) running_loss += loss hard_pred = torch.argmax(pred, 1) pred_list.append(hard_pred.cpu()) soft_pred_list.append(pred.detach().cpu()) label_list.append(labels.cpu()) all_file_list += filenames t.set_postfix(loss=running_loss.item() / (float(m + 1) * batch_size)) t.update() finalLoss = running_loss / (float(m + 1) * batch_size) # if process != 'trn': # pred_list, soft_pred_list, label_list = utils.test_time_aug( # all_file_list, # soft_pred_list, # label_list, 3) acc = utils.globalAcc(pred_list, label_list) if not isinstance(pred_list, torch.Tensor): f1 = sklearn.metrics.f1_score(torch.cat(label_list), torch.cat(pred_list), labels=None) else: f1 = sklearn.metrics.f1_score(label_list, pred_list, labels=None) auroc, auprc, fpr_tpr_arr, precision_recall_arr = utils.AUC( soft_pred_list, label_list) metrics = Metrics(finalLoss, acc, f1, auroc, auprc, fpr_tpr_arr, precision_recall_arr) utils.save_preds(soft_pred_list, pred_list, label_list, all_file_list, args.savename, process) return metrics
def FOIT(dataset_name='seed4', rho=1, clf_name='lr', threshold=0.6, with_balance=True, FOIT_type='cross-all'): # time and accs c0_acc = [] c0u_acc = [] foit_acc = [] time_c0 = [] time_c0u = [] time_foit = [] cd_count = 16 if dataset_name == 'seed4' else 9 if dataset_name == 'seed3' else print( 'Wrong dataset_name') iteration_number = 3 if FOIT_type == 'cross-subject' else 15 number_trial, number_label, labels = utils.get_number_of_label_n_trial( dataset_name) data, label = utils.load_source_data(dataset_name=dataset_name, FOIT_type=FOIT_type) for ite in range(iteration_number): # The data of 15th sub is taken as the cd and ud data in cross-subject for each session (iteration_number=3) # The data of 3rd session is taken as the cd and ud data in cross-session for each subject (iteration_number=14) # For each iteration, the data of one sub from session 2 is taken as the cd and ud data in cross-all situation # print("Iteration: {}".format(ite)) ''' Parameters ''' session_id = -1 sub_id = -1 accs_number = -1 if FOIT_type == 'cross-subject': session_id = ite sub_id = 14 accs_number = 14 elif FOIT_type == 'cross-session': session_id = 2 sub_id = ite accs_number = 2 elif FOIT_type == 'cross-all': session_id = 1 sub_id = ite accs_number = 15 else: print('Wrong FOIT type!') ''' Data ''' # data cd_data, cd_label, ud_data, ud_label = utils.pick_one_data( dataset_name, session_id=session_id, cd_count=cd_count, sub_id=sub_id) cd_data, cd_label = shuffle(cd_data, cd_label, random_state=0) ud_data, ud_label = shuffle(ud_data, ud_label, random_state=0) cd_data_min, cd_data_max = np.min(cd_data), np.max(cd_data) cd_data = utils.normalization(cd_data) ud_data = utils.normalization(ud_data) if FOIT_type == 'cross-all': data_ite, label_ite = data.copy(), label.copy() for i in range(len(data)): data_ite[i], label_ite[i] = shuffle(data_ite[i], label_ite[i], random_state=0) # data_ite, label_ite = shuffle(data, label, random_state=0) for i in range(len(data)): data_ite[i] = utils.norm_with_range(data_ite[i], cd_data_min, cd_data_max) # data_ite = utils.normalization(data_ite) elif FOIT_type == 'cross-session': data_ite, label_ite = data[ite], label[ite] for i in range(len(data_ite)): data_ite[i], label_ite[i] = shuffle(data_ite[i], label_ite[i], random_state=0) # data_ite[i] = utils.normalization(data_ite[i]) data_ite[i] = utils.norm_with_range(data_ite[i], cd_data_min, cd_data_max) # data_ite = utils.normalization(data_ite) else: data_ite, label_ite = data[ite], label[ite] for i in range(len(data_ite)): data_ite[i], label_ite[i] = shuffle(data_ite[i], label_ite[i], random_state=0) # data_ite, label_ite = shuffle(data_ite, label_ite, random_state=0) for i in range(len(data_ite)): # data_ite[i] = utils.normalization(data_ite[i]) data_ite[i] = utils.norm_with_range(data_ite[i], cd_data_min, cd_data_max) # data_ite = utils.normalization(data_ite) # print(len(data_ite), len(label_ite[0]), len(label_ite[0][0])) ''' A) ''' if clf_name == 'svm': clf = svm.LinearSVC(max_iter=30000) # clf = svm.SVC(probability=True, max_iter=10000) elif clf_name == 'lr': clf = LogisticRegression(max_iter=30000) else: print('Unexcepted clf name, using LR as the baseline now.') clf = svm.LinearSVC(max_iter=30000) clf = CalibratedClassifierCV(clf, cv=5) since = time.time() clf.fit(cd_data, cd_label.squeeze()) time_baseline = time.time() - since # print('Baseline training complete in {:.4f}'.format(time_baseline)) scoreA = utils.test(clf, ud_data, ud_label.squeeze()) # print('Baseline score: {}'.format(scoreA)) time_c0.append(time_baseline) c0_acc.append(scoreA) ''' B) ''' accs = [] clf_sources = [] for i in range(accs_number): if FOIT_type == 'cross-subject': path = 'models/' + dataset_name + '/csu/sesn' + str( ite) + '/lr' + str(i) + '.m' elif FOIT_type == 'cross-session': path = 'models/' + dataset_name + '/csn/sub' + str( ite) + '/lr' + str(i) + '.m' else: path = 'models/' + dataset_name + '/csun/lr' + str(i) + '.m' temp_clf = joblib.load(path) clf_sources.append(temp_clf) score = utils.test(temp_clf, ud_data, ud_label.squeeze()) accs.append(score) if FOIT_type == 'cross-session': pass else: accs = utils.normalization(accs) # print('Accs of classifiers, normalized: {}'.format(accs)) ''' C) ''' s_data_all, s_label_all = utils.stack_list(data_ite, label_ite) s_data_all_predict_proba = clf.predict_proba(s_data_all) s_label_all_proba = utils.get_one_hot(s_label_all.squeeze(), number_label) confidence = np.zeros((s_label_all_proba.shape[0], 1)) for i in range(s_label_all_proba.shape[0]): confidence[i] = s_label_all_proba[i].dot( s_data_all_predict_proba[i].T) if with_balance: ## divide into 4 categories data_ite_divided = [] label_ite_divided = [] conf_divided = [] for i in range(number_label): data_ite_divided.append([]) label_ite_divided.append([]) conf_divided.append([]) for i in range(len(s_data_all)): temp_label = s_label_all[i][0] data_ite_divided[temp_label].append(s_data_all[i]) label_ite_divided[temp_label].append(s_label_all[i]) conf_divided[temp_label].append(confidence[i]) indices = [] for i in range(number_label): indices.append(np.argsort(conf_divided[i], axis=0)[::-1]) topK_indices = [ indices[i][:int(rho * len(cd_label) / 4)] for i in range(len(indices)) ] S_data = None S_label = None for i in range(len(topK_indices)): for j in topK_indices[i]: # temp_conf = conf_divided[i][j[0]] one_data = data_ite_divided[i][j[0]] one_label = label_ite_divided[i][j[0]] if S_data is not None: S_data = np.vstack((S_data, one_data)) S_label = np.vstack((S_label, one_label)) else: S_data = one_data S_label = one_label elif with_balance is False: indices = np.argsort(confidence, axis=0)[::-1] topK_indices = indices[:int(rho * len(cd_label))] S_data = None S_label = None for i in topK_indices: one_data = s_data_all[i] one_label = s_label_all[i] if S_data is not None: S_data = np.vstack((S_data, one_data)) S_label = np.vstack((S_label, one_label)) else: S_data = one_data S_label = one_label else: print('Unexcepted value of with balance!') raise EnvironmentError ''' D) ''' L_S_data = cd_data.copy() L_S_label = cd_label.copy() L_S_data = np.vstack((L_S_data, S_data)) L_S_label = np.vstack((L_S_label, S_label)) L_S_data, L_S_label = shuffle(L_S_data, L_S_label, random_state=0) # L_S_data = utils.normalization(L_S_data) # to decide clf.fit(L_S_data, L_S_label.squeeze()) time_updated_baseline = time.time() - since # print('Updated baseline training complete in {:.4f}s'.format(time_updated_baseline)) time_c0u.append(time_updated_baseline) scoreD = utils.test(clf, ud_data, ud_label.squeeze()) # print('Updated model score: {}'.format(scoreD)) c0u_acc.append(scoreD) ''' E) ''' weight = (len(accs) + 1) / 2 proba_result_all = clf.predict_proba(ud_data) * weight if FOIT_type == 'cross-session': weight_for_clfs = utils.decide_which_clf_to_use(scoreD, accs) for j in range(len(weight_for_clfs)): proba_result_all += clf_sources[j].predict_proba( utils.normalization(ud_data)) * weight_for_clfs[j] else: for i in range(len(clf_sources)): if accs[i] > threshold: proba_result_all += clf_sources[i].predict_proba( ud_data) * accs[i] corrects = np.sum( np.argmax(proba_result_all, axis=1) == ud_label.squeeze()) time_ensemble = time.time() - since time_foit.append(time_ensemble) scoreE = corrects / len(ud_label) # print('Ensembled model score: {}'.format(scoreE)) foit_acc.append(scoreE) # print(c0_acc) # print(c0u_acc) # print(foit_acc) print('Mean acc and std of A: {} {}'.format(np.mean(c0_acc), np.std(c0_acc))) print('Mean acc and std of D: {} {}'.format(np.mean(c0u_acc), np.std(c0u_acc))) print('Mean acc and std of E: {} {}'.format(np.mean(foit_acc), np.std(foit_acc))) print("Time cost for training baseline: ", np.mean(time_c0)) print("Time cost for training updated baseline: ", np.mean(time_c0u)) print("Time cost for training FOIT: ", np.mean(time_foit))
return avatar_list def process_image(img): global id # resization img = misc.imresize(img, [aim_size, aim_size, 3]) print('{} finished.'.format(id)) id += 1 return img def dump_file(obj, dump_filename): with open(dump_filename, 'wb') as fout: pickle.dump(obj, fout) if __name__ == '__main__': avatar_list = get_avatar_with_tag(avatar_tag_path) result_list = [] for i, each in enumerate(avatar_list): if os.path.exists(each[3]): if int(each[1]) < 2005: continue # tag's one-hot, image-bytes result_list.append([ utils.get_one_hot(each[2]), process_image(misc.imread(each[3])) ]) dump_file(result_list, dump_filename)
def train(self, graphs_train, graphs_test=None, graphs_valid=None) -> None: """ Trains model with training set in multiple epochs. Args: graphs_train: A list of graphs as train set. graphs_test: A list of graphs as test set. graphs_valid: A list of graphs as validation set. """ # Enrich graphs with adj list for graph in graphs_train: graph[utils.AE.ADJ_LIST] = utils.graph_to_adjacency_lists( graph[utils.T.EDGES], self.config['tie_fwd_bkwd'] == 1)[0] # Extract graph sizes graph_sizes = [] for graph in graphs_train: graph_sizes.append(len(graph[utils.T.NODES])) best_epoch_loss = sys.maxsize best_epoch_accuracy = 0 best_epoch_count = 0 if graphs_valid: # Extract valid labels y_valid = [] for graph in graphs_valid: y_valid.append(graph[utils.L.LABEL_0]) y_valid = np.array(y_valid) if graphs_test: # Extract test labels y_test = [] for graph in graphs_test: y_test.append(graph[utils.L.LABEL_0]) y_test = np.array(y_test) # Run epochs for epoch in range(0, self.config['num_epochs']): # Training # ############################################ epoch_start_time = time.time() # Partition into batches batch_size = self.config['batch_size'] lst = list(zip(graphs_train, graph_sizes)) np.random.shuffle(lst) batches = [ lst[i * batch_size:(i + 1) * batch_size] for i in range((len(lst) + batch_size - 1) // batch_size) ] # Run batches training_losses = [] training_good_predicted_all = [] epoch_instances_per_secs = [] for batch in batches: start_time = time.time() batch_graphs, batch_graph_sizes = zip(*batch) batch_graphs = list(batch_graphs) batch_graph_sizes = list(batch_graph_sizes) # Extract train labels y_train = [] for graph in batch_graphs: y_train.append(graph[utils.L.LABEL_0]) y_train = np.array(y_train) # Build feed dict # 1. Graph info feed_dict = self._graphs_to_batch_feed_dict( batch_graphs, batch_graph_sizes, True) # Run batch result = self._run_batch(feed_dict) end_time = time.time() # Log instances_per_sec = len(batch_graphs) / (end_time - start_time) epoch_instances_per_secs.append(instances_per_sec) training_losses.append(result[0]) # Evaluate predictions on train set predictions = result[1] training_good_predicted = list( np.argmax(predictions, axis=1) == y_train) training_good_predicted_all += training_good_predicted training_accuracy = np.sum(training_good_predicted_all) / len( training_good_predicted_all) training_loss = np.sum(training_losses) epoch_instances_per_sec = np.mean(epoch_instances_per_secs) epoch_end_time = time.time() epoch_time = epoch_end_time - epoch_start_time # Logging # summary = tf.Summary() # summary.value.add(tag='train_accuracy', simple_value=training_accuracy) # summary.value.add(tag='train_loss', simple_value=training_loss) # Testing # ############################################ if graphs_valid and graphs_test: # Make predictions on valid set predictions = self.predict(graphs_valid) valid_loss = np.sum(predictions - utils.get_one_hot( y_valid, self.config['prediction_cell']['output_dim'])) valid_accuracy = np.sum( np.argmax(predictions, axis=1) == y_valid) / len( predictions) # Make predictions on test set predictions = self.predict(graphs_test) test_loss = np.sum(predictions - utils.get_one_hot( y_test, self.config['prediction_cell']['output_dim'])) test_accuracy = np.sum( np.argmax(predictions, axis=1) == y_test) / len( predictions) # Logging # summary.value.add(tag='valid_accuracy', simple_value=valid_accuracy) # summary.value.add(tag='test_accuracy', simple_value=test_accuracy) # print('epoch: %i, instances/sec: %.2f, epoch_time: %.2fs, train_loss: %.8f, train_accuracy: %.4f, valid_accuracy: %.4f, test_accuracy: %.4f' % (epoch, epoch_instances_per_sec, epoch_time, training_loss, training_accuracy, valid_accuracy, test_accuracy)) if valid_accuracy > best_epoch_accuracy: best_epoch_accuracy = valid_accuracy best_epoch_count += 1 if 'save_best_model_interval' in self.config and best_epoch_count >= self.config[ 'save_best_model_interval']: self.state.backup_best_weights() best_epoch_count = 0 elif graphs_test: # Make predictions on test set predictions = self.predict(graphs_test) test_loss = np.sum(predictions - utils.get_one_hot( y_test, self.config['prediction_cell']['output_dim'])) test_accuracy = np.sum( np.argmax(predictions, axis=1) == y_test) / len( predictions) # Logging # summary.value.add(tag='test_accuracy', simple_value=test_accuracy) # summary.value.add(tag='test_loss', simple_value=test_loss) # print('epoch: %i, instances/sec: %.2f, epoch_time: %.2fs, train_loss: %.8f, train_accuracy: %.4f, test_accuracy: %.4f' % (epoch, epoch_instances_per_sec, epoch_time, training_loss, training_accuracy, test_accuracy)) if training_loss < best_epoch_loss: best_epoch_loss = training_loss best_epoch_count += 1 if 'save_best_model_interval' in self.config and best_epoch_count >= self.config[ 'save_best_model_interval']: self.state.backup_best_weights() best_epoch_count = 0 else: # Logging # print('epoch: %i, instances/sec: %.2f, epoch_time: %.2fs, loss: %.8f' % (epoch, epoch_instances_per_sec, epoch_time, training_loss)) if training_loss < best_epoch_loss: best_epoch_loss = training_loss best_epoch_count += 1 if 'save_best_model_interval' in self.config and best_epoch_count >= self.config[ 'save_best_model_interval']: self.state.backup_best_weights() best_epoch_count = 0 # Logging #self.train_writer.add_summary(summary, epoch) self.state.restore_best_weights()
def _graphs_to_batch_feed_dict(self, graphs: list, graph_sizes: list, is_training: bool) -> dict: """Creates feed dict that is the format the tf model is trained with.""" num_edge_types = self.config['num_edge_types'] batch_data = { # Graph model 'adjacency_lists': [[] for _ in range(self.config['num_edge_types'])], 'embeddings_to_graph_mappings': [], 'labels': [], 'embeddings_in': [], 'node_values': [], } if self.with_aux_in: batch_data['aux_in'] = [] for graph_idx, graph in enumerate(graphs): num_nodes = graph_sizes[graph_idx] if self.config['use_edge_bias'] == 1: batch_data['num_incoming_edges_dicts_per_type'] = [] # Aux in if self.with_aux_in: if utils.I.AUX_IN_0 in graph: batch_data['aux_in'].append(graph[utils.I.AUX_IN_0]) # Labels if utils.L.LABEL_0 in graph: batch_data['labels'].append(graph[utils.L.LABEL_0]) # Graph model: Adj list adj_lists = graph[utils.AE.ADJ_LIST] for idx, adj_list in adj_lists.items(): if idx >= self.config['num_edge_types']: continue batch_data['adjacency_lists'][idx].append(adj_list) if self.config['use_edge_bias'] == 1: # Graph model: Incoming edge numbers num_incoming_edges_dicts_per_type = action[ utils.AE. NUMS_INCOMING_EDGES_BY_TYPE] if action else utils.graph_to_adjacency_lists( [], self.config['tie_fwd_bkwd'], self.config['edge_type_filter'])[0] num_incoming_edges_per_type = np.zeros( (num_nodes, num_edge_types)) for (e_type, num_incoming_edges_per_type_dict ) in num_incoming_edges_dicts_per_type.items(): for (node_id, edge_count ) in num_incoming_edges_per_type_dict.items(): num_incoming_edges_per_type[node_id, e_type] = edge_count batch_data['num_incoming_edges_dicts_per_type'].append( num_incoming_edges_per_type) graph_mappings_all = np.full(num_nodes, graph_idx) batch_data['embeddings_to_graph_mappings'].append( graph_mappings_all) node_types = utils.get_one_hot( np.array(graph[utils.T.NODES]), self.config['hidden_size_orig']).astype(float) batch_data['embeddings_in'].append(node_types) if self.config['use_node_values'] == 1: node_values = np.array( graph[utils.T.NODE_VALUES]).astype(float).reshape(-1, 1) node_values = node_values / 2147483647 batch_data['node_values'].append(node_values) # Build feed dict feed_dict = {} # Graph model: Adj list for idx, adj_list in enumerate( self.ggnn_layers[0].placeholders['adjacency_lists']): feed_dict[adj_list] = np.array(batch_data['adjacency_lists'][idx]) if len(feed_dict[adj_list]) == 0: feed_dict[adj_list] = np.zeros((0, 2), dtype=np.int32) else: feed_dict[adj_list] = feed_dict[adj_list][0] if self.config['use_edge_bias'] == 1: # Graph model: Incoming edge numbers feed_dict[self.ggnn_layers[0].placeholders['num_incoming_edges_per_type']] \ = np.concatenate(batch_data['num_incoming_edges_dicts_per_type'], axis=0) # Is training feed_dict[self.cells[0].placeholders['is_training']] = is_training # Aux in if self.with_aux_in: feed_dict[ self.cells[0].placeholders['aux_in']] = batch_data['aux_in'] # Labels if 'labels' in self.cells[0].placeholders: feed_dict[ self.cells[0].placeholders['labels']] = utils.get_one_hot( np.array(batch_data['labels']), self.config['prediction_cell']['output_dim']) # Embeddings feed_dict[self.placeholders['embeddings_in']] = np.concatenate( batch_data['embeddings_in'], axis=0) feed_dict[self.cells[0].placeholders['embeddings_to_graph_mappings']] \ = np.concatenate(batch_data['embeddings_to_graph_mappings'], axis=0) # Node values if self.config['use_node_values'] == 1: feed_dict[self.placeholders['node_values']] = np.concatenate( batch_data['node_values'], axis=0) return feed_dict