コード例 #1
0
ファイル: engine.py プロジェクト: piulin/nlu_predictor
def train(args):
    """
    Train the CNN.
    :param args:
    """

    # get our working device
    device = get_device(args)

    # read training samples from disk, and prepare the dataset (i.e., shuffle, scaling, and moving the data to tensors.)
    dts = dataset(device)

    dts.read_training_dataset(args['train_data'])

    train_dts, test_dts = dts, dts

    if args['D'] != None:
        train_dts, test_dts = dts.train_test_splits(args['D'], td=False)

    classifier = seq2seq(dts.words_converter.no_entries(),
                         dts.slots_converter.no_entries(),
                         dts.intent_converter.no_entries(), device,
                         train_dts.words_converter.T2id('<PAD>'),
                         train_dts.slots_converter.T2id('<PAD>'), args)

    if args['E'] != None:
        embeddings = gensim.models.KeyedVectors.load_word2vec_format(
            args['E'][0], binary=True)
        classifier.pretrained_embeddings(train_dts, embeddings)

    try:
        classifier.fit(train_dts,
                       test_dts,
                       args['e'],
                       args['b'],
                       args['lr'],
                       args['save_every'],
                       args['o'],
                       do_predict=not args['no_training_predictions'])
    except KeyboardInterrupt:
        pass

    intent_true, intent_pred, slots_true, slots_pred = classifier.predict_and_get_labels_batch(
        test_dts, args['b'])
    assess(dts, intent_true, intent_pred, slots_true, slots_pred)

    if args['o'] != None:

        # write it into a file.
        classifier.dump(args['o'])
コード例 #2
0
def add_level(node, play, level):
    if level <= 0: return

    gameState = node.contents[0]

    all_play_moves = {
        'w': white_moves.all_white_moves,
        'b': black_moves.all_black_moves
    }

    positions, moves = all_play_moves[play](gameState)

    play_moves = {'w': white_moves.move_white, 'b': black_moves.move_black}

    if play == 'w': next_play = 'b'
    elif play == 'b': next_play = 'w'

    for ind in range(len(positions)):
        for piece_move in moves[ind]:

            new_state = gameState.copy()

            play_moves[play](new_state, positions[ind], piece_move)

            new_node = node.addNode([new_state, positions[ind], piece_move],
                                    assessment.assess(new_state))

            add_level(new_node, next_play, level - 1)

            # Prune the node
            if play == 'w':
                node.pruneSetMax()
            elif play == 'b':
                node.pruneSetMin()
コード例 #3
0
def move(gameState, play, depth=1):
    trunk = tree.Node(0, [gameState, 0, 0], assessment.assess(gameState))
    start_play = play
    add_level(trunk, play, depth)
    if play == 'w':
        trunk.pruneSetMax()
    if play == 'b':
        trunk.pruneSetMin()
    return trunk.nodes[0].contents
コード例 #4
0
    def fit(self, train, dev, epochs, batch_size, learning_rate, save_every, save_model_path, print_every=50, do_predict=True):

        for i in range(epochs):
            print(f"******Epoch: {i}********")
            self.trainIters(train, batch_size, learning_rate=learning_rate, print_every=print_every, no_epochs=epochs)

            if do_predict:
                print('  predicting:')
                intent_true, intent_pred, slots_true, slots_pred = self.predict_and_get_labels_batch(dev, batch_size)
                intent_assessment, slots_assessment = assess(dev, intent_true, intent_pred, slots_true, slots_pred,
                                                             plot=False)
            if save_every != None and i % save_every  == 0:
                self.dump(save_model_path + '.epoch' + str(i))
コード例 #5
0
state = state.State()
print(state)

for itt in range(80):
    # White move
    positions, moves = white_moves.all_white_moves(state)
    ind = random.randrange(len(positions))
    position = positions[ind]
    dst = random.choice(moves[ind])
    white_moves.move_white(state, position, dst)
    print(
        str(itt) + ": White moves " + convert_position.coord2basic(position) +
        " --> " + convert_position.coord2basic(dst))
    print(state)
    print("Assessment: " + str(assessment.assess(state)))
    if state.isEnd(): break
    print()

    # Black move
    positions, moves = black_moves.all_black_moves(state)
    ind = random.randrange(len(positions))
    position = positions[ind]
    dst = random.choice(moves[ind])
    black_moves.move_black(state, position, dst)
    print(
        str(itt) + ": Black moves " + convert_position.coord2basic(position) +
        " --> " + convert_position.coord2basic(dst))
    print(state)
    print("Assessment: " + str(assessment.assess(state)))
    if state.isEnd(): break