【问题标题】:Shape of _observation_spec and shape of _action_spec in the Tf-agents environments exampleTf-agents 环境示例中 _observation_spec 的形状和 _action_spec 的形状
【发布时间】:2021-09-15 05:31:09
【问题描述】:

tensorflow documentation for TF-Agents Environments 中有一个简单(受二十一点启发)纸牌游戏的环境示例。

init 如下所示:

class CardGameEnv(py_environment.PyEnvironment):

  def __init__(self):
    self._action_spec = array_spec.BoundedArraySpec(
        shape=(), dtype=np.int32, minimum=0, maximum=1, name='action')
    self._observation_spec = array_spec.BoundedArraySpec(
        shape=(1,), dtype=np.int32, minimum=0, name='observation')
    self._state = 0
    self._episode_ended = False

动作规范只允许 0(不求卡)或 1(求卡),因此形状为 shape=()(只需要一个整数)是明智的。

但是我不太明白观察规范的形状是shape=(1,),因为它只表示当前回合中卡片的总和(所以也是一个整数)。

什么解释了形状上的差异?

【问题讨论】:

    标签: python tensorflow tensorflow-agents


    【解决方案1】:

    一开始我以为它们是一样的。为了测试它们,我在 W3 Schools Python "Try Editor" (I accessed it through this link) 上运行了以下代码:

    import numpy as np
    
    arr1 = np.zeros((), dtype=np.int32)
    arr2 = np.zeros((1), dtype=np.int32)
    
    print("This is the first array:", arr1, "\n")
    print("This is the second array:", arr2, "\n")
    

    我得到的输出是:

    This is the first array: 0
    
    This is the second array: [0] 
    

    这使我得出结论,shape=() 是一个简单的整数,被视为一个 0 维数组,但 shape=(1,) 是一个由单个整数组成的一维数组。我希望这是准确的,因为我自己想要一些确认。在第二次测试中进一步检查:

    import numpy as np
    
    arr1 = np.array(42)
    arr2 = np.array([1])
    arr3 = np.array([1, 2, 3, 4])
    
    print(arr1.shape)
    print(arr2.shape)
    print(arr3.shape)
    

    输出是:

    ()
    (1,)
    (4,)
    

    这似乎证实了我首先得出的结论,因为 arr1 是一个 0 维数组,而 arr3 是一个 4 个元素的一维数组 (as explained in the W3 Schools tutorial),数组 arr2 的形状与 arr3 相似,但是具有不同数量的元素。

    至于为什么动作和观察分别表示为整数和一个元素的数组,可能是因为TensorFlow使用张量(n维数组)工作,将观察视为数组可能更容易计算。

    该操作被声明为整数,可能是为了简化_step() 函数内的流程,因为使用 if/elif/else 结构的数组会有点乏味。有 other examples 的 action_specs 具有更多元素和离散/连续值,所以没有别的想法。

    我不确定这一切是否正确,但至少开始讨论似乎是个好点。

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      相关资源
      最近更新 更多