Ryosuke Satoh
Ryosuke Satoh

Reputation: 11

TensorFlow "Received a mix of batched and unbatched Tensors, or Tensors are not compatible with Specs"

I'm trying to implement DQN on a custom environment with TensorFlow.
I've finished implementing the environment class and integrate it as a part of the DQN environment.
However, I got an error,

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]))

I found the same error in the issues of Tensorflow, but I couldn't find the exact solution.
In the environment class I defined, I specified the action spec and observation spec as,

    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')

where nq is an integer value. I don't know why the TensorShapes look like those.

Thank you.

Upvotes: 1

Views: 1258

Answers (2)

DP_
DP_

Reputation: 87

I got the same problem, and what I was doing wrong was that in my custom environment I was not returning the state with the correct shape, I was inserting it into an array

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

instead of

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

This was the reason why the tensor shapes weren't adding up. I don't know if it would solve your problem, but it might help someone else

Upvotes: 1

pax77
pax77

Reputation: 25

I run into the same error. I don’t know exactly what solved it but one problem was with the notebook (ipynb) – you have to wait some time for the chapters to compute and maybe click again after some time, maybe try your code in a py-file without notebook. Another thing I changed is the definition of the observation space in my gym-env from spaces.Discrete to spaces.Box, e.g.:

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

Upvotes: 0

Related Questions