I train my deep learning model in tensorflow 2 (Tf2) environment because my latest GPU/Driver doesn't support tensorflow 1 (Tf1). However, the program I need to use post-training does not work with Tf2 models: it only accepts Tf1 (keras) based models. To manage, I have two different environments - one for training the model under Tf2, and one for using the SAME MODEL post training under Tf1.

This requires a hack of going in and out of different environments that is simply too cumbersome:

From TF2 env: I define the model with

model_Tf2 = tensorflow.keras.models.Model([input], outputs)

Next: save_the_weights to Tf2_weights_file.h5

Now from Tf1 env: I create the same model above using

model_Tf1 = keras.models.Model([input], outputs)

I then load the Tf2 weights into my model from Tf1 env:


I now have Tf1 keras model whose weights came from a Tf2 model

The Question: Is there a more efficient way to do this, all in "one" script without having to switch in and out of different environments? Ideal situation would be if a single function could convert a model from Tf2 to Tf1? Thank you!



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.