(我正在使用 Tensorflow 1.8.0 ...)
documentation from Keras on how to save a model提到保存顺序模型与从功能 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.Model() 替换为 net = tf.keras.models.Sequential() ,让 net 成为一个顺序模型,让上面的一切都能正常工作。
是否真的无法保存使用函数式 API 制作的 Keras 模型的结构(使用 Model 而不是 Sequential)?现在,我们只能减重吗?
请您参考如下方法:
当然可以保存模型,你所有的例子都有一个空模型,保存是没有意义的。 Keras 的作者根本没有实现这种情况。
如果您使用非空模型进行测试,您会发现保存效果很好。我们每天都在使用它。
