【连载】深度学习笔记13:Tensorflow实战之手写mnist手写数字...
作者头像
  • 陈炽
  • 2018-10-30 16:45:09 0

在本篇教程中,我们将探讨如何使用TensorFlow构建一个卷积神经网络(CNN)模型,以识别MNIST手写数字数据集中的数字。MNIST数据集是由Yann Lecun等人创建的,它包含70000张手写数字图像,涵盖从0到9的10个数字。

首先,我们需要导入MNIST数据集。幸运的是,TensorFlow已经为我们提供了方便的方法来加载和处理该数据集:

```python import tensorflow as tf

加载MNIST数据集

mnist = tf.keras.datasets.mnist (xtrain, ytrain), (xtest, ytest) = mnist.loaddata() xtrain, xtest = xtrain / 255.0, x_test / 255.0 ```

接下来,我们快速搭建一个基础的神经网络模型。我们首先定义一些变量和会话:

```python

创建会话

sess = tf.Session()

定义输入和输出变量

x = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10])

初始化权重和偏置

W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10]))

初始化全局变量

sess.run(tf.globalvariablesinitializer())

前向传播过程和损失函数

y = tf.nn.softmax(tf.matmul(x, W) + b) crossentropy = -tf.reducesum(y_ * tf.log(y))

模型训练

trainstep = tf.train.GradientDescentOptimizer(0.01).minimize(crossentropy)

进行1000次迭代训练

for _ in range(1000): batch = mnist.train.nextbatch(50) trainstep.run(feeddict={x: batch[0], y: batch[1]}) ```

尽管上述模型已经能够实现不错的识别精度,但为了进一步提高性能,我们可以引入卷积层。以下是改进后的模型架构:

  1. 初始化权重函数: ```python def weightvariable(shape): initial = tf.random.truncatednormal(shape, stddev=0.1) return tf.Variable(initial)

    def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) ```

  2. 卷积和池化操作: ```python def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

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

  3. 构建卷积层: ```python

    第一层卷积

    Wconv1 = weightvariable([5, 5, 1, 32]) bconv1 = biasvariable([32]) ximage = tf.reshape(x, [-1, 28, 28, 1]) hconv1 = tf.nn.relu(conv2d(ximage, Wconv1) + bconv1) hpool1 = maxpool2x2(h_conv1)

    第二层卷积

    Wconv2 = weightvariable([5, 5, 32, 64]) bconv2 = biasvariable([64]) hconv2 = tf.nn.relu(conv2d(hpool1, Wconv2) + bconv2) hpool2 = maxpool2x2(hconv2) ```

  4. 全连接层python W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_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)

  5. 防止过拟合python keep_prob = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

  6. 输出层python W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

  7. 模型训练和评估: ```python crossentropy = -tf.reducesum(y_ * tf.log(yconv)) trainstep = tf.train.AdamOptimizer(1e-4).minimize(crossentropy) correctprediction = tf.equal(tf.argmax(yconv, 1), tf.argmax(y, 1)) accuracy = tf.reducemean(tf.cast(correctprediction, tf.float32)) sess.run(tf.globalvariablesinitializer())

    for i in range(20000): batch = mnist.train.nextbatch(50) if i % 100 == 0: trainaccuracy = accuracy.eval(feeddict={x: batch[0], y: batch[1], keepprob: 1.0}) print("Step %d, Training Accuracy %g" % (i, trainaccuracy)) trainstep.run(feeddict={x: batch[0], y: batch[1], keepprob: 0.5})

    print("Test Accuracy %g" % accuracy.eval(feeddict={x: mnist.test.images, y: mnist.test.labels, keep_prob: 1.0})) ```

通过上述步骤,我们成功构建了一个卷积神经网络模型,并且在MNIST数据集上的识别准确率达到了约99.31%,这表明模型的性能得到了显著提升。

    本文来源:图灵汇
责任编辑: : 陈炽
声明:本文系图灵汇原创稿件,版权属图灵汇所有,未经授权不得转载,已经协议授权的媒体下载使用时须注明"稿件来源:图灵汇",违者将依法追究责任。
    分享
手写Tensorflow实战深度数字笔记连载学习mnist
    下一篇