Tensorflow實現卷積神經網絡識別mnist數字

很久以前寫的代碼,冒個泡

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf 
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)

def weight_variable(shape):
    initial =tf.truncated_normal(shape,stddev=0.1)
    #此處給權重加標準差爲0.1的正態分佈的截斷噪聲打破完全對稱
    return tf.Variable(initial) #用variable來聲明變量

def bias_variable(shape):
    initial=tf.constant(0.1,shape=shape)
    #給偏置增加噪聲,防止死亡節點
    return tf.Variable(initial)

def conv2d(x,W):
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
    #此處爲二維卷積函數,參數(輸入,參數,步長,邊界處理)
    #參數:[卷積核尺寸,卷積核尺寸,通道,卷積核數目]
    #邊界處理:SAME表示卷積的輸入輸出尺寸相同
'''第一個參數input:指需要做卷積的輸入圖像,它要求是一個Tensor,
    具有[batch, in_height, in_width, in_channels]這樣的shape,具體
    含義是[訓練時一個batch的圖片數量, 圖片高度, 圖片寬度, 圖像通道
    數],注意這是一個4維的Tensor,要求類型爲float32和float64其中之一

    第二個參數filter:相當於CNN中的卷積核,它要求是一個Tensor,具
    有[filter_height, filter_width, in_channels, out_channels]這樣的shape
    ,具體含義是[卷積核的高度,卷積核的寬度,圖像通道數,卷積核個數],
    要求類型與參數input相同,有一個地方需要注意,第三維in_channels,
    就是參數input的第四維

    第三個參數strides:卷積時在圖像每一維的步長,這是一個一維的向量,長度4

    第四個參數padding:string類型的量,只能是"SAME","VALID"其中之一,這個值
    決定了不同的卷積方式(後面會介紹)

    第五個參數:use_cudnn_on_gpu:bool類型,是否使用cudnn加速,默認爲true'''


def max_pool_2x2(x):
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
    #tf.nn.max_pool(value, ksize, strides, padding, name=None)
'''    參數是四個,和卷積很類似:
    第一個參數value:需要池化的輸入,一般池化層接在卷積層後面,所以輸入通常
    是feature map,依然是[batch, height, width, channels]這樣的shape

    第二個參數ksize:池化窗口的大小,取一個四維向量,一般是[1, height, width, 1],
    因爲我們不想在batch和channels上做池化,所以這兩個維度設爲了1

    第三個參數strides:和卷積類似,窗口在每一個維度上滑動的步長,
    一般也是[1, stride,stride, 1]

    第四個參數padding:和卷積類似,可以取'VALID' 或者'SAME'

    返回一個Tensor,類型不變,shape仍然是[batch, height, width, channels]這種形式'''

x=tf.placeholder(tf.float32,[None,784])
y_=tf.placeholder(tf.float32,[None,10])
#輸入變量
x_image=tf.reshape(x,[-1,28,28,1])
#一維轉圖,[數量,尺寸,尺寸,通道]

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)#卷積1
h_pool=max_pool_2x2(h_conv1)#池化1

W_conv2=weight_variable([5,5,32,64])
b_conv2=bias_variable([64])
h_conv2=tf.nn.relu(conv2d(h_pool,W_conv2)+b_conv2)
h_pool2=max_pool_2x2(h_conv2)

#兩次2x2的池化後,圖像尺寸縮小爲1/4,即7X7,有64個feature map,
# 則輸出tensor尺寸爲7×7*64,

W_fc1=weight_variable([7*7*64,1024])
b_fc1=bias_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)

#dropout

keep_prob=tf.placeholder(tf.float32)
h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)

#softmax
W_fc2=weight_variable([1024,10]) #前一層的1024個隱含節點,10類
b_fc2=bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)

#交叉熵
cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv),reduction_indices=[1]))
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))

#tf.global_variables_initializer().run()
sess=tf.Session()
sess.run(tf.global_variables_initializer()) 

for i in range(20000):
    batch=mnist.train.next_batch(50)
    if i%100==0:
        train_accuracy=accuracy.eval(session=sess,feed_dict={x:batch[0],y_:batch[1],keep_prob:1.0})
        print("step:%d,train accuracy %g"%(i,train_accuracy))
    train_step.run(session=sess,feed_dict={x:batch[0],y_:batch[1],keep_prob:0.5})
print("test accuracy %g"%accuracy.eval(session=sess,feed_dict={x:mnist.test.images,
y_:mnist.test.labels,keep_prob:1.0}))



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章