TensorFlow訓練MNIST數據集(1) —— softmax 單層神經網絡


 

1、MNIST數據集簡介

  首先通過下面兩行代碼獲取到TensorFlow內置的MNIST數據集:

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('./data/mnist', one_hot=True)

  MNIST數據集共有55000(mnist.train.num_examples)張用於訓練的數據,對應的有55000個標簽;共有10000(mnist.test.num_examples)張用於測試的圖片的數據,同樣的有10000個標簽與之對應。為了方便訪問,這些圖片或標簽的數據都是被格式化了的。

  MNIST數據集的訓練數據集(mnist.train.images)是一個 55000 * 784 的矩陣,矩陣的每一行代表一張圖片(28 * 28 * 1)的數據,圖片的數據范圍是 [0, 1],代表像素點灰度歸一化后的值。

  訓練集的標簽(mnist.train.labels)是一個55000 * 10 的矩陣,每一行的10個數字分別代表對應的圖片屬於數字0到9的概率,范圍是0或1。一個標簽行只有一個是1,表示該圖片的正確數字是對應的下標值, 其余是0。

  測試集與訓練集的類似,只是數據量不同。

  以下代碼顯示部分MNIST訓練圖片的形狀及標簽:

import numpy as np
import matplotlib.pyplot as plot
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('./data/mnist', one_hot=True)
trainImages = mnist.train.images
trainLabels = mnist.train.labels

plot.figure(1, figsize=(4, 3))
for i in range(6):
    curImage = np.reshape(trainImages[i, :], (28, 28))
    curLabel = np.argmax(trainLabels[i, :])
    ax = plot.subplot(int(str(23) + str(i+1)))
    plot.imshow(curImage, cmap=plot.get_cmap('gray'))
    plot.axis('off')
    ax.set_title(curLabel)

plot.suptitle('MNIST')
plot.show()

  上述代碼輸出的MNIST圖片及其標簽:

 2、通過單層神經網絡進行訓練

 1 def train(trainCycle=50000, debug=False):
 2     inputSize  = 784
 3     outputSize = 10
 4     batchSize  = 64
 5     inputs = tf.placeholder(tf.float32, shape=[None, inputSize])
 6 
 7     # x * w = [64, 784] * [784, 10]
 8     weights   = tf.Variable(tf.random_normal([784, 10], 0, 0.1))
 9     bias      = tf.Variable(tf.random_normal([outputSize], 0, 0.1))
10     outputs   = tf.add(tf.matmul(inputs, weights), bias)
11     outputs   = tf.nn.softmax(outputs)
12 
13     labels = tf.placeholder(tf.float32, shape=[None, outputSize])
14 
15     loss      = tf.reduce_mean(tf.square(outputs - labels))
16     optimizer = tf.train.GradientDescentOptimizer(0.1)
17     trainer   = optimizer.minimize(loss)
18 
19     sess = tf.Session()
20     sess.run(tf.global_variables_initializer())
21     for i in range(trainCycle):
22         batch = mnist.train.next_batch(batchSize)
23         sess.run([trainer, loss], feed_dict={inputs: batch[0], labels: batch[1]})
24 
25         if debug and i % 1000 == 0:
26             corrected = tf.equal(tf.argmax(labels, 1), tf.argmax(outputs, 1))
27             accuracy = tf.reduce_mean(tf.cast(corrected, tf.float32))
28             accuracyValue = sess.run(accuracy, feed_dict={inputs: batch[0], labels: batch[1]})
29             print(i, ' train set accuracy:', accuracyValue)
30 
31     # 測試
32     corrected = tf.equal(tf.argmax(labels, 1), tf.argmax(outputs, 1))
33     accuracy = tf.reduce_mean(tf.cast(corrected, tf.float32))
34     accuracyValue = sess.run(accuracy, feed_dict={inputs: mnist.test.images, labels: mnist.test.labels})
35     print("accuracy on test set:", accuracyValue)
36 
37     sess.close()

 

3、訓練結果

  上述模型的最終輸出為:

由打印日志可以看出,前期收斂速度很快,后期開始波動。最后該模型在訓練集上的正確率大概為90%,測試集上也差不多。精度還是比較低的,說明單層的神經網絡在處理圖片數據上存在着很大的缺陷,並不是一個很好的選擇。

 

本文地址:https://www.cnblogs.com/laishenghao/p/9576806.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM