受 tf.keras.Model 子类化 的启发,我创建了自定义模型。
我可以训练它并获得成功的结果,但 我无法保存它。
我将 python3.6 与 tensorflow v1.10(或 v1.9)一起使用
此处的最小完整代码示例:
import tensorflow as tf
from tensorflow.keras.datasets import mnist
class Classifier(tf.keras.Model):
def __init__(self):
super().__init__(name="custom_model")
self.batch_norm1 = tf.layers.BatchNormalization()
self.conv1 = tf.layers.Conv2D(32, (7, 7))
self.pool1 = tf.layers.MaxPooling2D((2, 2), (2, 2))
self.batch_norm2 = tf.layers.BatchNormalization()
self.conv2 = tf.layers.Conv2D(64, (5, 5))
self.pool2 = tf.layers.MaxPooling2D((2, 2), (2, 2))
def call(self, inputs, training=None, mask=None):
x = self.batch_norm1(inputs)
x = self.conv1(x)
x = tf.nn.relu(x)
x = self.pool1(x)
x = self.batch_norm2(x)
x = self.conv2(x)
x = tf.nn.relu(x)
x = self.pool2(x)
return x
if __name__ == '__main__':
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(*x_train.shape, 1)[:1000]
y_train = y_train.reshape(*y_train.shape, 1)[:1000]
x_test = x_test.reshape(*x_test.shape, 1)
y_test = y_test.reshape(*y_test.shape, 1)
y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)
model = Classifier()
inputs = tf.keras.Input((28, 28, 1))
x = model(inputs)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(10, activation="sigmoid")(x)
model = tf.keras.Model(inputs=inputs, outputs=x)
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
model.fit(x_train, y_train, epochs=1, shuffle=True)
model.save("./my_model")
错误信息:
1000/1000 [==============================] - 1s 1ms/step - loss: 4.6037 - acc: 0.7025
Traceback (most recent call last):
File "/home/user/Data/test/python/mnist/mnist_run.py", line 62, in <module>
model.save("./my_model")
File "/home/user/miniconda3/envs/ml3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1278, in save
save_model(self, filepath, overwrite, include_optimizer)
File "/home/user/miniconda3/envs/ml3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 101, in save_model
'config': model.get_config()
File "/home/user/miniconda3/envs/ml3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1049, in get_config
layer_config = layer.get_config()
File "/home/user/miniconda3/envs/ml3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1028, in get_config
raise NotImplementedError
NotImplementedError
Process finished with exit code 1
我查看了错误行,发现 get_config 方法检查 self._is_graph_network
有人处理这个问题吗?
谢谢!
更新 1:
在 keras 2.2.2 上(不是 tf.keras)
找到评论(用于模型保存)
文件:keras/engine/network.py
功能:get_config
子类网络不可序列化
#(除非序列化是由
# 子类网络的作者)。
所以,显然它不会工作……
我想知道,他们为什么不在 文档 中指出这一点(例如:“使用没有保存能力的子类!”)
更新 2:
在 keras 文档 中找到:
在子类模型中,模型的拓扑被定义为 Python 代码
(而不是作为层的静态图)。这意味着模型的
无法检查或序列化拓扑。结果,以下
方法和属性不适用于子类模型:
模型输入和模型输出。
model.to_yaml() 和 model.to_json()
model.get_config() 和 model.save()。
因此,无法使用子类化来保存模型。
可以只使用 Model.save_weights()
原文由 RedEyed 发布,翻译遵循 CC BY-SA 4.0 许可协议
张量流 2.2
感谢@cal 通知我新的 TensorFlow 支持保存自定义模型!
请参阅: 使用 Keras 保存和序列化模型 - 第二部分:子类模型的保存和加载
张量流 2.0
长话短说:
model.save()
用于自定义子类 keras 模型;save_weights()
和load_weights()
代替。在 Tensorflow 团队的帮助下,事实证明,保存自定义子类 Keras 模型的最佳做法是保存其权重并在需要时将其加载回来。
我们不能简单地保存 Keras 自定义子类模型的原因是它包含自定义代码,无法安全地序列化。但是,当我们具有相同的模型结构和自定义代码时,可以毫无问题地保存/加载权重。
Keras 的作者 Francois Chollet 写了一篇很棒的教程,介绍如何在 Colab 中的 Tensorflow 2.0 中保存/加载顺序/功能/Keras/自定义子类模型。在 保存子类模型 部分,它说: