tensorflow 1.x practical tutorial (8) - learning rate decay and display variable changes in TensorBoard

Target

This article aims to introduce the introductory knowledge points and practical examples of tensorflow. I hope that all novice students can be proficient in tensorflow related operations after learning.

Manually adjust the learning rate code

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST", one_hot=True)
batch_size = 64
n_batches = mnist.train.num_examples // batch_size

def variable_info(var): # 将所有变量都存起来,一会可以在 tensorboard 的图中观察其动态
    with tf.name_scope('summaries'):
        mean_value = tf.reduce_mean(var)
        tf.summary.scalar('mean', mean_value)
        with tf.name_scope('stddev'):
            stddev_value = tf.sqrt(tf.reduce_mean(tf.square(var - mean_value)))
        tf.summary.scalar('stddev', stddev_value)
        tf.summary.scalar('max', tf.reduce_max(var))
        tf.summary.scalar('min', tf.reduce_min(var))
        tf.summary.histogram('histogram',var)

with tf.name_scope("input_layer"):
    x = tf.placeholder(tf.float32, [None, 784])
    y = tf.placeholder(tf.float32, [None, 10])
    keep_prob = tf.placeholder(tf.float32)
    lr = tf.Variable(0.01,tf.float32) # 这里的学习率设置为变量
    tf.summary.scalar('learning_rate',lr) # 将 lr 存起来,一会可以在 tensorboard 的图中观察其动态
    
with tf.name_scope('network'): # 搭建网络结构
    with tf.name_scope("weights"):
        w = tf.Variable(tf.truncated_normal([784,10], stddev=0.1), name='w')
        variable_info(w)
    with tf.name_scope('baises'):
        b = tf.Variable(tf.zeros([10]) + 0.1, name="b")
        variable_info(b)
    with tf.name_scope('xw_plus_b'):
        a = tf.matmul(x,w) + b
    with tf.name_scope('softmax'):
        out = tf.nn.softmax(a)

with tf.name_scope("loss_train"): # 计算损失值,定义优化器
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out, labels=y))
    train_step = tf.train.AdamOptimizer(lr).minimize(loss)
    tf.summary.scalar("loss", loss) # 将 loss 存起来,一会可以在 tensorboard 的图中观察其动态

with tf.name_scope("eval"): # 计算准确率,评估模型效果
    with tf.name_scope("correct"):
        correct = tf.equal(tf.argmax(out, 1), tf.argmax(y, 1))
    with tf.name_scope("accuracy"):
        accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
        tf.summary.scalar('accuracy',accuracy)
        
init = tf.global_variables_initializer()
merged = tf.summary.merge_all()

with tf.Session() as sess:
    sess.run(init)
    writer = tf.summary.FileWriter('tflogs/', sess.graph) # 将结果文件都存入目标目录中,使用 tensorboard 工具可以查看
    for epoch in range(20):
        sess.run(tf.assign(lr, 0.001 * (0.95 ** epoch)))
        for batch in range(n_batches):
            batch_x, batch_y = mnist.train.next_batch(batch_size)
            summary, _ = sess.run([merged, train_step], feed_dict = {x:batch_x, y:batch_y, keep_prob:0.5})
            writer.add_summary(summary, epoch * n_batches + batch)
        loss_value, acc, lr_value = sess.run([loss, accuracy, lr], feed_dict = {x:mnist.test.images, y:mnist.test.labels, keep_prob:1.0})
        print("Iter: ", epoch, "Loss: ", loss_value, "Acc: ", acc, "lr: ", lr_value)
        
复制代码

result output

Iter:  0 Loss:  1.6054871 Acc:  0.895 lr:  0.001
Iter:  1 Loss:  1.5699576 Acc:  0.9148 lr:  0.00095
Iter:  2 Loss:  1.5598879 Acc:  0.9195 lr:  0.0009025
Iter:  3 Loss:  1.5546178 Acc:  0.9215 lr:  0.000857375
Iter:  4 Loss:  1.5502373 Acc:  0.9254 lr:  0.00081450626
Iter:  5 Loss:  1.5473799 Acc:  0.9269 lr:  0.0007737809
Iter:  6 Loss:  1.5452079 Acc:  0.9277 lr:  0.0007350919
Iter:  7 Loss:  1.5434842 Acc:  0.9294 lr:  0.0006983373
Iter:  8 Loss:  1.5427189 Acc:  0.9278 lr:  0.0006634204
Iter:  9 Loss:  1.5417348 Acc:  0.9293 lr:  0.0006302494
Iter:  10 Loss:  1.540729 Acc:  0.9293 lr:  0.0005987369
Iter:  11 Loss:  1.5403976 Acc:  0.9298 lr:  0.0005688001
Iter:  12 Loss:  1.5395288 Acc:  0.9301 lr:  0.0005403601
Iter:  13 Loss:  1.5395651 Acc:  0.9298 lr:  0.0005133421
Iter:  14 Loss:  1.5387015 Acc:  0.9307 lr:  0.000487675
Iter:  15 Loss:  1.5383359 Acc:  0.9308 lr:  0.00046329122
Iter:  16 Loss:  1.5379355 Acc:  0.931 lr:  0.00044012666
Iter:  17 Loss:  1.5374689 Acc:  0.9314 lr:  0.00041812033
Iter:  18 Loss:  1.5376941 Acc:  0.9305 lr:  0.00039721432
Iter:  19 Loss:  1.5371386 Acc:  0.9308 lr:  0.0003773536   
复制代码

point one

After the above process is executed, if you want to see the changes of the saved data in the figure, you can follow the steps below to

  • pip install tensorboard==1.10.0
  • Then the command line runs ./tensorboard --logdir=saved absolute location
  • Open http://localhost:6006/ in the browser to see the corresponding parameter changes, which is very convenient

point two

Adjusting the learning rate helps to train the model. At the beginning, use a larger learning rate to ensure the rapid convergence of the model effect. When it converges to the vicinity of the optimal point, try to reduce the learning rate to avoid back and forth oscillations. The most common learning rate decay method used here is exponential decay.

refer to

Reference for this article: blog.csdn.net/qq_19672707…

Guess you like

Origin juejin.im/post/7086660381753475102