tensorflow MNIST数据集的训练(线性模型)及tensorboard计算结果可视化

#下载MNIST数据
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
#参数设置
import tensorflow as tf
x = tf.placeholder(tf.float32,[None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W)+b)
y_ = tf.placeholder(tf.float32, [None, 10])
#交叉熵
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
#梯度下降法最小化交叉熵
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
#sess图表
sess = tf.InteractiveSession()
#初始化所有参数
tf.global_variables_initializer().run()
for _ in range(1000):
   batch_xs, batch_ys = mnist.train.next_batch(100)
   sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
#精度预测
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#tensorboard
writer = tf.summary.FileWriter('./graphs',sess.graph)
#sess.run
sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

运行方法:
命令提示符下运行

python name.py
tensorboard --logdir="./graphs" --port 6006

chrome浏览器打开localhost:6006
tensorflow MNIST数据集的训练(线性模型)及tensorboard计算结果可视化_第1张图片
tensorflow MNIST数据集的训练(线性模型)及tensorboard计算结果可视化_第2张图片
可视化后整个计算过程一目了然,包括softmax regression回归,Wx+b等等操作。计算的步骤如下所示。softmax的主要作用在于将计算结果转化成概率。

evidencei=jWi,jxj+bi

y=softmax(evidence)

softmax(x)i=exp(xi)jexp(xj)

你可能感兴趣的:(python,tensorflow,tensorboar,python,tensorflow,tboard)