Tensorflow 1.0:mnist及tensorboard实例

Tensorflow 1.0 mnist

  • mnist 简单实例
  • summary 用法
  • tensorboard 展示
# -*- coding: utf-8 -*-
"""
Created on Mon Apr  3 19:15:24 2017

@author: Jhy_BUPT
README:
    
REF:
    
"""
import tensorflow as tf
# 载入数据
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("C:\\tmp\\data", one_hot=True)

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

W = tf.Variable(tf.zeros([784, 10]), name='weight')
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 = tf.InteractiveSession()
tf.global_variables_initializer().run()

for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    fd = {x: batch_xs, y_: batch_ys}
    sess.run(train_step, feed_dict=fd)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
fd2 = {x: mnist.test.images, y_: mnist.test.labels}
print(sess.run(accuracy, feed_dict=fd2))
 # 总结graph ,并为tensorboard调用做准备
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter('C:\\tmp\\d44', sess.graph)

开启tensorboard

Paste_Image.png

查看tensorboard

Tensorflow 1.0:mnist及tensorboard实例_第1张图片
Paste_Image.png

你可能感兴趣的:(Tensorflow 1.0:mnist及tensorboard实例)