【发布时间】:2021-11-21 12:55:19
【问题描述】:
我创建了一个带有自定义层的模型,其中包含矩阵运算和类似的东西。我现在想在训练后保存我的模型。我试过了:
model.save("model.h5", save_format='tf')
但是出现了错误:
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`.
我发现了一些有用的东西:
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)
我的问题是:通过这种方式,我可以做与保存可序列化模型(如序列模型)相同的操作,还是将此检查点用于其他目的?
【问题讨论】:
标签: python tensorflow tensorflow2.0