TensorFlow MNIST数据集手写数字识别(并解决MNIST数据集下载问题)

本篇博客主要介绍通过TensorFlow实现MNIST数据集的手写数字识别。

准备数据:

首先需要获取数据,可以通过以下代码进行获取:

from tensorflow.examples.tutorials.mnist import input_data
# 获取数据,number 1 to 10
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

注:由于使用以上代码获取数据经常获取不到,因此需要先对数据进行下载,在代码同目录下创建MNIST_data目录,并在http://yann.lecun.com/exdb/mnist/下载下面四个文件,不用解压直接放到MNIST_data目录下。


搭建网络:

MNIST数据集包含了55000张训练图片,每张图片的分辨率为28x28,即网络的输入为28x28=784个像素,黑色的部分值值为1,白色的部分值为0


xs = tf.placeholder(tf.float32, [None, 784])  # 28x28

每张图片表示一个数字,即输出为10类,如输出为[0 1 0 0 0 0 0 0 0 0]表示数字1

ys = tf.placeholder(tf.float32, [None, 10])

计算损失:

激活函数选用softmax,softmax经常用于classification(分类)。

prediction = add_layer(xs, 784, 10, activation_function=tf.nn.softmax)

损失函数选用交叉熵函数,交叉熵函数用来衡量预测值和真实值之间的相似程度。如果完全相同,他们的交叉熵为0.

cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
                                              reduction_indices=[1]))

选用梯度下降算法更新参数。

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

完整代码:

# encoding:utf-8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data


# 获取数据,number 1 to 10
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)


def add_layer(inputs, in_size, out_size, activation_function=None):
    with tf.name_scope('layer'):
        with tf.name_scope('weights'):
            W = tf.Variable(tf.random_normal([in_size, out_size]), name='W')
        with tf.name_scope('bias'):
            b = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='b')
        with tf.name_scope('Wx_plus_b'):
            Wx_plus_b = tf.matmul(inputs, W) + b
        if activation_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activation_function(Wx_plus_b)
        return outputs


def compute_accuracy(v_xs, v_ys):
    global prediction
    y_pre = sess.run(prediction, feed_dict={xs: v_xs})
    corrct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))
    accuracy = tf.reduce_mean(tf.cast(corrct_prediction, tf.float32))
    result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys})
    return result

# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 784])  # 28x28
ys = tf.placeholder(tf.float32, [None, 10])

# add output layer, softmax通常用于做classification
prediction = add_layer(xs, 784, 10, activation_function=tf.nn.softmax)

# the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
                                              reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.Session()

# important step
sess.run(tf.initialize_all_variables())

for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={xs: batch_xs, ys:batch_ys})
    if i % 50 == 0:
       print(compute_accuracy(
           mnist.test.images, mnist.test.labels
       ))

运行结果:

Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
2018-07-09 15:15:20.559165: W c:\l\tensorflow_1501918863922\work\tensorflow-1.2.1\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE instructions, but these are available on your machine and could speed up CPU computations.
2018-07-09 15:15:20.559887: W c:\l\tensorflow_1501918863922\work\tensorflow-1.2.1\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE2 instructions, but these are available on your machine and could speed up CPU computations.
2018-07-09 15:15:20.560547: W c:\l\tensorflow_1501918863922\work\tensorflow-1.2.1\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
2018-07-09 15:15:20.561141: W c:\l\tensorflow_1501918863922\work\tensorflow-1.2.1\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.
2018-07-09 15:15:20.561767: W c:\l\tensorflow_1501918863922\work\tensorflow-1.2.1\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.
2018-07-09 15:15:20.562236: W c:\l\tensorflow_1501918863922\work\tensorflow-1.2.1\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.
2018-07-09 15:15:20.562993: W c:\l\tensorflow_1501918863922\work\tensorflow-1.2.1\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.
2018-07-09 15:15:20.563277: W c:\l\tensorflow_1501918863922\work\tensorflow-1.2.1\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.
WARNING:tensorflow:From D:\Users\Seavan_CC\Anaconda3\lib\site-packages\tensorflow\python\util\tf_should_use.py:170: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
0.0908
0.636
0.733
0.7702
0.7961
0.8105
0.824
0.8305
0.838
0.8426
0.8491
0.8514
0.8518
0.8556
0.8625
0.8645
0.8666
0.8704
0.8735
0.8699


猜你喜欢

转载自blog.csdn.net/github_39611196/article/details/80970677
今日推荐