【问题标题】:NotImplementedError: numpy() is only available when eager execution is enabled; while using TF2.4.1NotImplementedError: numpy() 仅在启用急切执行时可用;使用 TF2.4.1 时
【发布时间】:2021-04-13 15:18:57
【问题描述】:

使用 tensorflow 2.4.1,我在 keras 中覆盖 SimpleRNNCell.call,可在此处找到:

https://github.com/tensorflow/tensorflow/blob/85c8b2a817f95a3e979ecd1ed95bff1dc1335cff/tensorflow/python/keras/layers/recurrent.py#L1362

偏向部分如下:

    if self.bias is not None:
      bias_inv = np.arctanh(self.bias)
      h = K.bias_add(h, bias_inv)

我收到以下错误:

NotImplementedError: in user code:

    <ipython-input-12-a2655e34a197>:72 call  *
        inputs, mask=mask, training=training, initial_state=initial_state)
    <ipython-input-55-87c0b5bbed00>:23 call  *
        bias_inv = np.arctanh(self.bias)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/resource_variable_ops.py:483 __array__  **
        return np.asarray(self.numpy())
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/resource_variable_ops.py:620 numpy
        "numpy() is only available when eager execution is enabled.")

    NotImplementedError: numpy() is only available when eager execution is enabled.

我确定启用了 Eager Execution,这可能是什么问题?

【问题讨论】:

  • 不要在层内使用 numpy 操作,将其替换为 tf 或后端操作。
  • 感谢您的回复.. 我怎样才能实现带有后端操作的 arctanh()?你能用一个简单的例子澄清一下吗?

标签: python numpy tensorflow keras eager-execution


【解决方案1】:

您需要使用 tensorflow 函数来实现您的层,而不是 numpy 函数。在这种情况下,您应该将np.arctanh 替换为tf.math.atanh

bias_inv = tf.math.atanh(self.bias)

【讨论】:

    【解决方案2】:

    @拉米汉娜
    AFAIK,Keras 在执行时将所有层和模型转换为图形。因此,即使启用了 Eager 模式,您也可能会遇到此类错误。您可以通过以下任一方式避免它们:

    1. 将图层用作函数(以测试您所做的更改)
    2. 设置dynamic=True 标志(在文档中检查一次)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 2020-11-20
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      • 2019-08-28
      相关资源
      最近更新 更多