手写数字识别 CNN MNIST_data LeNet

MNIST_data数据集下载

链接:https://pan.baidu.com/s/1wble_t39TR4uAgTFZjnSnQ 
提取码:5wxq 

一、CNN识别手写数字

# coding = utf-8
# 2019/7/22  Luckyxxt:有趣的事,Python永远不会缺席!

import tensorflow as tf
import time
import tensorflow.examples.tutorials.mnist.input_data as input_data
old_v = tf.logging.get_verbosity()
tf.logging.set_verbosity(tf.logging.ERROR)
t1 = time.time()
mnist_data_folder = './MNIST_data'
mnist = input_data.read_data_sets(mnist_data_folder, one_hot=True)
tf.logging.set_verbosity(old_v)
print('mnist.train.images.shape', mnist.train.images.shape)
print('mnist.train.labels', mnist.train.labels.shape)
print('mnist.test.images.shape', mnist.test.images.shape)
print('mnist.test.labels.shape', mnist.test.labels.shape)
'''
1、训练数据 55000训练样本 784 为28 * 28 的像素值 mnist.train.images.shape (55000, 784)
2、训练数据label 10个种类 minist.train.labels (55000, 10)
3、测试数据 10000 minist.test.images.shape (10000, 784)
4、测试数据label 10个种类minist.test.labeld.shape (10000, 10)
'''
# 下面先构建数据流图 最后在会话里面启动
'''
#placeholder()函数是在神经网络构建graph的时候在模型中的占位,
#此时并没有把要输入的数据传入模型,它只会分配必要的内存。
#等建立session,在会话中,运行模型的时候通过feed_dict()函数向占位符传入数据
#这里建立了两个占位
'''

image_place = tf.placeholder(tf.float32, shape=([None, 784]))
label_place = tf.placeholder(tf.float32, shape=([None, 10]))

'''
#我们需要创建大量的权重和偏置量
#所以我们定义了两个函数
#权重应用少量的噪声来打破对称性以及避免0梯度
#用一个较小的正数来初始化偏置项
'''


def weight_variable(shape):
    # tf.truncated_normal从截断的正态分布中输出随机值。
    # shape表示生成张量的维度,stddev是标准差,均值默认为0
    initial = tf.truncated_normal(shape, stddev=0.1)
    # 定义一个图变量,用于在会话中启动
    return tf.Variable(initial)


def bias_variable(shape):
    # 创建一个常数张量,shape代表张量的维度
    initial = tf.constant(0.1, shape=shape)
    # 定义一个图变量,用于在会话中启动
    return tf.Variable(initial)


'''
tf.nn.conv2d (  input/输入图片,
                filter/滤镜,
                strides/步幅,
                padding/是否填充,
                use_cudnn_on_gpu=None,是否使用cudnn加速,默认为true
                data_format=None,
                name=None)
input是要进行卷积的图片,形状为[ batch, in_height, in_weight, in_channel ]
batch为图片的数量,in_height 为图片高度,in_weight 为图片宽度,in_channel 为图片的通道数(彩色为3,黑白为1)

filter/滤镜 也是一个张量,形状为为 [ filter_height, filter_weight, in_channel, out_channels ]
filter_height 为卷积核高度,filter_weight 为卷积核宽度,
in_channel 是图像通道数 ,和 input 的 in_channel 要保持一致,out_channel 是卷积核数量。

strides/ 步幅  卷积时在图像每一维的步长,这是一个一维的向量,
[ 1, strides, strides, 1],第一位和最后一位固定必须是1

padding/是否填充 "SAME"是考虑边界,不足的时候用0去填充周围,"VALID"则不考虑
'''
# 定义一个构建卷积层的函数


def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')


'''
tf.nn.max_pool(value, ksize, strides, padding, name=None)

value 卷积层的输入  [batch, height, width, channels]
ksize 池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],不在batch和channels上做池化
strides/ 步幅  卷积时在图像每一维的步长,这是一个一维的向量,
[ 1, strides, strides, 1],第一位和最后一位固定必须是1
padding/是否填充 "SAME"是考虑边界,不足的时候用0去填充周围,"VALID"则不考虑
'''

# 定义一个构建最大池化层的函数


def max_pool(x):
    return tf.nn.max_pool(
        x, ksize=[
            1, 2, 2, 1], strides=[
            1, 2, 2, 1], padding='SAME')


'''
构建第一层的滤镜矩阵W和偏置量B
表示卷积核的大小为5×5,输入为1,输出为32,即共有32个卷积核(滤镜)
偏置量的值为0.1,因为有32个滤镜 所以有32个偏置量
'''

W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])

print('W_conv1', W_conv1)
# W_conv1 <tf.Variable 'Variable:0' shape=(5, 5, 1, 32) dtype=float32_ref>
print('b_conv1', b_conv1)
# b_conv1 <tf.Variable 'Variable_1:0' shape=(32,) dtype=float32_ref>
# 改变X的形状࿰
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值