Example #1
0
def play_ner(replay, saved_robot, saved_initdata, nextepisode):
    actions = 2
    global AGENT
    if AGENT == "random":
        robot = RobotRandom(actions)
    elif AGENT == "DQN":
        robot = RobotDQN(actions)
    elif AGENT == "CNNDQN":
        robot = RobotCNNDQN(actions)
    else:
        print "** There is no robot."
        raise SystemExit

    if replay:
       robot.restore(saved_robot)

    global TRAIN_LANG, TRAIN_LANG_NUM, BUDGET, CHECKPOINT
    robot.setCheckPoint(CHECKPOINT)
    for i in range(TRAIN_LANG_NUM):
        train = TRAIN_LANG[i][0]
        test = TRAIN_LANG[i][1]
        dev = TRAIN_LANG[i][2]
        emb = TRAIN_LANG[i][3]
        tagger = TRAIN_LANG[i][4]
        # initilise a NER game
        game = initialise_game(train, test, dev, emb, BUDGET, True, replay, saved_initdata)
        # initialise a decision robot
        robot.update_embeddings(game.w2v)
        # tagger
        model = CRFTagger(tagger)
        model.clean()

        # play game
        episode = 1
        if replay:
           episode = nextepisode

        print(">>>>>> Playing game ..")
        while episode <= MAX_EPISODE:
            #copy the baseline model to the saved model
            #print tagger      
            #shutil.copy('eng.model.baseline', tagger) 

            print '>>>>>>> Current game round ', episode, 'Maximum ', MAX_EPISODE
            observation = game.get_frame(model)
            action = robot.get_action(observation)
            print '> Action', action
            reward, observation2, terminal = game.feedback(action, model, True)
            print '> Reward', reward
            robot.update(observation, action, reward, observation2, terminal)

            
            if terminal == True:
                episode += 1
                print '> Terminal <'
    return robot
Example #2
0
def test(robot):
    global TEST_LANG, TEST_LANG_NUM, BUDGET
    for i in range(TEST_LANG_NUM):
        train = TEST_LANG[i][0]
        test = TEST_LANG[i][1]
        dev = TEST_LANG[i][2]
        emb = TEST_LANG[i][3]
        tagger = TEST_LANG[i][4]
        game2 = initialise_game(train, test, dev, emb, BUDGET, False, False, '')
        robot.update_embeddings(game2.w2v)
        model = CRFTagger(tagger)
        model.clean()
        test_agent_batchNew(robot, game2, model, 1000, True)