#data_lm_preTrain, vocab_update_wiki, truncate_val = get_DataLoader(path=data_path, data_trn=df_trnC, # data_val=df_valC, n_labels=None, Save_Path = Data_path, DataLoader_save_path = 'DataLoader/data_lm_preTrain_wiki.pkl', vocab_save_path = 'vocab/preTrain_wiki_itos.pkl', bs = 32) # # ## Save the Dataloader # # save_workpsace(work_dir=Data_path, data_lm=data_lm_preTrain, save_type=['data_lm'], # DataLoaderLM_path='DataLoader/data_lm_preTrain_wiki.pkl') # Load the dataloader data_lm = load_data(Data_path / 'DataLoader', 'data_lm_preTrain_wiki.pkl') # print(awd_lstm_lm_config_custom) pre_train(Data_path, data_loader_folder='DataLoader', dataLoader_name='data_lm_preTrain_wiki.pkl', model_save_path='DataLoader/models/preTrain_wiki_model', encoding_save_path='encoding/preTrain_wiki_encoding', cuda_id=0, pretrained=False, custom_config=True, lr=1e-01, arch=AWD_LSTM) if args.domain_specific_train: # tokenization parameters data_path = DATA_DIR / 'yelp' input_sub = "tmp" train_fname = "train.csv" val_fname = "val.csv" n_labels = 1 n_textfield = 1 chunksize = 100000 lang = 'en'
def main(): torch.manual_seed(222) torch.cuda.manual_seed_all(222) np.random.seed(222) print(args) config = [('conv1d', [16, 1, 25, 1, 12, 0]), ('bn', [16]), ('relu', [True]), ('max_pool1d', [2, 0]), ('conv1d', [16, 16, 25, 1, 12, 0]), ('bn', [16]), ('relu', [True]), ('max_pool1d', [2, 0]), ('flatten', []), ('linear', [100, 4800]), ('linear', [args.n_way, 100])] config_pre = [('conv1d', [16, 1, 25, 1, 12, 0]), ('bn', [16]), ('relu', [True]), ('max_pool1d', [2, 0]), ('conv1d', [16, 16, 25, 1, 12, 0]), ('bn', [16]), ('relu', [True]), ('max_pool1d', [2, 0]), ('flatten', []), ('linear', [100, 4800]), ('linear', [args.pre_class, 100])] # config = [ # ('conv1d', [16, 1, 25, 1, 12, 1]), # ('bn', [16]), # ('relu', [True]), # ('conv1d', [16, 16, 25, 1, 12, 2]), # ('bn', [16]), # ('relu', [True]), # ('conv1d', [16, 16, 25, 1, 12, 3]), # ('concat', []), # ('bn', [48]), # ('relu', [True]), # ('conv1d', [16, 48, 25, 1, 12, 0]), # ('max_pool1d', [2, 1]), # ('bn', [16]), # ('relu', [True]), # ('conv1d', [16, 16, 25, 1, 12, 2]), # ('bn', [16]), # ('relu', [True]), # ('conv1d', [16, 16, 25, 1, 12, 3]), # ('concat', []), # ('bn', [48]), # ('relu', [True]), # ('conv1d', [16, 48, 25, 1, 12, 0]), # ('max_pool1d', [2, 0]), # ('flatten', []), # ('linear', [100, 4800]), # ('linear', [args.n_way, 100]) # ] # # # VOVnet # config_pre = [ # ('conv1d', [16, 1, 25, 1, 12, 1]), # ('bn', [16]), # ('relu', [True]), # ('conv1d', [16, 16, 25, 1, 12, 2]), # ('bn', [16]), # ('relu', [True]), # ('conv1d', [16, 16, 25, 1, 12, 3]), # ('concat', []), # ('bn', [48]), # ('relu', [True]), # ('conv1d', [16, 48, 25, 1, 12, 0]), # ('max_pool1d', [2, 1]), # ('bn', [16]), # ('relu', [True]), # ('conv1d', [16, 16, 25, 1, 12, 2]), # ('bn', [16]), # ('relu', [True]), # ('conv1d', [16, 16, 25, 1, 12, 3]), # ('concat', []), # ('bn', [48]), # ('relu', [True]), # ('conv1d', [16, 48, 25, 1, 12, 0]), # ('max_pool1d', [2, 0]), # ('flatten', []), # ('linear', [100, 4800]), # ('linear', [args.pre_class, 100]) # ] #pre_train pre_model = pre_train(args, config_pre) pre_data = sio.loadmat( r'L:\桌面文件\假期资料\小样本学习问题\MAML元学习不平衡分类pytorch实现\训练数据集\bearing_cwru_train.mat' ) pre_model(pre_data) device = torch.device('cuda') maml = meta(args, config).to(device) model_dict = maml.state_dict() # # model_dict['net.vars.0'] = pretrained_dict['vars.0'] # model_dict['net.vars.1'] = pretrained_dict['vars.1'] # model_dict['net.vars.2'] = pretrained_dict['vars.2'] # model_dict['net.vars.3'] = pretrained_dict['vars.3'] # model_dict['net.vars.4'] = pretrained_dict['vars.4'] # model_dict['net.vars.5'] = pretrained_dict['vars.5'] # model_dict['net.vars.6'] = pretrained_dict['vars.6'] # model_dict['net.vars.7'] = pretrained_dict['vars.7'] # model_dict['net.vars_bn.0'] = pretrained_dict['vars_bn.0'] # model_dict['net.vars_bn.1'] = pretrained_dict['vars_bn.1'] # model_dict['net.vars_bn.2'] = pretrained_dict['vars_bn.2'] # model_dict['net.vars_bn.3'] = pretrained_dict['vars_bn.3'] tmp = filter(lambda x: x.requires_grad, maml.parameters()) num = sum(map(lambda x: np.prod(x.shape), tmp)) print(maml) print('Total trainable tensors:', num) mini = few_shot_dataset('L:\桌面文件\假期资料\小样本学习问题\MAML元学习不平衡分类pytorch实现\训练数据集', mode='zhuzhou_bearing_meta_danyi_train', batchsz=args.task, n_way=args.n_way, k_shot=args.k_spt, k_query=args.k_qry, resize=1200, train_test_split=args.train_test_radio, selected_train=True) mini_test = few_shot_dataset( 'L:\桌面文件\假期资料\小样本学习问题\MAML元学习不平衡分类pytorch实现\训练数据集', mode='zhuzhou_bearing_meta_danyi_train', batchsz=args.task_test, n_way=args.n_way_test, k_shot=args.k_spt_test, k_query=args.k_qry_test, resize=1200, train_test_split=args.train_test_radio, selected_train=False) for epoch in range(1, args.epoch + 1): # fetch meta_batchsz num of episode each time db = DataLoader(mini, args.task_num, shuffle=True, num_workers=0, pin_memory=True) for step, (x_spt, y_spt, x_qry, y_qry) in enumerate(db): x_spt, y_spt, x_qry, y_qry = x_spt.to(device), y_spt.to( device), x_qry.to(device), y_qry.to(device) accs = maml(x_spt, y_spt, x_qry, y_qry) if epoch % 50 == 0: print('epoch:', epoch, '\ttraining acc:', accs) if epoch % 50 == 0: # evaluation db_test = DataLoader(mini_test, 1, shuffle=True, num_workers=0, pin_memory=True) accs_all_test = [] for i in range(10): for x_spt, y_spt, x_qry, y_qry in db_test: x_spt, y_spt, x_qry, y_qry = x_spt.squeeze(0).to(device), y_spt.squeeze(0).to(device), \ x_qry.squeeze(0).to(device), y_qry.squeeze(0).to(device) accs = maml.finetunning(x_spt, y_spt, x_qry, y_qry) accs_all_test.append(accs) # [b, update_step+1] accs = np.array(accs_all_test).mean(axis=0).astype(np.float16) print('Test acc:', accs)
else: train_loader = DIV2K(scale=args.UPSCALING, subset='train', HR_SIZE=args.HR_PATCH_SIZE) train_ds = train_loader.dataset(batch_size=args.BATCH_SIZE, random_transform=True, repeat_count=None) valid_loader = DIV2K(scale=args.UPSCALING, subset='valid', HR_SIZE=args.HR_PATCH_SIZE) valid_ds = valid_loader.dataset(batch_size=1, random_transform=False, repeat_count=1) generator = generator() discriminator = discriminator(HR_SIZE=args.HR_PATCH_SIZE) pre_train(generator, train_ds, valid_ds, steps=args.PREGENSTEPS, evaluate_every=1, lr_rate=args.lr_pre_gen) train(generator, discriminator, train_ds, valid_ds, steps=args.STEPS, lr_rate=args.lr_gen)
infile.close() raw_data = [] for line in raw: line = line[:-2].replace('"', '') line = line.split(',') raw_data.append(line) del raw # run general statistics for raw data doc_stats(raw_data) # train CRF model using domain KG file_list = [] # todo add retrain files into the list pre_train(cursor, file_list) # NER via hybrid model al_file = open('./outputs/al.tsv', 'w') itg_file = open('./outputs/al_itg.tsv', 'w') eva_file = open('./outputs/evaluation.tsv', 'w') count = 0 al_threshold = 0.2 random.seed(1000) random.shuffle(raw_data) for line in raw_data: if len(unicode(line[1])) <= 7: continue
def main(): # tf.logging.set_verbosity(tf.logging.INFO) parser = argparse.ArgumentParser() # parser.add_argument("--train_file", type=str, default="", help="Input train file.") # parser.add_argument("--vocab_file", type=str, default="", help="Input vocab file.") # parser.add_argument("--model_save_dir", type=str, default="", # help="Specified the directory in which the model should stored.") # parser.add_argument("--lstm_dim", type=int, default=100, help="Dimension of LSTM cell.") # parser.add_argument("--embedding_dim", type=int, default=100, help="Dimension of word embedding.") # parser.add_argument("--layer_num", type=int, default=2, help="LSTM layer num.") # parser.add_argument("--batch_size", type=int, default=32, help="Batch size.") # parser.add_argument("--train_step", type=int, default=10000, help="Number of training steps.") # parser.add_argument("--warmup_step", type=int, default=1000, help="Number of warmup steps.") # parser.add_argument("--learning_rate", type=float, default=0.001, help="The initial learning rate") # parser.add_argument("--dropout_rate", type=float, default=0.5, help="Dropout rate") parser.add_argument("--seed", type=int, default=2020, help="Random seed value.") # parser.add_argument("--print_step", type=int, default=1000, help="Print log every x step.") # parser.add_argument("--max_predictions_per_seq", type=int, default=10, # help="For each sequence, predict x words at most.") # parser.add_argument("--weight_decay", type=float, default=0, help='Weight decay rate') parser.add_argument("--mode", type=str, default='train') parser.add_argument("--init_checkpoint", type=str, default="", help="Initial checkpoint") parser.add_argument( "--pre_trained_model", type=str, default="E:/CodeSleepEatRepeat/competitions/58tech/pretrained-roberta", help="Initial checkpoint") parser.add_argument("--mlm_probability", type=float, default=0.15, help="mask prob.") parser.add_argument("--batch_size", type=int, default=128, help="batch size") parser.add_argument("--max_seq_len", type=int, default=50, help="max length of words in one sentence.") parser.add_argument("--embedding_dim", type=int, default=128, help="Dimension of word2vec.") parser.add_argument( "--pre_train_data_path", type=str, default="E:/CodeSleepEatRepeat/data/58tech/data/pre_train_data", help="pre-training data path.") parser.add_argument("--num_hidden_layers", type=int, default=12, help="Dimension of LSTM cell.") parser.add_argument("--hidden_size", type=int, default=128, help="Dimension of LSTM cell.") parser.add_argument("--intermediate_size", type=int, default=32, help="Dimension of LSTM cell.") parser.add_argument("--num_attention_heads", type=int, default=8, help="Dimension of LSTM cell.") parser.add_argument("--vocab_size", type=int, default=1000, help="Dimension of LSTM cell.") parser.add_argument( "--w2v_output", type=str, default="E:/CodeSleepEatRepeat/data/58tech/data/word2vec.txt", help="Dimension of LSTM cell.") parser.add_argument( "--vocab_file", type=str, default="E:/CodeSleepEatRepeat/data/58tech/data/vocab_new.txt", help="Dimension of LSTM cell.") parser.add_argument("--epochs", type=int, default=2, help="Dimension of LSTM cell.") parser.add_argument( "--pretrain_checkpoints_dir", type=str, default="E:/CodeSleepEatRepeat/competitions/58tech/checkpoints", help="Dimension of LSTM cell.") parser.add_argument( "--train_data_path", type=str, default="E:/CodeSleepEatRepeat/data/58tech/data/train_data", help="training data path.") parser.add_argument( "--checkpoints_dir", type=str, default="E:/CodeSleepEatRepeat/competitions/58tech/checkpoints-train", help="Dimension of LSTM cell.") args = parser.parse_args() np.random.seed(args.seed) tf.random.set_seed(args.seed) if args.mode == 'pretrian': pre_train(args) elif args.mode == 'train': train(args) gc.collect()
def main(): torch.manual_seed(222) torch.cuda.manual_seed_all(222) np.random.seed(222) print(args) # config_pre = [ # ('conv1d', [16, args.input_channel, 25, 1, 12, 1]), # ('bn', [16]), # ('relu', [True]), # ('conv1d', [16, 16, 25, 1, 12, 2]), # ('bn', [16]), # ('relu', [True]), # ('conv1d', [16, 16, 25, 1, 12, 3]), # ('concat', []), # ('SE', [1, int(48/args.se_radio), 48, True, 48, int(48/args.se_radio)]), # ('bn', [48]), # ('relu', [True]), # ('conv1d', [16, 48, 25, 1, 12, 0]), # ('max_pool1d', [4, 1]), # ('dropout', [True]), # ('bn', [16]), # ('relu', [True]), # ('conv1d', [16, 16, 25, 1, 12, 2]), # ('bn', [16]), # ('relu', [True]), # ('conv1d', [16, 16, 25, 1, 12, 3]), # ('concat', []), # ('SE', [1, int(48/args.se_radio), 48, True, 48, int(48/args.se_radio)]), # ('bn', [48]), # ('relu', [True]), # ('conv1d', [16, 48, 25, 1, 12, 0]), # ('max_pool1d', [4, 0]), # ('dropout', [True]), # ('flatten', []), # ('linear', [100, 1792]), # ('relu', [True]), # ('linear', [args.pre_class, 100]), # ('softmax', [True]) # ] config_pre = [ ('conv1d', [16, args.input_channel, 25, 1, 12, 1]), ('bn', [16]), ('relu', [True]), ('conv1d', [16, 16, 25, 1, 12, 2]), ('bn', [16]), ('relu', [True]), ('conv1d', [16, 16, 25, 1, 12, 3]), ('concat', []), ('bn', [48]), ('relu', [True]), ('conv1d', [16, 48, 25, 1, 12, 0]), ('max_pool1d', [4, 1]), ('dropout', [True]), ('bn', [16]), ('relu', [True]), ('conv1d', [16, 16, 25, 1, 12, 2]), ('bn', [16]), ('relu', [True]), ('conv1d', [16, 16, 25, 1, 12, 3]), ('concat', []), ('SE', [1, int(48/args.se_radio), 48, True, 48, int(48/args.se_radio)]), ('bn', [48]), ('relu', [True]), ('conv1d', [16, 48, 25, 1, 12, 0]), ('max_pool1d', [4, 0]), ('dropout', [True]), ('flatten', []), ('linear', [100, 1792]), ('relu', [True]), ('linear', [args.pre_class, 100]), ('softmax', [True]) ] # create imbalanced dataset # try: # train_test_data = sio.loadmat(r'E:\DL\JiangXinwei\python\weighted vovnet with self attention\train_test_data.mat') # x_train_im, y_train_im, im_radio, x_test_b, y_test_b, multi_sample_weight = dataprocess(args) # except: # train_test_data = sio.loadmat(r'E:\DL\JiangXinwei\python\weighted vovnet with self attention\train_test_data.mat') # x_train_im, y_train_im, im_radio, x_test_b, y_test_b, multi_sample_weight = train_test_data['x_train_im'], train_test_data['y_train_im'].T.squeeze(),\ # train_test_data['im_radio'].T.squeeze(), train_test_data['x_test_b'],\ # train_test_data['y_test_b'].squeeze(), train_test_data['multi_sample_weight'].T.squeeze() x_train_im, y_train_im, im_radio, x_test_b, y_test_b, multi_sample_weight = dataprocess(args) # pre_model = pre_train(args, config_pre) # tmp = filter(lambda x: x.requires_grad, pre_model.parameters()) # num = sum(map(lambda x: np.prod(x.shape), tmp)) # print(pre_model) # print('Total trainable tensors:', num) # # pre_train_acc, pre_test_acc, pre_time_train = pre_model(x_train_im, y_train_im, im_radio, x_test_b, y_test_b, multi_sample_weight) train_acc = np.zeros(12) test_acc = np.zeros(12) time_train = np.zeros(12) for i in range(1): pre_model = pre_train(args, config_pre) tmp = filter(lambda x: x.requires_grad, pre_model.parameters()) num = sum(map(lambda x: np.prod(x.shape), tmp)) print(pre_model) print('Total trainable tensors:', num) train_acc[i], test_acc[i], time_train[i], test_confusion, f_score, g_mean = pre_model(x_train_im, y_train_im, im_radio, x_test_b, y_test_b, multi_sample_weight) train_acc[10] = np.mean(train_acc[:10]) train_acc[11] = np.std(train_acc[:10]) test_acc[10] = np.mean(test_acc[:10]) test_acc[11] = np.std(test_acc[:10]) time_train[10] = np.mean(time_train[:10]) time_train[11] = np.std(time_train[:10]) book = xlwt.Workbook(encoding='utf-8',style_compression=0) sheet = book.add_sheet('proposed_method',cell_overwrite_ok=True) for i in range(11): for j in range(11): sheet.write(i, j, str(test_confusion[i][j])) sheet.write(12, 1, f_score) sheet.write(13, 1, g_mean) book.save('proposed_method.xls')
def main(): print("pre train is running") pre_train.pre_train()