hidden_dim = 128 # 全连接层神经元 dropout_keep_prob = 0.5 # dropout保留比例 learning_rate = 1e-3 # 学习率 batch_size = 64 # 每批训练大小 num_epochs = 10 # 总迭代轮次 print_per_batch = 100 # 每多少轮输出一次结果 save_per_batch = 10 # 每多少轮存入tensorboard config = MLPConfig() x_train, y_train, vocab_processor, x_val, y_val = preprocess() model = Sequential() # 模型结构:嵌入-卷积池化*2-dropout-BN-全连接-dropout-全连接 model.add(Embedding(11021, 300, input_length=1000)) model.add(Convolution1D(256, 3, padding='same')) model.add(MaxPool1D(3, 3, padding='same')) model.add(Convolution1D(128, 3, padding='same')) model.add(MaxPool1D(3, 3, padding='same')) model.add(Convolution1D(64, 3, padding='same')) model.add(Flatten()) model.add(Dropout(0.5)) model.add(BatchNormalization()) # (批)规范化层 model.add(Dense(256, activation='relu')) model.add(Dropout(0.8))
from keras import optimizers if __name__ == '__main__': args = argparse.ArgumentParser() args.add_argument('--strmaxlen', type=int, default=150) args.add_argument('--epochs', type=int, default=100) args.add_argument('--batch', type=int, default=10) args.add_argument('--embedding', type=int, default=256) args.add_argument('--featuresize', type=int, default=129) # ascii code 기준 0~127 + 1 config = args.parse_args() inputs = layers.Input((config.strmaxlen,)) layer = layers.Embedding(config.featuresize, config.embedding, input_length=config.strmaxlen, mask_zero = True)(inputs) layer = layers.Bidirectional(layers.GRU(256, return_sequences=True))(layer) layer = layers.Bidirectional(layers.GRU(256, return_sequences=False))(layer) layer_dense = layers.Dense(3)(layer) outputs_softmax = layers.Activation('softmax')(layer_dense) model = models.Model(inputs=inputs, outputs=outputs_softmax) model.summary() model.compile(optimizer=optimizers.Adam(lr=0.001,amsgrad=True), loss='binary_crossentropy', metrics=['accuracy']) filename = "model-epoch 7" model.load_weights(filename) preprocessed_data = data_helper.preprocess("i am a boy how are you?", config.strmaxlen) output_prediction = model.predict(preprocessed_data)[0].flatten().tolist() output_prediction_label = output_prediction.index(max(output_prediction)) print(output_prediction) print(output_prediction_label)
type=float, help='The fraction of validation.(default=0.05)') parser.add_argument( '--results_dir', default='./results/', type=str, help= 'The results dir including log, model, vocabulary and some images.(default=./results/)' ) args = parser.parse_args() print('Parameters:', args, '\n') if not os.path.exists(args.results_dir): os.mkdir(args.results_dir) timestamp = time.strftime("%Y-%m-%d-%H-%M", time.localtime(time.time())) os.mkdir(os.path.join(args.results_dir, timestamp)) os.mkdir(os.path.join(args.results_dir, timestamp, 'log/')) if not os.path.exists("./data/train_data.csv") or not os.path.exists( "./data/test_data.csv"): data_helper.load_data_and_write_to_file("./data/fenduan_clean.xlsx", "./data/train_data.csv", "./data/test_data.csv", args.test_sample_percentage) x_train, y_train, vocab_size = data_helper.preprocess( "./data/train_data.csv", os.path.join(args.results_dir, timestamp, "vocab.json"), args.padding_size) train(x_train, y_train, vocab_size, args.padding_size, os.path.join(args.results_dir, timestamp, 'Bi_LSTM.h5'))
patch_coors_dir = osp.join(result_root, 'patch_coors') split_dir = osp.join(result_root, 'split.pkl') model_save_dir = osp.join(result_root, 'model_best.pth') # check directions assert check_dir(data_root, make=False) assert check_dir(svs_dir, make=False) assert check_dir(patch_ft_dir, make=False) check_dir(result_root) check_dir(sampled_vis) check_dir(patch_coors_dir) data_dict = split_train_val(svs_dir, ratio=0.8, save_split_dir=split_dir, resplit=False) preprocess(data_dict, patch_ft_dir, patch_coors_dir, num_sample=num_sample, patch_size=patch_size, sampled_vis=sampled_vis, mini_frac=mini_frac) dataloaders, dataset_sizes, len_ft = get_dataloaders(data_dict, patch_ft_dir) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') def train_model(model, criterion, optimizer, scheduler, num_epochs=25): since = time.time() best_model_wts = copy.deepcopy(model.state_dict()) mini_loss = 1e8 c_index_train = CIndexMeter() c_index_val = CIndexMeter() for epoch in range(num_epochs):
trainresult = dkt.test_on_batch(skill_response_array_train[:, :-1, :],response_array_train[:, :-1, np.newaxis], skill_array_train[:, 1:, :]) print('Evalutaion training data result', trainresult) print('Evalutaion validation data result', result) ''' ''' You should implement your own evaluation function here to evaluate your result on the validation set if each sample have different timesteps ''' prediction = dkt.predict(skill_response_array[0:1, :-1, :],skill_array[0:1, 1:, :]) print('Check Prediction Output:', prediction,response_array[:, :-1, np.newaxis]) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('-S',"--skill_df", type=str, required=True) parser.add_argument('-R',"--response_df",type=str, required=True) parser.add_argument('-dict',"--skill_dict", type=str, required=True) args = parser.parse_args() print(args) response_df,skill_df,skill_dict=load_data(args.skill_df,args.response_df,args.skill_dict) # response_df,skill_df,skill_dict=load_data() skills_num = len(skill_dict) print('Number of skills are :{}'.format(skills_num)) skill_array, response_array, skill_response_array = preprocess(skill_df, response_df, skills_num) # train(skill_array, response_array, skill_response_array) train_on_batch(skill_array, response_array, skill_response_array) # python train.py -S skill_df_delft_15.csv -R response_df_delft_15.csv -dict skill_dict_delft_all.json
data_root = '/repository/HyperG_example/example_data/heart_mri/processed' result_root = '/repository/HyperG_example/tmp/heart_mri' k_nearest = 7 patch_size = (5, 5) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') split_dir = osp.join(result_root, 'split.pkl') vis_dir = osp.join(result_root, 'vis') # check directions assert check_dir(data_root, make=False) check_dir(result_root) check_dir(vis_dir) data_dict = split_train_val(data_root, ratio=0.8, save_split_dir=split_dir, resplit=True) x, H, target, mask_train, mask_val, img_size = preprocess(data_dict, patch_size, k_nearest) x_ch = x.size(1) n_class = target.max().item() + 1 model = HGNN(x_ch, n_class, hiddens=[16]) model, x, H, target, mask_train, mask_val = model.to(device), x.to(device), H.to(device), \ target.to(device), mask_train.to(device), mask_val.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4) def train(): model.train() optimizer.zero_grad() pred = model(x, H) F.nll_loss(pred[mask_train], target[mask_train]).backward()
if __name__ == '__main__': parser = argparse.ArgumentParser( description='This is the TextCNN test project.') parser.add_argument( '--results_dir', default='./results/', type=str, help= 'The results dir including log, model, vocabulary and some images.(default=./results/)' ) parser.add_argument('-p', '--padding_size', default=128, type=int, help='Padding size of sentences.(default=128)') parser.add_argument('-c', '--num_classes', default=9, type=int, help='Number of target classes.(default=18)') args = parser.parse_args() print('Parameters:', args) x_test, y_test = preprocess("./data/test_data.csv", os.path.join(args.results_dir, "vocab.json"), args.padding_size, test=True) print("Loading model...") model = load_model('./results/2020-11-06-01-14/TextCNN.h5') test(model, x_test, y_test)
def train(): print("Configuring TensorBoard and Saver...") # 配置 Tensorboard,重新训练时,请将tensorboard文件夹删除,不然图会覆盖 tensorboard_dir = 'tensorboard/textcnn' if not os.path.exists(tensorboard_dir): os.makedirs(tensorboard_dir) tf.summary.scalar("loss", model.loss) tf.summary.scalar("accuracy", model.acc) merged_summary = tf.summary.merge_all() writer = tf.summary.FileWriter(tensorboard_dir) # 配置 Saver saver = tf.train.Saver() if not os.path.exists(save_dir): os.makedirs(save_dir) print("Loading training and validation data...") # 载入训练集与验证集 start_time = time.time() x_train, y_train, vocab_processor, x_val, y_val = preprocess() # x_train, y_train = process_file(train_dir, word_to_id, cat_to_id, config.seq_length) # x_val, y_val = process_file(val_dir, word_to_id, cat_to_id, config.seq_length) time_dif = get_time_dif(start_time) print("Time usage:", time_dif) # 创建session session = tf.Session() session.run(tf.global_variables_initializer()) writer.add_graph(session.graph) print('Training and evaluating...') start_time = time.time() total_batch = 0 # 总批次 best_acc_val = 0.0 # 最佳验证集准确率 last_improved = 0 # 记录上一次提升批次 require_improvement = 2000 # 如果超过1000轮未提升,提前结束训练 flag = False for epoch in range(config.num_epochs): print('Epoch:', epoch + 1) batch_train = batch_iter(x_train, y_train, config.batch_size) for x_batch, y_batch in batch_train: feed_dict = feed_data(x_batch, y_batch, config.dropout_keep_prob) if total_batch % config.save_per_batch == 0: # 每多少轮次将训练结果写入tensorboard scalar s = session.run(merged_summary, feed_dict=feed_dict) writer.add_summary(s, total_batch) if total_batch % config.print_per_batch == 0: # 每多少轮次输出在训练集和验证集上的性能 feed_dict[model.keep_prob] = config.dropout_keep_prob loss_train, acc_train = session.run([model.loss, model.acc], feed_dict=feed_dict) loss_val, acc_val = evaluate(session, x_val, y_val) # todo if acc_val > best_acc_val: # 保存最好结果 best_acc_val = acc_val last_improved = total_batch saver.save(sess=session, save_path=save_path) improved_str = '*' else: improved_str = '' time_dif = get_time_dif(start_time) msg = 'Iter: {0:>6}, Train Loss: {1:>6.2}, Train Acc: {2:>7.2%},' \ + ' Val Loss: {3:>6.2}, Val Acc: {4:>7.2%}, Time: {5} {6}' print(msg.format(total_batch, loss_train, acc_train, loss_val, acc_val, time_dif, improved_str)) session.run(model.optim, feed_dict=feed_dict) # 运行优化 total_batch += 1 if total_batch - last_improved > require_improvement: # 验证集正确率长期不提升,提前结束训练 print("No optimization for a long time, auto-stopping...") flag = True break # 跳出循环 if flag: # 同上 break