tensorflow(9)tensorboard网络评价标准

评价标准可视化代码:

  • 主要是添加#参数概要 。
  • 在运行出错的时候,利用菜单栏中的 Kernel中的“Restart & Run All
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data#载入数据(可以是某盘的绝对路径)(我的数据存储在运行路径下)
#可视化(暂时不关心结果,只关心结构)
#载入数据
mnist = input_data.read_data_sets('MNIST_data',one_hot = True)

#每个批次100张照片 每个批次的大小
batch_size = 100
 
#计算一共有多少个批次 (整除符号)
n_batch = mnist.train.num_examples // batch_size

#参数概要,传入一个参数,分别计算以下结果
def variable_summaries(var):
    with tf.name_scope('summaries'):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean',mean)#平均值,给参数一个名字
        with tf.name_scope('stddev'):
            stddev=tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
        tf.summary.scalar('stddev',stddev)#标准差
        tf.summary.scalar('max',tf.reduce_max(var))#最大值
        tf.summary.scalar('min',tf.reduce_min(var))#最小值
        tf.summary.histogram('histogram',var)#直方图
                   

#定义一个命名空间,x,y注意缩进
with tf.name_scope('input'):
    #定义两个placeholder,None=100批次
    x = tf.placeholder(tf.float32,[None,784],name='x-input')
    y = tf.placeholder(tf.float32,[None,10],name='y-input')
    
with tf.name_scope('layer'):
    #创建一个简单的神经网络(这里只是2层)
    #输入层784个神经元,输出层10个神经元
    with tf.name_scope('weights'):
        W = tf.Variable(tf.zeros([784,10]),name='W')
        variable_summaries(W)#
    with tf.name_scope('biases'):
        b = tf.Variable(tf.zeros([10]),name='b')
        variable_summaries(b)#
    with tf.name_scope('wx_plus_b'):
        wx_plus_b=tf.matmul(x,W)+b
    with tf.name_scope('softmax'):
        prediction = tf.nn.softmax(wx_plus_b)#得到很多概率(对应标签的10个概率)
        
# loss = tf.reduce_mean(tf.square(y-prediction)) 正确率是91.34%
#另一种损失(交叉熵函数)如果输出神经元是S型的,适合用交叉熵函数(对数似然函数) 正确率是92.17% 
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
    tf.summary.scalar('loss',loss)#数据比较多的时候调用variable_summaries(),在这里因为loss只有一个 就没必要调用
with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#初始化变量
init = tf.global_variables_initializer()
 
    
#定义一个求准确率的方法
#结果存放在一个布尔型列表中(比较两个参数是否相等,是返回true)
#tf.argmax(prediction,1)返回最大的值(概率是在哪个位置)所在的位置,标签是几
#tf.argmax(y,1) one-hot方法对应的是否是1  就是对应的标签
with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
    #求准确率(bool类型是true和false)转化为浮点型  显示1的和总的数据的比值就是准确率
    with tf.name_scope('accuracy'):
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
        tf.summary.scalar('accuracy',accuracy)
            
#合并所有的summary(所有检测的指标进行合并)
merged=tf.summary.merge_all()
 
 
with tf.Session() as sess:
    sess.run(init)
    writer=tf.summary.FileWriter('logs/',sess.graph)#将图的结构存储在当前目录中
    for epoch in range(30):
        for batch in range(n_batch):
            batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
            summary,_=sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys})#一遍训练,一边统计之前的loss等值
            
        writer.add_summary(summary,epoch)#summery写到文件内   
        acc = sess.run(accuracy,feed_dict = {x:mnist.test.images,y:mnist.test.labels})
        print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))
        
        
        
  • 展现方式如文章(9)的讲解, 部分结果展示:

  • 如果想在指标中显示更多的点,则修改代码:
for i in range(2001):
    batch_xs,batch_ys =  mnist.train.next_batch(100)
    summary,_=sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys})#一遍训练,一边统计之前的loss等值
    if(i%500==0)           
        print(sess.run(accuracy,feed_dict = {x:mnist.test.images,y:mnist.test.labels}))
        

猜你喜欢

转载自blog.csdn.net/zhaoshuling1109/article/details/81503573