Lenet-5 MNIST Tensorboard 参数可视化 直方图 tf.Summary.histogram,tf.Summary.scalar,tf.Summary.image

后边增加了Lenet-5实现MNIST识别的可视化代码

先上出图:这里因为只训练了3000次,可能并没有达到最优,仅作示意。
Lenet-5 MNIST Tensorboard 参数可视化 直方图 tf.Summary.histogram,tf.Summary.scalar,tf.Summary.image_第1张图片
Lenet-5 MNIST Tensorboard 参数可视化 直方图 tf.Summary.histogram,tf.Summary.scalar,tf.Summary.image_第2张图片
Lenet-5 MNIST Tensorboard 参数可视化 直方图 tf.Summary.histogram,tf.Summary.scalar,tf.Summary.image_第3张图片
Lenet-5 MNIST Tensorboard 参数可视化 直方图 tf.Summary.histogram,tf.Summary.scalar,tf.Summary.image_第4张图片
Lenet-5 MNIST Tensorboard 参数可视化 直方图 tf.Summary.histogram,tf.Summary.scalar,tf.Summary.image_第5张图片
上个代码,前提先下载MNIST数据集,MNIST四个
二注意改代码里两个目录,一个mnist数据存放目录,一个存放tensorboard文件目录

# -*- coding: utf-8 -*-
"""
Created on Thu Jan 25 15:32:27 2018

@author: Administrator
"""

# filename:ts09.04.py # 监控指标可视化

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

# 1. 生成变量监控信息并定义生成监控信息日志的操作
# C:\Python35>tensorboard --port=6006 --debug --logdir=c:/python35/tensorlog/show04
SUMMARY_DIR = "D:\pyprogram\google_frame\para"
BATCH_SIZE = 100
TRAIN_STEPS = 3000

def variable_summaries(var, name):
    with tf.name_scope('summaries'):
        tf.summary.histogram(name, var)
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean/' + name, mean)
        stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
        tf.summary.scalar('stddev/' + name, stddev)

# 2. 生成一层全链接的神经网络
def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu):
    with tf.name_scope(layer_name):
        with tf.name_scope('weights'):
            weights = tf.Variable(tf.truncated_normal([input_dim, output_dim], stddev=0.1))
            variable_summaries(weights, layer_name + '/weights')
        with tf.name_scope('biases'):
            biases = tf.Variable(tf.constant(0.0, shape=[output_dim]))
            variable_summaries(biases, layer_name + '/biases')
        with tf.name_scope('Wx_plus_b'):
            preactivate = tf.matmul(input_tensor, weights) + biases
            tf.summary.histogram(layer_name + '/pre_activations', preactivate)
        activations = act(preactivate, name='activation')

        # 记录神经网络节点输出在经过激活函数之后的分布。
        tf.summary.histogram(layer_name + '/activations', activations)
        return activations


def main():
    mnist = input_data.read_data_sets("D:\pyprogram", one_hot=True)

    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, [None, 784], name='x-input')
        y_ = tf.placeholder(tf.float32, [None, 10], name='y-input')

    with tf.name_scope('input_reshape'):
        image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])
        tf.summary.image('input', image_shaped_input, 10)

    hidden1 = nn_layer(x, 784, 500, 'layer1')
    y = nn_layer(hidden1, 500, 10, 'layer2', act=tf.identity)

    with tf.name_scope('cross_entropy'):
        cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y, labels=y_))
        tf.summary.scalar('cross_entropy', cross_entropy)

    with tf.name_scope('train'):
        train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)

    with tf.name_scope('accuracy'):
        with tf.name_scope('correct_prediction'):
            correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        with tf.name_scope('accuracy'):
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        tf.summary.scalar('accuracy', accuracy)

    merged = tf.summary.merge_all()

    with tf.Session() as sess:
        summary_writer = tf.summary.FileWriter(SUMMARY_DIR, sess.graph)
        tf.global_variables_initializer().run()

        for i in range(TRAIN_STEPS):
            xs, ys = mnist.train.next_batch(BATCH_SIZE)
            # 运行训练步骤以及所有的日志生成操作,得到这次运行的日志。
            summary, _ = sess.run([merged, train_step], feed_dict={x: xs, y_: ys})
            # 将得到的所有日志写入日志文件,这样TensorBoard程序就可以拿到这次运行所对应的
            # 运行信息。
            summary_writer.add_summary(summary, i)

    summary_writer.close()

if __name__ == '__main__':
    main()

执行完,在存放
events.out.tfevents.1516861583.MS-20170531FUXG
等文件的目录上层,shift+右键,打开cmd
输入:

tensorboard --logdir =D:\pyprogram\google_frame\para

这个是我的,自己根据代码中的位置改。
打开google,输入localhost:6006
出现。

这里用到的是tf.summary的类,主要包括 tf.summary.histogram,tf.summary.scalar, tf.summary.image, tf.summary.FileWriter(分别生成日志)
tf.summary.merge_all,(整理日志操作的,sess.run一次就不用对上述分别run)

tf.summary.histogram生成Histogram 和distribution

分布图

