【问题标题】:tf_agents custom time_step_spectf_agents 自定义 time_step_spec
【发布时间】:2019-10-11 20:41:23
【问题描述】:

我正在修改 tf-agents,但在制作自定义 time_step_spec 时遇到问题。

我正在尝试在健身房“Breakout-v0”中训练一个 tf-agent,我已经创建了一个函数来预处理观察结果(游戏像素),现在我想修改 time_step 和 time_step_spec 以反映新数据.

原来time_step_spec.observation()是:

BoundedTensorSpec(shape=(210, 160, 3), dtype=tf.uint8, name='observation', minimum=array(0, dtype=uint8), maximum=array(255, dtype=uint8))  

我的应该是:

BoundedTensorSpec(shape=(1, 165, 150), dtype=tf.float32, name='observation', minimum=array(0., dtype=float32), maximum=array(1., dtype=float32))        

我已经设法创建了一个自定义的 BoundedTensorSpec 并使用函数修改了 time_step

processed_timestep = timestep._replace(observation=processed_obs)

现在我无法理解如何修改 time_step_spec 我不完全理解它是什么,也不知道如何修改它的组件。

原来的time_step_spec是:

TimeStep(step_type=TensorSpec(shape=(), dtype=tf.int32, name='step_type'), reward=TensorSpec(shape=(), dtype=tf.float32, name='reward'), discount=BoundedTensorSpec(shape=(), dtype=tf.float32, name='discount', minimum=array(0., dtype=float32), maximum=array(1., dtype=float32)), observation=BoundedTensorSpec(shape=(210, 160, 3), dtype=tf.uint8, name='observation', minimum=array(0, dtype=uint8), maximum=array(255, dtype=uint8)))

它到底是什么结构?张量数组?
如何访问其组件?
我可以使用多个组件制作自定义 time_step_spec 吗? (奖励、观察等)
我可以只修改一个组件吗?

【问题讨论】:

    标签: python-3.x tensorflow reinforcement-learning


    【解决方案1】:

    这可以通过使用环境轻松解决。在 TF-Agents 中,环境需要遵循 PyEnvironment 类(然后用 TFPyEnvironment 包装它以并行执行多个环境)。如果你已经定义了你的环境来匹配这个类的规范,那么你的环境应该已经为你提供了env.time_step_spec()和env.action_spec()这两种方法。只需将这两个提供给您的代理即可。

    要创建包含多个组件的 time_step,您可以查看以下示例:

    self._observation_spec = {'observations': array_spec.ArraySpec(shape=(100,), dtype=np.float64),
                              'legal_moves': array_spec.ArraySpec(shape=(self.num_moves(),), dtype=np.bool_),
                              'other_output1': array_spec.ArraySpec(shape=(10,), dtype=np.int64),
                              'other_output2': array_spec.ArraySpec(shape=(2, 5, 2), dtype=np.int64)}
    

    这一行进入环境的__init__ 函数。你不需要知道这实际上在做什么,但你可以看到它基本上是一个带有字符串键和 ArraySpec 值的字典。请注意,如果您这样做,您将必须定义一个observation_and_action_constraint_splitter 以传递给代理,该代理会丢弃不应提供给代理输入的所有组件。这是一个示例,说明如何在您的 env._step 方法中使用此观察字典构建适当的 TimeStep:

    observations_and_legal_moves = {'observations': current_agent_obs,
                                    'legal_moves': np.ones(shape=(self.num_moves(), dtype=np.bool_),
                                    'other_output1': np.ones(shape=(10, dtype=np.int64,
                                    'other_output2': np.ones(shape=(2, 5, 2), dtype=np.int64}
    
    ts.transition(observations_and_legal_moves, reward, self.gamma)
    

    如果您的环境已经构建了其 Tensor 输出,而您只是无法确定合适的 TensorSpec 应该是什么(获取它可能非常繁琐),那么您可以简单地调用 tf.TensorSpec.from_tensor(tensor) 来找出什么你必须定义。

    【讨论】:

      猜你喜欢
      • 2019-12-07
      • 2021-02-24
      • 2022-06-16
      • 2022-10-15
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多