4

I have tried the keras nmt code in the following link:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/nmt_with_attention/nmt_with_attention.ipynb

But when I tried to save the model, I get a NotImplementedError:

File "m.py", line 310, in <module> main() File "m.py", line 244, in main encoder.save('/home/zzj/temp/encoder.h5') File "/home/zzj/tensorflow/lib/python3.5/site-packages/tensorflow/python/keras/engine/network.py", line 1218, in save raise NotImplementedError 

The Encoder,Decoder subclassed the tf.keras.Model, and tf.keras.Model is a subclass of Network. After reading the code in https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/engine/network.py

I found that these two class's _is_graph_network became False. I tried to set the flag to be true but get another error. So how can I save the model the author defined in the code?

2
  • could you post the code that is throwing the error? Commented Jul 28, 2018 at 7:25
  • Yes, you should include the code in your question. Commented Jul 28, 2018 at 7:41

3 Answers 3

2

I had a similar problem with the current tf version (1.11). I used the tf.keras API to define my model and trained it without problems. When I wanted to save my model using tensorflow.keras.models.save_model or model.save()(which just calls save_model) I got the following exception:

NotImplementedError: __deepcopy__() is only available when eager execution is enabled.

So I called tf.enable_eager_execution(), but because of the usage of a Lambda Layer in my architecture, I ended up with another NotImplementedError of "compute_output_shape".. If your architecture does not contain a Lambda Layer the enabling of eager_execution could fix your problem in tf 1.11.

My final "way to go" was to use model.save_weights('model_weights.h5') because I did not need to save the model architecture just the trained weights. Btw.: in my case it was also possible to switch from tensorflow.keras.* imports to keras.* and use just "plain" keras with tf backend (model.save() works here - of course).

Sign up to request clarification or add additional context in comments.

Comments

1

model.save('model.h5py') may solve the problem. The key is to save as h5py file.

1 Comment

Try adding some explanation as to why this would solve the problem.
0

Consider using tf.keras.models.save_model() and load_model(), it may work.

1 Comment

Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. How to Answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.