如何在Keras中定义DQN模型的输出层形状

时间:2019-07-23 16:16:17

标签: python keras deep-learning reinforcement-learning q-learning

我正在尝试学习DQN代理,以使用Keras玩井字游戏。问题是我的输出形状与预期不同。

详细信息: 输入形状:(BOARD_SIZE ^ 2) * 3->它是一个热编码游戏板 输出形状:我希望输出的列表大小为(BOARD_SIZE^2),因为它应该包含可用操作的数量

问题: 输出具有输入层[(BOARD_SIZE ^ 2) *3] * Number of actions (BOARD_SIZE^2)

的形状

我试图寻找解决方案,但是Keras文档非常差。请帮助

这是我的模型

    def create_model(self, game: GameController) -> Sequential:
    input_size = (game.shape ** 2) * 3

    model = Sequential()
    model.add(Dense(input_size, input_dim=1, activation='relu'))
    model.add(Dense(int(input_size / 2), activation='relu'))
    model.add(Dense(int(input_size / 2), activation='relu'))
    model.add(Dense((game.shape ** 2), activation='linear'))
    model.compile(loss="mean_squared_error", optimizer=Adam(self.alpha))

    return model

这就是我要输出的方式

q_values = self.model.predict(processed_input)

这是预处理程序(一种热编码)

def preprocess_input(self, game: GameController) -> list:
    encoded_x = copy.deepcopy(game.board)
    encoded_o = copy.deepcopy(game.board)
    encoded_blank = copy.deepcopy(game.board)

    for row in range(game.shape):
        for col in range(game.shape):
            if encoded_x[row][col] == 'X':
                encoded_x[row][col] = 1
            else:
                encoded_x[row][col] = 0

            if encoded_o[row][col] == 'O':
                encoded_o[row][col] = 1
            else:
                encoded_o[row][col] = 0

            if encoded_blank[row][col] == '-':
                encoded_blank[row][col] = 1
            else:
                encoded_blank[row][col] = 0

    chained_x = list(chain.from_iterable(encoded_x))
    chained_o = list(chain.from_iterable(encoded_o))
    chained_blank = list(chain.from_iterable(encoded_blank))

    string_board = list(chain(chained_x, chained_o, chained_blank))
    board_to_int = [int(element) for element in string_board]

    return board_to_int

1 个答案:

答案 0 :(得分:0)

好吧,经过几次尝试后,我发现我的输入已经被移调了,所以我将input_dim设置为((BOARD_SIZE ^ 2)* 3),并将input_board重塑为(1,(BOARD_SIZE ^ 2)* 3)已解决的问题。希望将来能对其他人有所帮助:)