TensorFlow学习-基于CNN实现手写数字识别

本文档详细介绍了如何使用TensorFlow构建一个基于CNN的手写数字识别模型。模型包括两个卷积层,两个池化层和两个全连接层。在训练过程中,采用one-hot编码处理标签,使用交叉熵作为损失函数,并通过随机梯度下降进行优化。在模型评估阶段,通过设置评估指标来检查模型性能。完整代码可在TensorFlow官方仓库找到。
摘要由CSDN通过智能技术生成



一、网络结构

使用2个卷积层,2个池化层, 2个全连接层组成网络

输入→ 卷积→ ReLU→max polling→ 卷积→ ReLU→max polling→ FC→输出

  1. 输入
    一个4维的tensor: [batch_size, image_width, image_height, channels], 分别代表梯度下降处理的批量数据大小,图片宽度,图片高度和图片的channel个数(彩色图片channel数为3[Red, Green, Blue],单色图片channel数为1)

    # Input Layer
    # Reshape X to 4-D tensor: [batch_size, width, height, channels]
    # MNIST images are 28x28 pixels, and have one color channel
    input_layer  =  tf.reshape(features, [ - 1 28 28 1 ])


  2. 卷积层#1:
    采用32(channel)个5*5的过滤器(kernel)对原始输入图像做卷积(局部感知), 另外对输入矩阵加了zero padding以保持卷积输出宽高和输入一致,并用ReLU作为激活函数引入非线性特性

    # Convolutional Layer #1
      # Computes 32 features using a 5x5 filter with ReLU activation.
      # Padding is added to preserve width and height.
      # Input Tensor Shape: [batch_size, 28, 28, 1]
      # Output Tensor Shape: [batch_size, 28, 28, 32]
      conv1  =  tf.layers.conv2d(
          inputs = input_layer,
          filters = 32 ,
          kernel_size = [ 5 5 ],
          padding = "same" ,
          activation = tf.nn.relu)


  3. 池化层#1
    采用2*2的过滤器(stride=2)对卷积层#1的输出做最大值下采样(max polling), 降低了数据纬度,并避免过拟合

    # Pooling Layer #1
    # First max pooling layer with a 2x2 filter and stride of 2
    # Input Tensor Shape: [batch_size, 28, 28, 32]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值