saver = tf.train.Saver() 模型参数的保存与恢复

参考模型的保存与恢复SAVER

里面讲解了

saver = tf.train.Saver(max_to_keep=5)

saver.save(sess,'filepath,global_step=step)

tf.train.latest_checkpoint(checkpoint_dir, latest_filename=None)

saver.restore(sess, save_path)

 

加载模型参数

def load_weights(checkpoint_dir, sess, saver):
    """
    Load the weights of a model stored in saver.

    Parameters
    ----------
    checkpoint_dir : str
        The directory of checkpoints.
    sess : tf.Session
        A Session to use to restore the parameters.
    saver : tf.train.Saver

    Returns
    -----------
    int
        training step of checkpoint
    """
    ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
    if ckpt and ckpt.model_checkpoint_path:
        logging.info(ckpt.model_checkpoint_path)
        file = os.path.basename(ckpt.model_checkpoint_path)
        checkpoint_path = os.path.join(checkpoint_dir, file)
        saver.restore(sess, checkpoint_path)
        return int(file.split('-')[1])

ckpt = tf.train.get_checkpoint_state(checkpoint_dir)

ckpt.model_checkpoint_path和ckpt.all_model_checkpoint_paths

该函数返回的是checkpoint文件CheckpointState proto类型的内容,其中有model_checkpoint_path和all_model_checkpoint_paths两个属性,里面的内容都是路径。其中model_checkpoint_path保存了最新的tensorflow模型文件的文件名,all_model_checkpoint_paths则有未被删除的所有tensorflow模型文件的文件名。如果没有checkpoint文件那么返回None。点击查看具体例子

os.path.basename(ckpt.model_checkpoint_path)

Returns the final component of a pathname也就是返回路径最后的那部分(路径是以'/'为界限划块的),相当于ckpt.model_checkpoint_path.split('/')[-1]

saver.restore(sess, checkpoint_path)

sess:表示当前会话,之前保存的结果将被加载入这个会话;checkpoint_path:表示模型存储的位置,不需要提供模型的名字,它会去查看checkpoint文件,看看最新的是谁,叫做什么

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值