是一個識別手寫數字圖片的計算機視覺集,它包含各類手寫數字圖片和每張圖片對應的標籤函數
softmax迴歸是logistic迴歸的一種,它是多元分類(包含二分類)。測試
sotfmax迴歸能夠把多分類任務多輸出轉換爲各類類別的可能機率,把最大機率值對應的類別做爲輸入樣本的輸出類別預測atom
sotfmax激活函數公式:spa
z[l]=W[l]a[l]+b[l]code
g(z[l])=e(z[l])blog
理解sotfmax:索引
sotfmax的loss function:圖片
單個樣本 L(ŷ ,y)=∑yjlogŷj, j=[1,...,x] y爲標籤值,ŷ爲預測機率,x爲輸入特徵數量
input
樣本集J(w[1],b[1],…)=1/m∑L(ŷ (i),y(i)) it
sotfmax梯度降低公式: ∂J/∂z[L]=dz[L]=ŷ −y
訓練樣本:60000個(mnist.train),其中55000個用於訓練,5000個用於驗證
測試樣本:10000個(mnist.test)
每個MNIST數據單元包含兩部分:圖片(mnist.train.images)和標籤(mnist.train.labels),每張圖片包含28像素X28像素,用向量表示長度是28X28=784,所以圖片(mnist.train.images)爲[60000, 784]的張量,第一個維度用了索引圖片,第二個維度用來索引每張圖片的像素點,標籤介於0-9的數字,向量化表示爲[1,0,0,0,0,0,0,0,0],所以標籤mnist.train.labels)爲[60000, 10]的張量
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) # 輸入特徵佔位 x = tf.placeholder('float', [None, 784]) # 權重和偏置值 W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) # 預測值 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) # 初始化全部變量 init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) # 開始訓練模型 for _ in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, y_:batch_ys}) # 評估模型效果 correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_:mnist.test.labels}))