Esempio n. 1
0
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
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 4
0
         os.remove(tagger_temp)
     model_temp = CRFTagger(tagger_temp, num_classes=num_classes)
     model_temp.train(train_la_temp)
     f1_temp = model_temp.test(train_val, label2str)
     if (f1_temp > f1):
         bestindex = row
         f1 = f1_temp
         newseq = seq
         newidx = idx
     row = row + 1
     del model_temp
     del train_la_temp
     gc.collect()
 #get the state and action
 state = utilities.getAllState(train_la_idx, random_pool,
                               random_pool_idx, model, w2v, max_len,
                               num_classes)
 #action=bestindex
 if (coin > 0.5):
     action = bestindex
     #tempstates= np.ndarray((1,K,len(state[0])), buffer=np.array(state))
 else:
     # sent_content= np.expand_dims(state[0], axis=0)
     # marginal_prob = np.expand_dims(state[1], axis=0)
     # confidence_score = np.expand_dims(state[2], axis=0)
     # labeled_data = np.expand_dims(state[3], axis=0)
     action = agent.predict_action(k, state[0], state[1], state[2],
                                   state[3], state[4], state[5])
 states.append(state)
 actions.append(action)
 #update the model
Esempio n. 5
0
            train_pool_idx.append(train_idx[indices[i]])
    #Initialise the model
    model = CRFTagger('esp.tagger')
    model.train(train_la)

    coin = np.random.rand(1)
    states = []
    actions = []
    for t in range(0, BUDGET):
        print('Episode: ' + str(tau + 1) + ' Budget: ' + 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)

        #get the state and action
        state = utilities.getAllState(random_pool, random_pool_idx, model, w2v,
                                      200)
        tempstates = np.expand_dims(state, axis=0)
        action = policy.predict_classes(tempstates, verbose=0)[0]

        theindex = queryindices[action]
        #states.append(state)
        #actions.append(action)
        data_new = train_pool[theindex]
        data_new_idx = train_pool_idx[theindex]
        unscore = utilities.getCRFunRank(train_pool, model, data_new)
        discore = utilities.getCRFdiRand(train_la_idx, train_pool_idx, model,
                                         data_new_idx)
        unranklist.append(unscore)
        diranklist.append(discore)
        print("Uncertainty rank:" + str(unscore))
        print("Diversity rank:" + str(discore))