I'm trying to create a DqnAgent using an own environment. But i'm receiving the following error
AttributeError: 'tuple' object has no attribute 'rank' In call to configurable 'DqnAgent' (<class 'tf_agents.agents.dqn.dqn_agent.DqnAgent'>)
Below is the code for my environment myEnv
import numpy as np
import tensorflow as tf
from tf_agents.environments import py_environment
from tf_agents.specs import array_spec
from tf_agents.utils import common
from tf_agents.networks import q_network
from tf_agents.agents.dqn import dqn_agent
class myEnv(py_environment.PyEnvironment):
def __init__(self):
self._action_spec = array_spec.BoundedArraySpec(
shape=(), dtype=np.int32, minimum=0, maximum=2, name='action')
self._observation_spec = array_spec.BoundedArraySpec(
shape=(2,), dtype=np.int32, minimum=0, name='observation')
self._state = np.zeros(shape=(2,),dtype=np.int32)
def action_spec(self):
return self._action_spec
def observation_spec(self):
return self._observation_spec
def _reset(self):
self._state = np.zeros(shape=(8,),dtype=np.int32)
self._c_reward = 0
return ts.restart(self._state)
def _step(self, action):
self._state[action] = self._state[action] + 1
return ts.transition(self._state, reward=0.0, discount=1.0)
I can create an instance of myEnv without any errors.
train_env = myEnv()
But if i try to create the agent with the following code
q_net = q_network.QNetwork(
train_env.observation_spec(),
train_env.action_spec(),
fc_layer_params=(10,))
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3)
train_step_counter = tf.Variable(0)
agent = dqn_agent.DqnAgent(
train_env.time_step_spec(),
train_env.action_spec(),
q_network=q_net,
optimizer=optimizer,
td_errors_loss_fn=common.element_wise_squared_loss,
train_step_counter=train_step_counter)
I'm receiving this Traceback
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-a730748f947f> in <module>()
15 optimizer=optimizer,
16 td_errors_loss_fn=common.element_wise_squared_loss,
---> 17 train_step_counter=train_step_counter)
4 frames
/usr/local/lib/python3.7/dist-packages/tf_agents/agents/dqn/dqn_agent.py in _check_action_spec(self, action_spec)
293
294 # TODO(oars): Get DQN working with more than one dim in the actions.
--> 295 if len(flat_action_spec) > 1 or flat_action_spec[0].shape.rank > 0:
296 raise ValueError(
297 'Only scalar actions are supported now, but action spec is: {}'
AttributeError: 'tuple' object has no attribute 'rank'
In call to configurable 'DqnAgent' (<class 'tf_agents.agents.dqn.dqn_agent.DqnAgent'>)
How to fix this error?