因此,我一直遵循DQN代理示例/教程,并且像示例中那样进行设置,唯一的不同是,我构建了自己的自定义python环境,然后将其包装在TensorFlow中。但是,无论我如何调整观察结果和操作规范,似乎都无法通过观察和请求操作来使它起作用。这是我得到的错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError:In [0]是不是矩阵相反,它的形状为[10] [Op:MatMul]
这是我设置代理的方式:
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)
这是我设置环境的方式:
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
这是我的输入/观察结果:
tf.Tensor([0. 0. 0. 0. 0. 0. 0. 0.-1.-1.-1.-1.-1.0.0.0.-1。],shape =(16,),dtype = float32)
我已经尝试过Q_Net的形状和深度,在我看来,错误中的[10]与我的q网络的形状有关。将其图层参数设置为(4,)会产生以下错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError:In [0]是不是矩阵相反,它的形状为[4] [Op:MatMul]
从错误消息中的关键字matrix中,我认为TF期望的是二维张量而不是一维的张量。
我建议将图层参数设置为(4, 1)
(或(1, 4)
)。
我将尝试使用它来验证我的答案。