OpenAI 健身房的月球着陆器模型未收敛

Posted

技术标签:

【中文标题】OpenAI 健身房的月球着陆器模型未收敛【英文标题】:Model for OpenAI gym's Lunar Lander not converging 【发布时间】:2018-12-27 18:30:17 【问题描述】:

我正在尝试使用带有 keras 的深度强化学习来训练代理学习如何玩Lunar Lander OpenAI gym environment。问题是我的模型没有收敛。这是我的代码:

import numpy as np
import gym

from keras.models import Sequential
from keras.layers import Dense
from keras import optimizers

def get_random_action(epsilon):
    return np.random.rand(1) < epsilon

def get_reward_prediction(q, a):
    qs_a = np.concatenate((q, table[a]), axis=0)
    x = np.zeros(shape=(1, environment_parameters + num_of_possible_actions))
    x[0] = qs_a
    guess = model.predict(x[0].reshape(1, x.shape[1]))
    r = guess[0][0]
    return r

results = []
epsilon = 0.05
alpha = 0.003
gamma = 0.3
environment_parameters = 8
num_of_possible_actions = 4
obs = 15
mem_max = 100000
epochs = 3
total_episodes = 15000

possible_actions = np.arange(0, num_of_possible_actions)
table = np.zeros((num_of_possible_actions, num_of_possible_actions))
table[np.arange(num_of_possible_actions), possible_actions] = 1

env = gym.make('LunarLander-v2')
env.reset()

i_x = np.random.random((5, environment_parameters + num_of_possible_actions))
i_y = np.random.random((5, 1))

model = Sequential()
model.add(Dense(512, activation='relu', input_dim=i_x.shape[1]))
model.add(Dense(i_y.shape[1]))

opt = optimizers.adam(lr=alpha)

model.compile(loss='mse', optimizer=opt, metrics=['accuracy'])

total_steps = 0
i_x = np.zeros(shape=(1, environment_parameters + num_of_possible_actions))
i_y = np.zeros(shape=(1, 1))

mem_x = np.zeros(shape=(1, environment_parameters + num_of_possible_actions))
mem_y = np.zeros(shape=(1, 1))
max_steps = 40000

for episode in range(total_episodes):
    g_x = np.zeros(shape=(1, environment_parameters + num_of_possible_actions))
    g_y = np.zeros(shape=(1, 1))
    q_t = env.reset()
    episode_reward = 0

    for step_number in range(max_steps):
        if episode < obs:
            a = env.action_space.sample()
        else:
            if get_random_action(epsilon, total_episodes, episode):
                a = env.action_space.sample()
            else:
                actions = np.zeros(shape=num_of_possible_actions)

                for i in range(4):
                    actions[i] = get_reward_prediction(q_t, i)

                a = np.argmax(actions)

        # env.render()
        qa = np.concatenate((q_t, table[a]), axis=0)

        s, r, episode_complete, data = env.step(a)
        episode_reward += r

        if step_number is 0:
            g_x[0] = qa
            g_y[0] = np.array([r])
            mem_x[0] = qa
            mem_y[0] = np.array([r])

        g_x = np.vstack((g_x, qa))
        g_y = np.vstack((g_y, np.array([r])))

        if episode_complete:
            for i in range(0, g_y.shape[0]):
                if i is 0:
                    g_y[(g_y.shape[0] - 1) - i][0] = g_y[(g_y.shape[0] - 1) - i][0]
                else:
                    g_y[(g_y.shape[0] - 1) - i][0] = g_y[(g_y.shape[0] - 1) - i][0] + gamma * g_y[(g_y.shape[0] - 1) - i + 1][0]

            if mem_x.shape[0] is 1:
                mem_x = g_x
                mem_y = g_y
            else:
                mem_x = np.concatenate((mem_x, g_x), axis=0)
                mem_y = np.concatenate((mem_y, g_y), axis=0)

            if np.alen(mem_x) >= mem_max:
                for l in range(np.alen(g_x)):
                    mem_x = np.delete(mem_x, 0, axis=0)
                    mem_y = np.delete(mem_y, 0, axis=0)

        q_t = s

        if episode_complete and episode >= obs:
            if episode%10 == 0:
                model.fit(mem_x, mem_y, batch_size=32, epochs=epochs, verbose=0)

        if episode_complete:
            results.append(episode_reward)
            break

我正在运行数万集,但我的模型仍然无法收敛。它将开始减少约 5000 集以上的平均策略变化,同时增加平均奖励,但随后它会偏离深度,之后每集的平均奖励实际上会下降。我试过弄乱超参数,但我没有得到任何结果。我正在尝试在 DeepMind DQN paper 之后为我的代码建模。

【问题讨论】:

【参考方案1】:

您可能希望将 get_random_action 函数更改为随每一集衰减 epsilon。毕竟,假设您的代理可以学习最佳策略,那么在某些时候您根本不想采取随机行动,对吧?这是一个稍有不同的get_random_action 版本,可以为您执行此操作:

def get_random_action(epsilon, total_episodes, episode):
        explore_prob = epsilon - (epsilon * (episode / total_episodes))
        return np.random.rand(1) < explore_prob

在您的函数的这个修改版本中,epsilon 会随着每一集而略微减少。这可能有助于您的模型收敛。

有几种方法可以衰减参数。欲了解更多信息,请查看this Wikipedia article。

【讨论】:

嗯...我会试试这个并回复你 希望对您有所帮助!【参考方案2】:

我最近成功地实现了这一点。 https://github.com/tianchuliang/techblog/tree/master/OpenAIGym

基本上,我让代理随机运行 3000 帧,同时收集这些作为初始训练数据(状态)和标签(奖励),然后每 100 帧训练我的神经网络模型并让模型做出决定什么动作会得到最好的分数。

查看我的 github,它可能会有所帮助。哦,我的训练迭代也在 YouTube 上,https://www.youtube.com/watch?v=wrrr90Pevuwhttps://www.youtube.com/watch?v=TJzKbFAlKa0https://www.youtube.com/watch?v=y91uA_cDGGs

【讨论】:

以上是关于OpenAI 健身房的月球着陆器模型未收敛的主要内容,如果未能解决你的问题,请参考以下文章

如何在 OpenAI 中创建新的健身房环境?

python TORCS包裹在openai健身房环境中

OpenAI 健身房:如何在 CartPole-v0 中获取像素

Keras 模型:RL 代理的输入形状尺寸误差

月球宇航员怎样寻找合适的着陆点,引力势会有变化吗?

如果与健身相关,则对推文进行分类