I'm trying to pass in a BoundedArraySpec
as time_step_spec
tf_agents.agents.DqnAgent(
time_step_spec = tf_agents.specs.BoundedArraySpec(...
but ultimately, I get this error
AttributeError: 'BoundedArraySpec' object has no attribute 'observation'
In call to configurable 'DqnAgent' (<class 'tf_agents.agents.dqn.dqn_agent.DqnAgent'>)
All the tutorials I'm following suggest you should use bounded array specs, but the documentation isn't very clear https://www.tensorflow.org/agents/api_docs/python/tf_agents/agents/DqnAgent
Why am I getting this error and how can I fix it?
What object should I use here? I'm guessing this is the wrong object type since it's missing methods.
Full code below
import tensorflow as tf
from tf_agents.networks import q_network
from tf_agents.agents.dqn import dqn_agent
import tf_agents
import numpy as np
import tf_agents.environments.py_environment as PyEnvironment
from tf_agents.trajectories import time_step as ts
action_spec = tf_agents.specs.BoundedArraySpec(shape=(), dtype=np.int32, minimum=0, maximum=2, name='action')
observation_spec = tf_agents.specs.BoundedArraySpec(shape=(1, 9,9), dtype=np.int32, minimum=0, name='observation') #### this apparently has no method observation
### neural net layers below ###################################
def make_tut_layer(size):
return tf.keras.layers.Dense(
units= size,
activation= tf.keras.activations.relu,
kernel_initializer=tf.keras.initializers.RandomNormal(mean=0., stddev=1.)
)
def make_q_layer(num_actions):
q_values_layer = tf.keras.layers.Dense ( # last layer gives probability distribution over all actions so we can pick best action
num_actions ,
activation = tf.keras.activations.relu ,
kernel_initializer = tf.keras.initializers.RandomUniform( minval = 0.03 , maxval = 0.03),
bias_initializer = tf.keras.initializers.Constant(-0.2)
)
return q_values_layer;
print(mi_agent)
############################## stick together neural net below
normal_layers = []
for i in range(3):
normal_layers.append(make_tut_layer(81))
q_layer = make_q_layer(9)
q_net = keras.Sequential(normal_layers + [q_layer])
###################################### below is the problem
agent = dqn_agent.DqnAgent( observation_spec, action_spec, q_net, "optimizer" )
full output below
---> 36 agent = dqn_agent.DqnAgent( observation_spec, action_spec, q_net, "optimizer" )
File ~\anaconda3\lib\site-packages\gin\config.py:1605, in _make_gin_wrapper.<locals>.gin_wrapper(*args, **kwargs)
1603 scope_info = " in scope '{}'".format(scope_str) if scope_str else ''
1604 err_str = err_str.format(name, fn_or_cls, scope_info)
-> 1605 utils.augment_exception_message_and_reraise(e, err_str)
File ~\anaconda3\lib\site-packages\gin\utils.py:41, in augment_exception_message_and_reraise(exception, message)
39 proxy = ExceptionProxy()
40 ExceptionProxy.__qualname__ = type(exception).__qualname__
---> 41 raise proxy.with_traceback(exception.__traceback__) from None
File ~\anaconda3\lib\site-packages\gin\config.py:1582, in _make_gin_wrapper.<locals>.gin_wrapper(*args, **kwargs)
1579 new_kwargs.update(kwargs)
1581 try:
-> 1582 return fn(*new_args, **new_kwargs)
1583 except Exception as e: # pylint: disable=broad-except
1584 err_str = ''
File ~\anaconda3\lib\site-packages\tf_agents\agents\dqn\dqn_agent.py:235, in DqnAgent.__init__(self, time_step_spec, action_spec, q_network, optimizer, observation_and_action_constraint_splitter, epsilon_greedy, n_step_update, boltzmann_temperature, emit_log_probability, target_q_network, target_update_tau, target_update_period, td_errors_loss_fn, gamma, reward_scale_factor, gradient_clipping, debug_summaries, summarize_grads_and_vars, train_step_counter, training_data_spec, name)
232 self._observation_and_action_constraint_splitter = (
233 observation_and_action_constraint_splitter)
234 self._q_network = q_network
--> 235 net_observation_spec = time_step_spec.observation
236 if observation_and_action_constraint_splitter:
237 net_observation_spec, _ = observation_and_action_constraint_splitter(
238 net_observation_spec)
AttributeError: 'BoundedArraySpec' object has no attribute 'observation'
In call to configurable 'DqnAgent' (<class 'tf_agents.agents.dqn.dqn_agent.DqnAgent'>)