代码如下
import tensorflow as tf
import random
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
tf.set_random_seed(1)
mnist = input_data.read_data_sets(r'MNIST_data',one_hot=True)
learning_rate = 0.03
n_timmes = 5
batch_size = 100
X = tf.placeholder(tf.float32,shape=[None,784],name='X')
Y = tf.placeholder(tf.float32,shape=[None,10],name='Y')
keep_prob = tf.placeholder(tf.float32)
X_img = tf.reshape(X,shape=[-1,28,28,1])
tf.summary.image('input',X_img)
with tf.variable_scope('layer1') as scope:
W1 = tf.Variable(tf.random_normal([3,3,1,32],stddev=0.01),name='W1')
l1 = tf.nn.conv2d(X_img,W1,strides=[1],padding='SAME')
l1 = tf.nn.relu(l1)
l1 = tf.nn.max_pool(l1,[1,2,2,1],[1,2,2,1],'SAME')
l1 = tf.nn.dropout(l1,keep_prob=keep_prob)
tf.summary.histogram('W',W1)
tf.summary.histogram('l',l1)
tf.summary.histogram('X_img',X_img)
with tf.variable_scope('layer2') as scope:
W2 = tf.Variable(tf.random_normal([3, 3, 32,64], stddev=0.01), name='W2')
l2 = tf.nn.conv2d(l1, W2, strides=[1], padding='SAME')
l2 = tf.nn.relu(l2)
l2 = tf.nn.max_pool(l2, [1, 2, 2, 1], [1, 2, 2, 1], 'SAME')
l2 = tf.nn.dropout(l2, keep_prob=keep_prob)
tf.summary.histogram('W', W2)
tf.summary.histogram('l2', l2)
tf.summary.histogram('l', l1)
with tf.variable_scope('layer3') as scope:
W3 = tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.01), name='W3')
l3 = tf.nn.conv2d(l2, W3, strides=[1], padding='SAME')
l3 = tf.nn.relu(l3)
l3 = tf.nn.max_pool(l3, [1, 2, 2, 1], [1, 2, 2, 1], 'SAME')
l3 = tf.nn.dropout(l3, keep_prob=keep_prob)
l3_flat = tf.reshape(l3,shape=[-1,4*4*128])
tf.summary.histogram('W', W2)
tf.summary.histogram('l2', l2)
tf.summary.histogram('l', l1)
tf.summary.histogram('l3_flat', l3_flat)
with tf.variable_scope('layer4') as scope:
W4 = tf.get_variable("W4",shape=[4*4*128,50],initializer=tf.contrib.layers.xavier_initializer())
b4 = tf.Variable(tf.random_normal([50]), name='b4')
l4 = tf.nn.relu(tf.matmul(l3_flat,W4) + b4)
l4 = tf.nn.dropout(l4,keep_prob=keep_prob)
tf.summary.histogram('W', W4)
tf.summary.histogram('l4', l4)
tf.summary.histogram('l', l3_flat)
tf.summary.histogram('b4', b4)
with tf.variable_scope('layer5') as scope:
W5 = tf.get_variable("W5",shape=[50,10],initializer=tf.contrib.layers.xavier_initializer())
b5 = tf.Variable(tf.random_normal([10]), name='b5')
l5 = tf.nn.softmax(tf.matmul(l4,W5) + b5)
tf.summary.histogram('W', W5)
tf.summary.histogram('l5', l5)
tf.summary.histogram('l', l4)
tf.summary.histogram('b5', b5)
cost = - tf.reduce_mean(tf.reduce_sum(Y * tf.log(l5),1))
tf.summary.scalar('loss',cost)
acc = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(l5,1),tf.argmax(Y,1)),dtype=tf.float32))
train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
summary = tf.summary.merge_all()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
Writer = tf.summary.FileWriter('mnist')
Writer.add_graph(sess.graph,global_step=0)
for n in range(n_timmes):
avg_cost = 0
n_batch = mnist.train.num_examples // batch_size
for i in range(n_batch):
x_train,y_train = mnist.train.next_batch(batch_size)
c,_ = sess.run([cost,train],feed_dict={X:x_train,Y:y_train,keep_prob:0.7})
avg_cost += c / n_batch
print(avg_cost)
print(sess.run(acc,feed_dict={X:mnist.test.images,Y:mnist.test.labels,keep_prob:1}))
print('结束')
r = random.randint(0,mnist.test.num_examples - 1)
print('标签',sess.run(tf.argmax(mnist.test.labels[r:r+1],1)))
print('预测',sess.run(tf.argmax(l5,1),feed_dict={X:mnist.test.images[r:r+1],Y:mnist.test.labels[r:r+1],keep_prob:1}))
plt.imshow(
mnist.test.images[r:r+1].reshape(28,28),
cmap='Greys',
interpolation='nearest'
)
plt.show()
'''
此时会生成一个可视化文件日志的文件夹
在命令行中输入:tensorboard --logdir=文件夹路径 --host=127.0.0.1
然后在谷歌浏览器中输入127.0.0.1:6006
就可以显示可视化了
'''