Android tensorflow softmax 實現mnist分類
阿新 • • 發佈:2019-01-12
今天記錄下 tensorflow 基於 mnist 相簿集完成softmax迴歸;mnist庫是手寫阿拉伯數字圖片集,如下圖所示
下面是下載並讀取資料集,資料集存放在linux 根目錄 下的 /data 下 mnist 資料夾下
from tensorflow.examples.tutorials import mnist
mnist_data = mnist.input_data.read_data_sets('/data/mnist', one_hot=True)
下面是建立 線性模型 y = w*x + b,w為權值,b為偏值;注意這裡的w 、b、x、y都是多維陣列,可以看做矩陣
y_ 為真實值 y為預測值
# x is feature value
x = tf.placeholder(tf.float32, [None, 784])
# w is weight for feature
w = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, w) + b # y = x*w + b
# y_ is true value
y_ = tf.placeholder(tf.float32, [None, 10])
下面是建立softmax 交叉熵 和 對應的損失函式及選擇最優值的優化器,裡面分別使用了兩種方法,都可以編譯試一下,看一下最終的識別率
#損失均值的方法
#cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
#損失和的方法
cross_entropy = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
learn_rate = 0.01
#梯度下降優化器
#train_step = tf.train.GradientDescentOptimizer(learn_rate).minimize(cross_entropy)
#自適應優化器
train_step = tf.train.AdamOptimizer(learn_rate).minimize(cross_entropy)
學習率就是尋找最優值時調整的值的大小,就是梯度的大小,minimize 就是使損失函式達到最小值
全部程式碼如下:
#!/user/bin/env python
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt #用於顯示
from IPython.display import display, HTML
from tensorflow.examples.tutorials import mnist #下載mnist資料集
#讀取mnist資料集
mnist_data = mnist.input_data.read_data_sets('/data/mnist', one_hot=True)
# x is feature value
x = tf.placeholder(tf.float32, [None, 784])
# w is weight for feature
w = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, w) + b
# y_ is true value
y_ = tf.placeholder(tf.float32, [None, 10])
#cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
cross_entropy = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
learn_rate = 0.01
#梯度下降優化器
#train_step = tf.train.GradientDescentOptimizer(learn_rate).minimize(cross_entropy)
#自適應優化器,學習率為 0.01
train_step = tf.train.AdamOptimizer(learn_rate).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
# mnist_data.train training data
for _ in range(10000):
#select 100 data as training set
batch_xs, batch_ys = mnist_data.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
#預測與真實值比較,得出準確率
correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#打印出準確率
print(sess.run(accuracy, feed_dict={x: mnist_data.test.images,y_: mnist_data.test.labels}))
#下面是將識別錯誤的列印並顯示出來
for i in range(0, len(mnist_data.test.images)):
result = sess.run(correct_prediction, feed_dict={x: np.array([mnist_data.test.images[i]]), y_: np.array([mnist_data.test.labels[i]])})
if not result:
print('the pre value:',sess.run(y, feed_dict={x: np.array([mnist_data.test.images[i]]), y_: np.array([mnist_data.test.labels[i]])}))
print('the true value:',sess.run(y_,feed_dict={x: np.array([mnist_data.test.images[i]]), y_: np.array([mnist_data.test.labels[i]])}))
one_pic_arr = np.reshape(mnist_data.test.images[i], (28, 28))
pic_matrix = np.matrix(one_pic_arr, dtype="float")
plt.imshow(pic_matrix)
plt.show()
break