def learning_phase(sent_trn, idx_trn, sent_pool, idx_pool, sent_test, model, agent, max_data_points, step, f1_list): logger.info(' * Start learning phase *') for t in range(0, max_data_points): step += 1 # Random sample k points from D_un sent_rand_unl, idx_rand_unl, queryindices = utilities.randomKSamples(sent_pool, idx_pool, k) # Use the policy to get best sample state = utilities.getAllState(idx_trn, sent_rand_unl, idx_rand_unl, model, w2v, max_len) action=agent.predict(state)[0] theindex = queryindices[action] sent_trn.append(sent_pool[theindex]) idx_trn.append(idx_pool[theindex]) model.train(sent_trn) # delete the selected data point from the pool del sent_pool[theindex] del idx_pool[theindex] if (step + 1) % 5 == 0: f1_score = model.test(sent_test, label2str) f1_list.append(f1_score) logger.info('[Learning phase] Budget used so far: {}'.format(str(step))) logger.info(' * Labeled data size: {}'.format(str(len(sent_trn)))) logger.info(' * Unlabeled data size: {}'.format(str(len(sent_pool)))) logger.info(" [Step {}] Accurary : {}".format(str(step), str(f1_score))) return model, step, f1_list, sent_trn, idx_trn, sent_pool, idx_pool
def learning_phase(sent_trn, idx_trn, sent_pool, idx_pool, sent_test, model, agent, max_data_points, step, f1_list): logger.info(' * Start learning phase *') for t in range(0, max_data_points): step += 1 # Random sample k points from D_un if args.al_candidate_selection_mode == 'random': logger.info(" * Random candidate selections") sent_rand_unl, idx_rand_unl, queryindices = utilities.randomKSamples( sent_pool, idx_pool, args.k_learning) elif args.al_candidate_selection_mode == 'uncertainty': logger.info(" * Uncertainty candidate selections") sent_rand_unl, idx_rand_unl, queryindices = utilities.sample_from_top_n_uncertainty( sent_pool, idx_pool, model, args.n_learning, args.k_learning) else: logger.info( " * Unknown mode. use Uncertainty candidate selections") sent_rand_unl, idx_rand_unl, queryindices = utilities.sample_from_top_n_uncertainty( sent_pool, idx_pool, model, args.n_learning, args.k_learning) # Use the policy to get best sample state = utilities.getAllState(idx_trn, sent_rand_unl, idx_rand_unl, model, w2v, max_len, num_classes) action = agent.predict(args.k_learning, state) logger.info("Learning action {}".format(action)) theindex = queryindices[action] sent_trn.append(sent_pool[theindex]) idx_trn.append(idx_pool[theindex]) model.train(sent_trn) # delete the selected data point from the pool del sent_pool[theindex] del idx_pool[theindex] if (step + 1) % 5 == 0: f1_score = model.test(sent_test, label2str) f1_list.append(f1_score) logger.info('[Learning phase] Budget used so far: {}'.format( str(step))) logger.info(' * Labeled data size: {}'.format(str(len(sent_trn)))) logger.info(' * Unlabeled data size: {}'.format(str( len(sent_pool)))) logger.info(" [Step {}] Accurary : {}".format( str(step), str(f1_score))) return model, step, f1_list, sent_trn, idx_trn, sent_pool, idx_pool
def dreaming_phase(train_la, train_la_idx, train_pool, train_pool_idx, sent_dev, budget, episodes, agent, expert): logger.info(' * Start Dreaming phase * ') states = [] actions = [] for tau in range(0, episodes): # Shuffle and split initial train, validation set sent_trn = list(train_la) idx_trn = list(train_la_idx) sent_pool = list(train_pool) idx_pool = list(train_pool_idx) logger.info( "[Episode {}] Partition data: labeled = {}, val = {}, unlabeled pool = {} " .format(str(tau), len(sent_trn), len(sent_dev), len(sent_pool))) # Memory (two lists) to store states and actions tagger_dreamming = "{}/{}_tagger_temp.h5".format( args.output, DATASET_NAME) if os.path.exists(tagger_dreamming): os.remove(tagger_dreamming) model = CRFTagger(tagger_dreamming, num_classes=num_classes) if len(sent_trn) > 0: model.train(sent_trn) # In every episode, run the trajectory for t in range(0, budget): logger.info('[Dreaming phase] Episode:' + str(tau + 1) + ' Budget:' + str(t + 1)) row = 0 f1 = -1 # save the index of best data point or acturally the index of action bestindex = 0 # Random sample k points from D_pool if args.dreaming_candidate_selection_mode == 'random': logger.info(" * Random candidate selections") random_pool, random_pool_idx, queryindices = utilities.randomKSamples( sent_pool, idx_pool, k) elif args.dreaming_candidate_selection_mode == 'certainty': logger.info(" * Certainty candidate selections") random_pool, random_pool_idx, queryindices = utilities.sample_from_top_n_certainty( sent_pool, idx_pool, expert, args.n_learning, k) elif args.dreaming_candidate_selection_mode == 'mix': logger.info(" * Mix method candidate selections") c = np.random.rand(1) if c > 0.5: random_pool, random_pool_idx, queryindices = utilities.randomKSamples( sent_pool, idx_pool, k) else: random_pool, random_pool_idx, queryindices = utilities.sample_from_top_n_certainty( sent_pool, idx_pool, expert, args.n_learning, k) else: logger.info(" * Unknown mode, use Random candidate selections") random_pool, random_pool_idx, queryindices = utilities.randomKSamples( sent_pool, idx_pool, k) logger.debug(' * Generate label using expert') x_tokens = [' '.join(expert.sent2tokens(s)) for s in random_pool] y_labels = expert.predict(random_pool) pred_sents = utilities.data2sents(x_tokens, y_labels) for datapoint in zip(pred_sents, random_pool_idx): seq = datapoint[0] idx = datapoint[1] train_la_temp = list(sent_trn) train_la_temp.append(seq) if os.path.exists(tagger_temp): os.remove(tagger_temp) model_temp = CRFTagger(tagger_temp, num_classes=num_classes) model_temp.train(train_la_temp) f1_temp = model_temp.test(dev_sents, label2str) if (f1_temp > f1): bestindex = row f1 = f1_temp row = row + 1 del model_temp del train_la_temp gc.collect() # get the state and action state = utilities.getAllState(idx_trn, random_pool, random_pool_idx, model, w2v, max_len, num_classes) # action=bestindex coin = np.random.rand(1) if (coin > 0.5): logger.debug(' * Use the POLICY [coin = {}]'.format(str(coin))) action = bestindex else: action = agent.predict(args.k, state) states.append(state) actions.append(action) # update the model theindex = queryindices[bestindex] sent_trn.append(sent_pool[theindex]) idx_trn.append(idx_pool[theindex]) model.train(sent_trn) # delete the selected data point from the pool del sent_pool[theindex] del idx_pool[theindex] cur_actions = to_categorical(np.asarray(actions), num_classes=k) agent.train_policy(args.k, states, cur_actions) del sent_pool del idx_pool del sent_trn del idx_trn gc.collect() return agent
else: train_pool.append(allsents[indices[i]]) train_pool_idx.append(allidx[indices[i]]) #Initialise the model if os.path.exists(tagger_output): os.remove(tagger_output) model = CRFTagger(tagger_output, num_classes=num_classes) if args.initial_training_size > 0: model.train(train_la) for t in range(0, BUDGET): if (t % 10) == 0: logger.info(' * Episode: {} Budget so far: {}'.format( str(tau + 1), str(t + 1))) #Random get k sample from train_pool and train_pool_idx random_pool, random_pool_idx, queryindices = utilities.randomKSamples( train_pool, train_pool_idx, k) row = 0 f1 = -1 bestindex = 0 newseq = [] newidx = [] coin = np.random.rand(1) # beta=0.5 fixed #coin=max(0.5,1-0.01*tau) #beta=max(0.5,1-0.01*tau) linear decay # coin=0.9**tau #beta= 0.9**tau exponential decay #coin=5/(5+np.exp(tau/5)) #beta=5/(5+exp(tau/5)) inverse sigmoid decay for datapoint in zip(random_pool, random_pool_idx): seq = datapoint[0] idx = datapoint[1] train_la_temp = [] train_la_temp = list(train_la)
queryscores=list(itertools.chain.from_iterable(allscores.tolist())) #print(tempscores) theindex=queryscores.index(max(queryscores)) print(theindex) train_la.append(train_pool[theindex]) train_la_idx.append(train_pool_idx[theindex]) model.train(train_la) #delete the selected data point from the pool del train_pool[theindex] del train_pool_idx[theindex] ''' #Random get k sample from train_pool and train_pool_idx if args.al_candidate_selection_mode == 'random': logger.info(" * Random candidate selections") random_pool, random_pool_idx, queryindices = utilities.randomKSamples( train_pool, train_pool_idx, args.k_learning) elif args.al_candidate_selection_mode == 'uncertainty': logger.info(" * Uncertainty candidate selections") random_pool, random_pool_idx, queryindices = utilities.sample_from_top_n_uncertainty( train_pool, train_pool_idx, model, args.n_learning, args.k_learning) else: logger.info(" * Unknown mode. Use Random candidate selections") random_pool, random_pool_idx, queryindices = utilities.randomKSamples( train_pool, train_pool_idx, args.k_learning) #get the state and action state = utilities.getAllState(train_la_idx, random_pool, random_pool_idx, model, w2v, max_len, num_classes) action = agent.predict(args.k_learning, state) # logger.info(action)