tensorflow学习——tf.train.Supervisor()与tf.train.saver()

本文介绍如何使用TensorFlow进行线性模型训练,通过tf.train.Supervisor和tf.train.Saver管理模型训练过程,包括自动加载checkpoint、保存checkpoint和Summary,简化了模型训练流程。
摘要由CSDN通过智能技术生成

1、tf.train.Supervisor()

import tensorflow as tf
import numpy as np
import os

log_path = 'ckptdir/'
log_name = 'liner.ckpt'
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3

w = tf.Variable(tf.random_normal([1]))
b = tf.Variable(tf.zeros([1]))
y = w*x_data + b

loss = tf.reduce_mean(tf.square(y-y_data))
train = tf.train.AdamOptimizer(0.5).minimize(loss)

tf.summary.scalar('loss', loss)

saver = tf.train.Saver()
init = tf.global_variables_initializer()
merged = tf.summary.merge_all()

sv = tf.train.Supervisor(logdir=log_path, init_op=init)  # logdir用来保存checkpoint和summary
saver = sv.saver   # 创建saver

with sv.managed_session() as sess:  # 会自动去logdir中去找checkpoint,如果没有的话,自动执行初始化
#    sess.run(init)
#    if len(os.listdir(log_path)) != 0:
#        saver.restore(sess, os.path.join(log_path, log_name))
    for step in range(201):
        sess.run(train)
        if step%50 == 0:
            print(step, sess.run(w), sess.run(b))
            merged_summary = sess.run(merged)
            sv.summary_computed(sess, merged_summary,global_step=step)
    saver.save(sess, os.path.join(log_path, 'liner.ckpt'))

从上面代码可以看出,Supervisor帮助我们处理一些事情
(1)自动去checkpoint加载数据或初始化数据
(2)自身有一个Saver,可以用来保存checkpoint
(3)有一个summary_computed用来保存Summary
所以,我们就不需要:
(1)手动初始化或从checkpoint中加载数据
(2)不需要创建Saver,使用sv内部的就可以
(3)不需要创建summary writer
2、tf.train.Saver()

import tensorflow as tf
import numpy as np
import os

log_path = 'ckptdir'
log_name = 'liner.ckpt'
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3

w = tf.Variable(tf.random_normal([1]))
b = tf.Variable(tf.zeros([1]))
y = w*x_data + b

loss = tf.reduce_mean(tf.square(y-y_data))
train = tf.train.AdamOptimizer(0.5).minimize(loss)
tf.summary.scalar('loss', loss)

saver = tf.train.Saver()
init = tf.global_variables_initializer()
merged = tf.summary.merge_all()

with tf.Session() as sess:
    sess.run(init)
    print("loading model from checkpoint")
    checkpoint = tf.train.latest_checkpoint(os.path.join(log_path, log_name))
    restore_saver.restore(sess, checkpoint)
    #if len(os.listdir(log_path)) != 0:
    #    saver.restore(sess, os.path.join(log_path, log_name))
    for step in range(201):
        sess.run(train)
        if step%50 ==0:
            print(step, sess.run(w), sess.run(b))
    summary_writer = tf.summary.FileWriter(log_path, sess.graph)
    summary_all = sess.run(merged)
    summary_writer.add_summary(summary_all)
    summary_writer.close()

    saver.save(sess, os.path.join(log_path, 'liner.ckpt'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值