So I've been following the DQN agent example / tutorial and I set it up like in the example, only difference is that I built my own custom python environment which I then wrapped in TensorFlow. However, no matter how I shape my observations and action specs, I can't seem to get it to work whenever I give it an observation and request an action. Here's the error that I get:
tensorflow.python.framework.errors_impl.InvalidArgumentError: In[0] is not a matrix. Instead it has shape [10] [Op:MatMul]
Here's how I'm setting up my agent:
layer_parameters = (10,) #10 layers deep, shape is unspecified
#placeholders
learning_rate = 1e-3 # @param {type:"number"}
train_step_counter = tf.Variable(0)
#instantiate agent
optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=learning_rate)
env = SumoEnvironment(self._num_actions,self._num_states)
env2 = tf_py_environment.TFPyEnvironment(env)
q_net= q_network.QNetwork(env2.observation_spec(),env2.action_spec(),fc_layer_params = layer_parameters)
print("Time step spec")
print(env2.time_step_spec())
agent = dqn_agent.DqnAgent(env2.time_step_spec(),
env2.action_spec(),
q_network=q_net,
optimizer = optimizer,
td_errors_loss_fn=common.element_wise_squared_loss,
train_step_counter=train_step_counter)
And here's how I'm setting up my environment:
class SumoEnvironment(py_environment.PyEnvironment):
def __init__(self, no_of_Actions, no_of_Observations):
#this means that the observation consists of a number of arrays equal to self._num_states, with datatype float32
self._observation_spec = specs.TensorSpec(shape=(16,),dtype=np.float32,name='observation')
#action spec, shape unknown, min is 0, max is the number of actions
self._action_spec = specs.BoundedArraySpec(shape=(1,),dtype=np.int32,minimum=0,maximum=no_of_Actions-1,name='action')
self._state = 0
self._episode_ended = False
And here is what my input / observations look like:
tf.Tensor([ 0. 0. 0. 0. 0. 0. 0. 0. -1. -1. -1. -1. 0. 0. 0. -1.], shape=(16,), dtype=float32)
I've tried experimenting with the shape and depth of my Q_Net and it seems to me that the [10] in the error is related to the shape of my q network. Setting its layer parameters to (4,) yields an error of:
tensorflow.python.framework.errors_impl.InvalidArgumentError: In[0] is not a matrix. Instead it has shape [4] [Op:MatMul]