TensorFlow学习(五)mnist简单实现模型+tensorboard

一、简单模型

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

#载入数据集
mnist=input_data.read_data_sets('D:\\ATest\\MNIST_data',one_hot=True)
#每个批次的大小
batch_size=100
#计算一共有多少个批次
n_batch=mnist.train.num_examples

x=tf.placeholder(tf.float32,[None,784])
y=tf.placeholder(tf.float32,[None,10])

#创建一个简单的神经网络
W=tf.Variable(tf.zeros([784,10]))
b=tf.Variable(tf.zeros([10]))
prediction=tf.nn.softmax(tf.matmul(x,W)+b)

#二次代价函数
loss=tf.reduce_mean(tf.square(y-prediction))
#使用梯度下降法
train_step=tf.train.GradientDescentOptimizer(0.2).minimize((loss))

#初始化变量
init=tf.global_variables_initializer()

#结果存在一个布尔类型列表中
correct_prediction=tf.equal(tf.arg_max(y,1),tf.argmax(prediction,1))
#求准确率
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(21):
        for batch in range(n_batch):
            batch_xs,batch_ys=mnist.train.next_batch((batch_size))
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
        acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter "+str(epoch)+", Tseting Accuracy "+str(acc))
Iter 0, Tseting Accuracy 0.9256
Iter 1, Tseting Accuracy 0.9291
Iter 2, Tseting Accuracy 0.9299
Iter 3, Tseting Accuracy 0.9306
Iter 4, Tseting Accuracy 0.9312
Iter 5, Tseting Accuracy 0.9311
Iter 6, Tseting Accuracy 0.9312
Iter 7, Tseting Accuracy 0.9308
Iter 8, Tseting Accuracy 0.9307
Iter 9, Tseting Accuracy 0.9303
Iter 10, Tseting Accuracy 0.9297
Iter 11, Tseting Accuracy 0.9301
Iter 12, Tseting Accuracy 0.9299
Iter 13, Tseting Accuracy 0.9298
Iter 14, Tseting Accuracy 0.9302
Iter 15, Tseting Accuracy 0.9302
Iter 16, Tseting Accuracy 0.93
Iter 17, Tseting Accuracy 0.9311
Iter 18, Tseting Accuracy 0.9312
Iter 19, Tseting Accuracy 0.931
Iter 20, Tseting Accuracy 0.9318

二、tensorboard
tensorboard是TensorFlow自带的可视化工具

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

#载入数据集
mnist=input_data.read_data_sets('D:\\ATest\\MNIST_data',one_hot=True)
#每个批次的大小
batch_size=100
#计算一共有多少个批次
n_batch=mnist.train.num_examples

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('layer'):
    #创建一个简单的神经网络
    with tf.name_scope('weights'):
        W=tf.Variable(tf.zeros([784,10]),name='W')
    with tf.name_scope('biases'):
        b=tf.Variable(tf.zeros([10]),name='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)

with tf.name_scope('loss'):
    #二次代价函数
    loss=tf.reduce_mean(tf.square(y-prediction))
with tf.name_scope('train'):
    #使用梯度下降法
    train_step=tf.train.GradientDescentOptimizer(0.2).minimize((loss))

#初始化变量
init=tf.global_variables_initializer()

with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        #结果存在一个布尔类型列表中
        correct_prediction=tf.equal(tf.arg_max(y,1),tf.argmax(prediction,1))
    with tf.name_scope('accuracy'):
        #求准确率
        accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:
    sess.run(init)
    writer=tf.summary.FileWriter('logs/',sess.graph)#将该图写入文件中
    for epoch in range(1):#训练次数修改为1次
        for batch in range(n_batch):
            batch_xs,batch_ys=mnist.train.next_batch((batch_size))
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
        acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter "+str(epoch)+", Tseting Accuracy "+str(acc))

①命名块
②将图写入文件
TensorFlow学习(五)mnist简单实现模型+tensorboard_第1张图片
得到的程序文件
③在cmd中运行tensorboard
ps:如果tensorboard显示不是内部或外部命令也不是可执行文件时,就要把tensorboard.exe所在的文件的路径添加到Path中
TensorFlow学习(五)mnist简单实现模型+tensorboard_第2张图片
将tensorboard运行得到的网址复制到GoogleChrome中,即可得到graph的流程
TensorFlow学习(五)mnist简单实现模型+tensorboard_第3张图片
nice!

你可能感兴趣的:(TensorFlo)