def baseline(prep_cache_dir, sol_dir): print "Starting Baseline" e = Evaluation(prep_cache_dir) print "Loading cache" t_users = load_cache(prep_cache_dir, 'target_users_set') local_t_items = load_cache(prep_cache_dir, 'target_items_local_set') local_baseline_counts = load_cache(prep_cache_dir, 'tr_baseline_counts') online_t_items = set(load_cache(prep_cache_dir, 'target_items_list')) online_baseline_counts = load_cache(prep_cache_dir, 'tr_va_baseline_counts') prem_users = load_cache(prep_cache_dir, 'prem_user_set') print "Done loading" print "Score users by tr obs" local_temp = score_users_by_obs(t_users, local_baseline_counts, prem_users) print "Score users by tr+va obs" online_temp = score_users_by_obs(t_users, online_baseline_counts, prem_users) strategy = [1, 2, 3] for s in strategy: print "Baseline strategy", s local_filename = str(s) + '_baseline_local_eval_scores.txt' sub_filename = str(s) + '_baseline_submission.txt' local_rec_dump = str(s) + '_baseline_local_raw_rec' online_rec_dump = str(s) + '_baseline_online_raw_rec' local_recs = baseline_recommend(local_temp, local_t_items, s) online_recs = baseline_recommend(online_temp, online_t_items, s) log_recs(local_recs, local_rec_dump, sol_dir) log_recs(online_recs, online_rec_dump, sol_dir) e.format_submission(local_recs, online_recs, sol_dir, local_filename, sub_filename) print "Done with Baseline"
def expectimax(game, placements, feature_weights, beam = 1, return_all = False): eval = Evaluation(game, feature_weights) if not placements: return None, eval.value() def _expectimax(game, placements): if not placements: return eval.value() value = 0 for p in placements[0]: best = float('-inf') moves = game.own.field.moves(p) if moves and game.own.skips: moves.append(None) for m in moves: eval.update(m, *game.own.move(m)) v = _expectimax(game, placements[1:]) eval.rollback(*game.own.undo()) if v > best: best = v value += best return value / len(placements[0]) best = None, float('-inf') all = [] if return_all else None moves = game.own.field.moves(placements[0][0]) if moves and game.own.skips: moves.append(None) if beam < 1 and len(placements) > 1: def _snap_eval(m): eval.update(m, *game.own.move(m)) v = eval.value() eval.rollback(*game.own.undo()) return v num_beam = int(math.ceil(beam * len(moves))) moves = heapq.nlargest(num_beam, moves, key=_snap_eval) for m in moves: eval.update(m, *game.own.move(m)) v = _expectimax(game, placements[1:]) eval.rollback(*game.own.undo()) if v > best[1]: best = m, v if all is not None: all.append((m, v)) return (best, all) if return_all else best
def __init__(self, input, evaluate=False, instant=False): self.instant = instant self.model = load_model(MODEL) self.evaluate = evaluate if self.evaluate: self.evaluator = Evaluation() self.X_train, \ self.Y_train, \ self.word2int, \ self.int2word, \ self.tag2int, \ self.int2tag, \ self.tag2instances = load_data() self.input_texts = input self.words_pro_sent = [] self.predicted_tags = [] self.correct_tags = [] self.tokenized_sentence = []
def __init__(self, backbone, head, train_data_loader, val_dataset, criterions=None, loss_weights=None, optimizer=None, backbone_name='backbone', head_name='head', lowest_train_loss=5, use_cuda=True, gpu_list=None): self.train_data_loader = train_data_loader self.backbone = backbone self.head = head self.backbone_name = backbone_name self.head_name = head_name self.loss_forward = loss_forward self.evaluation = Evaluation(val_dataset, 'lfw',self.batch_inference) self.criterions = criterions self.loss_weights = loss_weights self.optimizer = optimizer self.lowest_train_loss = lowest_train_loss self.use_cuda = use_cuda self.gpu_list = gpu_list self.writer = SummaryWriter() sys.stdout = Logger() self.epoch = 0 self.max_epoch = 400 self.combine_conv_bn_epoch = 150 self.init_meter() if self.criterions is None: self.criterions = {'xent': torch.nn.CrossEntropyLoss()} if self.loss_weights is None: self.loss_weights = torch.as_tensor([1.0]*len(self.criterions)) if self.gpu_list is None: self.gpu_list = range(torch.cuda.device_count()) if self.use_cuda: self.backbone.cuda() self.head.cuda() self.loss_weights = self.loss_weights.cuda()
# for each experiment, tuning num_epoch times NUM_EPOCH = 1 BATCH_SIZE = 899 BETA = 1 EPSILON = 0.1 # create environment dist1 = Distribution(id=0, vals=[2], probs=[1]) dist2 = Distribution(id=1, vals=[5], probs=[1]) dist3 = Distribution(id=2, vals=[2,8], probs=[0.5,0.5]) env = Environment(total_bandwidth = 10,\ distribution_list=[dist1,dist2,dist3], \ mu_list=[1,2,3], lambda_list=[3,2,1],\ num_of_each_type_distribution_list=[300,300,300]) evaluation = Evaluation() class PPODataset(Dataset): def __init__(self, obs_list, action_list, advantage_list): self.obs_list = torch.cat(obs_list, 0) self.action_list = torch.tensor(action_list, dtype=torch.int64) self.advantage_list = torch.tensor(advantage_list, dtype=torch.float32) def __getitem__(self, index): return self.obs_list[index,:], self.action_list[index], self.advantage_list[index] def __len__(self): return self.obs_list.shape[0] #1. Initialize network class PPO(nn.Module):
def run_epoch(data, is_training, model, optimizer): ''' Train model for one pass of train data, and return loss, acccuracy ''' data_loader = torch.utils.data.DataLoader(data, batch_size=20, shuffle=True, num_workers=4, drop_last=False) losses = [] if is_training: model.train() else: model.eval() for batch in data_loader: pid_title = torch.unsqueeze(Variable(batch['pid_title']), 1) pid_body = torch.unsqueeze(Variable(batch['pid_body']), 1) rest_title = Variable(batch['rest_title']) rest_body = Variable(batch['rest_body']) pid_title_pad = torch.unsqueeze(Variable(batch['pid_title_pad']), 1) pid_body_pad = torch.unsqueeze(Variable(batch['pid_body_pad']), 1) rest_title_pad = Variable(batch['rest_title_pad']) rest_body_pad = Variable(batch['rest_body_pad']) pid_title, pid_body = pid_title.cuda(), pid_body.cuda() rest_title, rest_body = rest_title.cuda(), rest_body.cuda() pid_title_pad, pid_body_pad = pid_title_pad.cuda(), pid_body_pad.cuda() rest_title_pad, rest_body_pad = rest_title_pad.cuda( ), rest_body_pad.cuda() if is_training: optimizer.zero_grad() pt = model(pid_title) pb = model(pid_body) rt = model(rest_title) rb = model(rest_body) # we need to take the mean pooling taking into account the padding # tensors are of dim batch_size x samples x output_size x (len - kernel + 1) # pad tensors are of dim batch_size x samples x (len - kernel + 1) pid_title_pad_ex = torch.unsqueeze(pid_title_pad, 2).expand_as(pt) pid_body_pad_ex = torch.unsqueeze(pid_body_pad, 2).expand_as(pb) rest_title_pad_ex = torch.unsqueeze(rest_title_pad, 2).expand_as(rt) rest_body_pad_ex = torch.unsqueeze(rest_body_pad, 2).expand_as(rb) pt = torch.squeeze(torch.sum(pt * pid_title_pad_ex, dim=3), dim=3) pb = torch.squeeze(torch.sum(pb * pid_body_pad_ex, dim=3), dim=3) rt = torch.squeeze(torch.sum(rt * rest_title_pad_ex, dim=3), dim=3) rb = torch.squeeze(torch.sum(rb * rest_body_pad_ex, dim=3), dim=3) # tensors are not of dim batch_size x samples x output_size # need to scale down because not all uniformly padded ptp_norm = torch.sum(pid_title_pad, dim=2).clamp(min=1).expand_as(pt) pbp_norm = torch.sum(pid_body_pad, dim=2).clamp(min=1).expand_as(pb) rtp_norm = torch.sum(rest_title_pad, dim=2).clamp(min=1).expand_as(rt) rbp_norm = torch.sum(rest_body_pad, dim=2).clamp(min=1).expand_as(rb) pt = pt / ptp_norm pb = pb / pbp_norm rt = rt / rtp_norm rb = rb / rbp_norm pid_tensor = (pt + pb) / 2 rest_tensor = (rt + rb) / 2 if is_training: loss = loss_function(pid_tensor, rest_tensor) loss.backward() losses.append(loss.cpu().data[0]) optimizer.step() else: expanded = pid_tensor.expand_as(rest_tensor) similarity = cs(expanded, rest_tensor, dim=2).squeeze(2) similarity = similarity.data.cpu().numpy() labels = batch['labels'].numpy() l = convert(similarity, labels) losses.extend(l) # Calculate epoch level scores if is_training: avg_loss = np.mean(losses) return avg_loss else: e = Evaluation(losses) MAP = e.MAP() * 100 MRR = e.MRR() * 100 P1 = e.Precision(1) * 100 P5 = e.Precision(5) * 100 return (MAP, MRR, P1, P5)
def evaluate_hand(self, list_cards): evaluation = Evaluation(list_cards).evaluate_hand() return evaluation
rec, neg_u = load_data(daily_dir) print('before filtering: total counts {}'.format(total_count_ui_pairs(rec))) rec_filtered = filter_out_negs(rec, neg_u) print('after filtering: total counts {}'.format(total_count_ui_pairs(rec_filtered))) rec_processed = process_rec_single_user_online_round(rec_filtered) print('after postprocess: total counts {}'.format(total_count_ui_pairs(rec_processed))) ''' local evalation part ''' prep_dir='../examples/preprocessing_cache/' raw_data='../raw_data/xing/' if len(sys.argv) >= 3: gt_dir = sys.argv[2] from evaluate import Evaluation e = Evaluation(prep_dir, raw_data, daily_dir, gt_dir) scores = e.local_eval_on(rec_processed) print('scores: {}'.format(scores))
def compute_scores(raw_data_dir=FLAGS.raw_data, data_dir=FLAGS.data_dir, dataset=FLAGS.dataset, save_recommendation=FLAGS.saverec, train_dir=FLAGS.train_dir, test=FLAGS.test, true_targets=FLAGS.true_targets): from evaluate import Evaluation from post_processing import filter_out_negs e = Evaluation(FLAGS.prep_dir, raw_data_dir, FLAGS.raw_data_daily) daily_raw_data_dir=FLAGS.raw_data_daily if true_targets: if FLAGS.reverse: user_file_name = os.path.join(daily_raw_data_dir, 'daily_target_items_list') else: user_file_name = os.path.join(daily_raw_data_dir, 'daily_target_users_set') else: if FLAGS.reverse: user_file_name = os.path.join(raw_data_dir, 'daily_target_local_list') else: user_file_name = os.path.join(raw_data_dir, 'target_users_set') if FLAGS.new_users: if FLAGS.reverse: item_file_name = os.path.join(daily_raw_data_dir, 'daily_target_users_set') else: item_file_name = os.path.join(daily_raw_data_dir, 'daily_target_items_list') else: if FLAGS.reverse: item_file_name = os.path.join(raw_data_dir, 'target_users_set') else: item_file_name = os.path.join(raw_data_dir, 'daily_target_local_list') # if FLAGS.reverse: # target_users = pickle.load(open(user_file_name, 'rb')) # t_ids = pickle.load(open(item_file_name, 'rb')) # else: target_users = pickle.load(open(item_file_name, 'rb')) t_ids = pickle.load(open(user_file_name, 'rb')) #t_ids = pickle.load(open(item_file_name, 'rb')) t_ids = list(t_ids) target_users = set(target_users) print(FLAGS.new_users, FLAGS.true_targets) print(len(t_ids),len(target_users)) if FLAGS.reverse: suf = '' else: suf = '_rev' if true_targets and FLAGS.new_users: reclogfile = 'online_raw_rec_hmf' + suf elif not FLAGS.true_targets and not FLAGS.new_users: reclogfile = 'local_raw_rec_hmf' + suf # if true_targets: # reclogfile = "online_raw_rec_hmf" # t_ids = pickle.load(open(os.path.join(daily_raw_data_dir, 'daily_target_items_list'), 'rb')) # target_users = pickle.load(open(os.path.join(daily_raw_data_dir, 'daily_target_users_set'), 'rb')) # else: # reclogfile = "local_raw_rec_hmf" # t_ids = pickle.load(open(os.path.join(raw_data_dir, 'target_items_local_list'), 'rb')) # target_users = pickle.load(open(os.path.join(raw_data_dir, 'target_users_set'), 'rb')) R = recommend(t_ids, data_dir=data_dir) # rec_save_path = os.path.join(daily_raw_data_dir, reclogfile) # R = pickle.load(open(rec_save_path, 'rb')) if save_recommendation: rec_save_path = os.path.join(daily_raw_data_dir, reclogfile) pickle.dump(R, open(rec_save_path, 'wb')) # R = filter_recs(R, set(target_users)) if not FLAGS.reverse: print('no post processing is needed. return') return neg_users_set = pickle.load(open(os.path.join(FLAGS.prep_dir, 'neg_users_set')+suf, 'rb')) # e.online_solutions_write(R, daily_raw_data_dir, 'basic_rec_done') R_filtered = filter_out_negs(R, neg_users_set) # e.online_solutions_write(R_filtered, daily_raw_data_dir, 'fitered_out_negs_done') if true_targets: # R = process_rec_single_user_online_round(R) # e.online_solutions_write(R, daily_raw_data_dir, 'online_submission.txt') R_filtered = process_rec_single_user_online_round(R_filtered) e.online_solutions_write(R_filtered, daily_raw_data_dir, 'neg_filtered_online_submission' + suf + '.txt') else: scores = e.local_eval_on(R) e.local_write_scores(scores, 'local_eval_scores'+suf+'.txt', train_dir) scores_filteredR = e.local_eval_on(R_filtered) e.local_write_scores(scores_filteredR, 'neg_filtered_local_eval_scores'+suf+'.txt', train_dir)
def main(): # create environment dist1 = Distribution(id=0, vals=[2], probs=[1]) dist2 = Distribution(id=1, vals=[5], probs=[1]) dist3 = Distribution(id=2, vals=[2, 8], probs=[0.5, 0.5]) env = Environment(total_bandwidth = 10,\ distribution_list=[dist1,dist2,dist3], \ mu_list=[1,2,3], lambda_list=[3,2,1],\ num_of_each_type_distribution_list=[300,300,300]) evaluation = Evaluation() obs_dim = 6 act_dim = 2 # logger.info('obs_dim {}, act_dim {}'.format(obs_dim, act_dim)) # 根据parl框架构建agent model = PGTorchModel(state_dim=obs_dim, act_dim=act_dim) alg = PGTorchAlgorithm(model, lr=LEARNING_RATE) agent = PGTorchAgent(alg, obs_dim=obs_dim, act_dim=act_dim) # 加载模型 if os.path.exists('./pg_torch_model'): agent.restore('./pg_torch_model') writer = SummaryWriter() for i in range(1000): obs_list, action_list, reward_list = run_episode(env, agent) writer.add_scalars('Reward/train', {'train_reward':sum(reward_list)/len(reward_list), \ 'reject when full': evaluation.reject_when_full_avg_reward, \ 'always accept': evaluation.always_accept_avg_reward,\ 'always reject': evaluation.always_reject_avg_reward}, i) # writer.add_scalar('Reward/train', evaluation.always_reject_avg_reward, i) if i % 10 == 0: # logger.info("Episode {}, Reward Sum {}.".format( # i, sum(reward_list))) print("Episode {}, Reward Sum {}.".format( i, sum(reward_list) / len(reward_list))) batch_obs = torch.from_numpy(np.array(obs_list)) batch_action = torch.from_numpy(np.array(action_list)) batch_reward = torch.from_numpy( calc_reward_to_go(reward_list, gamma=0.9)) loss = agent.learn(batch_obs, batch_action, batch_reward) writer.add_scalar('Loss/train', loss, i) if (i + 1) % 100 == 0: avg_reward, avg_acc_rate = evaluation.evaluate(agent) writer.add_scalars('reward Test', {'test reward': avg_reward, \ 'reject when full': evaluation.reject_when_full_avg_reward, \ 'always accept': evaluation.always_accept_avg_reward,\ 'always reject': evaluation.always_reject_avg_reward}, i) writer.add_scalars('accept rate Test', {'test rate': avg_acc_rate, \ 'reject when full': evaluation.reject_when_full_avg_acc_rate, \ 'always accept': evaluation.always_accept_avg_acc_rate,\ 'always reject': evaluation.always_reject_avg_acc_rate}, i) print('avg_reward', avg_reward, 'avg_acc_rate', avg_acc_rate, 'base ', evaluation.reject_when_full_avg_reward) writer.close() # save the parameters to ./pg_torch_model agent.save('./pg_torch_model')
print "Finished loading" print "Starting post processing" t_users_int_set = load_cache(prep_dir, 'target_users_set') neg_user_int_set = load_cache(prep_dir, 'neg_users_set') filtered_rec_local = filter_recs(rec_local, t_users_int_set) filtered_rec_online = filter_recs(rec_online, t_users_int_set) print "Intermediate" neg_filtered_rec_local = filter_out_negs(filtered_rec_local, neg_user_int_set) neg_filtered_rec_online = filter_out_negs(filtered_rec_online, neg_user_int_set) print "Cutoff" filtered_rec_local = cutoff100(filtered_rec_local) filtered_rec_online = cutoff100(filtered_rec_online) neg_filtered_rec_local = cutoff100(neg_filtered_rec_local) neg_filtered_rec_online = cutoff100(neg_filtered_rec_online) print "Finished post processing" print "Starting evaluation and submission" e = Evaluation(prep_cache_dir=prep_dir) e.format_submission(filtered_rec_local, filtered_rec_online, sol_dir, '_eval_scores_hmf1_filtered_1000to100.txt', '_subm_hmf1_filtered_1000to100.txt') e.format_submission(neg_filtered_rec_local, neg_filtered_rec_online, sol_dir, '_eval_scores_hmf1_negfiltered_1000to100.txt', '_subm_hmf1_negfiltered_1000to100.txt') print "Done"
def run_epoch(data, is_training, model, optimizer, transfer=False): # Make batches data_loader = torch.utils.data.DataLoader( data, batch_size=10, shuffle=True, num_workers=4, drop_last=False) losses = [] actual = [] expected = [] if is_training: model.train() else: model.eval() for batch in data_loader: # Unpack training instances pid_title = torch.unsqueeze(Variable(batch['pid_title']), 1).cuda() # Size: batch_size x 1 x title_length=40 pid_title_mask = torch.unsqueeze(Variable(batch['pid_title_mask']), 1).cuda() # Size: batch_size x 1 x title_length=40 pid_body = torch.unsqueeze(Variable(batch['pid_body']), 1).cuda() # Size: batch_size x 1 x body_length=100 pid_body_mask = torch.unsqueeze(Variable(batch['pid_body_mask']), 1).cuda() # Size: batch_size x 1 x body_length=100 candidate_title = Variable(batch['candidate_titles']).cuda() # Size: batch_size x # candidates (21 in training) x title_length=40 candidate_title_mask = Variable(batch['candidate_titles_mask']).cuda() # Size: batch_size x # candidates (21 in training) x title_length=40 candidate_body = Variable(batch['candidate_body']).cuda() # Size: batch_size x # candidates (21 in training) x body_length=100 candidate_body_mask = Variable(batch['candidate_body_mask']).cuda() # Size: batch_size x # candidates (21 in training) x body_length=40 if is_training: optimizer.zero_grad() # Run text through model pid_title = model(pid_title) # batch_size x 1 x output_size=500 x title_length=40(-kernel_size+1 if CNN) pid_body = model(pid_body) # batch_size x 1 x output_size=500 x body_length=100(-kernel_size+1 if CNN) candidate_title = model(candidate_title) # batch_size x # candidates (21 in training) x output_size=500 x title_length=40(-kernel_size+1 if CNN) candidate_body = model(candidate_body) # batch_size x # candidates (21 in training) x output_size=500 x body_length=100(-kernel_size+1 if CNN) pid_title_mask = torch.unsqueeze(pid_title_mask, 2).expand_as(pid_title) # batch_size x 1 x output_size=500 x title_length=40(-kernel_size+1 if CNN) pid_body_mask = torch.unsqueeze(pid_body_mask, 2).expand_as(pid_body) # batch_size x 1 x output_size=500 x body_length=100(-kernel_size+1 if CNN) candidate_title_mask = torch.unsqueeze(candidate_title_mask, 2).expand_as(candidate_title)# batch_size x # candidates (21 in training) x output_size=500 x title_length=40(-kernel_size+1 if CNN) candidate_body_mask = torch.unsqueeze(candidate_body_mask, 2).expand_as(candidate_body) # batch_size x # candidates (21 in training) x output_size=500 x body_length=100(-kernel_size+1 if CNN) good_title = torch.sum(pid_title * pid_title_mask, 3) # batch_size x 1 x output_size=500 good_body = torch.sum(pid_body * pid_body_mask, 3) # batch_size x 1 x output_size=500 cand_titles = torch.sum(candidate_title * candidate_title_mask, 3) # batch_size x # candidates (21 in training) x output_size=500 cand_bodies = torch.sum(candidate_body * candidate_body_mask, 3) # batch_size x # candidates (21 in training) x output_size=500 good_tensor = (good_title + good_body)/2 # batch_size x 1 x output_size=500 cand_tensor = (cand_titles + cand_bodies)/2 # batch_size x # candidates (21 in training) x output_size=500 if is_training: l = loss(good_tensor, cand_tensor, 1.0) l.backward() losses.append(l.cpu().data[0]) optimizer.step() else: similarity = cosine_sim(good_tensor.expand_as(cand_tensor), cand_tensor, dim=2) if transfer: similarity = torch.FloatTensor(similarity.data.cpu().numpy()) else: similarity = similarity.data.cpu().numpy() if transfer: labels = batch['labels'] else: labels = batch['labels'].numpy() def predict(sim, labels): predictions = [] for i in range(sim.shape[0]): sorted_cand = (-sim[i]).argsort() predictions.append(labels[i][sorted_cand]) return predictions if transfer: for sim in similarity: actual.append(sim) expected.extend(labels.view(-1)) else: l = predict(similarity, labels) losses.extend(l) if is_training: avg_loss = np.mean(losses) return avg_loss else: if transfer: auc = AUCMeter() auc.reset() auc.add(torch.cat(actual), torch.LongTensor(expected)) return auc.value(max_fpr=0.05) else: e = Evaluation(losses) MAP = e.MAP()*100 MRR = e.MRR()*100 P1 = e.Precision(1)*100 P5 = e.Precision(5)*100 return (MAP, MRR, P1, P5)