Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Only scalar actions are supported now!! #665

Open
shamim237 opened this issue Sep 30, 2021 · 7 comments
Open

ValueError: Only scalar actions are supported now!! #665

shamim237 opened this issue Sep 30, 2021 · 7 comments

Comments

@shamim237
Copy link

I am getting this error while trying to create a custom environment and feed it to the DQN agent.

ValueError: Only scalar actions are supported now, but action spec is: BoundedTensorSpec(shape=(1,), dtype=tf.float32, name='action', minimum=array(0., dtype=float32), maximum=array(1., dtype=float32))
In call to configurable 'DqnAgent' (<class 'tf_agents.agents.dqn.dqn_agent.DqnAgent'>)

Here is my action_spec:

self._action_spec = array_spec.BoundedArraySpec(shape = (1,), dtype = np.float32, minimum = 0, maximum = 1, name = 'action')

@sguada
Copy link
Member

sguada commented Oct 7, 2021

Change your action spec to
array_spec.BoundedArraySpec(shape = (), dtype = np.float32, minimum = 0, maximum = 1, name = 'action')

@ayarmak
Copy link

ayarmak commented Oct 11, 2021

I'm getting a similar error, but I cannot apply the solution by @sguada above, because I need my action to be a tuple of two integers.

My action_spec:

self._action_spec = array_spec.BoundedArraySpec(
        shape=(2,),
        dtype=np.int32, minimum=0, maximum=4, name='action')

The error I'm getting:

ValueError: Only scalar actions are supported now, but action spec is: BoundedTensorSpec(shape=(2,), dtype=tf.int32, name='action', minimum=array(0, dtype=int32), maximum=array(4, dtype=int32))
  In call to configurable 'DqnAgent' (<class 'tf_agents.agents.dqn.dqn_agent.DqnAgent'>)

Any advice on what I might be doing wrong?

@sguada
Copy link
Member

sguada commented Oct 11, 2021

As the error message shows, only scalar actions are supported for DQN, you can however transform your environment to have 1 action which represents the cross-product of all your actions.

@sguada
Copy link
Member

sguada commented Oct 11, 2021 via email

@ayarmak
Copy link

ayarmak commented Oct 11, 2021

Thanks a lot - this is super helpful @sguada !!

@jacklu333333
Copy link

Hi, @sguada
I have the same error, but I am still a little bit confused. I try the automatic method with FlattenActionWrapper. My understanding is that FlattenActionWrapper will automatically flatten the action, and then I have to convert it to TFPyEnvironment so that array_spec will become tensor_spec.

However, when I tried the following code

train_py_env = myenv()

train_env = tf_py_environment.TFPyEnvironment(
    wrappers.FlattenActionWrapper(
        train_py_env,
        np.uint8
    ), 
    isolation=True
)  

the following error pop up

only size-1 arrays can be converted to Python scalars
It seems the FlattenActionWrapper is ignored by TFPyEnvironment somehow. I couldn't find any other example online. I hope you can point out where did I do wrong.

Thanks in advance.

Best Regards,
Jack Lu

@KumarRupesh
Copy link

Hi, my action need to be a tuple of three integers. I am using a DQN agent and a custom PyEnvironment environment. I am also getting same error as Jack has shown above when I use FlattenActionWrapper. Please let me know if some one was able to resolve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants