深度学习之CNN卷积神经网络(二)MNIST手写数字识别和tensorboard可视化

代码如下

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)

# 开启第2层空间
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)

# 开启第3层空间
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)

# 开启第4层空间
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)

# 开启第5层空间
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
        # Writer.add_graph(summ,step)
        # step += 1
    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
	就可以显示可视化了
'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值