mnist手写数字识别python_Mnist手写数字识别 Tensorflow

本文档介绍了如何使用Tensorflow搭建并训练MNIST手写数字识别模型。首先,简要介绍MNIST数据集,然后通过Python代码详细展示了如何加载数据、构建卷积神经网络模型、训练及测试模型,并利用Tensorboard进行可视化分析。
摘要由CSDN通过智能技术生成

Mnist手写数字识别 Tensorflow

任务目标

了解mnist数据集

搭建和测试模型

编辑环境

操作系统:Win10

python版本:3.6

集成开发环境:pycharm

tensorflow版本:1.*

了解mnist数据集

mnist数据集:mnist数据集下载地址

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

??图片是以字节的形式进行存储, 我们需要把它们读取到 NumPy array 中, 以便训练和测试算法。

读取mnist数据集

mnist = input_data.read_data_sets("mnist_data", one_hot=True)

搭建和测试模型

代码

def getMnistModel(savemodel,is_train):

"""

:param savemodel: 模型保存路径

:param is_train: true为训练,false为测试模型

:return:None

"""

mnist = input_data.read_data_sets("mnist_data", one_hot=True)

with tf.variable_scope("data"):

x = tf.placeholder(tf.float32,shape=[None,784]) # 784=28*28*1 宽长为28,单通道图片

y_true = tf.placeholder(tf.int32,shape=[None,10]) # 10个类别

with tf.variable_scope("conv1"):

w_conv1 = tf.Variable(tf.random_normal([10,10,1,32])) # 10*10的卷积核 1个通道的输入图像 32个不同的卷积核,得到32个特征图

b_conv1 = tf.Variable(tf.constant(0.0,shape=[32]))

x_reshape = tf.reshape(x,[-1,28,28,1]) # n张 28*28 的单通道图片

conv1 = tf.nn.relu(tf.nn.conv2d(x_reshape,w_conv1,strides=[1,1,1,1],padding="SAME")+b_conv1) #[1, 1, 1, 1] 中间2个1,卷积每次滑动的步长 padding=‘SAME‘ 边缘自动补充

pool1 = tf.nn.max_pool(conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME") # 池化窗口为[1,2,2,1] 中间2个2,池化窗口每次滑动的步长 padding="SAME" 考虑边界,如果不够用 用0填充

with tf.variable_scope("conv2"):

w_conv2 = tf.Variable(tf.random_normal([10,10,32,64]))

b_conv2 = tf.Variable(tf.constant(0.0,shape=[64]))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值