TypeError: Unable to serialize 16.0 to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.
May you share the transformer model architecture you used so we can reproduce the error?  Good morning Zeyad;
Thank you very much for helping.
The attached file one_step_transformer_components.py contains the definition of the transformer I am trying to save when I experience the issue.
I am using the transformer to do one step ahead time series predictions

Here are the dimensions of the transformer I am trying to save.
num_layers = 2num_heads = 2model_dimensions = 16ffn_units = 64features_shape = (1024, 3, 2)labels_shape = (1024, 3, 2)dropout_rate = 0.2

I tried decorating the transformed functions call() and predict with an @tf.function() hoping it might dodge the issue by taking a different route through the save_model() logic.  I did not. I still received the same error message.
While running TensorFlow 2.10. I also found that the tf.keras.layers.Normalization layer I was using was not doing its inversion calculations correctly so I coded the inversion myself in the transformer call() function. I have not yet tested the Normalization layer in version 2.12.
Thank you again for helping
RegardsCraig(630) 453-0404








    On Saturday, April 29, 2023 at 05:53:31 AM CDT, Zeyad Abd El-Raheem ***@***.***> wrote:  
 
 


May you share the transformer model architecture you used so we can reproduce the error?

—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.Message ID: ***@***.***>
   Hi @cpodczerwinski, Apologies for the delay.
