Ejemplo n.º 1
0
    def pick_model_move(self, color):
        # If asked to make a move for enemy player, switch perspective as if we are them
        if channel_from_color(color) == gvg.player_channel:
            self.board = go_board.switch_player_perspec(self.board)

        enc_board = go_board.get_encoded_board(self.board)
        pred = np.asarray(self.model.predict(enc_board. \
        reshape(1, gvg.board_size, gvg.board_size, gvg.enc_board_channels))). \
        reshape(gvg.board_size * gvg.board_size)
        prob_board = np.array(
            self.model.predict(
                go_board.get_encoded_board(
                    self.board.reshape(
                        gvg.board_size, gvg.board_size,
                        gvg.board_channels)).reshape(1, 19, 19, 20))).reshape(
                            (gvg.board_size, gvg.board_size))
        self.last_move_probs = prob_board

        move = go_learn.predict_move(self.board,
                                     self.model,
                                     prob_board=prob_board)

        return Move(move[0], move[1])
Ejemplo n.º 2
0
def predict_move(orig_board, model, level=0, prob_board=None):
    if prob_board is None:
        prob_board = np.array(
            model.predict(go_board.get_encoded_board(orig_board))).reshape(
                gvg.board_size, gvg.board_size)

    found_move = False
    while found_move == False:
        move = nanargmax(prob_board)
        if go_board.make_move(orig_board,
                              move,
                              gvg.bot_channel,
                              gvg.player_channel,
                              result_only=True) == False:
            prob_board[move[0]][move[1]] = -999999.0
        else:
            found_move = True

    return move
Ejemplo n.º 3
0
def train_network(game_data, model):
    vs = gvg.validation_split  # What fraction of games are reserved for validation

    hm_batches = int(len(game_data) / batch_size)

    train_boards = []
    train_next_moves = []
    for game_index in range(len(game_data)):
        board = go_board.setup_board(game_data[game_index])
        for node in game_data[game_index].get_main_sequence():
            board = go_board.switch_player_perspec(
                board
            )  # Changes player perspective, black becomes white and vice versa

            node_move = node.get_move()[1]
            if node_move is not None:
                train_boards.append(go_board.get_encoded_board(board))

                next_move = np.zeros(gvg.board_size * gvg.board_size).reshape(
                    gvg.board_size, gvg.board_size)
                next_move[node_move[0], node_move[
                    1]] = gvg.filled  # y = an array in the form [board_x_position, board_y_position]
                train_next_moves.append(
                    next_move.reshape(gvg.board_size * gvg.board_size))

                board = go_board.make_move(
                    board, node_move, gvg.bot_channel,
                    gvg.player_channel)  # Update board with new move
                if board is None:
                    print("ERROR! Illegal move, {}, while training".format(
                        node_move[1]))
            if len(train_boards
                   ) >= batch_size:  # Send chunk to GPU at batch limit
                model.fit(
                    {"input": train_boards[:int(-len(train_boards) * vs)]}, {
                        "target":
                        train_next_moves[:int(-len(train_boards) * vs)]
                    },
                    validation_set=({
                        "input":
                        train_boards[int(-len(train_boards) * vs):]
                    }, {
                        "target":
                        train_next_moves[int(-len(train_boards) * vs):]
                    }),
                    n_epoch=1,
                    batch_size=gvg.train_batch_size,
                    snapshot_step=7500,
                    show_metric=True)
                train_boards = []
                train_next_moves = []

    # Finish of what is remaining in the batch
    model.fit({"input": train_boards[:int(-len(train_boards) * vs)]},
              {"target": train_next_moves[:int(-len(train_boards) * vs)]},
              validation_set=({
                  "input":
                  train_boards[int(-len(train_boards) * vs):]
              }, {
                  "target":
                  train_next_moves[int(-len(train_boards) * vs):]
              }),
              n_epoch=1,
              batch_size=gvg.train_batch_size,
              snapshot_step=7500,
              show_metric=True)
Ejemplo n.º 4
0
    print("Done loading {} games".format(len(games)))

    print("Being data processing...")
    train_boards = []
    train_next_moves = []
    for game_index in range(len(games)):
        board = go_board.setup_board(games[game_index])
        for node in games[game_index].get_main_sequence():
            board = go_board.switch_player_perspec(
                board
            )  # Changes player perspective, black becomes white and vice versa

            node_move = node.get_move()[1]
            if node_move is not None:
                train_boards.append(go_board.get_encoded_board(board))
                next_move = np.zeros(gvg.board_size * gvg.board_size).reshape(
                    gvg.board_size, gvg.board_size)
                next_move[node_move[0], node_move[
                    1]] = gvg.filled  # y = an array in the form [board_x_position, board_y_position]
                train_next_moves.append(
                    next_move.reshape(gvg.board_size * gvg.board_size))

                board = go_board.make_move(
                    board, node_move, gvg.bot_channel,
                    gvg.player_channel)  # Update board with new move
                if board is None:
                    print("ERROR! Illegal move, {}, while training".format(
                        node_move))
    print("Finished data processing...")