我正在使用Tensorflow 1.8.0版本...
Keras文档中关于如何保存模型的说明没有提到保存序贯模型与从函数式API创建的模型之间的区别。但是,以下所有代码块都无法正常运行:
import tensorflow as tf
net = tf.keras.models.Model()
net.save('file')
或者
import tensorflow as tf
net = tf.keras.models.Model()
print(net.to_json())
或者
import tensorflow as tf
net = tf.keras.models.Model()
print(net.to_yaml())
或者
import tensorflow as tf
net = tf.keras.models.Model()
print(net.get_config())
他们引发了一个NotImplementedError
。在Keras模块中,相关行是
if not self._is_graph_network:
raise NotImplementedError
这段文字中提到的内容出现在
.save
和get_config
函数中(后者也被to_json
和to_yaml
调用)。唯一有效的方法是以下内容。
import tensorflow as tf
net = tf.keras.models.Model()
net.save_weights('file')
如果这种情况下权重保存成功并且可以通过net.load_weights
成功加载。
然而,将上述代码块的第二行替换为net = tf.keras.models.Sequential()
,使net成为一个序列模型,可以使得上述所有内容都能正常运行。
使用函数式API(使用Model
而不是Sequential
)创建的Keras模型的结构真的无法保存吗?现在,我们只能保存权重吗?
NotImplementedError
的说法并不荒谬,而是可以重复和验证的。老实说,这是一个误导性的错误。 - Jay Calamari