I can see a different error when replicating the issue in Colab with Tensorflow 2.12. Please find the gist [here](https://colab.sandbox.google.com/gist/synandi/32a2ad838dbac60391952d4b6b2be476/untitled355.ipynb). It would help us if you could share the complete reproducible code to expedite the troubleshooting process. Thank you!   Good morning;
Thank you again for helping.
The attached file one_step_unit_tests.py contains a script that you can run with the file one_step_transformer_components.py that I provided in my previous email. It reproduces the error.  The output I received when I executed it follows;
The following transformer member function fixes the error that you received when saving the tutorial tensor that you ran
def get_config(self):
 config = {
 'd_model': self.model_dimensions,
 'warmup_steps': self.warmup_steps,

 }
 return config


RegardsCraig

C:\Users\cpodc\AppData\Local\Programs\Python\Python311\python.exe D:\Craig\Python\one_step_unit_tests.py transformer_output[0].shape:  (1024, 3, 2)WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.preprocessing.normalization.Normalization object at 0x000001E0ACA99250>, because it is not built.WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.preprocessing.normalization.Normalization object at 0x000001E0ACB4D650>, because it is not built.WARNING:absl:Found untraced functions such as _update_step_xla, dense_layer_call_fn, dense_layer_call_and_return_conditional_losses, positional_embedding_layer_call_fn, positional_embedding_layer_call_and_return_conditional_losses while saving (showing 5 of 165). These functions will not be directly callable after loading.Traceback (most recent call last):  File "D:\Craig\Python\one_step_unit_tests.py", line 30, in <module>    tf.keras.saving.save_model( transformer, 'Output\\saved_transformer')  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\saving\saving_api.py", line 145, in save_model    return legacy_sm_saving_lib.save_model(           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler    raise e.with_traceback(filtered_tb) from None  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 200, in encode    chunks = self.iterencode(o, _one_shot=True)             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 258, in iterencode    return _iterencode(o, 0)           ^^^^^^^^^^^^^^^^^TypeError: Unable to serialize 16.0 to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.
Process finished with exit code 1


    On Monday, May 8, 2023 at 08:11:21 AM CDT, synandi ***@***.***> wrote:  
 
 


Hi @cpodczerwinski, Apologies for the delay.
I can see a different error when replicating the issue in Colab with Tensorflow 2.12. Please find the gist here. It would help us if you could share the complete reproducible code to expedite the troubleshooting process. Thank you!

—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
    I forget to attach the file one_step_unit_tests,py.  Here is a copy of it.
RegardsCraig
    On Thursday, May 11, 2023 at 04:44:02 PM CDT, Craig Podczerwinski ***@***.***> wrote:  
 
  Good morning;
Thank you again for helping.
The attached file one_step_unit_tests.py contains a script that you can run with the file one_step_transformer_components.py that I provided in my previous email. It reproduces the error.  The output I received when I executed it follows;
The following transformer member function fixes the error that you received when saving the tutorial tensor that you ran
def get_config(self):
 config = {
 'd_model': self.model_dimensions,
 'warmup_steps': self.warmup_steps,

 }
 return config


RegardsCraig

C:\Users\cpodc\AppData\Local\Programs\Python\Python311\python.exe D:\Craig\Python\one_step_unit_tests.py transformer_output[0].shape:  (1024, 3, 2)WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.preprocessing.normalization.Normalization object at 0x000001E0ACA99250>, because it is not built.WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.preprocessing.normalization.Normalization object at 0x000001E0ACB4D650>, because it is not built.WARNING:absl:Found untraced functions such as _update_step_xla, dense_layer_call_fn, dense_layer_call_and_return_conditional_losses, positional_embedding_layer_call_fn, positional_embedding_layer_call_and_return_conditional_losses while saving (showing 5 of 165). These functions will not be directly callable after loading.Traceback (most recent call last):  File "D:\Craig\Python\one_step_unit_tests.py", line 30, in <module>    tf.keras.saving.save_model( transformer, 'Output\\saved_transformer')  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\saving\saving_api.py", line 145, in save_model    return legacy_sm_saving_lib.save_model(           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler    raise e.with_traceback(filtered_tb) from None  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 200, in encode    chunks = self.iterencode(o, _one_shot=True)             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 258, in iterencode    return _iterencode(o, 0)           ^^^^^^^^^^^^^^^^^TypeError: Unable to serialize 16.0 to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.
Process finished with exit code 1


    On Monday, May 8, 2023 at 08:11:21 AM CDT, synandi ***@***.***> wrote:  
 
 


Hi @cpodczerwinski, Apologies for the delay.
I can see a different error when replicating the issue in Colab with Tensorflow 2.12. Please find the gist here. It would help us if you could share the complete reproducible code to expedite the troubleshooting process. Thank you!

—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
     @cpodczerwinski 
The code provided is not complete hence it would be difficult for us to pinpoint the issue. Please share complete stand alone code to replicate the issue or a colab gist with the error reported.? That will allow us to determine the source of the issue easily. Thank you!  Hello Syed;
I am resending you the files onestep_unit_tests.py and one_step_transformer_components.py which are attached. Executing the file one_step_unit_tests.py will reproduce the problem.  The code in one_step_unit_tests.py uses the classes and functions defined in one_step_transformer_components.py. I just executed one_step_unit_tests.py on my laptop and received the following output.  I am using the PyCharm IDE.
Thank you for helping.RegardsCraig(630) 453-0404

D:\Craig\Python\Projects\ModelSaveProblem\Scripts\python.exe D:\Craig\Python\one_step_unit_tests.py transformer_output[0].shape:  (1024, 3, 2)WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.preprocessing.normalization.Normalization object at 0x000001EB3B7B3590>, because it is not built.WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.preprocessing.normalization.Normalization object at 0x000001EB3B7B1B50>, because it is not built.WARNING:absl:Found untraced functions such as _update_step_xla, dense_layer_call_fn, dense_layer_call_and_return_conditional_losses, positional_embedding_layer_call_fn, positional_embedding_layer_call_and_return_conditional_losses while saving (showing 5 of 165). These functions will not be directly callable after loading.Traceback (most recent call last):  File "D:\Craig\Python\one_step_unit_tests.py", line 30, in <module>    tf.keras.saving.save_model( transformer, 'Output\\saved_transformer')  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\saving\saving_api.py", line 145, in save_model    return legacy_sm_saving_lib.save_model(           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler    raise e.with_traceback(filtered_tb) from None  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 200, in encode    chunks = self.iterencode(o, _one_shot=True)             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 258, in iterencode    return _iterencode(o, 0)           ^^^^^^^^^^^^^^^^^TypeError: Unable to serialize 16.0 to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.
Process finished with exit code 1
    On Monday, May 8, 2023 at 08:11:21 AM CDT, synandi ***@***.***> wrote:  
 
 


Hi @cpodczerwinski, Apologies for the delay.
I can see a different error when replicating the issue in Colab with Tensorflow 2.12. Please find the gist here. It would help us if you could share the complete reproducible code to expedite the troubleshooting process. Thank you!

—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
    Good morning;
Have you been able to reproduce and identify a corrective action for this issue. Are there any work around actions I can take?
Thank you for helping
RegardsCraig
    On Monday, May 15, 2023 at 03:38:30 PM CDT, Craig Podczerwinski ***@***.***> wrote:  
 
  Hello Syed;
I am resending you the files onestep_unit_tests.py and one_step_transformer_components.py which are attached. Executing the file one_step_unit_tests.py will reproduce the problem.  The code in one_step_unit_tests.py uses the classes and functions defined in one_step_transformer_components.py. I just executed one_step_unit_tests.py on my laptop and received the following output.  I am using the PyCharm IDE.
Thank you for helping.RegardsCraig(630) 453-0404

D:\Craig\Python\Projects\ModelSaveProblem\Scripts\python.exe D:\Craig\Python\one_step_unit_tests.py transformer_output[0].shape:  (1024, 3, 2)WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.preprocessing.normalization.Normalization object at 0x000001EB3B7B3590>, because it is not built.WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.preprocessing.normalization.Normalization object at 0x000001EB3B7B1B50>, because it is not built.WARNING:absl:Found untraced functions such as _update_step_xla, dense_layer_call_fn, dense_layer_call_and_return_conditional_losses, positional_embedding_layer_call_fn, positional_embedding_layer_call_and_return_conditional_losses while saving (showing 5 of 165). These functions will not be directly callable after loading.Traceback (most recent call last):  File "D:\Craig\Python\one_step_unit_tests.py", line 30, in <module>    tf.keras.saving.save_model( transformer, 'Output\\saved_transformer')  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\saving\saving_api.py", line 145, in save_model    return legacy_sm_saving_lib.save_model(           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler    raise e.with_traceback(filtered_tb) from None  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 200, in encode    chunks = self.iterencode(o, _one_shot=True)             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 258, in iterencode    return _iterencode(o, 0)           ^^^^^^^^^^^^^^^^^TypeError: Unable to serialize 16.0 to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.
Process finished with exit code 1
    On Monday, May 8, 2023 at 08:11:21 AM CDT, synandi ***@***.***> wrote:  
 
 


Hi @cpodczerwinski, Apologies for the delay.
I can see a different error when replicating the issue in Colab with Tensorflow 2.12. Please find the gist here. It would help us if you could share the complete reproducible code to expedite the troubleshooting process. Thank you!

—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
     @cpodczerwinski,
We can see that the code which you provided was not sufficient for debugging the issue. Could you please provide the complete standalone code or the colab gist which helps to analyse the issue in an effective way. 
And if you are trying to use efficientnet, please try to run below and also the PR which was merged.
https://github.com/keras-team/keras/pull/17498

```
tf.__version__

base_model_v2 = tf.keras.applications.efficientnet_v2.EfficientNetV2M(include_top=False)

...

model.save('../saved_models/food101_env2m_30epochs')
```

Thank you!  Good morning;
Please send me a copy of the debugger output that you received when you executed the code that I sent you and a description of the environment in which you executed it.
Thank you for helpingCraig
    On Monday, June 19, 2023 at 11:18:34 AM CDT, tilakrayal ***@***.***> wrote:  
 
 


@cpodczerwinski,
We can see that the code which you provided was not sufficient for debugging the issue. Could you please provide the complete standalone code or the colab gist which helps to analyse the issue in an effective way. Thank you!

—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
   @cpodczerwinski,
Could you please find the [gist](https://colab.sandbox.google.com/gist/tilakrayal/ebd543e370bee148d882a828889bb822/untitled355.ipynb) of the code which you have mentioned and I was able to face a different error. Kindly provide the information for the same. Thank you! This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60437">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60437">No</a>

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

No

### Source

source

### Tensorflow Version

2.10 and 2.12

### Custom Code

No

### OS Platform and Distribution

Windows 10 

### Mobile device

_No response_

### Python version

3.11.3

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

RTX M960 4GB

### Current Behaviour?

I have been unable to save my transformer model using any of the Tensorflow complete model saving functions. While running Tensorflow 2.10 I received the following using message attempting to save using tf.keras.models.save_model:

TypeError: Unable to serialize 16.0 to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.

I was able to successfully save and read the model weights back in.  I was unable to recover it's normalization layer.

This issue was previously reported as a bug in 2.10

I uninstalled all python software and packages and the PyCharm IDE and installed python 3.11.3 and Tensorflow 2.12 and the latest PyCharm Community edition hoping that would correct the problem.  I used tf.keras.saving.save_model( ) using saved formats tf and keras. It did not correct the problem.  

The model I am trying to save is a simplied version to the Tensorflow Translator tutorial at https://www.tensorflow.org/text/tutorials/transformer

Here is a listing of the error message I received from PyCharm. It seems to have something to do with the python class tensorflow.python.framework.ops.EagerTensor Is there anything I can do with my model to dodge the issue

Thank you in advance for your help.

WARNING:absl:Found untraced functions such as _update_step_xla, dense_20_layer_call_fn, dense_20_layer_call_and_return_conditional_losses, positional_embedding_4_layer_call_fn, positional_embedding_4_layer_call_and_return_conditional_losses while saving (showing 5 of 165). These functions will not be directly callable after loading.

Traceback (most recent call last):
  File "D:\Craig\Python\Projects\CraigsPackages\transformer_component_unit_tests.py", line 333, in <module>
    return_pair = predict_investment_return(transformer, index, features, decoder_inputs,return_labels,  warm_up=warm_up)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Craig\Python\Projects\CraigsPackages\transformer_component_unit_tests.py", line 252, in predict_investment_return
    tf.keras.saving.save_model(transformer, "transformer_model_save_test", save_format="tf")
  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\saving\saving_api.py", line 145, in save_model
    return legacy_sm_saving_lib.save_model(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
TypeError: Unable to serialize 16.0 to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.



### Standalone code to reproduce the issue

```shell
tf.keras.saving.save_model(transformer, "transformer_model_save_test", save_format="tf")
tf.keras.saving.save_model(transformer, "transformer_model_save_test", save_format="keras")
```


### Relevant log output

```shell
WARNING:absl:Found untraced functions such as _update_step_xla, dense_20_layer_call_fn, dense_20_layer_call_and_return_conditional_losses, positional_embedding_4_layer_call_fn, positional_embedding_4_layer_call_and_return_conditional_losses while saving (showing 5 of 165). These functions will not be directly callable after loading.

Traceback (most recent call last):
  File "D:\Craig\Python\Projects\CraigsPackages\transformer_component_unit_tests.py", line 333, in <module>
    return_pair = predict_investment_return(transformer, index, features, decoder_inputs,return_labels,  warm_up=warm_up)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Craig\Python\Projects\CraigsPackages\transformer_component_unit_tests.py", line 252, in predict_investment_return
    tf.keras.saving.save_model(transformer, "transformer_model_save_test", save_format="tf")
  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\saving\saving_api.py", line 145, in save_model
    return legacy_sm_saving_lib.save_model(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cpodc\AppData\Local\Programs\Python\Python311\Lib\json\encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
TypeError: Unable to serialize 16.0 to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.
```
</details>