Lenet-5 MNIST Tensorboard 参数可视化 直方图 tf.Summary.histogram,tf.Summary.scalar,tf.Summary.image_第6张图片
图表上的每一行表示某个百分位数的数据分布随时间变化:,一共九条。最下方线显示最小值如何随时间变化,从上到下的几条线分别是 [maximum, μ+1.5σ, μ+σ, μ+0.5σ, μ, μ-0.5σ, μ-σ, μ-1.5σ, minimum]
([maximum, 93%, 84%, 69%, 50%, 31%, 16%, 7%, minimum])

直方图

Lenet-5 MNIST Tensorboard 参数可视化 直方图 tf.Summary.histogram,tf.Summary.scalar,tf.Summary.image_第7张图片
直方图有两种显示模式:OVERLAY和OFFSET:
图为offset
各轴坐标含义:

横轴:变量的value值,符合某个分布的(eg正态)
纵轴:从上到下训练step
鼠标停在图片会显示数量(这个变量有多少次(约)等于这个value)

OVERLAY(相当于3dim->2dim,压扁)
各轴含义如下:
横轴:值
纵轴:数量
不同的线代表不同的时间/step。如果较晚的线与较早的线重合,就以覆盖方式画线。
将鼠标停留在一个点上,会加黑显示三个数字,含义是:在step xxx1时,有xxx2个元素的值(约等于)xxx3。
embedding(PCA)没不懂,做什么用??
其他比较好理解。
ps:图像貌似只输出最近一次的调用,在研究如何像caffe那种特征图可视化,如果有大神会的话,请指条路,谢谢~

代码分析:

生成日志,给我的感觉是,
在原本训练的基础上,把需要可视化的参数外层加一个name_scope,
主要把握命名的层次,可以对应代码和图的名字,清楚整个结构。

reference(后三个是博客):

gihub tensorflow(v1)
[2]
[3]
[4]

训练过程文件

# -*- coding: utf-8 -*-
"""
Created on Tue Jan 23 19:22:01 2018

@author: Administrator
"""

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference
import os
import numpy as np

BATCH_SIZE=100
LEARNING_RATE_BASE=0.8
LEARNING_RATE_DECAY=0.99
REGULARAZTION_RATE=0.0001
TRAINING_STEPS=3000
MOVING_AVERAGE_DECAY=0.99

MODEL_SAVE_PATH="D:\pyprogram\google_frame"
MODEL_NAME="MODEL.ckpt"

def train(mnist):
    x=tf.placeholder(tf.float32,[BATCH_SIZE,mnist_inference.IMAGE_SIZE,
                                 mnist_inference.IMAGE_SIZE,
                                 mnist_inference.NUM_CHANNELS],name='x-input')
    y_=tf.placeholder(tf.float32,[None,mnist_inference.OUTPUT_NODE],name='y-input')

    regularizer=tf.contrib.layers.l2_regularizer(REGULARAZTION_RATE)
    #forward calculate
    y=mnist_inference.inference(x,regularizer)

    global_step=tf.Variable(0,trainable=False)
    #loss
    variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    variables_averages_op = variable_averages.apply(tf.trainable_variables())
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
    cross_entropy_mean = tf.reduce_mean(cross_entropy)
    loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses'))
    learning_rate = tf.train.exponential_decay(
        LEARNING_RATE_BASE,
        global_step,
        mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY,
        staircase=True)
    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
    with tf.control_dependencies([train_step, variables_averages_op]):
        train_op = tf.no_op(name='train')
    #持久化类
    saver=tf.train.Saver()
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        for i in range(TRAINING_STEPS):
            xs, ys = mnist.train.next_batch(BATCH_SIZE)
            reshaped_xs=np.reshape(xs,(BATCH_SIZE,
                                       mnist_inference.IMAGE_SIZE,
                                       mnist_inference.IMAGE_SIZE,
                                       mnist_inference.NUM_CHANNELS))
            loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: reshaped_xs, y_: ys})
            if i % 1000 == 0:
                print("After %d training step(s), loss on training batch is %g." % (step, loss_value))
                saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
def main(argv=None):
    mnist=input_data.read_data_sets("D:\pyprogram",one_hot=True)
    train(mnist)
if __name__ == '__main__':
    main()


前向传播代码

# -*- coding: utf-8 -*-
"""
Created on Tue Jan 23 17:33:54 2018

@author: Administrator
"""

import tensorflow as tf
#para
INPUT_NODE=784
OUTPUT_NODE=10
LAYER1_NODE=500

#创建变量,训练时创建,测试时加载,
def get_weight_variable(shape,regularizer):
    weights=tf.get_variable("weights",shape,initializer=tf.truncated_normal_initializer(stddev=0.1))
    if regularizer!=None:
        tf.add_to_collection("losses",regularizer(weights))
    return weights

def inference(input_tensor,regularizer):
    #1st layer
    with tf.variable_scope('layer1'):
        weights=get_weight_variable([INPUT_NODE,LAYER1_NODE],regularizer)
        biases=tf.get_variable('biases',[LAYER1_NODE],initializer=tf.constant_initializer(0.0))
        layer1=tf.nn.relu(tf.matmul(input_tensor,weights)+biases)

    with tf.variable_scope('layer2'):
        weights=get_weight_variable([LAYER1_NODE,OUTPUT_NODE],regularizer)
        biases=tf.get_variable('biases',[OUTPUT_NODE],initializer=tf.constant_initializer(0.0))
        layer2=tf.nn.relu(tf.matmul(layer1,weights)+biases)        

    return layer2

你可能感兴趣的:(tensorflow)