【问题标题】:Something wrong with Keras code Q-learning OpenAI gym FrozenLakeKeras 代码 Q-learning OpenAI 健身房 FrozenLake 出了点问题
【发布时间】:2018-02-02 19:51:37
【问题描述】:

也许我的问题看起来很愚蠢。

我正在研究 Q-learning 算法。为了更好的理解,我尝试将this FrozenLake示例的 Tenzorflow 代码改写成 Keras 代码。

我的代码:

import gym
import numpy as np
import random

from keras.layers import Dense
from keras.models import Sequential
from keras import backend as K    

import matplotlib.pyplot as plt
%matplotlib inline

env = gym.make('FrozenLake-v0')

model = Sequential()
model.add(Dense(16, activation='relu', kernel_initializer='uniform', input_shape=(16,)))
model.add(Dense(4, activation='softmax', kernel_initializer='uniform'))

def custom_loss(yTrue, yPred):
    return K.sum(K.square(yTrue - yPred))

model.compile(loss=custom_loss, optimizer='sgd')

# Set learning parameters
y = .99
e = 0.1
#create lists to contain total rewards and steps per episode
jList = []
rList = []

num_episodes = 2000
for i in range(num_episodes):
    current_state = env.reset()
    rAll = 0
    d = False
    j = 0
    while j < 99:
        j+=1

        current_state_Q_values = model.predict(np.identity(16)[current_state:current_state+1], batch_size=1)
        action = np.reshape(np.argmax(current_state_Q_values), (1,))

        if np.random.rand(1) < e:
            action[0] = env.action_space.sample() #random action

        new_state, reward, d, _ = env.step(action[0])

        rAll += reward
        jList.append(j)
        rList.append(rAll)

        new_Qs = model.predict(np.identity(16)[new_state:new_state+1], batch_size=1)
        max_newQ = np.max(new_Qs)

        targetQ = current_state_Q_values
        targetQ[0,action[0]] = reward + y*max_newQ
        model.fit(np.identity(16)[current_state:current_state+1], targetQ, verbose=0, batch_size=1)
        current_state = new_state

        if d == True:
            #Reduce chance of random action as we train the model.
            e = 1./((i/50) + 10)
            break
print("Percent of succesful episodes: " + str(sum(rList)/num_episodes) + "%")

当我运行它时,它运行得不好:成功剧集的百分比:0.052%

plt.plot(rList)

original Tensorflow code 更好:成功剧集的百分比:0.352%

plt.plot(rList)

我做错了什么?

【问题讨论】:

  • 你找到解决方案了吗?
  • 一篇评论中提到的文章作者应该禁用bias(Dense层构造函数中参数bias=False),但是对我来说好像没有任何作用。很想了解为什么纯 tensorflow 有效而 keras 无效
  • 我也有同样的问题。您是否找到了解决方案、改进或一些提示?

标签: python tensorflow artificial-intelligence keras q-learning


【解决方案1】:

除了将 use_bias=False 设置为 cmets 中提到的 @Maldus 之外,您可以尝试的另一件事是从更高的 epsilon 值开始(例如 0.5、0.75)?一个技巧可能是仅在达到目标时才减小 epsilon 值。即不要在每集结束时减少 epsilon。这样你的玩家可以继续随机探索地图,直到它开始收敛到一条好的路线上,然后减少 epsilon 参数是个好主意。

我实际上在gist 中使用卷积层而不是密集层在 keras 中实现了一个类似的模型。设法让它在 2000 集以下工作。可能对其他人有一些帮助:)

【讨论】:

    猜你喜欢
    • 2017-04-04
    • 2019-02-05
    • 2019-10-16
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多