此模型中,输入是28*28*1的图片,经过两个卷积层(卷积+池化)层之后,尺寸变为7*7*64,将最后一个卷积层展成一个以为向量,然后接两个全连接层,第一个全连接层加一个dropout,最后一个全连接层输出10个分类的预测结果,然后计算损失,进行训练。

  代码如下:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data #定义一个获取卷积核的函数
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial) #定义一个获取偏置值的函数
def bias_variable(shape):
initial = tf.constant(0.1,shape=shape)
return tf.Variable(initial) #定义一个卷积函数
def conv2d(x,W):
return tf.nn.conv2d(x,W,[1,1,1,1],padding="SAME") #定义一个池化函数
def max_pool_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1], strides=[1,2,2,1],padding="VALID") if __name__ == "__main__":
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
x = tf.placeholder(shape=[None,28*28],dtype=tf.float32)
lable = tf.placeholder(shape=[None,10],dtype=tf.float32) x_image = tf.reshape(x,[-1,28,28,1]) #第一个卷积层
W_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
#14*14*32 #第二个卷积层
W_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
#7*7*64 #全连接层,输出为1024维向量
W_fc1 = weight_variable([7*7*64,1024])
b_fc1 = weight_variable([1024])
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_dropout = tf.nn.dropout(h_fc1,keep_prob=keep_prob) #把1024维向量转换成10维,对应10个类别
W_fc2 = weight_variable([1024,10])
b_fc2 = weight_variable([10])
y_conv = tf.matmul(h_fc1,W_fc2)+b_fc2 #直接使用tf.nn.softmax_cross_entropy_with_logits直接计算交叉熵
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=lable,logits=y_conv))
#定义train_step
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) #定义测试的准确率
correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(lable,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) # 创建Session和变量初始化
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer()) #训练20000步
for i in range(20000):
batch = mnist.train.next_batch(50)
if i % 100==0:
train_accuracy = sess.run(accuracy,feed_dict={
x:batch[0],lable:batch[1],keep_prob: 1.0})
print("step %d, training accuracy %g" % (i, train_accuracy))
_ = sess.run(train_step, feed_dict={x: batch[0], lable: batch[1], keep_prob: 0.5})
print("test accuracy %g" % sess.run(accuracy, feed_dict={
x: mnist.test.images, lable: mnist.test.labels, keep_prob: 1.0}))

Tensorflow项目实战一:MNIST手写数字识别的更多相关文章

  1. TensorFlow—多层感知器—MNIST手写数字识别

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

  2. mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)

    前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...

  3. Android+TensorFlow+CNN+MNIST 手写数字识别实现

    Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...

  4. 基于tensorflow的MNIST手写数字识别(二)--入门篇

    http://www.jianshu.com/p/4195577585e6 基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型 基于tensorflow的MNIST手写数字识 ...

  5. Tensorflow之MNIST手写数字识别:分类问题(1)

    一.MNIST数据集读取 one hot 独热编码独热编码是一种稀疏向量,其中:一个向量设为1,其他元素均设为0.独热编码常用于表示拥有有限个可能值的字符串或标识符优点:   1.将离散特征的取值扩展 ...

  6. TensorFlow——MNIST手写数字识别

    MNIST手写数字识别 MNIST数据集介绍和下载:http://yann.lecun.com/exdb/mnist/   一.数据集介绍: MNIST是一个入门级的计算机视觉数据集 下载下来的数据集 ...

  7. 基于TensorFlow的MNIST手写数字识别-初级

    一:MNIST数据集    下载地址 MNIST是一个包含很多手写数字图片的数据集,一共4个二进制压缩文件 分别是test set images,test set labels,training se ...

  8. Tensorflow实现MNIST手写数字识别

    之前我们讲了神经网络的起源.单层神经网络.多层神经网络的搭建过程.搭建时要注意到的具体问题.以及解决这些问题的具体方法.本文将通过一个经典的案例:MNIST手写数字识别,以代码的形式来为大家梳理一遍神 ...

  9. mnist 手写数字识别

    mnist 手写数字识别三大步骤 1.定义分类模型2.训练模型3.评价模型 import tensorflow as tfimport input_datamnist = input_data.rea ...

  10. 持久化的基于L2正则化和平均滑动模型的MNIST手写数字识别模型

    持久化的基于L2正则化和平均滑动模型的MNIST手写数字识别模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献Tensorflow实战Google深度学习框架 实验平台: Tens ...

随机推荐

  1. Python 配置日志的几种方式

    Python配置日志的几种方式 作为开发者,我们可以通过以下3种方式来配置logging: (1)使用Python代码显式的创建loggers,handlers和formatters并分别调用它们的配 ...

  2. 【倍增】LCM QUERY

    给一个序列,每次给一个长度l,问长度为l的区间中lcm最小的. 题解:因为ai<60,所以以某个点为左端点的区间的lcm只有最多60种的情况,而且相同的lcm区间的连续的. 所以就想到一个n*6 ...

  3. 2018牛客多校第五场 H.subseq

    题意: 给出a数组的排列.求出字典序第k小的b数组的排列,满足1<=bi<=n,bi<bi+1,a[b[i]]<a[b[i+1]],m>0. 题解: 用树状数组倒着求出以 ...

  4. BZOJ4597:[SHOI2016]随机序列——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4597 你的面前有N个数排成一行.分别为A1, A2, … , An.你打算在每相邻的两个 Ai和 ...

  5. Codeforces Round #337 (Div. 2)B

    B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. HDU3376 最小费用最大流 模板2

    Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)To ...

  7. mysql按月统计六个月内不同类型订单的成交金额

    mysql按月统计六个月内不同类型订单的成交金额 创建数据库 CREATE DATABASE test; 创建订单表 CREATE TABLE `t_order` ( `id` ) NOT NULL ...

  8. jedis在线文档网址

    jedis在线文档网址:http://tool.oschina.net/apidocs/apidoc?api=jedis-2.1.0

  9. 任务调度 Quartz 学习(三) CronTrigger 表达式

    CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表. CronT ...

  10. SpringBoot打war包并部署到tomcat下运行

    一.修改pom.xml. 1.packaging改为war 2.build节点添加<finalName>你的项目名</finalName> 二.修改项目启动类,继承Spring ...