tensorflow / text

Making text a first-class citizen in TensorFlow.
https://www.tensorflow.org/beta/tutorials/tensorflow_text/intro
Apache License 2.0
1.22k stars 337 forks source link

Error when exporting translator in Jupyter Notebook #998

Open lancioni opened 2 years ago

lancioni commented 2 years ago

The notebook "Transformer model for language understanding" works fine on Jupyter Notebook (WSL2 Ubuntu 18.04, Python 3.7) apart for a few warnings until the model has to be exported.

Here the following error obtains:

OperatorNotAllowedInGraphError: Iterating over a symbolic tf.Tensor is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.

in relation to

for i in tf.range(max_length)

in Translator class.

If I rplace tf.range(max_length) with range(max_length), as suggested elsewhere, a similar error obtains:

OperatorNotAllowedInGraphError: Using a symbolic tf.Tensor as a Python bool is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.

in relation to

if predicted_id == end: break

in same class.

A reason might perhaps be found in the previous warning:

WARNING: AutoGraph could not transform <bound method ExportTranslator.call of <tensorflow.python.eager.function.TfMethodTarget object at 0x7f11a7d3ffd0>> and will run it as-is.

Any hint or workaround:

thank you very much,

Giuliano

broken commented 2 years ago

Yes, both of the following statements are trying to use the result of a TensorFlow op in the Python control flow. While this may work in eager mode when these are computed immediately, they should not in graph mode (when you save the graph).

However, I do find this comment interesting:

# tf.TensorArray is required here (instead of a python list) so that the # dynamic-loop can be traced by tf.function.

Autograph is relatively new, and there may be some sort of dynamic tracing that is supposed to happen here that I'm not familiar with which would lead to TF building ops for the loop & conditional statements. Let me reach out to the author to see what the intent here is.