I've created a model with custom layers, which have matrix operations and stuff like that. I want to now, after training, save my model. I've tried:
model.save("model.h5", save_format='tf')
But an error comes out:
NotImplementedError: Saving the model to HDF5 format requires the model to be
a Functional model or a Sequential model. It does not work for subclassed models,
because such models are defined via the body of a Python method, which isn't safely serializable.
Consider saving to the Tensorflow SavedModel format (by setting save_format="tf") or using `save_weights`.
I've found something that works:
checkpoint_path = "checkpoints"
ckpt = tf.train.Checkpoint(model=model,
optimizer=optimizer)
ckpt_manager = tf.train.CheckpointManager(ckpt, checkpoint_path, max_to_keep=5)
# if a checkpoint exists, restore the latest checkpoint.
if ckpt_manager.latest_checkpoint:
ckpt.restore(ckpt_manager.latest_checkpoint)
My question is: by using this way, I could do the same as saving a serializable model (like a Sequential Model), or this checkpoints is used for other purposes?
CodePudding user response:
There are actually two formats you can use to save your model. Either you simply save your model with the older Keras H5 format model.save("test", save_format='h5')
or you use the Tensorflow SavedModel format
by either explicitly setting model.save("test", save_format='tf')
or simply model.save("test")
, since the tf
format is used by default when you call model.save
. With model.save("model.h5", save_format='tf')
, you seem to be trying to use both formats at once, which does not look like it is working. Saving your model with the tf
format should work. More information can be found here. The following model, for example, can only be saved when using model.save
or model.save("test", save_format='tf')
:
import tensorflow as tf
class SomeModel(tf.keras.Model):
def __init__(self):
super(SomeModel, self).__init__()
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu, )
self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)
def call(self, inputs):
x = self.dense1(inputs)
return self.dense2(x)
model = SomeModel()
model.compute_output_shape(input_shape=(1,1))
model.save("model")
Calling model.save("test", save_format='h5')
or model.save("test.h5")
or even model.save("model.h5", save_format='tf')
on this subclassed model will in result in an error.
Checkpoints
are especially useful when you need to interrupt the training or it crashes and you want to resume training your model from a saved state. During inference, you could easily load the latest checkpoint of your model and make predictions without having to recompile it.