TensorFlow教程Softmax邏輯回歸識別手寫數字MNIST數據集

基於MNIST數據集的邏輯回歸模型做十分類任務

沒有隱含層的Softmax Regression隻能直接從圖像的像素點推斷是哪個數字,而沒有特征抽象的過程。多層神經網絡依靠隱含層,則可以組合出高階特征,比如橫線、豎線、圓圈等,之後可以將這些高階特征或者說組件再組合成數字,就能實現精準的匹配和分類。

import tensorflow as tf
import numpy as np
import input_data
print('Download and Extract MNIST dataset')
mnist = input_data.read_data_sets('data/', one_hot=True) # one_hot=True意思是編碼格式為01編碼
print("tpye of 'mnist' is %s" % (type(mnist)))
print("number of train data is %d" % (mnist.train.num_examples))
print("number of test data is %d" % (mnist.test.num_examples))
trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel = mnist.test.labels
print("MNIST loaded")

"""
print("type of 'trainimg' is %s"    % (type(trainimg)))
print("type of 'trainlabel' is %s"  % (type(trainlabel)))
print("type of 'testimg' is %s"     % (type(testimg)))
print("type of 'testlabel' is %s"   % (type(testlabel)))
print("------------------------------------------------")
print("shape of 'trainimg' is %s"   % (trainimg.shape,))
print("shape of 'trainlabel' is %s" % (trainlabel.shape,))
print("shape of 'testimg' is %s"    % (testimg.shape,))
print("shape of 'testlabel' is %s"  % (testlabel.shape,))

"""
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10]) # None is for infinite
w = tf.Variable(tf.zeros([784, 10])) # 為瞭方便直接用0初始化,可以高斯初始化
b = tf.Variable(tf.zeros([10])) # 10分類的任務,10種label,所以隻需要初始化10個b
pred = tf.nn.softmax(tf.matmul(x, w) + b) # 前向傳播的預測值
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=[1])) # 交叉熵損失函數
optm = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
corr = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) # tf.equal()對比預測值的索引和真實label的索引是否一樣,一樣返回True,不一樣返回False
accr = tf.reduce_mean(tf.cast(corr, tf.float32))
init = tf.global_variables_initializer() # 全局參數初始化器
training_epochs = 100 # 所有樣本迭代100次
batch_size = 100 # 每進行一次迭代選擇100個樣本
display_step = 5
# SESSION
sess = tf.Session() # 定義一個Session
sess.run(init) # 在sess裡run一下初始化操作
# MINI-BATCH LEARNING
for epoch in range(training_epochs): # 每一個epoch進行循環
    avg_cost = 0. # 剛開始損失值定義為0
    num_batch = int(mnist.train.num_examples/batch_size)
    for i in range(num_batch): # 每一個batch進行選擇
        batch_xs, batch_ys = mnist.train.next_batch(batch_size) # 通過next_batch()就可以一個一個batch的拿數據,
        sess.run(optm, feed_dict={x: batch_xs, y: batch_ys}) # run一下用梯度下降進行求解,通過placeholder把x,y傳進來
        avg_cost += sess.run(cost, feed_dict={x: batch_xs, y:batch_ys})/num_batch
    # DISPLAY
    if epoch % display_step == 0: # display_step之前定義為5,這裡每5個epoch打印一下
        train_acc = sess.run(accr, feed_dict={x: batch_xs, y:batch_ys})
        test_acc = sess.run(accr, feed_dict={x: mnist.test.images, y: mnist.test.labels})
        print("Epoch: %03d/%03d cost: %.9f TRAIN ACCURACY: %.3f TEST ACCURACY: %.3f"
              % (epoch, training_epochs, avg_cost, train_acc, test_acc))
print("DONE")

迭代100次跑一下模型,最終,在測試集上可以達到92.2%的準確率,雖然還不錯,但是還達不到實用的程度。手寫數字的識別的主要應用場景是識別銀行支票,如果準確率不夠高,可能會引起嚴重的後果。

Epoch: 095/100 loss: 0.283259882 train_acc: 0.940 test_acc: 0.922

插一些知識點,關於tensorflow中一些函數的用法

sess = tf.InteractiveSession()
arr = np.array([[31, 23,  4, 24, 27, 34],
                [18,  3, 25,  0,  6, 35],
                [28, 14, 33, 22, 30,  8],
                [13, 30, 21, 19,  7,  9],
                [16,  1, 26, 32,  2, 29],
                [17, 12,  5, 11, 10, 15]])
在tensorflow中打印要用.eval()
tf.rank(arr).eval() # 打印矩陣arr的維度
tf.shape(arr).eval() # 打印矩陣arr的大小
tf.argmax(arr, 0).eval() # 打印最大值的索引,參數0為按列求索引,1為按行求索引

以上就是TensorFlow教程Softmax邏輯回歸識別手寫數字MNIST數據集的詳細內容,更多關於Softmax邏輯回歸MNIST數據集手寫識別的資料請關註WalkonNet其它相關文章!

推薦閱讀: