tensorflow / agents

TF-Agents: A reliable, scalable and easy to use TensorFlow library for Contextual Bandits and Reinforcement Learning.
Apache License 2.0
2.78k stars 719 forks source link

Action Spec for custom environment returning AttributeError #86

Closed jmribeiro closed 5 years ago

jmribeiro commented 5 years ago

I've created a custom environment for the Pursuit Domain, based on the colabs environment tutorial, validated it using utils.validate_py_environment(environment, episodes=5), however when testing it with a QNetworkAgent, I get the following AttributeError:

AttributeError: 'tuple' object has no attribute 'ndims' from agents/dqn/dqn_agent.pydqn_agent.py, line 159:

# TODO(oars): Get DQN working with more than one dim in the actions.
if len(flat_action_spec) > 1 or flat_action_spec[0].shape.ndims > 1:
        raise ValueError('Only one dimensional actions are supported now.')

Here is my environment's constructor and getters:

 def __init__(self, team="greedy", world_size=(5, 5)):

        super().__init__()

        self.teammates = self._spawn_team(team)
        self.total_agents = len(self.teammates) + 1
        self.world_size = world_size

        self._action_spec = BoundedArraySpec(shape=(1,),
                                             dtype=np.int32,
                                             minimum=0, maximum=3,
                                             name='action')
        self._observation_spec = BoundedArraySpec(shape=(2 * self.total_agents,),
                                                  dtype=np.int32,
                                                  name='observation')
        self.visualizers = ()
        self.initialized_visualizers = self.visualizers != ()

def action_spec(self):
        return self._action_spec

def observation_spec(self):
        return self._observation_spec

The CardGameEnv also has the same BoundedArraySpec with shape set to a tuple (also not working with the DQN). What am I missing?

oars commented 5 years ago

When training the agent, are you converting the environment into a tfenv?

tf_env = tf_py_environment.TFPyEnvironment(YourEnvironment())

jmribeiro commented 5 years ago

Now I am! Working properly. Thank you very much!