Note Abele to Save in h5 Format When Text Vectoriszation is Involved in the Model

Hi everyone,

I’m preparing for the TensorFlow Developer certification exam, and I’ve been stuck with an issue all day. I’m training a TensorFlow model for text classification using the TextVectorization layer in it. I’ve been able to build and fit the model and save it in the TensorFlow format, but I’m having trouble saving it in the ‘.h5’ format, which is mandatory for the certification exam.

I’ve tried searching for a solution on Google and other forums, but haven’t been able to find one. Can anyone help me with this issue? I’d appreciate any suggestions or insights on how to save my model in the required ‘.h5’ format.

Thank you in advance!

Error Message : Save or restore weights that is not an instance of tf.Variable is not supported in h5, use save_format='tf' instead. Received a model or layer TextVectorization with weights

Hi @Rahul_Raj, Welcome to the TF forum!

If you are saving the model using tf.keras.models.save_model for the save_format argument pass the value h5 or if you are using model.save(‘model.h5’ ) The ‘.h5’ extension indicates that the model should be saved to HDF5. Thank You.

Hi @Kiran_Sai_Ramineni

Thanks for your response.
I think your answer is about general way of saving a model in TensorFlow.

Requesting you to kindly go through my specific situation where i am not able save the model in h5.

  1. Please see the error related to “TextVectorization Layer” while saving a model in h5, I can save the same model in tf format.

Error Message : “Save or restore weights that is not an instance of tf.Variable is not supported in h5, use save_format='tf' instead. Received a model or layer TextVectorization with weights”

  1. I am preparing for the TensorFlow Developer Certificate Exam, as per my understanding only model saved in h5 fromat will be accepted by the exam environment.

  2. As per tensorflow website, Tokenizer is deprecated and for the NLP problems, they suggest to use TextVectorization layer only, which does not support saving support h5 format (throwing the above mentioned error ), on the other hand Tensorflow developer certificate Exam only accepts .h5 format.

  3. I would appreciate if you could suggest a work around for the particular situation. thank you

[ Ref ] From Tensorflow.org >>
Deprecated:** [tf.keras.preprocessing.text.Tokenizer] does not operate on tensors and is not recommended for new code. Prefer [tf.keras.layers.TextVectorization] which provides equivalent functionality through a layer which accepts [tf.Tensor]

@Rahul_Raj , Unfortunately, model with Text Vectorization layer does not support saving in .h5 format.
Going forward, the ideal way to use it would be to use the new saving format with model.save("model.keras", save_format="keras").

We will bring it to Tensorflow certification team’s attention, till then the workaround would be to use the Text Vectorization in your data pipeline to something like Load text  |  TensorFlow Core and then use the normal Keras Sequential API for the model part.