def main(): # load data data_path = '../data/output/records_final.pkl' voc_path = '../data/output/voc_final.pkl' ddi_adj_path = '../data/output/ddi_A_final.pkl' device = torch.device('cuda:{}'.format(args.cuda)) ddi_adj = dill.load(open(ddi_adj_path, 'rb')) data = dill.load(open(data_path, 'rb')) voc = dill.load(open(voc_path, 'rb')) diag_voc, pro_voc, med_voc = voc['diag_voc'], voc['pro_voc'], voc[ 'med_voc'] np.random.seed(1203) np.random.shuffle(data) split_point = int(len(data) * 3 / 5) data_train = data[:split_point] eval_len = int(len(data[split_point:]) / 2) data_test = data[split_point:split_point + eval_len] data_eval = data[split_point + eval_len:] voc_size = (len(diag_voc.idx2word), len(pro_voc.idx2word), len(med_voc.idx2word)) print(voc_size) model = DualNN(voc_size, ddi_adj, emb_dim=args.dim, device=device) # model.load_state_dict(torch.load(open(args.resume_path, 'rb'))) if args.Test: model.load_state_dict(torch.load(open(args.resume_path, 'rb'))) model.to(device=device) tic = time.time() label_list, prob_add, prob_delete = eval(model, data_eval, voc_size, 0, 1) threshold1, threshold2 = [], [] for i in range(label_list.shape[1]): _, _, boundary_add = roc_curve(label_list[:, i], prob_add[:, i], pos_label=1) _, _, boundary_delete = roc_curve(label_list[:, i], prob_delete[:, i], pos_label=0) threshold1.append(boundary_add[min(round(len(boundary_add) * 0.05), len(boundary_add) - 1)]) threshold2.append(boundary_delete[min( round(len(boundary_delete) * 0.05), len(boundary_delete) - 1)]) # threshold1 = np.ones(voc_size[2]) * np.mean(threshold1) # threshold2 = np.ones(voc_size[2]) * np.mean(threshold2) print(np.mean(threshold1), np.mean(threshold2)) eval(model, data_test, voc_size, 0, 0, threshold1, threshold2) print('test time: {}'.format(time.time() - tic)) return model.to(device=device) print('parameters', get_n_params(model)) # exit() optimizer = RMSprop(list(model.parameters()), lr=args.lr, weight_decay=args.weight_decay) # start iterations history = defaultdict(list) best_epoch, best_ja = 0, 0 EPOCH = 40 for epoch in range(EPOCH): t = 0 tic = time.time() print('\nepoch {} --------------------------'.format(epoch + 1)) model.train() for step, input in enumerate(data_train): if len(input) < 2: continue loss = 0 for adm_idx, adm in enumerate(input): if adm_idx == 0: continue seq_input = input[:adm_idx + 1] loss_bce_target = np.zeros((1, voc_size[2])) loss_bce_target[:, adm[2]] = 1 loss_bce_target_last = np.zeros((1, voc_size[2])) loss_bce_target_last[:, input[adm_idx - 1][2]] = 1 add_target = np.zeros((1, voc_size[2])) add_target[:, np.where(loss_bce_target == 1)[1]] = 1 delete_target = np.zeros((1, voc_size[2])) delete_target[:, np.where(loss_bce_target == 0)[1]] = 1 loss_multi_target = np.full((1, voc_size[2]), -1) for idx, item in enumerate(adm[2]): loss_multi_target[0][idx] = item loss_multi_target_last = np.full((1, voc_size[2]), -1) for idx, item in enumerate(input[adm_idx - 1][2]): loss_multi_target_last[0][idx] = item loss_multi_add_target = np.full((1, voc_size[2]), -1) for i, item in enumerate(np.where(add_target == 1)[0]): loss_multi_add_target[0][i] = item loss_multi_delete_target = np.full((1, voc_size[2]), -1) for i, item in enumerate(np.where(delete_target == 1)[0]): loss_multi_delete_target[0][i] = item add_result, delete_result = model(seq_input) loss_bce = F.binary_cross_entropy_with_logits(add_result, torch.FloatTensor(add_target).to(device)) + \ F.binary_cross_entropy_with_logits(delete_result, torch.FloatTensor(delete_target).to(device)) loss_multi = F.multilabel_margin_loss(F.sigmoid(add_result), torch.LongTensor(loss_multi_add_target).to(device)) + \ F.multilabel_margin_loss(F.sigmoid(delete_result), torch.LongTensor(loss_multi_delete_target).to(device)) # l2 = 0 # for p in model.parameters(): # l2 = l2 + (p ** 2).sum() loss += 0.95 * loss_bce + 0.05 * loss_multi optimizer.zero_grad() loss.backward(retain_graph=True) optimizer.step() llprint('\rtraining step: {} / {}'.format(step, len(data_train))) print() tic2 = time.time() ddi_rate, ja, prauc, avg_p, avg_r, avg_f1, add, delete, avg_med = eval( model, data_eval, voc_size, epoch) print('training time: {}, test time: {}'.format( time.time() - tic, time.time() - tic2)) history['ja'].append(ja) history['ddi_rate'].append(ddi_rate) history['avg_p'].append(avg_p) history['avg_r'].append(avg_r) history['avg_f1'].append(avg_f1) history['prauc'].append(prauc) history['add'].append(add) history['delete'].append(delete) history['med'].append(avg_med) if epoch >= 5: print( 'ddi: {}, Med: {}, Ja: {}, F1: {}, Add: {}, Delete: {}'.format( np.mean(history['ddi_rate'][-5:]), np.mean(history['med'][-5:]), np.mean(history['ja'][-5:]), np.mean(history['avg_f1'][-5:]), np.mean(history['add'][-5:]), np.mean(history['delete'][-5:]))) torch.save(model.state_dict(), open(os.path.join('saved', args.model_name, \ 'Epoch_{}_JA_{:.4}_DDI_{:.4}.model'.format(epoch, ja, ddi_rate)), 'wb')) if epoch != 0 and best_ja < ja: best_epoch = epoch best_ja = ja print('best_epoch: {}'.format(best_epoch)) dill.dump( history, open( os.path.join('saved', args.model_name, 'history_{}.pkl'.format(args.model_name)), 'wb'))
def main(): # load data data_path = '../data/output/records_final.pkl' voc_path = '../data/output/voc_final.pkl' ddi_adj_path = '../data/output/ddi_A_final.pkl' device = torch.device('cuda:{}'.format(args.cuda)) ddi_adj = dill.load(open(ddi_adj_path, 'rb')) data = dill.load(open(data_path, 'rb')) voc = dill.load(open(voc_path, 'rb')) diag_voc, pro_voc, med_voc = voc['diag_voc'], voc['pro_voc'], voc[ 'med_voc'] np.random.seed(1203) np.random.shuffle(data) split_point = int(len(data) * 3 / 5) data_train = data[:split_point] eval_len = int(len(data[split_point:]) / 2) data_test = data[split_point:split_point + eval_len] data_eval = data[split_point + eval_len:] voc_size = (len(diag_voc.idx2word), len(pro_voc.idx2word), len(med_voc.idx2word)) print(voc_size) model = SimNN(voc_size, ddi_adj, emb_dim=args.dim, device=device) # model.load_state_dict(torch.load(open(args.resume_path, 'rb'))) if args.Test: model.load_state_dict(torch.load(open(args.resume_path, 'rb'))) model.to(device=device) tic = time.time() eval(model, data_test, voc_size, 0) print('test time: {}'.format(time.time() - tic)) return model.to(device=device) print('parameters', get_n_params(model)) # exit() optimizer = RMSprop(list(model.parameters()), lr=args.lr, weight_decay=args.weight_decay) # start iterations history = defaultdict(list) best_epoch, best_ja = 0, 0 criterion = torch.nn.CrossEntropyLoss() EPOCH = 40 for epoch in range(EPOCH): t = 0 tic = time.time() print('\nepoch {} --------------------------'.format(epoch + 1)) model.train() for step, input in enumerate(data_train): if len(input) < 2: continue loss = 0 for adm_idx, adm in enumerate(input): if adm_idx == 0: continue seq_input = input[:adm_idx + 1] loss_bce_target = np.zeros((1, voc_size[2])) loss_bce_target[:, adm[2]] = 1 loss_bce_target_last = np.zeros((1, voc_size[2])) loss_bce_target_last[:, input[adm_idx - 1][2]] = 1 target_list = loss_bce_target - loss_bce_target_last target_list[target_list == -1] = 2 result = model(seq_input) loss += criterion(result, torch.LongTensor(target_list[0]).to(device)) # l2 = 0 # for p in model.parameters(): # l2 = l2 + (p ** 2).sum() optimizer.zero_grad() loss.backward(retain_graph=True) optimizer.step() llprint('\rtraining step: {} / {}'.format(step, len(data_train))) print() tic2 = time.time() ddi_rate, ja, prauc, avg_p, avg_r, avg_f1, add, delete, avg_med = eval( model, data_eval, voc_size, epoch) print('training time: {}, test time: {}'.format( time.time() - tic, time.time() - tic2)) history['ja'].append(ja) history['ddi_rate'].append(ddi_rate) history['avg_p'].append(avg_p) history['avg_r'].append(avg_r) history['avg_f1'].append(avg_f1) history['prauc'].append(prauc) history['add'].append(add) history['delete'].append(delete) history['med'].append(avg_med) if epoch >= 5: print( 'ddi: {}, Med: {}, Ja: {}, F1: {}, Add: {}, Delete: {}'.format( np.mean(history['ddi_rate'][-5:]), np.mean(history['med'][-5:]), np.mean(history['ja'][-5:]), np.mean(history['avg_f1'][-5:]), np.mean(history['add'][-5:]), np.mean(history['delete'][-5:]))) torch.save(model.state_dict(), open(os.path.join('saved', args.model_name, \ 'Epoch_{}_JA_{:.4}_DDI_{:.4}.model'.format(epoch, ja, ddi_rate)), 'wb')) if epoch != 0 and best_ja < ja: best_epoch = epoch best_ja = ja print('best_epoch: {}'.format(best_epoch)) dill.dump( history, open( os.path.join('saved', args.model_name, 'history_{}.pkl'.format(args.model_name)), 'wb'))
def main(): # load data data_path = '../data/output/records_final.pkl' voc_path = '../data/output/voc_final.pkl' device = torch.device('cuda:{}'.format(args.cuda)) data = dill.load(open(data_path, 'rb')) voc = dill.load(open(voc_path, 'rb')) diag_voc, pro_voc, med_voc = voc['diag_voc'], voc['pro_voc'], voc[ 'med_voc'] split_point = int(len(data) * 2 / 3) data_train = data[:split_point] eval_len = int(len(data[split_point:]) / 2) data_test = data[split_point:split_point + eval_len] data_eval = data[split_point + eval_len:] voc_size = (len(diag_voc.idx2word), len(pro_voc.idx2word), len(med_voc.idx2word)) END_TOKEN = voc_size[2] + 1 model = Leap(voc_size, device=device) # model.load_state_dict(torch.load(open(args.resume_path, 'rb'))) if args.Test: model.load_state_dict(torch.load(open(args.resume_path, 'rb'))) model.to(device=device) tic = time.time() result = [] for _ in range(10): test_sample = np.random.choice(data_test, round(len(data_test) * 0.8), replace=True) ddi_rate, ja, prauc, avg_p, avg_r, avg_f1, avg_med = eval( model, test_sample, voc_size, 0) result.append([ddi_rate, ja, avg_f1, prauc, avg_med]) result = np.array(result) mean = result.mean(axis=0) std = result.std(axis=0) outstring = "" for m, s in zip(mean, std): outstring += "{:.4f} $\pm$ {:.4f} & ".format(m, s) print(outstring) print('test time: {}'.format(time.time() - tic)) return model.to(device=device) print('parameters', get_n_params(model)) optimizer = Adam(model.parameters(), lr=args.lr) history = defaultdict(list) best_epoch, best_ja = 0, 0 EPOCH = 50 for epoch in range(EPOCH): tic = time.time() print('\nepoch {} --------------------------'.format(epoch + 1)) model.train() for step, input in enumerate(data_train): for adm in input: loss_target = adm[2] + [END_TOKEN] output_logits = model(adm) loss = F.cross_entropy( output_logits, torch.LongTensor(loss_target).to(device)) optimizer.zero_grad() loss.backward(retain_graph=True) optimizer.step() llprint('\rtraining step: {} / {}'.format(step, len(data_train))) print() tic2 = time.time() ddi_rate, ja, prauc, avg_p, avg_r, avg_f1, avg_med = eval( model, data_eval, voc_size, epoch) print('training time: {}, test time: {}'.format( time.time() - tic, time.time() - tic2)) history['ja'].append(ja) history['ddi_rate'].append(ddi_rate) history['avg_p'].append(avg_p) history['avg_r'].append(avg_r) history['avg_f1'].append(avg_f1) history['prauc'].append(prauc) history['med'].append(avg_med) if epoch >= 5: print('ddi: {}, Med: {}, Ja: {}, F1: {}, PRAUC: {}'.format( np.mean(history['ddi_rate'][-5:]), np.mean(history['med'][-5:]), np.mean(history['ja'][-5:]), np.mean(history['avg_f1'][-5:]), np.mean(history['prauc'][-5:]))) torch.save(model.state_dict(), open(os.path.join('saved', args.model_name, \ 'Epoch_{}_JA_{:.4}_DDI_{:.4}.model'.format(epoch, ja, ddi_rate)), 'wb')) if epoch != 0 and best_ja < ja: best_epoch = epoch best_ja = ja print('best_epoch: {}'.format(best_epoch)) dill.dump( history, open( os.path.join('saved', args.model_name, 'history_{}.pkl'.format(args.model_name)), 'wb'))
def main(): if not os.path.exists(os.path.join("saved", model_name)): os.makedirs(os.path.join("saved", model_name)) data_path = '../data/records_final.pkl' voc_path = '../data/voc_final.pkl' ehr_adj_path = '../data/ehr_adj_final.pkl' ddi_adj_path = '../data/ddi_A_final.pkl' device = torch.device('cuda:0') ehr_adj = dill.load(open(ehr_adj_path, 'rb')) ddi_adj = dill.load(open(ddi_adj_path, 'rb')) data = dill.load(open(data_path, 'rb')) voc = dill.load(open(voc_path, 'rb')) diag_voc, pro_voc, med_voc = voc['diag_voc'], voc['pro_voc'], voc[ 'med_voc'] split_point = int(len(data) * 2 / 3) data_train = data[:split_point] eval_len = int(len(data[split_point:]) / 2) data_test = data[split_point:split_point + eval_len] data_eval = data[split_point + eval_len:] EPOCH = 40 LR = 0.0002 TEST = args.eval Neg_Loss = args.ddi DDI_IN_MEM = args.ddi TARGET_DDI = 0.05 T = 0.5 decay_weight = 0.85 voc_size = (len(diag_voc.idx2word), len(pro_voc.idx2word), len(med_voc.idx2word)) model = GAMENet(voc_size, ehr_adj, ddi_adj, emb_dim=64, device=device, ddi_in_memory=DDI_IN_MEM) if TEST: model.load_state_dict(torch.load(open(resume_name, 'rb'))) model.to(device=device) print('parameters', get_n_params(model)) optimizer = Adam(list(model.parameters()), lr=LR) if TEST: eval(model, data_test, voc_size, 0) else: history = defaultdict(list) best_epoch = 0 best_ja = 0 for epoch in range(EPOCH): loss_record1 = [] start_time = time.time() model.train() prediction_loss_cnt = 0 neg_loss_cnt = 0 for step, input in enumerate(data_train): for idx, adm in enumerate(input): seq_input = input[:idx + 1] loss1_target = np.zeros((1, voc_size[2])) loss1_target[:, adm[2]] = 1 loss3_target = np.full((1, voc_size[2]), -1) for idx, item in enumerate(adm[2]): loss3_target[0][idx] = item target_output1, batch_neg_loss = model(seq_input) loss1 = F.binary_cross_entropy_with_logits( target_output1, torch.FloatTensor(loss1_target).to(device)) loss3 = F.multilabel_margin_loss( F.sigmoid(target_output1), torch.LongTensor(loss3_target).to(device)) if Neg_Loss: target_output1 = F.sigmoid( target_output1).detach().cpu().numpy()[0] target_output1[target_output1 >= 0.5] = 1 target_output1[target_output1 < 0.5] = 0 y_label = np.where(target_output1 == 1)[0] current_ddi_rate = ddi_rate_score([[y_label]]) if current_ddi_rate <= TARGET_DDI: loss = 0.9 * loss1 + 0.01 * loss3 prediction_loss_cnt += 1 else: rnd = np.exp((TARGET_DDI - current_ddi_rate) / T) if np.random.rand(1) < rnd: loss = batch_neg_loss neg_loss_cnt += 1 else: loss = 0.9 * loss1 + 0.01 * loss3 prediction_loss_cnt += 1 else: loss = 0.9 * loss1 + 0.01 * loss3 optimizer.zero_grad() loss.backward(retain_graph=True) optimizer.step() loss_record1.append(loss.item()) llprint( '\rTrain--Epoch: %d, Step: %d/%d, L_p cnt: %d, L_neg cnt: %d' % (epoch, step, len(data_train), prediction_loss_cnt, neg_loss_cnt)) # annealing T *= decay_weight ddi_rate, ja, prauc, avg_p, avg_r, avg_f1 = eval( model, data_eval, voc_size, epoch) history['ja'].append(ja) history['ddi_rate'].append(ddi_rate) history['avg_p'].append(avg_p) history['avg_r'].append(avg_r) history['avg_f1'].append(avg_f1) history['prauc'].append(prauc) end_time = time.time() elapsed_time = (end_time - start_time) / 60 llprint( '\tEpoch: %d, Loss: %.4f, One Epoch Time: %.2fm, Appro Left Time: %.2fh\n' % (epoch, np.mean(loss_record1), elapsed_time, elapsed_time * (EPOCH - epoch - 1) / 60)) torch.save( model.state_dict(), open( os.path.join( 'saved', model_name, 'Epoch_%d_JA_%.4f_DDI_%.4f.model' % (epoch, ja, ddi_rate)), 'wb')) print('') if epoch != 0 and best_ja < ja: best_epoch = epoch best_ja = ja dill.dump(history, open(os.path.join('saved', model_name, 'history.pkl'), 'wb')) # test torch.save( model.state_dict(), open(os.path.join('saved', model_name, 'final.model'), 'wb')) print('best_epoch:', best_epoch)
def main(): data_path = '../data/output/records_final.pkl' voc_path = '../data/output/voc_final.pkl' ehr_adj_path = '../data/output/ehr_adj_final.pkl' ddi_adj_path = '../data/output/ddi_A_final.pkl' device = torch.device('cuda:{}'.format(args.cuda)) ehr_adj = dill.load(open(ehr_adj_path, 'rb')) ddi_adj = dill.load(open(ddi_adj_path, 'rb')) data = dill.load(open(data_path, 'rb')) voc = dill.load(open(voc_path, 'rb')) diag_voc, pro_voc, med_voc = voc['diag_voc'], voc['pro_voc'], voc[ 'med_voc'] # np.random.seed(2048) # np.random.shuffle(data) split_point = int(len(data) * 2 / 3) data_train = data[:split_point] eval_len = int(len(data[split_point:]) / 2) data_test = data[split_point:split_point + eval_len] data_eval = data[split_point + eval_len:] voc_size = (len(diag_voc.idx2word), len(pro_voc.idx2word), len(med_voc.idx2word)) model = GAMENet(voc_size, ehr_adj, ddi_adj, emb_dim=args.dim, device=device, ddi_in_memory=args.ddi) # model.load_state_dict(torch.load(open(args.resume_path, 'rb'))) if args.Test: model.load_state_dict(torch.load(open(args.resume_path, 'rb'))) model.to(device=device) tic = time.time() result = [] for _ in range(10): test_sample = np.random.choice(data_test, round(len(data_test) * 0.8), replace=True) ddi_rate, ja, prauc, avg_p, avg_r, avg_f1, avg_med = eval( model, test_sample, voc_size, 0) result.append([ddi_rate, ja, avg_f1, prauc, avg_med]) result = np.array(result) mean = result.mean(axis=0) std = result.std(axis=0) outstring = "" for m, s in zip(mean, std): outstring += "{:.4f} $\pm$ {:.4f} & ".format(m, s) print(outstring) print('test time: {}'.format(time.time() - tic)) return model.to(device=device) print('parameters', get_n_params(model)) optimizer = Adam(list(model.parameters()), lr=args.lr) history = defaultdict(list) best_epoch, best_ja = 0, 0 EPOCH = 50 for epoch in range(EPOCH): tic = time.time() print('\nepoch {} --------------------------'.format(epoch + 1)) prediction_loss_cnt, neg_loss_cnt = 0, 0 model.train() for step, input in enumerate(data_train): for idx, adm in enumerate(input): seq_input = input[:idx + 1] loss_bce_target = np.zeros((1, voc_size[2])) loss_bce_target[:, adm[2]] = 1 loss_multi_target = np.full((1, voc_size[2]), -1) for idx, item in enumerate(adm[2]): loss_multi_target[0][idx] = item target_output1, loss_ddi = model(seq_input) loss_bce = F.binary_cross_entropy_with_logits( target_output1, torch.FloatTensor(loss_bce_target).to(device)) loss_multi = F.multilabel_margin_loss( F.sigmoid(target_output1), torch.LongTensor(loss_multi_target).to(device)) if args.ddi: target_output1 = F.sigmoid( target_output1).detach().cpu().numpy()[0] target_output1[target_output1 >= 0.5] = 1 target_output1[target_output1 < 0.5] = 0 y_label = np.where(target_output1 == 1)[0] current_ddi_rate = ddi_rate_score( [[y_label]], path='../data/output/ddi_A_final.pkl') if current_ddi_rate <= args.target_ddi: loss = 0.9 * loss_bce + 0.1 * loss_multi prediction_loss_cnt += 1 else: rnd = np.exp( (args.target_ddi - current_ddi_rate) / args.T) if np.random.rand(1) < rnd: loss = loss_ddi neg_loss_cnt += 1 else: loss = 0.9 * loss_bce + 0.1 * loss_multi prediction_loss_cnt += 1 else: loss = 0.9 * loss_bce + 0.1 * loss_multi optimizer.zero_grad() loss.backward(retain_graph=True) optimizer.step() llprint('\rtraining step: {} / {}'.format(step, len(data_train))) args.T *= args.decay_weight print() tic2 = time.time() ddi_rate, ja, prauc, avg_p, avg_r, avg_f1, avg_med = eval( model, data_eval, voc_size, epoch) print('training time: {}, test time: {}'.format( time.time() - tic, time.time() - tic2)) history['ja'].append(ja) history['ddi_rate'].append(ddi_rate) history['avg_p'].append(avg_p) history['avg_r'].append(avg_r) history['avg_f1'].append(avg_f1) history['prauc'].append(prauc) history['med'].append(avg_med) if epoch >= 5: print('ddi: {}, Med: {}, Ja: {}, F1: {}, PRAUC: {}'.format( np.mean(history['ddi_rate'][-5:]), np.mean(history['med'][-5:]), np.mean(history['ja'][-5:]), np.mean(history['avg_f1'][-5:]), np.mean(history['prauc'][-5:]))) torch.save(model.state_dict(), open(os.path.join('saved', args.model_name, \ 'Epoch_{}_JA_{:.4}_DDI_{:.4}.model'.format(epoch, ja, ddi_rate)), 'wb')) if epoch != 0 and best_ja < ja: best_epoch = epoch best_ja = ja print('best_epoch: {}'.format(best_epoch)) dill.dump( history, open( os.path.join('saved', args.model_name, 'history_{}.pkl'.format(args.model_name)), 'wb'))
def main(): # load data data_path = '../data/output/records_final.pkl' voc_path = '../data/output/voc_final.pkl' ddi_adj_path = '../data/output/ddi_A_final.pkl' device = torch.device('cuda:{}'.format(args.cuda)) ddi_adj = dill.load(open(ddi_adj_path, 'rb')) data = dill.load(open(data_path, 'rb')) voc = dill.load(open(voc_path, 'rb')) diag_voc, pro_voc, med_voc = voc['diag_voc'], voc['pro_voc'], voc[ 'med_voc'] np.random.seed(1203) np.random.shuffle(data) split_point = int(len(data) * 3 / 5) data_train = data[:split_point] eval_len = int(len(data[split_point:]) / 2) data_test = data[split_point:split_point + eval_len] data_eval = data[split_point + eval_len:] voc_size = (len(diag_voc.idx2word), len(pro_voc.idx2word), len(med_voc.idx2word)) model = MICRON(voc_size, ddi_adj, emb_dim=args.dim, device=device) # model.load_state_dict(torch.load(open(args.resume_path, 'rb'))) if args.Test: model.load_state_dict(torch.load(open(args.resume_path, 'rb'))) model.to(device=device) tic = time.time() label_list, prob_list = eval(model, data_eval, voc_size, 0, 1) threshold1, threshold2 = [], [] for i in range(label_list.shape[1]): _, _, boundary = roc_curve(label_list[:, i], prob_list[:, i], pos_label=1) # boundary1 should be in [0.5, 0.9], boundary2 should be in [0.1, 0.5] threshold1.append( min( 0.9, max(0.5, boundary[max(0, round(len(boundary) * 0.05) - 1)]))) threshold2.append( max( 0.1, min( 0.5, boundary[min(round(len(boundary) * 0.95), len(boundary) - 1)]))) print(np.mean(threshold1), np.mean(threshold2)) threshold1 = np.ones(voc_size[2]) * np.mean(threshold1) threshold2 = np.ones(voc_size[2]) * np.mean(threshold2) eval(model, data_test, voc_size, 0, 0, threshold1, threshold2) print('test time: {}'.format(time.time() - tic)) return model.to(device=device) print('parameters', get_n_params(model)) # exit() optimizer = RMSprop(list(model.parameters()), lr=args.lr, weight_decay=args.weight_decay) # start iterations history = defaultdict(list) best_epoch, best_ja = 0, 0 weight_list = [[0.25, 0.25, 0.25, 0.25]] EPOCH = 40 for epoch in range(EPOCH): t = 0 tic = time.time() print('\nepoch {} --------------------------'.format(epoch + 1)) sample_counter = 0 mean_loss = np.array([0, 0, 0, 0]) model.train() for step, input in enumerate(data_train): loss = 0 if len(input) < 2: continue for adm_idx, adm in enumerate(input): if adm_idx == 0: continue # sample_counter += 1 seq_input = input[:adm_idx + 1] loss_bce_target = np.zeros((1, voc_size[2])) loss_bce_target[:, adm[2]] = 1 loss_bce_target_last = np.zeros((1, voc_size[2])) loss_bce_target_last[:, input[adm_idx - 1][2]] = 1 loss_multi_target = np.full((1, voc_size[2]), -1) for idx, item in enumerate(adm[2]): loss_multi_target[0][idx] = item loss_multi_target_last = np.full((1, voc_size[2]), -1) for idx, item in enumerate(input[adm_idx - 1][2]): loss_multi_target_last[0][idx] = item result, result_last, _, loss_ddi, loss_rec = model(seq_input) loss_bce = 0.75 * F.binary_cross_entropy_with_logits(result, torch.FloatTensor(loss_bce_target).to(device)) + \ (1 - 0.75) * F.binary_cross_entropy_with_logits(result_last, torch.FloatTensor(loss_bce_target_last).to(device)) loss_multi = 5e-2 * (0.75 * F.multilabel_margin_loss(F.sigmoid(result), torch.LongTensor(loss_multi_target).to(device)) + \ (1 - 0.75) * F.multilabel_margin_loss(F.sigmoid(result_last), torch.LongTensor(loss_multi_target_last).to(device))) y_pred_tmp = F.sigmoid(result).detach().cpu().numpy()[0] y_pred_tmp[y_pred_tmp >= 0.5] = 1 y_pred_tmp[y_pred_tmp < 0.5] = 0 y_label = np.where(y_pred_tmp == 1)[0] current_ddi_rate = ddi_rate_score( [[y_label]], path='../data/output/ddi_A_final.pkl') # l2 = 0 # for p in model.parameters(): # l2 = l2 + (p ** 2).sum() if sample_counter == 0: lambda1, lambda2, lambda3, lambda4 = weight_list[-1] else: current_loss = np.array([ loss_bce.detach().cpu().numpy(), loss_multi.detach().cpu().numpy(), loss_ddi.detach().cpu().numpy(), loss_rec.detach().cpu().numpy() ]) current_ratio = (current_loss - np.array(mean_loss)) / np.array(mean_loss) instant_weight = np.exp(current_ratio) / sum( np.exp(current_ratio)) lambda1, lambda2, lambda3, lambda4 = instant_weight * 0.75 + np.array( weight_list[-1]) * 0.25 # update weight_list weight_list.append([lambda1, lambda2, lambda3, lambda4]) # update mean_loss mean_loss = (mean_loss * (sample_counter - 1) + np.array([loss_bce.detach().cpu().numpy(), \ loss_multi.detach().cpu().numpy(), loss_ddi.detach().cpu().numpy(), loss_rec.detach().cpu().numpy()])) / sample_counter # lambda1, lambda2, lambda3, lambda4 = weight_list[-1] if current_ddi_rate > 0.08: loss += lambda1 * loss_bce + lambda2 * loss_multi + \ lambda3 * loss_ddi + lambda4 * loss_rec else: loss += lambda1 * loss_bce + lambda2 * loss_multi + \ lambda4 * loss_rec optimizer.zero_grad() loss.backward(retain_graph=True) optimizer.step() llprint('\rtraining step: {} / {}'.format(step, len(data_train))) tic2 = time.time() ddi_rate, ja, prauc, avg_p, avg_r, avg_f1, add, delete, avg_med = eval( model, data_eval, voc_size, epoch) print('training time: {}, test time: {}'.format( time.time() - tic, time.time() - tic2)) history['ja'].append(ja) history['ddi_rate'].append(ddi_rate) history['avg_p'].append(avg_p) history['avg_r'].append(avg_r) history['avg_f1'].append(avg_f1) history['prauc'].append(prauc) history['add'].append(add) history['delete'].append(delete) history['med'].append(avg_med) if epoch >= 5: print( 'ddi: {}, Med: {}, Ja: {}, F1: {}, Add: {}, Delete: {}'.format( np.mean(history['ddi_rate'][-5:]), np.mean(history['med'][-5:]), np.mean(history['ja'][-5:]), np.mean(history['avg_f1'][-5:]), np.mean(history['add'][-5:]), np.mean(history['delete'][-5:]))) torch.save(model.state_dict(), open(os.path.join('saved', args.model_name, \ 'Epoch_{}_JA_{:.4}_DDI_{:.4}.model'.format(epoch, ja, ddi_rate)), 'wb')) if epoch != 0 and best_ja < ja: best_epoch = epoch best_ja = ja print('best_epoch: {}'.format(best_epoch)) dill.dump( history, open( os.path.join('saved', args.model_name, 'history_{}.pkl'.format(args.model_name)), 'wb'))
def main(): # load data data_path = '../data/output/records_final.pkl' voc_path = '../data/output/voc_final.pkl' device = torch.device('cuda:{}'.format(args.cuda)) data = dill.load(open(data_path, 'rb')) voc = dill.load(open(voc_path, 'rb')) diag_voc, pro_voc, med_voc = voc['diag_voc'], voc['pro_voc'], voc[ 'med_voc'] np.random.seed(1203) np.random.shuffle(data) split_point = int(len(data) * 3 / 5) data_train = data[:split_point] eval_len = int(len(data[split_point:]) / 2) data_test = data[split_point:split_point + eval_len] data_eval = data[split_point + eval_len:] voc_size = (len(diag_voc.idx2word), len(pro_voc.idx2word), len(med_voc.idx2word)) model = Retain(voc_size, device=device) # model.load_state_dict(torch.load(open(os.path.join("saved", args.model_name, args.resume_path), 'rb'))) if args.Test: model.load_state_dict( torch.load( open(os.path.join("saved", args.model_name, args.resume_path), 'rb'))) model.to(device=device) tic = time.time() eval(model, data_test, voc_size, 0) print('test time: {}'.format(time.time() - tic)) return print('parameters', get_n_params(model)) optimizer = Adam(model.parameters(), args.lr) model.to(device=device) history = defaultdict(list) best_epoch, best_ja = 0, 0 EPOCH = 40 for epoch in range(EPOCH): tic = time.time() print('\nepoch {} --------------------------'.format(epoch + 1)) model.train() for step, input in enumerate(data_train): if len(input) < 2: continue loss = 0 for i in range(1, len(input)): target = np.zeros((1, voc_size[2])) target[:, input[i][2]] = 1 output_logits = model(input[:i]) loss += F.binary_cross_entropy_with_logits( output_logits, torch.FloatTensor(target).to(device)) optimizer.zero_grad() loss.backward() optimizer.step() llprint('\rtraining step: {} / {}'.format(step, len(data_train))) print() tic2 = time.time() ddi_rate, ja, prauc, avg_p, avg_r, avg_f1, avg_add, avg_delete, avg_med = eval( model, data_eval, voc_size, epoch) print('training time: {}, test time: {}'.format( time.time() - tic, time.time() - tic2)) history['ja'].append(ja) history['ddi_rate'].append(ddi_rate) history['avg_p'].append(avg_p) history['avg_r'].append(avg_r) history['avg_f1'].append(avg_f1) history['avg_add'].append(avg_add) history['avg_delete'].append(avg_delete) history['prauc'].append(prauc) history['med'].append(avg_med) if epoch >= 5: print( 'ddi: {}, Med: {}, Ja: {}, F1: {}, Add: {}, Delete: {}'.format( np.mean(history['ddi_rate'][-5:]), np.mean(history['med'][-5:]), np.mean(history['ja'][-5:]), np.mean(history['avg_f1'][-5:]), np.mean(history['avg_add'][-5:]), np.mean(history['avg_delete'][-5:]), np.mean(history['prauc'][-5:]))) torch.save(model.state_dict(), open(os.path.join('saved', args.model_name, \ 'Epoch_{}_JA_{:.4}_DDI_{:.4}.model'.format(epoch, ja, ddi_rate)), 'wb')) if epoch != 0 and best_ja < ja: best_epoch = epoch best_ja = ja print('best_epoch: {}'.format(best_epoch)) dill.dump( history, open( os.path.join('saved', args.model_name, 'history_{}.pkl'.format(args.model_name)), 'wb'))
def main(): if not os.path.exists(os.path.join("saved", model_name)): os.makedirs(os.path.join("saved", model_name)) data_path = '../../data/records_final.pkl' voc_path = '../../data/voc_final.pkl' device = torch.device('cuda:0') data = dill.load(open(data_path, 'rb')) voc = dill.load(open(voc_path, 'rb')) diag_voc, pro_voc, med_voc = voc['diag_voc'], voc['pro_voc'], voc[ 'med_voc'] split_point = int(len(data) * 2 / 3) data_train = data[:split_point] eval_len = int(len(data[split_point:]) / 2) data_test = data[split_point:split_point + eval_len] data_eval = data[split_point + eval_len:] voc_size = (len(diag_voc.idx2word), len(pro_voc.idx2word), len(med_voc.idx2word)) EPOCH = 30 LR = 0.0002 TEST = False END_TOKEN = voc_size[2] + 1 model = Leap(voc_size, device=device) if TEST: model.load_state_dict( torch.load( open(os.path.join("saved", model_name, resume_name), 'rb'))) # pass model.to(device=device) print('parameters', get_n_params(model)) optimizer = Adam(model.parameters(), lr=LR) if TEST: eval(model, data_test, voc_size, 0) else: history = defaultdict(list) for epoch in range(EPOCH): loss_record = [] start_time = time.time() model.train() for step, input in enumerate(data_train): for adm in input: loss_target = adm[2] + [END_TOKEN] output_logits = model(adm) loss = F.cross_entropy( output_logits, torch.LongTensor(loss_target).to(device)) loss_record.append(loss.item()) optimizer.zero_grad() loss.backward(retain_graph=True) optimizer.step() llprint('\rTrain--Epoch: %d, Step: %d/%d' % (epoch, step, len(data_train))) ddi_rate, ja, prauc, avg_p, avg_r, avg_f1 = eval( model, data_eval, voc_size, epoch) history['ja'].append(ja) history['ddi_rate'].append(ddi_rate) history['avg_p'].append(avg_p) history['avg_r'].append(avg_r) history['avg_f1'].append(avg_f1) history['prauc'].append(prauc) end_time = time.time() elapsed_time = (end_time - start_time) / 60 llprint( '\tEpoch: %d, Loss1: %.4f, One Epoch Time: %.2fm, Appro Left Time: %.2fh\n' % (epoch, np.mean(loss_record), elapsed_time, elapsed_time * (EPOCH - epoch - 1) / 60)) torch.save( model.state_dict(), open( os.path.join( 'saved', model_name, 'Epoch_%d_JA_%.4f_DDI_%.4f.model' % (epoch, ja, ddi_rate)), 'wb')) print('') dill.dump(history, open(os.path.join('saved', model_name, 'history.pkl'), 'wb')) # test torch.save( model.state_dict(), open(os.path.join('saved', model_name, 'final.model'), 'wb'))