TensorFlow之CNN实现MNIST手写数字识别

1. MNSIT介绍:

    MNIST( http://yann.lecun.com/exdb/mnist/)是深度学习入门必定会接触到,很多的教程都是用这个数据集做例子

    MNIST 数据集来自美国国家标准与技术研究所, National Institute of Standards and Technology (NIST). 训练集 (training set) 由来自 250 个不同人手写的数字构成, 其中 50% 是高中学生, 50% 来自人口普查局 (the Census Bureau) 的工作人员. 测试集(test set) 也是同样比例的手写数字数据    

MNIST数据集包含四个部分:

    >Training set image

    >Training set labels

    >Test set image

    >Test set labels

2. CNN实现MNIST网络结结构(TensorFlow)


#coding=utf-8  

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


# 1、准备数据
data = input_data.read_data_sets(train_dir="C:/Users/Leon/Desktop/AI/data/mnist/input_data/", one_hot=True)


# 2、输入层
x = tf.placeholder(dtype=tf.float32)
y = tf.placeholder(dtype=tf.int32)
# 3、卷积层1(激活)
x_image = tf.reshape(x, shape=[-1, 28, 28, 1])
filter1 = tf.Variable(tf.random_normal(shape=[5, 5, 1, 32]))
conv1 = tf.nn.conv2d(x_image, filter1, strides=[1, 1, 1, 1], padding="SAME")
bias1 = tf.Variable(tf.random_normal(shape=[32]))
relu1 = tf.nn.relu(conv1 + bias1)
# 4、池化层1
pool1 = tf.nn.max_pool(relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
# [14, 14, 32]
# 5、卷积层2(激活)
filter2 = tf.Variable(tf.random_normal(shape=[5, 5, 32, 64]))
conv2 = tf.nn.conv2d(pool1, filter2, strides=[1, 1, 1, 1], padding="SAME")
bias2 = tf.Variable(tf.random_normal(shape=[64]))
relu2 = tf.nn.relu(conv2 + bias2)
# 7、池化层2
pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
# [7, 7, 64]
# 8、全连接层(激活)
fc_input = tf.reshape(pool2, shape=[-1, 7 * 7 * 64])
weight1 = tf.Variable(tf.random_normal(shape=[7 * 7 * 64, 1024]))
fc_bias1 = tf.Variable(tf.random_normal(shape=[1024]))
fc_out = tf.nn.relu(tf.matmul(fc_input, weight1) + fc_bias1)
# 10、原始输出层
weight2 = tf.Variable(tf.random_normal([1024, 10]))
fc_bias2 = tf.Variable(tf.random_normal(shape=[10]))
logits = tf.matmul(fc_out, weight2) + fc_bias2
# 11、Softmax回归
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=logits))
# 12、梯度下降,开启会话,训练模型
train_op = tf.train.GradientDescentOptimizer(learning_rate=0.0001).minimize(loss)
# 13、准确率的计算
equal_list = tf.equal(tf.argmax(y, axis=1), tf.argmax(logits, axis=1))
accuracy = tf.reduce_mean(tf.cast(equal_list, dtype=tf.float32))


init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for i in range(100):
        image_batch, label_batch = data.train.next_batch(batch_size=50)
        sess.run(train_op, feed_dict={x: image_batch, y: label_batch})
        print(sess.run(accuracy, feed_dict={x: image_batch, y: label_batch}))


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值