keras model.save() 引发 NotImplementedError [英] keras model.save() raise NotImplementedError
问题描述
我在以下链接中尝试了 keras nmt 代码:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/nmt_with_attention/nmt_with_attention.ipynb
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
但是当我尝试保存模型时,出现 NotImplementedError:
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
Encoder,Decoder 是 tf.keras.Model 的子类,tf.keras.Model 是 Network 的子类.阅读https://github中的代码后.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/engine/network.py
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
我发现这两个类的_is_graph_network变成了False.我试图将标志设置为 true 但又出现了一个错误.那么如何保存作者在代码中定义的模型呢?
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?
推荐答案
我在当前的 tf 版本 (1.11) 中遇到了类似的问题.我使用 tf.keras API 来定义我的模型并毫无问题地训练它.当我想使用 tensorflow.keras.models.save_model
或 model.save()
(它只是调用 save_model)保存我的模型时,我得到了以下异常:
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__() 仅在启用急切执行时可用.
NotImplementedError: __deepcopy__() is only available when eager execution is enabled.
所以我调用了 tf.enable_eager_execution()
,但由于在我的架构中使用了 Lambda 层,我最终遇到了另一个 NotImplementedError .. 如果您的架构不包含 Lambda 层,则启用eager_execution 可以解决您在 tf 1.11 中的问题.
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.
我最后的要走的路"是使用 model.save_weights('model_weights.h5')
因为我不需要只保存经过训练的权重的模型架构.顺便说一句:在我的情况下,也可以从 tensorflow.keras.* 导入切换到 keras.* 并仅使用带有 tf 后端的普通"keras(model.save()
在这里工作 - 的当然).
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).
这篇关于keras model.save() 引发 NotImplementedError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!