TensorFlow MNIST資料集手寫數字識別(並解決MNIST資料集下載問題)
阿新 • • 發佈:2019-02-06
本篇部落格主要介紹通過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