TensorFlow“收到了成批和未成批张量的混合,或张量与规范不兼容”

2024-05-14 08:52:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用TensorFlow在自定义环境中实现DQN。
我已经完成了环境类的实现,并将其集成为DQN环境的一部分。
但是我有一个错误

Received a mix of batched and unbatched Tensors, or Tensors are not compatible with Specs. num_outer_dims: 1. Saw tensor_shapes: TimeStep(step_type=TensorShape([1]), reward=TensorShape([1]), discount=TensorShape([1]), observation=TensorShape([1, 1, 6]))
And spec_shapes: TimeStep(step_type=TensorShape([]), reward=TensorShape([]), discount=TensorShape([]), observation=TensorShape([6]))

我在Tensorflow的问题中发现了同样的错误,但我找不到确切的解决方案。
在我定义的环境类中,我将操作规范和观察规范指定为

    self._action_spec = array_spec.BoundedArraySpec(
        shape=(), dtype=np.int32, minimum=0, maximum=nq, name='action')
    self._observation_spec = array_spec.BoundedArraySpec(
        shape=(nq,), dtype=np.int32, minimum=0, maximum=1, name='observation')

其中,nq是一个整数值。我不知道为什么张量形状看起来像这样

多谢各位


Tags: 规范环境typestep错误dqndiscountspec
2条回答

我遇到了同样的问题,我做错的是,在我的自定义环境中,我没有返回具有正确形状的状态,而是将其插入到数组中

return np.array([self.state], dtype=np.float64)

而不是

return np.array(self.state, dtype=np.float64)

这就是为什么张量形状不能相加的原因。我不知道它是否能解决你的问题,但它可能会帮助其他人

我遇到了同样的错误。我不知道到底是什么解决了这个问题,但笔记本电脑(ipynb)有一个问题——你必须等待一段时间,等待章节计算,也许过了一段时间再点击,也许在没有笔记本的py文件中尝试你的代码。 我改变的另一件事是我的健身房环境中观察空间的定义,从spaces.Discrete到spaces.Box,例如:

self.observation_space = spaces.Box(low=0, high=2000, shape=(20, 20), dtype=np.int32)

相关问题 更多 >

    热门问题