TensorFlow_6断点续训与Tensorboard可视化

TensorFlow学习系列:

TensorFlow_1参数初始化方法

TensorFlow_2学习率

TensorFlow_3激活函数

TensorFlow_4正则化

TensorFlow_5dropout

TensorFlow_6断点续训与Tensorboard可视化

TensorFlow_7优化器

TensorFlow_8损失函数

 

一,断点续训

       断点续训就是每训练N轮保存一次结果,如果中途有突发状况,导致训练中断,下次再训练时不至于从开始,而可以接着原来的训练的结果继续训练。

        在MNIST相关代码和验证码识别的代码里我也加入了断点续训功能,不过那个代码比较长,看起来比较麻烦,这里只留下比较核心的代码。

MODEL_SAVE_PATH = "./model/"
MODEL_NAME = "xxx_model"


saver = tf.train.Saver()

with tf.Session() as sess:
   init_op = tf.global_variables_initializer()
   sess.run(init_op)

   #加入断点续训功能
   ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
   if ckpt and ckpt.model_checkpoint_path:
      saver.restore(sess,ckpt.model_checkpoint_path)

   for i in range(STEPS):
      xs,ys = mnist.train.next_batch(BATCH_SIZE)
      _,loss_value,step = sess.run([train_op,loss,global_step],feed_dict={x:xs,y_:ys})
      if i % 100 == 0: #每隔100代打印一次结果,并保存一下模型
         print("Ater {} training step(s),loss on training batch is {} ".format(step,loss_value))
         saver.save(sess,os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step=global_step)

二,Tensorboard可视化

       在复杂的问题中,网络往往都是很复杂的。为了方便调试参数以及调整网络结构,我们需要将计算图可视化出来,以便能够更好的进行下一步的决策。Tensorflow提供了一个TensorBoard工具,可以满足上面的需求。

       TensorBoard是一个可视化工具,能够有效地展示Tensorflow在运行过程中的计算图、各种指标随着时间的变化趋势以及训练中使用到的数据信息。可以查看TensorBoard Github ReadMe 详细阅读适应方法。

代码:

import tensorflow as tf
import numpy as np

#输入数据
x_data = np.linspace(-1,1,300)[:, np.newaxis]
noise = np.random.normal(0,0.05, x_data.shape)
y_data = np.square(x_data)-0.5+noise

#输入层
with tf.name_scope('input_layer'): #输入层。将这两个变量放到input_layer作用域下,tensorboard会把他们放在一个图形里面
    xs = tf.placeholder(tf.float32, [None, 1], name = 'x_input') # xs起名x_input,会在图形上显示
    ys = tf.placeholder(tf.float32, [None, 1], name = 'y_input') # ys起名y_input,会在图形上显示

#隐层
with tf.name_scope('hidden_layer'): #隐层。将隐层权重、偏置、净输入放在一起
    with tf.name_scope('weight'): #权重
        W1 = tf.Variable(tf.random_normal([1,10]))
        tf.summary.histogram('hidden_layer/weight', W1)
    with tf.name_scope('bias'): #偏置
        b1 = tf.Variable(tf.zeros([1,10])+0.1)
        tf.summary.histogram('hidden_layer/bias', b1)
    with tf.name_scope('Wx_plus_b'): #净输入
        Wx_plus_b1 = tf.matmul(xs,W1) + b1
        tf.summary.histogram('hidden_layer/Wx_plus_b',Wx_plus_b1)
output1 = tf.nn.relu(Wx_plus_b1)

#输出层
with tf.name_scope('output_layer'): #输出层。将输出层权重、偏置、净输入放在一起
    with tf.name_scope('weight'): #权重
        W2 = tf.Variable(tf.random_normal([10,1]))
        tf.summary.histogram('output_layer/weight', W2)
    with tf.name_scope('bias'): #偏置
        b2 = tf.Variable(tf.zeros([1,1])+0.1)
        tf.summary.histogram('output_layer/bias', b2)
    with tf.name_scope('Wx_plus_b'): #净输入
        Wx_plus_b2 = tf.matmul(output1,W2) + b2
        tf.summary.histogram('output_layer/Wx_plus_b',Wx_plus_b2)
output2 = Wx_plus_b2

#损失
with tf.name_scope('loss'): #损失
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-output2),reduction_indices=[1]))
    tf.summary.scalar('loss',loss)
with tf.name_scope('train'): #训练过程
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

#初始化
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
merged = tf.summary.merge_all() #将图形、训练过程等数据合并在一起
writer = tf.summary.FileWriter('logs',sess.graph) #将训练日志写入到logs文件夹下

#训练
for i in range(1000):
    sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
    if(i%50==0): #每50次写一次日志
        result = sess.run(merged,feed_dict={xs:x_data,ys:y_data}) #计算需要写入的日志数据
        writer.add_summary(result,i) #将日志数据写入文件

 运行cmd命令行,先转到log所在的盘,我这是“e”盘,然后输入

tensorboard --logdir="不含中文的log详细路径"

再将弹出来的网址复制到“谷歌浏览器”(其他浏览器不见得好使,你可以试试)

 

欢迎扫码关注我的微信公众号

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值