为什么在Spyder上可视化我的训练崩溃ipython

时间:2019-12-27 12:03:13

标签: python matplotlib tkinter keras q-learning

我正在尝试制作一个在网上找到的强化学习迷宫示例。当可视化我的预训练模型时,一切正常,但是当我在训练python崩溃时尝试可视化游戏时。我正在用pyplot制作GUI。
这是适用于经过训练的模型的功能:


def play_game(model, qmaze, rat_cell, visualize = False):
    qmaze.reset(rat_cell)
    envstate = qmaze.observe()
    while True:
        prev_envstate = envstate
        # get next action
        q = model.predict(prev_envstate)
        action = np.argmax(q[0])

        # apply action, get rewards and new state
        envstate, reward, game_status = qmaze.act(action)
        if visualize:
            show(qmaze)
        if game_status == 'win':
            return True
        elif game_status == 'lose':
            return False

这是我在训练时用于可视化的功能:

 # Apply action, get reward and new envstate
            envstate, reward, game_status = qmaze.act(action)
            if visualize:
                show(qmaze)
            if game_status == 'win':
                win_history.append(1)
                game_over = True
            elif game_status == 'lose':
                win_history.append(0)
                game_over = True
            else:
                game_over = False

我使用的show函数如下:

oldcanvas = None
def show(qmaze):
    global oldcanvas
    nrows, ncols = qmaze.maze.shape
    canvas = np.copy(qmaze.maze)
    for row,col in qmaze.visited:
        canvas[row,col] = 0.6
    rat_row, rat_col, _ = qmaze.state
    canvas[rat_row, rat_col] = 0.3   # rat cell
    canvas[nrows-1, ncols-1] = 0.9 # cheese cell
    if not np.array_equal(oldcanvas, canvas):
        plt.clf()
        plt.grid('on')
        ax = plt.gca()
        ax.set_xticks(np.arange(0.5, nrows, 1))
        ax.set_yticks(np.arange(0.5, ncols, 1))
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        img = plt.imshow(canvas, interpolation='none', cmap='gray')
        oldcanvas = np.copy(canvas)
        plt.draw()
        plt.pause(0.001)
        time.sleep(0.001)
        return img

我没有使show功能正常运行,还是pyplot有问题?

0 个答案:

没有答案