[英]What's wrong with Dyna-Q ? (Dyna-Q vs Q-learning)
我實現了Q-learning算法,並在 OpenAI 健身房的FrozenLake-v0上使用了它。 我在 10000 集的訓練期間獲得了 185 個總獎勵,在測試期間獲得了 7333 個總獎勵。 這個好嗎?
我也嘗試了Dyna-Q算法。 但它的性能比 Q-learning 差。 大約。 訓練期間的總獎勵為 200 個,測試期間的總獎勵為 700-900 個,共 10000 集,包含 50 個計划步驟。
為什么會這樣?
下面是代碼。 代碼有問題嗎?
# Setup
env = gym.make('FrozenLake-v0')
epsilon = 0.9
lr_rate = 0.1
gamma = 0.99
planning_steps = 0
total_episodes = 10000
max_steps = 100
訓練和測試():
while t < max_steps:
action = agent.choose_action(state)
state2, reward, done, info = agent.env.step(action)
# Removed in testing
agent.learn(state, state2, reward, action)
agent.model.add(state, action, state2, reward)
agent.planning(planning_steps)
# Till here
state = state2
def add(self, state, action, state2, reward):
self.transitions[state, action] = state2
self.rewards[state, action] = reward
def sample(self, env):
state, action = 0, 0
# Random visited state
if all(np.sum(self.transitions, axis=1)) <= 0:
state = np.random.randint(env.observation_space.n)
else:
state = np.random.choice(np.where(np.sum(self.transitions, axis=1) > 0)[0])
# Random action in that state
if all(self.transitions[state]) <= 0:
action = np.random.randint(env.action_space.n)
else:
action = np.random.choice(np.where(self.transitions[state] > 0)[0])
return state, action
def step(self, state, action):
state2 = self.transitions[state, action]
reward = self.rewards[state, action]
return state2, reward
def choose_action(self, state):
if np.random.uniform(0, 1) < epsilon:
return self.env.action_space.sample()
else:
return np.argmax(self.Q[state, :])
def learn(self, state, state2, reward, action):
# predict = Q[state, action]
# Q[state, action] = Q[state, action] + lr_rate * (target - predict)
target = reward + gamma * np.max(self.Q[state2, :])
self.Q[state, action] = (1 - lr_rate) * self.Q[state, action] + lr_rate * target
def planning(self, n_steps):
# if len(self.transitions)>planning_steps:
for i in range(n_steps):
state, action = self.model.sample(self.env)
state2, reward = self.model.step(state, action)
self.learn(state, state2, reward, action)
我想這可能是因為環境是隨機的。 在隨機環境中學習 model 可能會導致次優策略。 在 Sutton & Barto 的 RLBook 中,他們說他們假設了確定性環境。
檢查在執行 model 步驟之后,是否執行下一個 state 即state2
中的計划步驟示例。
如果沒有,計划可能會從 self.env 給出的相同起始self.env
開始重復步驟。
不過我可能誤解了self.model.sample(self.env)
中self.env
參數的作用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.