pythonnumpytensorflowkeraseager-execution

NotImplementedError: numpy() is only available when eager execution is enabled; while using TF2.4.1


Using tensorflow 2.4.1, I'm overriding SimpleRNNCell.call in keras, found here:

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

exactly the bias part as follows:

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

I got the following error:

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.

I'm sure that eager execution is enabled, what could be a problem?


Solution

  • You need to use tensorflow functions to implement your layer, not numpy functions. In this case you should replace np.arctanh with tf.math.atanh:

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