TensorFlow在MNIST中的应用-卷积神经网络CNN

参考:

《TensorFlow技术解析与实战》


########################################################################################

用TensorFlow搭建一个卷积神经网络CNN模型,并用来训练MNIST数据集。


# -*- coding:utf-8 -*-
# ==============================================================================
# 20171115
# HelloZEX
# 卷积神经网络
# ==============================================================================

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data

#定义训练和评估时的批次大小
batch_size = 128
test_size = 256

#初始化权重函数
def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))

#神经网络模型的构建,传入以下参数
# x:输入数据
# w:每一层的权重
# p_keep_conv,p_keep_hidden;dropout要保留的神经元比例
def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden):
    #第一组卷积和池化层,最后dropout一些神经元
    l1a = tf.nn.relu(tf.nn.conv2d(X, w,                       # l1a shape=(?, 28, 28, 32)
                        strides=[1, 1, 1, 1], padding='SAME'))
    l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1],              # l1 shape=(?, 14, 14, 32)
                        strides=[1, 2, 2, 1], padding='SAME')
    l1 = tf.nn.dropout(l1, p_keep_conv)

    #第二组卷积和池化层,最后dropout一些神经元
    l2a = tf.nn.relu(tf.nn.conv2d(l1, w2,                     # l2a shape=(?, 14, 14, 64)
                        strides=[1, 1, 1, 1], padding='SAME'))
    l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1],              # l2 shape=(?, 7, 7, 64)
                        strides=[1, 2, 2, 1], padding='SAME')
    l2 = tf.nn.dropout(l2, p_keep_conv)

    #第三组卷积和池化层,最后dropout一些神经元
    l3a = tf.nn.relu(tf.nn.conv2d(l2, w3,                     # l3a shape=(?, 7, 7, 128)
                        strides=[1, 1, 1, 1], padding='SAME'))
    l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1],              # l3 shape=(?, 4, 4, 128)
                        strides=[1, 2, 2, 1], padding='SAME')
    l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]])    # reshape to (?, 2048)
    l3 = tf.nn.dropout(l3, p_keep_conv)

    #全连接层,最后dropout一些神经元
    l4 = tf.nn.relu(tf.matmul(l3, w4))
    l4 = tf.nn.dropout(l4, p_keep_hidden)

    #输出层
    pyx = tf.matmul(l4, w_o)
    return pyx

#得到训练和测试的图片
mnist = input_data.read_data_sets("MNIST_Labels_Images", one_hot=True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels
trX = trX.reshape(-1, 28, 28, 1)  # 28x28x1 input img
teX = teX.reshape(-1, 28, 28, 1)  # 28x28x1 input imgp_keep_hidden: 1.0})))

X = tf.placeholder("float", [None, 28, 28, 1])
Y = tf.placeholder("float", [None, 10])

#初始化权重
w = init_weights([3, 3, 1, 32])       # patch大小为3x3,输入维度为1 ,输出维度为32
w2 = init_weights([3, 3, 32, 64])     # patch大小为3x3,输入维度为32 ,输出维度为64
w3 = init_weights([3, 3, 64, 128])    # patch大小为3x3,输入维度为64 ,输出维度为128
w4 = init_weights([128 * 4 * 4, 625]) # 全连接层
w_o = init_weights([625, 10])         # 输出层,输入维度为625,输出维度为10代表十个分类(labels)

#我们定义dropout的占位符 keep_conv,他表示在一层中有多少比例的神经元被保留下来。生成网络模型,得到预测值
p_keep_conv = tf.placeholder("float")
p_keep_hidden = tf.placeholder("float")
py_x = model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden)

#定义的损失函数,并作均值处理。采用实现RMSProp算法的优化器
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))
train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
#定义预测的操作(predict_op)
predict_op = tf.argmax(py_x, 1)

