You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

导入GrayScaleObservation后Gym环境reset()报ValueError解包错误

问题:使用GrayScaleObservation包装器触发ValueError错误

从gym.wrappers导入GrayScaleObservation后,用它包装Super Mario环境时触发ValueError,报错提示“too many values to unpack (expected 2)”。移除GrayScaleObservation包装后一切正常,原本期望env.reset()返回形状为(240, 256, 1)的数组。

导入代码

# Import Frame Stacker Wrapper and Grayscling Wrapper
from gym.wrappers import FrameStack, GrayScaleObservation

目标代码

env = gym_super_mario_bros.make('SuperMarioBros-v0')
env = JoypadSpace(env, SIMPLE_MOVEMENT)
env = GrayScaleObservation(env, keep_dim=True) 

state = env.reset()
state.shape

错误日志

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/adhuy/code/led8/AI tools/Nicholas Renotte/Reinforcement Learning/mario_tutotial.ipynb Cell 19 in 5
      2 env = JoypadSpace(env, SIMPLE_MOVEMENT)
      3 env = GrayScaleObservation(env, keep_dim=True) 
----> 5 env.reset()

File ~/.pyenv/versions/3.10.6/envs/lewagon/lib/python3.10/site-packages/gym/core.py:379, in ObservationWrapper.reset(self, **kwargs)
    377 def reset(self, **kwargs):
    378     """Resets the environment, returning a modified observation using :meth:`self.observation`."""
---> 379     obs, info = self.env.reset(**kwargs)
    380     return self.observation(obs), info

ValueError: too many values to unpack (expected 2)

解决方案

这个问题源于Gym版本与gym-super-mario-bros环境的兼容性冲突:

  • 新版Gym(0.26+)的GrayScaleObservation包装器要求底层环境的reset()方法返回(observation, info)二元组;
  • 但旧版gym-super-mario-bros的SuperMarioBros-v0环境reset()仅返回单个observation值,导致包装器解包时出错。

修复方式二选一:

  1. 升级游戏环境适配新版Gym
    安装支持Gym 0.26+的gym-super-mario-bros版本:

    pip install gym-super-mario-bros==7.4.0
    

    该版本环境的reset()会返回标准二元组,与GrayScaleObservation兼容。

  2. 降级Gym到旧版本
    若不想升级游戏环境,可将Gym降级到0.25.x版本,此版本的ObservationWrapper不要求reset()返回info:

    pip install gym==0.25.2
    

修复后运行目标代码,state.shape即可返回预期的(240, 256, 1)。

内容的提问来源于stack exchange,提问作者led8

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 11:05:26