TensorFlow 优化器中的 _get_hyper 和 _set_hyper 是什么?

What are _get_hyper and _set_hyper in TensorFlow optimizers?

我在 __init__ 中看到它,例如Adam optimizer: self._set_hyper('beta_1', beta_1). There are also _get_hyper and _serialize_hyperparameter throughout the code. I don't see these in Keras 优化器 - 它们是可选的吗?在创建自定义优化器时应该或不应该使用它们?

它们启用设置和获取 Python 文字(intstr 等)、callables 和张量。用法是为了 方便 一致性 :通过 _set_hyper 设置的任何内容都可以通过 _get_hyper 检索,避免重复样板代码。我已经在所有主要的 TF 和 Keras 版本中实现了 Keras AdamW,并将其用作参考。

  • t_cur is a tf.Variable. Each time we "set" it, we must invoke K.set_value; if we do self.t_cur=5, this will destroy tf.Variable and wreck optimizer functionality. If instead we used model.optimizer._set_hyper('t_cur', 5), it'd set it appropriately - but this requires for it to have been defined via set_hyper previously.
  • _get_hyper_set_hyper 都启用 程序化 属性处理 - 例如,我们可以用列表创建一个 for 循环仅使用 _get_hyper_set_hyper 获取或设置的属性名称,否则我们需要编写条件和类型检查代码。此外,_get_hyper(name) 要求之前通过 set_hyper.

  • 设置 name
  • _get_hyper 通过 dtype= 启用类型转换。例如:默认情况下,beta_1_t Adam 被转换为与 var 相同的数字类型(例如层权重),这是某些操作所必需的。再次方便,因为我们可以手动进行类型转换 (math_ops.cast).

  • _set_hyper 启用 _serialize_hyperparameter 的使用,它检索 Python 值(intfloat 等)可调用项、张量或已经 Python 的值。名称源于需要将张量和可调用对象转换为 Pythonics,例如pickling 或 json-serializing - 但可以用作在 Graph 执行中查看张量值的便利。

  • 最后;通过 _set_hyper 实例化的所有内容都分配给 optimizer._hyper 字典,然后在 _create_hypers. The else in the loop casts all Python numerics to tensors - so _set_hyper will not create int, float, etc attributes. Worth noting is the aggregation= kwarg 中迭代,其文档为:"Indicates how a distributed variable will be aggregated"。这是比 "for convenience" 多一点的部分(需要复制很多代码)。

    • _set_hyper 有一个 限制 :不允许实例化 dtype。如果 _create_hypers 中的 add_weight 方法需要 dtype,则应直接调用它。

何时使用与不使用:如果优化器通过 TensorFlow 操作使用该属性,则使用 - 即如果它需要 tf.Variable。例如,epsilon 是定期设置的,因为它永远不需要作为张量变量。