深度 Q 学习修改
Deep Q-learning modification
@编辑:
我正在尝试创建一个代理来玩俄罗斯方块游戏,使用将棋盘状态 + 当前棋子作为输入的卷积神经网络。根据我的阅读,深度 Q 学习在这方面不是很好,我刚刚证实了这一点。
@结束编辑
假设代理正在学习玩游戏的策略,其中每个游戏步骤都可以表示为
s, a, r, s', done
代表
state, action, reward, next state, game over
在深度Q学习算法中,代理处于状态s并采取一些行动a(遵循 epsilon-greedy 策略),观察到奖励 r 并进入下一个状态 s' .
代理的行为是这样的:
# returns an action index
get_action(state, epsilon)
if random() < epsilon
return random_action_index
else
return argmax(nnet.predict(state))
通过贪婪地观察状态s'中的最大Q值来更新参数,所以我们有
# action taken was 'a' in state 's' leading to 's_'
prediction = nnet.predict(s)
if done
target = reward
else
target = reward + gamma * max(nnet.predict(s_))
prediction[a] = target
[prediction, target] 被提供给一些 nnet 以进行权重更新。所以这个nnet得到一个状态s作为输入,并输出一个维度为n_actions的q值向量。这些我都清楚了。
现在,假设我的状态动作太吵了,这种方法根本行不通。因此,我的 nnet 输出不是输出维度 n_actions 的向量,而是单个值,表示“状态质量”(该状态的理想程度)。
现在我的代理人是这样的:
# returns an action based on how good the next state is
get_action(state, epsilon):
actions = []
for each action possible in state:
game.deepcopy().apply(action)
val = nnet.predict(game.get_state())
action.value = val
actions.append(action)
if random() < epsilon
return randomChoice(actions)
else
return action with_max_value from actions
而我的[预测,目标]是这样的:
# action taken was 'a' in state 's' leading to 's_'
prediction = nnet.predict(s)
if done
target = reward
else
target = reward + gamma * nnet.predict(s_)
我对第二种算法有一些疑问:
a) Does it makes sense to act non greedily sometimes?
直觉上不会,因为如果我以糟糕的状态着陆,那可能是因为随机动作不好,而不是因为之前的状态是 'bad'。 Q-learning 更新会调整不好的动作,但是这第二种算法会错误的调整之前的状态。
b) What kind of algorithm is this? Where does it fits in Reinforcement Learning?
c) In the case of Tetris, the state almost never repeats, so what can I do in this case? Is that the reason deep q-learning fails here?
这可能看起来令人困惑,但该算法确实有效。如果需要我可以提供额外的细节,谢谢!
Now, suppose that my state-actions are so noise, that this approach will simply not work. So, instead of outputting a vector of dimension n_actions, my nnet output is a single value, representing the "state-quality" (how desirable is that state).
Now my agent acts like this:
# returns an action based on how good the next state is
get_action(state, epsilon):
actions = []
for each action possible in state:
game.apply(action)
val = nnet.predict(game.get_state())
action.value = val
actions.append(action)
if random() < epsilon
return randomChoice(actions)
else
return action with_max_value from actions
首先简要说明该伪代码:我认为这行不通,因为您没有模拟不同动作对游戏状态 副本 的影响,但只是直接在游戏状态上。您可能希望首先创建游戏状态的单独副本,然后 运行 每个动作在不同的副本上一次。
无论如何,这种算法通常被认为在强化学习设置中是不可能的。在 RL 中,我们通常假设我们没有 "simulator" 或 "forward model" 或类似的东西。我们通常假设我们有一个位于真实环境中的代理,我们可以在其中生成可用于学习的经验。在这种假设下,我们无法实现那种 for each action possible in state
循环来模拟如果我们在同一游戏状态下执行不同的动作会发生什么。假设我们首先必须选择一个动作,执行它,然后从特定的经验轨迹中学习; 我们不能再"go back",想象我们选择了一个不同的动作,并从那个轨迹中学习。
当然,在实践中这样做往往是可以的,因为我们通常确实有一个模拟器(例如机器人模拟器,或者游戏等)。大多数 RL 研究仍然假设我们没有这样的模拟器,因为这导致算法最终可能在 "real-world" 情况下可用(例如,真实世界的物理机器人)。实现您上面描述的想法实际上意味着您更多地转向 搜索算法 (例如蒙特卡洛树搜索),而不是强化学习算法。这意味着您的方法仅限于您有可用模拟器的场景(例如,游戏)。
a) Does it makes sense to act non greedily sometimes?
即使您包括了循环遍历所有操作并模拟其所有效果的类似搜索的过程,我怀疑如果您想收敛到好的策略,您仍然需要进行某种形式的探索,所以您有时你必须不贪婪地行动。是的,看起来这会导致您的算法收敛到与 "optimal policy" 的传统解释不同的东西。如果您的 epsilon 相当低,这不是什么大问题。在实践中,它很可能倾向于学习略微 "safer" 的策略。另见 my answer to this other question。
b) What kind of algorithm is this? Where does it fits in Reinforcement Learning?
除了我上面关于这实际上如何向搜索算法领域而不是 RL 算法领域移动的讨论之外,在我看来这也是一个 on-policy 算法而不是 off-policy 算法(标准 Q-learning 是 off-policy,因为它学习贪婪策略,同时通过非贪婪策略产生经验)。我上面链接的问题的大多数答案也详细讨论了这种区别。
@编辑:
我正在尝试创建一个代理来玩俄罗斯方块游戏,使用将棋盘状态 + 当前棋子作为输入的卷积神经网络。根据我的阅读,深度 Q 学习在这方面不是很好,我刚刚证实了这一点。
@结束编辑
假设代理正在学习玩游戏的策略,其中每个游戏步骤都可以表示为
s, a, r, s', done
代表
state, action, reward, next state, game over
在深度Q学习算法中,代理处于状态s并采取一些行动a(遵循 epsilon-greedy 策略),观察到奖励 r 并进入下一个状态 s' .
代理的行为是这样的:
# returns an action index
get_action(state, epsilon)
if random() < epsilon
return random_action_index
else
return argmax(nnet.predict(state))
通过贪婪地观察状态s'中的最大Q值来更新参数,所以我们有
# action taken was 'a' in state 's' leading to 's_'
prediction = nnet.predict(s)
if done
target = reward
else
target = reward + gamma * max(nnet.predict(s_))
prediction[a] = target
[prediction, target] 被提供给一些 nnet 以进行权重更新。所以这个nnet得到一个状态s作为输入,并输出一个维度为n_actions的q值向量。这些我都清楚了。
现在,假设我的状态动作太吵了,这种方法根本行不通。因此,我的 nnet 输出不是输出维度 n_actions 的向量,而是单个值,表示“状态质量”(该状态的理想程度)。
现在我的代理人是这样的:
# returns an action based on how good the next state is
get_action(state, epsilon):
actions = []
for each action possible in state:
game.deepcopy().apply(action)
val = nnet.predict(game.get_state())
action.value = val
actions.append(action)
if random() < epsilon
return randomChoice(actions)
else
return action with_max_value from actions
而我的[预测,目标]是这样的:
# action taken was 'a' in state 's' leading to 's_'
prediction = nnet.predict(s)
if done
target = reward
else
target = reward + gamma * nnet.predict(s_)
我对第二种算法有一些疑问:
a) Does it makes sense to act non greedily sometimes?
直觉上不会,因为如果我以糟糕的状态着陆,那可能是因为随机动作不好,而不是因为之前的状态是 'bad'。 Q-learning 更新会调整不好的动作,但是这第二种算法会错误的调整之前的状态。
b) What kind of algorithm is this? Where does it fits in Reinforcement Learning?
c) In the case of Tetris, the state almost never repeats, so what can I do in this case? Is that the reason deep q-learning fails here?
这可能看起来令人困惑,但该算法确实有效。如果需要我可以提供额外的细节,谢谢!
Now, suppose that my state-actions are so noise, that this approach will simply not work. So, instead of outputting a vector of dimension n_actions, my nnet output is a single value, representing the "state-quality" (how desirable is that state).
Now my agent acts like this:
# returns an action based on how good the next state is get_action(state, epsilon): actions = [] for each action possible in state: game.apply(action) val = nnet.predict(game.get_state()) action.value = val actions.append(action) if random() < epsilon return randomChoice(actions) else return action with_max_value from actions
首先简要说明该伪代码:我认为这行不通,因为您没有模拟不同动作对游戏状态 副本 的影响,但只是直接在游戏状态上。您可能希望首先创建游戏状态的单独副本,然后 运行 每个动作在不同的副本上一次。
无论如何,这种算法通常被认为在强化学习设置中是不可能的。在 RL 中,我们通常假设我们没有 "simulator" 或 "forward model" 或类似的东西。我们通常假设我们有一个位于真实环境中的代理,我们可以在其中生成可用于学习的经验。在这种假设下,我们无法实现那种 for each action possible in state
循环来模拟如果我们在同一游戏状态下执行不同的动作会发生什么。假设我们首先必须选择一个动作,执行它,然后从特定的经验轨迹中学习; 我们不能再"go back",想象我们选择了一个不同的动作,并从那个轨迹中学习。
当然,在实践中这样做往往是可以的,因为我们通常确实有一个模拟器(例如机器人模拟器,或者游戏等)。大多数 RL 研究仍然假设我们没有这样的模拟器,因为这导致算法最终可能在 "real-world" 情况下可用(例如,真实世界的物理机器人)。实现您上面描述的想法实际上意味着您更多地转向 搜索算法 (例如蒙特卡洛树搜索),而不是强化学习算法。这意味着您的方法仅限于您有可用模拟器的场景(例如,游戏)。
a) Does it makes sense to act non greedily sometimes?
即使您包括了循环遍历所有操作并模拟其所有效果的类似搜索的过程,我怀疑如果您想收敛到好的策略,您仍然需要进行某种形式的探索,所以您有时你必须不贪婪地行动。是的,看起来这会导致您的算法收敛到与 "optimal policy" 的传统解释不同的东西。如果您的 epsilon 相当低,这不是什么大问题。在实践中,它很可能倾向于学习略微 "safer" 的策略。另见 my answer to this other question。
b) What kind of algorithm is this? Where does it fits in Reinforcement Learning?
除了我上面关于这实际上如何向搜索算法领域而不是 RL 算法领域移动的讨论之外,在我看来这也是一个 on-policy 算法而不是 off-policy 算法(标准 Q-learning 是 off-policy,因为它学习贪婪策略,同时通过非贪婪策略产生经验)。我上面链接的问题的大多数答案也详细讨论了这种区别。