kpe / bert-for-tf2

A Keras TensorFlow 2.0 implementation of BERT, ALBERT and adapter-BERT.
https://github.com/kpe/bert-for-tf2
MIT License
803 stars 193 forks source link

Confusing error log for incorrect unpacking tupled output from the BERT layer #57

Closed KacperKubara closed 4 years ago

KacperKubara commented 4 years ago

Hey!

I've been trying to add some layers on top of the BERT using subclassing API. This line of the code:

_, self.X = self.bert_layer([inputs[:, 0], inputs[:, 1]])

Generated following error:

Traceback (most recent call last):
  File "C:\Users\kacpe\Miniconda3\envs\lplp\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 773, in __call__
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "C:\Users\kacpe\Miniconda3\envs\lplp\lib\site-packages\tensorflow_core\python\autograph\impl\api.py", line 237, in wrapper
    raise e.ag_error_metadata.to_exception(e)
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: in converted code:

    C:\Users\kacpe\Desktop\Github\lplp\model.py:86 call  *
        _, self.X = self.bert_layer([inputs[:, 0], inputs[:, 1]])
    C:\Users\kacpe\Miniconda3\envs\lplp\lib\site-packages\tensorflow_core\python\framework\ops.py:539 __iter__
        self._disallow_iteration()
    C:\Users\kacpe\Miniconda3\envs\lplp\lib\site-packages\tensorflow_core\python\framework\ops.py:535 _disallow_iteration
        self._disallow_in_graph_mode("iterating over `tf.Tensor`")
    C:\Users\kacpe\Miniconda3\envs\lplp\lib\site-packages\tensorflow_core\python\framework\ops.py:515 _disallow_in_graph_mode
        " this function with @tf.function.".format(task))

    OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.

Which occurred because I've tried to unpack the results. I lost a fair amount of time to fix the problem. The error log is rather quite misleading in this case. When I changed the code to the following:

self.X = self.bert_layer([inputs[:, 0], inputs[:, 1]])

The problem dissappeared.

Closing the issue as it has been solved by feel free to open if necessary.