python - 自定义环境的 Tensorflow 2.0 DQN 代理问题

标签 python tensorflow tensorflow2.0 agent dqn

所以我一直在关注 DQN 代理示例/教程,并按照示例中的方式进行设置,唯一的区别是我构建了自己的自定义 python 环境,然后将其包装在 TensorFlow 中。然而,无论我如何塑造我的观察和行动规范,每当我给它一个观察并请求一个行动时,我似乎都无法让它发挥作用。这是我得到的错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: In[0] is not a matrix. Instead it has shape [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. 0. 0. 0. -1.], shape=(16,), dtype=float32)

我已经尝试试验 Q_Net 的形状和深度,在我看来,错误中的 [10] 与我的 q 网络的形状有关。将其层参数设置为 (4,) 会产生以下错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: In[0] is not a matrix. Instead it has shape [4] [Op:MatMul]

最佳答案

在您的 Python 环境中,您应该将 self._observation_spec 定义为类型 BoundedArraySpec 而不是 TensorSpec,然后是 tf_py_environment。 TFPyEnvironment(env) 将python环境转换为tensorflow环境。

不确定它会导致该错误,但至少这是代码的问题。

关于python - 自定义环境的 Tensorflow 2.0 DQN 代理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59141439/

相关文章:

python - 在 QLabel 元素中实时显示 CV2

python - (已解决)Tensorflow 联合 | tff.learning.from_keras_model() 带有 DenseFeature 层和多个输入的模型

tensorflow - Google Cloud ML 引擎中的分布式 Tensorflow 设备放置

python - Tensorflow - 训练期间的 Nan 成本值 - 尝试了通常的修复但没有成功

tensorflow - 检查tensorflow keras模型中的下一层

python - python 抓包指南

python - 运行基于 OpenMPI 的库时出错

python - 如何在 Python 中将人类可读的日期和时间转换为 Unix 时间戳?

classification - Tensorflow 中类不平衡二元分类器的损失函数

python - Tensorflow 解析 CSV 迭代器按行移位