全文參考地址:
https://github.com/jikexueyuanwiki/tensorflow-zh/blob/130a43845711cfad8e7d46e75300558349b36b57/SOURCE/tutorials/mnist_beginners.md
修改部分爲input_data代碼(該文件用於提供Minst的下載和導入),本身該程序提供自動下載功能,可是一直不成功,之後只能手動下載,下載之後將文件放入當前目錄的MINST_data文件夾下。修改代碼如下(就只修改了read_data_sets函數):
def read_data_sets(train_dir, fake_data=False, one_hot=False, dtype=tf.float32):
class DataSets(object):
pass
data_sets = DataSets()
if fake_data:
def fake():
return DataSet([], [], fake_data=True, one_hot=one_hot, dtype=dtype)
data_sets.train = fake()
data_sets.validation = fake()
data_sets.test = fake()
return data_sets
TRAIN_IMAGES = 'train-images-idx3-ubyte.gz'
TRAIN_LABELS = 'train-labels-idx1-ubyte.gz'
TEST_IMAGES = 't10k-images-idx3-ubyte.gz'
TEST_LABELS = 't10k-labels-idx1-ubyte.gz'
VALIDATION_SIZE = 5000
#local_file = maybe_download(TRAIN_IMAGES, train_dir)
local_file = train_dir + "/" + TRAIN_IMAGES
train_images = extract_images(local_file)
#local_file = maybe_download(TRAIN_LABELS, train_dir)
local_file = train_dir + "/" + TRAIN_LABELS
train_labels = extract_labels(local_file, one_hot=one_hot)
#local_file = maybe_download(TEST_IMAGES, train_dir)
local_file = train_dir + "/" + TEST_IMAGES
test_images = extract_images(local_file)
#local_file = maybe_download(TEST_LABELS, train_dir)
local_file = train_dir + "/" + TEST_LABELS
test_labels = extract_labels(local_file, one_hot=one_hot)
validation_images = train_images[:VALIDATION_SIZE]
validation_labels = train_labels[:VALIDATION_SIZE]
train_images = train_images[VALIDATION_SIZE:]
train_labels = train_labels[VALIDATION_SIZE:]
data_sets.train = DataSet(train_images, train_labels, dtype=dtype)
data_sets.validation = DataSet(validation_images, validation_labels,
dtype=dtype)
data_sets.test = DataSet(test_images, test_labels, dtype=dtype)
return data_sets
softmax 簡單測試代碼如下:
#導入Minst數據集
import input_data
mnist = input_data.read_data_sets("MINST_data",one_hot=True)
#導入tensorflow庫
import tensorflow as tf
#輸入變量,把28*28的圖片變成一維數組(丟失結構信息)
x = tf.placeholder("float",[None,784])
#權重矩陣,把28*28=784的一維輸入,變成0-9這10個數字的輸出
w = tf.Variable(tf.zeros([784,10]))
#偏置
b = tf.Variable(tf.zeros([10]))
#核心運算,其實就是softmax(x*w+b)
y = tf.nn.softmax(tf.matmul(x,w) + b)
#這個是訓練集的正確結果
y_ = tf.placeholder("float",[None,10])
#交叉熵,作爲損失函數
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
#梯度下降算法,最小化交叉熵
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
#初始化,在run之前必須進行的
init = tf.initialize_all_variables()
#創建session以便運算
sess = tf.Session()
sess.run(init)
#迭代1000次
for i in range(1000):
#獲取訓練數據集的圖片輸入和正確表示數字
batch_xs, batch_ys = mnist.train.next_batch(100)
#運行剛纔建立的梯度下降算法,x賦值爲圖片輸入,y_賦值爲正確的表示數字
sess.run(train_step,feed_dict = {x:batch_xs, y_: batch_ys})
#tf.argmax獲取最大值的索引。比較運算後的結果和本身結果是否相同。
#這步的結果應該是[1,1,1,1,1,1,1,1,0,1...........1,1,0,1]這種形式。
#1代表正確,0代表錯誤
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
#tf.cast先將數據轉換成float,防止求平均不準確。
#tf.reduce_mean由於只有一個參數,就是上面那個數組的平均值。
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
#輸出
print sess.run(accuracy,feed_dict={x:mnist.test.images,y_: mnist.test.labels})
這個程序就是了解簡單的tensorflow流程,api可以參考https://www.tensorflow.org