def train(corp, model, config): train_optimizer = optim.Adam(model.parameters(), lr=config.lr, weight_decay=config.l2_weight) # train_data: conv_vecs, conv_lens, conv_turn_lens, my_labels train_data = corp.train_data padded_quotes = corp.padded_quotes quote_lens = corp.quote_lens padded_exp = corp.padded_exp exp_lens = corp.exp_lens corp.test_corpus(config.valid_file, mode='VALID') valid_data = MyDataset(corp, config, 'VALID') valid_loader = data.DataLoader(valid_data, collate_fn=valid_data.my_collate, batch_size=config.batch_size, num_workers=0) best_state = None best_valid_thr = 0.0 best_valid_map = -1.0 best_valid_loss = 999999.99 no_improve = 0 for epoch in range(config.max_epoch): train_epoch(model, train_data, config.loss_weights, train_optimizer, epoch, config, padded_quotes, quote_lens, padded_exp, exp_lens) valid_map, valid_loss = valid_evaluate(model, valid_loader, config, padded_quotes, quote_lens, padded_exp, exp_lens) if best_valid_map < valid_map or best_valid_loss > valid_loss: no_improve = 0 best_state = model.state_dict() if best_valid_map < valid_map: best_valid_map = valid_map print('New Best MAP Valid Result!!! Valid MAP: %g, Valid Loss: %g' % (valid_map, valid_loss)) if best_valid_loss > valid_loss: best_valid_loss = valid_loss print('New Best Loss Valid Result!!! Valid MAP: %g, Valid Loss: %g' % (valid_map, valid_loss)) else: no_improve += 1 print( 'No improve! Current Valid MAP: %g, Best Valid AUC: %g; Current Valid Loss: %g, Best Valid Loss: %g' % ( valid_map, best_valid_map, valid_loss, best_valid_loss)) if no_improve == 5: break model.load_state_dict(best_state) # Final step: Evaluate the model corp.test_corpus(config.test_file, mode='TEST') test_data = MyDataset(corp, config, 'TEST') test_loader = data.DataLoader(test_data, collate_fn=test_data.my_collate, batch_size=config.batch_size, num_workers=0) res = test_evaluate(model, test_loader, config, padded_quotes, quote_lens, padded_exp, exp_lens) print('Result in test set: MAP %g, P@1 %g, P@3 %g, nDCG@5 %g,nDCG@10 %g' % (res[0], res[1], res[2], res[3], res[4])) torch.save(model.state_dict(), config.path + 'map%.4f_p@1%.4f_best_seed%d.model' % (res[0], res[1], config.random_seed)) with open(config.path + 'map%.4f_p@1%.4f_best_seed%d.res' % (res[0], res[1], config.random_seed), 'w') as f: f.write('MAP %g \t P@1 %g \t P@3 %g \t nDCG@5 %g\t nDCG@10 %g\n'% (res[0], res[1], res[2], res[3], res[4])) f.write('\n\nParameters:\n') for key in config.__dict__: f.write('%s : %s\n' % (key, config.__dict__[key]))
def predict(corp, model, config): # print attention weights assert config.print_attention if config.model_path is not None: print('Loading model from:', config.model_path) model.load_state_dict(torch.load(config.model_path, map_location='cpu')) print('Begin prediction!') corp.test_corpus(config.valid_file, mode='VALID') corp.test_corpus(config.test_file, mode='TEST') train_data = MyDataset(corp, config, 'TRAIN') train_loader = data.DataLoader(train_data, collate_fn=corp.train_data.my_collate, batch_size=config.batch_size, num_workers=0) print_attention(corp, model, train_loader, config)
#-*-coding:utf8-*- __author__ = "buyizhiyou" __date__ = "2017-10-18" from alexnet import AlexNet import torch.optim as optim import torch.nn as nn from torch.autograd import Variable from data_process import MyDataset from torchvision import transforms, utils from torch.utils.data import DataLoader import torch train_data = MyDataset(txt='./data/train.txt', transform=transforms.ToTensor()) train_loader = DataLoader(train_data, batch_size=50, shuffle=True) #返回的是迭代器 test_data = MyDataset(txt='./data/val.txt', transform=transforms.ToTensor()) test_loader = DataLoader(test_data, batch_size=50) model = AlexNet().cuda() #使用gpu,将模型加载到显存 print(model) #print(list(model.parameters())) optimizer = optim.Adam(model.parameters(), lr=0.001) loss_func = nn.CrossEntropyLoss() #开始训练 for epoch in range(30): print('epoch {}'.format(epoch + 1)) #training------------------------ train_loss = 0
def train(config): filename = config.filename modelname = config.modelname corp = Corpus(filename, config.max_word_num, config.pred_pc) config.user_num, config.conv_num, config.vocab_num = corp.userNum, corp.convNum, corp.wordNum config.sampling = True if 'reddit' in filename else False train_ratings, test_data, dev_data = form_dataset(corp, config.batch_size, sampling=config.sampling) if modelname != "NCF": conv_data = create_conv_data(corp.convs) if config.ncf_pretrained: ncf_path = "BestModels/NCF/For_pretrained/" + filename.split(".")[0] + "/" + str(config.factor_dim) + \ "_" + str(config.text_factor_dim) + "_" + str(config.pred_pc) + ".model" if torch.cuda.is_available(): # run in GPU weights = torch.load(ncf_path) else: weights = torch.load(ncf_path, map_location='cpu') ncf_weights = (weights['mf_user_embedding.weight'], weights['mf_conv_embedding.weight'], weights['mlp_user_embedding.weight']) else: ncf_weights = None if modelname == "NCFGCN" or modelname == "NCFGRN": arcs = create_arc_info(conv_data) if modelname == "NCF": embedding_matrix = None else: embedding_matrix = create_embedding_matrix(filename, corp, config.embedding_dim) if modelname == 'NCF': model = NCF(config) path_name = str(config.pred_pc) + "_" + str(config.batch_size) + "_" + str(config.factor_dim) + "_" + \ str(config.text_factor_dim) + "_" + str(config.mlp_layers_num) + "_" + str(config.neg_sample_num) + "_" + \ str(config.lr) + "_" + str(int(config.pos_weight)) + "-" + str(config.runtime) elif modelname == 'NCFBiLSTM': model = NCFBiLSTM(config, conv_data, embedding_matrix, ncf_weights) path_name = str(config.pred_pc) + "_" + str(config.batch_size) + "_" + str(config.factor_dim) + "_" + \ str(config.text_factor_dim) + "_" + str(config.kernal_num) + "_" + str(config.hidden_dim) + "_" + \ str(config.mlp_layers_num) + "_" + str(config.neg_sample_num) + "_" + str(config.lr) + "_" + \ str(int(config.pos_weight)) + "_" + ("bi" if config.bi_direction else "nbi") + "_" + \ ("pt" if config.ncf_pretrained else "npt") + "-" + str(config.runtime) elif modelname == 'NCFGCN': model = NCFGModel(config, modelname, conv_data, arcs, embedding_matrix, ncf_weights) path_name = str(config.pred_pc) + "_" + str(config.batch_size) + "_" + str(config.factor_dim) + "_" + \ str(config.text_factor_dim) + "_" + str(config.kernal_num) + "_" + str(config.hidden_dim) + "_" + \ str(config.mlp_layers_num) + "_" + str(config.gcn_layers_num) + "_" + str(config.neg_sample_num) + \ "_" + str(config.lr) + "_" + str(int(config.pos_weight)) + "_" + ("g" if config.use_gates else "ng") + "_" + \ ("lstm" if config.use_lstm else "nlstm") + "_" + ("pt" if config.ncf_pretrained else "npt") + "-" + str(config.runtime) elif modelname == 'NCFGRN': model = NCFGModel(config, modelname, conv_data, arcs, embedding_matrix, ncf_weights) path_name = str(config.pred_pc) + "_" + str(config.batch_size) + "_" + str(config.factor_dim) + "_" + \ str(config.text_factor_dim) + "_" + str(config.kernal_num) + "_" + str(config.hidden_dim) + "_" + \ str(config.mlp_layers_num) + "_" + str(config.grn_states_num) + "_" + str(config.neg_sample_num) + "_" + \ str(config.lr) + "_" + str(int(config.pos_weight)) + "_" + ("pt" if config.ncf_pretrained else "npt") + "-" + str(config.runtime) else: print 'Modelname Wrong!' exit() res_path = "BestResults/" + modelname + "/" + filename.split('.')[0] + "/" mod_path = "BestModels/" + modelname + "/" + filename.split('.')[0] + "/" if not os.path.isdir(res_path): os.makedirs(res_path) if not os.path.isdir(mod_path): os.makedirs(mod_path) mod_path += path_name + '.model' res_path += path_name + '.data' loss_weights = torch.Tensor([1, config.pos_weight]) if torch.cuda.is_available(): # run in GPU model = model.cuda() loss_weights = loss_weights.cuda() if config.optim == 'adam': optimizer = optim.Adam(model.parameters(), lr=config.lr) else: optimizer = optim.SGD(model.parameters(), lr=config.lr) scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda epoch: 1.0 / ((epoch + 1) ** 0.5)) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda epoch: 0.1 ** (epoch // 10)) best_dev_map = -1.0 best_epoch = -1 no_improve = 0 for epoch in range(config.max_epoch): scheduler.step() train_data = MyDataset(corp, train_ratings, config.neg_sample_num, True) train_loader = data.DataLoader(train_data, batch_size=config.batch_size, num_workers=0, shuffle=True) loss = train_epoch(model, train_loader, loss_weights, optimizer, epoch) dev_map = evaluate(model, dev_data, dev=True) if dev_map > best_dev_map: no_improve = 0 best_dev_map = dev_map os.system('rm ' + mod_path) best_epoch = epoch print('New Best Dev!!! MAP: %g' % best_dev_map) torch.save(model.state_dict(), mod_path) else: no_improve += 1 print('Current Best Dev MAP: %g, Dev MAP: %g' % (best_dev_map, dev_map)) if no_improve > 8: break model.load_state_dict(torch.load(mod_path)) res = evaluate(model, test_data) print('Result in test set: MAP: %g, Precision@1: %g, Precision@5: %g, nDCG@5: %g, nDCG@10: %g' % (res[0], res[1], res[2], res[3], res[4])) with open(res_path, 'w') as f: f.write('MAP: %g, Precision@1: %g, Precision@5: %g, nDCG@5: %g, nDCG@10: %g\n' % (res[0], res[1], res[2], res[3], res[4])) f.write('Dev MAP: %g\n' % best_dev_map) f.write('Best epoch: %d\n' % best_epoch)
def train(config): print 'Start training: ' + config.filename + ' ' + config.modelname trainfile, testfile, validfile = find_files(config.filename, config.month_num) modelname = config.modelname # Process the corpus and prepare pretrained parameters (if any) corp = Corpus(trainfile, testfile, validfile) config.user_num, config.conv_num, config.vocab_num = corp.userNum, corp.convNum, corp.wordNum train_convs, train_convs_per_user, test_data, dev_data = form_dataset(corp, config.batch_size) conv_data = create_tensor(corp.convs) if config.pretrained_file == 'NULL': embedding_matrix = None else: embedding_matrix = create_pretrain_embeddings(corp, config.embedding_dim, config.pretrained_file) # Set the model and saving path if modelname == 'DCR': user_history = create_history_tensor(corp.userNum, corp.user_history) arcs = create_arc_info(conv_data, no_time=True) model = DCR(config, conv_data, user_history, arcs, embedding_matrix) path_name = str(config.batch_size) + "_" + str(config.factor_dim) + "_" + str(config.embedding_dim) + "_" + \ str(config.kernal_num) + "_" + str(config.hidden_dim) + "_" + str(config.gcn_layers_num) + "_" + \ str(config.neg_sample_num) + "_" + str(config.lr) + "_" + str(int(config.pos_weight)) + "_" + config.att if config.month_num != 4: path_name += "_" + str(config.month_num) + "m_" + str(config.runtime) else: path_name += "_" + str(config.runtime) if config.pretrained_file == 'NULL': path_name += "_npembed" if config.no_lstm: path_name += "_nlstm" if config.no_gcn: path_name += "_ngcn" if config.mlp_layers_num == 0: path_name += "_nmlp" else: print 'Modelname Wrong!' exit() res_path = "BestResults/" + modelname + "/" + config.filename + "/" mod_path = "BestModels/" + modelname + "/" + config.filename + "/" if not os.path.isdir(res_path): os.makedirs(res_path) if not os.path.isdir(mod_path): os.makedirs(mod_path) mod_path += path_name + '.model' res_path += path_name + '.data' # Set the optimizing parameters loss_weights = torch.Tensor([1, config.pos_weight]) if torch.cuda.is_available(): # run in GPU model = model.cuda() loss_weights = loss_weights.cuda() if config.optim == 'adam': optimizer = optim.Adam(model.parameters(), lr=config.lr) else: optimizer = optim.SGD(model.parameters(), lr=config.lr) scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda epoch: 1.0 / ((epoch + 1) ** 0.5)) best_dev_map = 0.0 best_epoch = -1 best_epoch_loss = 999999 no_improve = 0 # Begin training for epoch in range(config.max_epoch): train_data = MyDataset(train_convs, train_convs_per_user, train=True, num_sampling=config.neg_sample_num) train_loader = data.DataLoader(train_data, batch_size=config.batch_size, num_workers=0, shuffle=True) loss = train_epoch(model, train_loader, loss_weights, optimizer, epoch) dev_map = evaluate(model, dev_data, dev=True) if dev_map > best_dev_map: no_improve = 0 best_dev_map = dev_map os.system('rm ' + mod_path) best_epoch = epoch best_epoch_loss = loss print('New Best Dev!!! MAP: %g' % best_dev_map) torch.save(model.state_dict(), mod_path) else: no_improve += 1 print('Current Best Dev MAP: %g, Dev MAP: %g' % (best_dev_map, dev_map)) if no_improve > 8: break scheduler.step() model.load_state_dict(torch.load(mod_path)) # Evaluate and save results res = evaluate(model, test_data) print('Result in test set: MAP: %g, Precision@1: %g, Precision@5: %g, nDCG@5: %g, nDCG@10: %g, MRR: %g' % (res[0], res[1], res[2], res[3], res[4], res[5])) with open(res_path, 'w') as f: f.write('MAP\tPre@1\tPre@5\tnDCG@5\tnDCG@10\tMRR\n') f.write('%g\t%g\t%g\t%g\t%g\t%g\n' % (res[0], res[1], res[2], res[3], res[4], res[5])) if modelname != "Pop" and modelname != "Random": f.write('Dev MAP: %g\n' % best_dev_map) f.write('Best epoch: %d\n' % best_epoch) f.write('Best epoch loss: %g\n' % best_epoch_loss)