deep-learning - 当使用 OpenAI Gym 的跳帧包装器时,np.max 行的目的是什么?
问题描述
我正在实现以下通常在 OpenAI 的 Gym 中用于跳帧的包装器。它可以在 dqn/atari_wrappers.py 中找到
我对以下行感到非常困惑:
max_frame = np.max(np.stack(self._obs_buffer), axis=0)
我在代码中为我理解的部分添加了注释,以帮助任何可能提供帮助的人。
np.stack(self._obs_buffer)
将两个状态堆叠在_obs_buffer
.
np.max
返回沿轴 0 的最大值。
但我不明白我们为什么要这样做,或者它到底在做什么。
class MaxAndSkipEnv(gym.Wrapper):
"""Return only every 4th frame"""
def __init__(self, env=None, skip=4):
super(MaxAndSkipEnv, self).__init__(env)
# Initialise a double ended queue that can store a maximum of two states
self._obs_buffer = deque(maxlen=2)
# _skip = 4
self._skip = skip
def _step(self, action):
total_reward = 0.0
done = None
for _ in range(self._skip):
# Take a step
obs, reward, done, info = self.env.step(action)
# Append the new state to the double ended queue buffer
self._obs_buffer.append(obs)
# Update the total reward by summing the (reward obtained from the step taken) + (the current
# total reward)
total_reward += reward
# If the game ends, break the for loop
if done:
break
max_frame = np.max(np.stack(self._obs_buffer), axis=0)
return max_frame, total_reward, done, info
解决方案
在for
循环结束时,self._obs_buffer
保存最后两帧。然后将这两个帧最大池化,产生包含一些时间信息的观察。
推荐阅读
- php - 在 Linux 服务器上无法识别 DOCTYPE(仅在 IE 中)
- node.js - Gitlab webhook 系统
- elasticsearch - 在一个索引中通过 1-N 个相关数据的多个字段进行弹性搜索
- java - TimeFormat 始终来自特定时区
- python - Leetcode:提交的函数产生意外的输出
- python - 如何在python中使用空行结束程序?
- html - 如何在html中的单选按钮前面创建一个颜色框?
- python - Python:如何在“for”循环中的某些条件后修改迭代器值
- php - 如何使用 eloquent 访问 laravel 中的表与未直接附加
- regex - 正则表达式逗号分隔字符串中的子字符串