0

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?

Ramy Hanna
  • 13
  • 1
  • 5
  • Do not use numpy operations inside a layer, replace it with a tf or backend operation. – Dr. Snoopy Apr 13 '21 at 15:40
  • Thanks for your reply .. how can I implement an arctanh() with a backend operation? can you please clarify with a simple example? – Ramy Hanna Apr 13 '21 at 17:02

2 Answers2

0

@Ramy Hanna
AFAIK, Keras converts all layers and models into graphs when executing. Thus, even though eager mode is on, you may encounter such errors. You can avoid them by either:

  1. Use the layer as a function (to test the changes you made)
  2. Setting the dynamic=True flag (check once in docs)
Aditya Kane
  • 371
  • 3
  • 5
0

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)
Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140