1. 程式人生 > 其它 >baselines庫中atari_wrappers.py中的環境包裝器的順序問題 baselines庫中cmd_util.py模組對atari遊戲的包裝為什麼要分成兩部分並在中間加入flatten操作呢?

baselines庫中atari_wrappers.py中的環境包裝器的順序問題 baselines庫中cmd_util.py模組對atari遊戲的包裝為什麼要分成兩部分並在中間加入flatten操作呢?

如題:

在baselines中對atari遊戲環境進行包裝的程式碼在atari_wrappers.py模組中,

 

def make_atari(env_id, max_episode_steps=None):
    env = gym.make(env_id)
    assert 'NoFrameskip' in env.spec.id
    env = NoopResetEnv(env, noop_max=30)
    env = MaxAndSkipEnv(env, skip=4)
    if max_episode_steps is not None:
        env 
= TimeLimit(env, max_episode_steps=max_episode_steps) return env def wrap_deepmind(env, episode_life=True, clip_rewards=True, frame_stack=False, scale=False): """Configure environment for DeepMind-style Atari. """ if episode_life: env = EpisodicLifeEnv(env) if 'FIRE' in
env.unwrapped.get_action_meanings(): env = FireResetEnv(env) env = WarpFrame(env) if scale: env = ScaledFloatFrame(env) if clip_rewards: env = ClipRewardEnv(env) if frame_stack: env = FrameStack(env, 4) return env

 

baselines庫中cmd_util.py模組對atari遊戲的包裝為什麼要分成兩部分並在中間加入flatten操作呢?

可以知道在make_atari函式中不對observation進行處理,wrap_deepmind函式對observation進行處理。

 

WarpFrame, ScaledFloatFrame, FrameStack, 這三個環境包裝類是對observation進行處理包裝的。

其中,WarpFrame要求observation必須是shape為(height, width, channels)的np.array。

換句話說,warp_deepmind 的observation變數應該為圖片型別的np.array 。

 

 

還有一個需要注意的是,warp_deepmind中包裝類FireResetEnv是否應該在make_atari函式中的NoopResetEnv前面,根據國外的一些相關文章所指出的,認為應該是將FireResetEnv放在NoopResetEnv前面,修改後的程式碼為:

修改後的程式碼為:

 

def make_atari(env_id, max_episode_steps=None):
    env = gym.make(env_id)
    assert 'NoFrameskip' in env.spec.id
    if 'FIRE' in env.unwrapped.get_action_meanings():
        env = FireResetEnv(env)
    env = NoopResetEnv(env, noop_max=30)
    env = MaxAndSkipEnv(env, skip=4)
    if max_episode_steps is not None:
        env = TimeLimit(env, max_episode_steps=max_episode_steps)
    return env


def wrap_deepmind(env, episode_life=True, clip_rewards=True, frame_stack=False, scale=False):
    """Configure environment for DeepMind-style Atari.
    """
    if episode_life:
        env = EpisodicLifeEnv(env)
    env = WarpFrame(env)
    if scale:
        env = ScaledFloatFrame(env)
    if clip_rewards:
        env = ClipRewardEnv(env)
    if frame_stack:
        env = FrameStack(env, 4)
    return env

 

這樣修改的邏輯是,如果一個環境需要Fire button來啟動遊戲那麼在它之前進行NoopResetEnv是沒有意義的。

 

也就是說,在有fire操作和noop操作同時存在的情況下,最好是先進行fire操作再進行noop操作,在reset過程中fire操作後再進行noop操作。

 

 

 

==================================================