# Launch the graph in a session
with tf.Session() as sess:
    # you need to initialize all variables
    tf.global_variables_initializer().run()

    for i in range(100):
        training_batch = zip(range(0, len(trX), batch_size), range(batch_size, len(trX)+1, batch_size))
        for start, end in training_batch:
            sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end], p_keep_conv: 0.8, p_keep_hidden: 0.5})

        test_indices = np.arange(len(teX)) # Get A Test Batch
        np.random.shuffle(test_indices)
        test_indices = test_indices[0:test_size]

        print(i, np.mean(np.argmax(teY[test_indices], axis=1) ==
                         sess.run(predict_op, feed_dict={X: teX[test_indices],
                                                         Y: teY[test_indices],
                                                         p_keep_conv: 1.0,
                                                         p_keep_hidden: 1.0})))

########################################################################################

/usr/bin/python2.7 /home/zhengxinxin/Desktop/PyCharm/Spark/SparkMNIST/SparkMNIST_CNN.py
Extracting MNIST_Labels_Images/train-images-idx3-ubyte.gz
Extracting MNIST_Labels_Images/train-labels-idx1-ubyte.gz
Extracting MNIST_Labels_Images/t10k-images-idx3-ubyte.gz
Extracting MNIST_Labels_Images/t10k-labels-idx1-ubyte.gz
2017-11-15 09:55:12.123463: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-11-15 09:55:12.123492: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-11-15 09:55:12.123497: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-11-15 09:55:12.123500: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-11-15 09:55:12.123503: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
(0, 0.9453125)
(1, 0.97265625)
(2, 0.98828125)
(3, 0.984375)
(4, 0.98046875)
(5, 1.0)
(6, 0.984375)
(7, 0.99609375)
(8, 0.9921875)
(9, 0.99609375)
(10, 0.99609375)
(11, 0.99609375)
(12, 0.98828125)
(13, 0.99609375)
(14, 1.0)
(15, 0.98046875)
(16, 0.9921875)
(17, 0.99609375)
(18, 0.99609375)
(19, 1.0)
(20, 0.98828125)
(21, 0.9921875)
(22, 0.98828125)
(23, 1.0)
(24, 1.0)
(25, 0.98828125)
(26, 1.0)
(27, 0.9921875)
(28, 0.9921875)
(29, 0.9921875)
(30, 0.99609375)
(31, 0.99609375)
(32, 1.0)
(33, 1.0)
(34, 0.9921875)
(35, 0.99609375)
(36, 1.0)
(37, 0.9921875)
(38, 0.984375)
(39, 0.99609375)
(40, 0.9921875)
(41, 0.98828125)
(42, 0.98828125)
(43, 1.0)
(44, 1.0)
(45, 0.9921875)
(46, 1.0)
(47, 1.0)
(48, 0.98828125)
(49, 0.9921875)
(50, 0.99609375)
(51, 0.9921875)
(52, 0.9921875)
(53, 0.98828125)
(54, 0.98828125)
(55, 0.98828125)
(56, 0.98828125)
(57, 0.9921875)
(58, 0.99609375)
(59, 0.99609375)
(60, 0.984375)
(61, 0.99609375)
(62, 0.99609375)
(63, 0.99609375)
(64, 0.99609375)
(65, 0.9921875)
(66, 0.99609375)
(67, 0.99609375)
(68, 0.9765625)
(69, 0.99609375)
(70, 0.9921875)
(71, 0.9921875)
(72, 0.99609375)
(73, 0.9921875)
(74, 0.9921875)
(75, 0.9921875)
(76, 0.98828125)
(77, 0.99609375)
(78, 0.99609375)
(79, 0.99609375)
(80, 0.984375)
(81, 0.9921875)
(82, 0.9921875)
(83, 0.98828125)
(84, 0.9765625)
(85, 0.99609375)
(86, 1.0)
(87, 1.0)
(88, 0.984375)
(89, 0.99609375)
(90, 0.9921875)
(91, 0.9921875)
(92, 0.984375)
(93, 0.9921875)
(94, 0.99609375)
(95, 1.0)
(96, 0.99609375)
(97, 1.0)
(98, 0.99609375)
(99, 0.984375)

Process finished with exit code 0
########################################################################################

上面输出了训练的次数和准确度的关系。可以看到100轮后准确度已经非常高了。通过回归模型和卷积神经网络模型,可以看出卷积神经网络的效果非常好。下一节使用RNN训练MNIST。


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值