簡述
這次是在看《21個項目玩轉深度學習》那本書的第一章節后做的筆記。
這段時間,打算把TensorFlow再補補,提升一下技術水平~ 希望我能堅持下來,抽空把這本書刷下來吧~
導入數據
下面的代碼會直接下載數據,如果沒有那個文件夾的話,但是,如果有這個文件夾而且里面有那幾個文件的話,就會直接調用那個數據。 這次直接在網上搜MNIST下載,就知道怎么下載啦~ 這里我更關注TensorFlow和算法本身
from tensorflow
. examples
. tutorials
. mnist
import input_data
mnist
= input_data
. read_data_sets
( 'MNIST_data/' , one_hot
= True )
print ( mnist
. train
. images
. shape
)
print ( mnist
. test
. images
. shape
)
(55000, 784) (10000, 784)
比較有意思的是,這里的labels的數據安排。
比如說,這就是第一個圖片的所對應的信息。表示是7。
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 ]
書中比較有意思的介紹了Softmax回歸 ,之前雖然有用過Softmax,也大致知道意思,但卻沒有較為詳細的了解過它。這倒是這次閱讀的驚喜。
Softmax回歸
Softmax回歸,來自于Logistics 回歸。只不過邏輯回歸只是binary classification的活動,而Softmax就可以實現多元的回歸的情況。 意思很簡單,就是返回某個物體屬于labels的概率集合。然后選擇概率最大的label。
現在對每個類做分析。和Logistics regression一樣,先會有一個線性函數來給每個類打分。同樣的,這個分數是在實數范圍內的,但是概率空間只有0和1,所以需要做個映射。
分數 為(a, b, c)時,對應的概率分別為:
分數概率 a eaea+eb+ec\frac{e^a}{e^a+e^b+e^c} e a + e b + e c e a ? b ebea+eb+ec\frac{e^b}{e^a+e^b+e^c} e a + e b + e c e b ? c ecea+eb+ec\frac{e^c}{e^a+e^b+e^c} e a + e b + e c e c ?
現在我們回想起logistics regression的sigmoid函數,會發現,其實就是假設了另外一個label的得分永遠為0的情況。
所以Softmax模型其實為
y=Softmax(WTx+b)y=Softmax(W^Tx+b) y = S o f t m a x ( W T x + b )
繼續實驗部分
import tensorflow
as tf
tensorflow是靜態圖。其實很簡單,就是我們需要手動的描述一張圖。把整個圖畫完之后,再傳遞參數進去,讓它自己開始運行(session)。
描述這張圖 下面我們給出了幾個定義。最后一個 其實表示的是真正的label值。而y是我們算出來的值。
x
= tf
. placeholder
( tf
. float32
, [ 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
( tf
. float32
, [ None , 10 ] )
cross_entropy
= tf
. reduce_mean
( - tf
. reduce_sum
( y_
* tf
. log
( y
) ) )
train_step
= tf
. train
. GradientDescentOptimizer
( 0.01 ) . minimize
( cross_entropy
)
sess
= tf
. InteractiveSession
( )
tf
. global_variables_initializer
( ) . run
( )
訓練, 每次用mini batch的思路選幾個來做 注意每次都需要把整個圖跑一遍
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
, tf
. float32
) )
輸出一下:
print ( sess
. run
( accuracy
, feed_dict
= { x
: mnist
. test
. images
, y_
: mnist
. test
. labels
} ) )
Tips:如果想單獨看下某個數據,可以使用下面的方法
a
= sess
. run
( correct_prediction
, feed_dict
= { x
: mnist
. test
. images
, y_
: mnist
. test
. labels
} )
然后我們會很驚奇的發現,這其實是一個numpy的數據
使用卷積神經網絡來實現
x
= tf
. placeholder
( tf
. float32
, [ None , 784 ] )
y_
= tf
. placeholder
( tf
. float32
, [ None , 10 ] )
x_image
= tf
. reshape
( x
, [ - 1 , 28 , 28 , 1 ] )
def weight_variable ( shape
) : initial
= tf
. truncated_normal
( shape
, stddev
= 0.1 ) return tf
. Variable
( initial
) def bias_variable ( shape
) : initial
= tf
. constant
( 0.1 , shape
= shape
) return initial
def conv2d ( x
, W
) : return tf
. nn
. conv2d
( x
, W
, strides
= [ 1 , 1 , 1 , 1 ] , padding
= 'SAME' ) def max_pool_2x2 ( x
) : return tf
. nn
. max_pool
( x
, ksize
= [ 1 , 2 , 2 , 1 ] , strides
= [ 1 , 2 , 2 , 1 ] , padding
= 'SAME' )
W_conv1
= weight_variable
( [ 5 , 5 , 1 , 32 ] )
b_conv1
= bias_variable
( [ 32 ] )
h_conv1
= tf
. nn
. relu
( conv2d
( x_image
, W_conv1
) + b_conv1
)
h_pool1
= max_pool_2x2
( h_conv1
)
W_conv2
= weight_variable
( [ 5 , 5 , 32 , 64 ] )
b_conv2
= bias_variable
( [ 64 ] )
h_conv2
= tf
. nn
. relu
( conv2d
( h_pool1
, W_conv2
) + b_conv2
)
h_pool2
= max_pool_2x2
( h_conv2
)
W_fc1
= weight_variable
( [ 7 * 7 * 64 , 1024 ] )
b_fc1
= weight_variable
( [ 1024 ] )
h_pool2_flat
= tf
. reshape
( h_pool2
, [ - 1 , 7 * 7 * 64 ] )
h_fc1
= tf
. nn
. relu
( tf
. matmul
( h_pool2_flat
, W_fc1
) + b_fc1
)
keep_prob
= tf
. placeholder
( tf
. float32
)
h_fc1_drop
= tf
. nn
. dropout
( h_fc1
, keep_prob
)
W_fc2
= weight_variable
( [ 1024 , 10 ] )
b_fc2
= weight_variable
( [ 10 ] )
y_conv
= tf
. matmul
( h_fc1_drop
, W_fc2
) + b_fc2
cross_entropy
= tf
. reduce_mean
( tf
. nn
. softmax_cross_entropy_with_logits
( labels
= y_
, logits
= y_conv
) )
train_step
= tf
. train
. AdamOptimizer
( 1e - 4 ) . minimize
( cross_entropy
)
correct_prediction
= tf
. equal
( tf
. argmax
( y_conv
, 1 ) , tf
. argmax
( y_
, 1 ) )
accuracy
= tf
. reduce_mean
( tf
. cast
( correct_prediction
, tf
. float32
) )
sess
= tf
. InteractiveSession
( )
tf
. global_variables_initializer
( ) . run
( )
for i
in range ( 20000 ) : batch
= mnist
. train
. next_batch
( 50 ) if i
% 1000 == 0 : train_accuracy
= accuracy
. eval ( feed_dict
= { x
: batch
[ 0 ] , y_
: batch
[ 1 ] , keep_prob
: 1.0 } ) print ( train_accuracy
) train_step
. run
( feed_dict
= { x
: batch
[ 0 ] , y_
: batch
[ 1 ] , keep_prob
: 0.5 } )
結果可以達到接近100%
感覺還不錯~
總結
以上是生活随笔 為你收集整理的简单探索MNIST(Softmax回归和两层CNN)-Tensorflow学习 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。