【DL】cnn實現MNIST的分類

超全註釋

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

batch_size = 100
num_batch = mnist.train.num_examples // batch_size   #550

#定義初始化權值
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 tf.Variable(initial)

#定義卷積層
def conv2d(x,W):
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
    

#定義最大池化層
def max_pool_2x2(x):
    """
    ksize  是窗口大小~~~
    strides 是步長
    """
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')  

#定義兩個placeholder
x = tf.placeholder(tf.float32,[None,784])  #28*28
y = tf.placeholder(tf.float32,[None,10])

#改變x的格式,轉換爲4D向量  [batch,height,width,channels]
x_image = tf.reshape(x,[-1,28,28,1])   #batch=-1表示不確定個數,相當於前面定義的100

#初始化第一個卷積層的權值和偏置
W_conv1 = weight_variable([5,5,1,32])  #卷積核5*5,1個特徵平面,32個卷積核
b_conv1 = weight_variable([32])  #每個卷積核1個偏置

#把x_image進行卷積(x_image×卷積+偏置,再用relu函數激活)
A_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
#再經過最大池化層
A_pool1 = max_pool_2x2(A_conv1)


#初始化第二個卷積層的權值和偏置
W_conv2 = weight_variable([5,5,32,64])
b_conv2 = weight_variable([64])

A_conv2 = tf.nn.relu(conv2d(A_pool1,W_conv2)+b_conv2)
A_pool2 = max_pool_2x2(A_conv2)
# print('A_pool2的shape:',A_pool2.shape)

#初始化第一個全連接層的權值
W_fc1 = weight_variable([7*7*64,1024])
b_fc1 = weight_variable([1024])

#把池化層2的輸出扁平化爲1維
A_pool2_flag = tf.reshape(A_pool2,[-1,7*7*64])
# print('A_pool2_flag的shape:',A_pool2_flag.shape)

#求第一個全連接層的輸出
A_fc1 = tf.nn.relu(tf.matmul(A_pool2_flag,W_fc1)+b_fc1)

#keep_prob用來表示神經元的輸出概率
keep_prob = tf.placeholder(tf.float32)
A_fc1_dropout = tf.nn.dropout(A_fc1,keep_prob)

#初始化第二個全連接層
W_fc2 = weight_variable([1024,10])
b_fc2 = weight_variable([10])

#求第二個全連接層的輸出(輸出用softmax函數)
prediction = tf.nn.softmax(tf.matmul(A_fc1_dropout,W_fc2)+b_fc2)

#定義代價函數爲交叉熵
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#優化器選用adam
train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)
#結果存放在一個bool列表中
correct_prection = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
#求準確率
accuracy = tf.reduce_mean(tf.cast(correct_prection,tf.float32))


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epoch in range(21):
        for batch in range(num_batch):
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.7})
        
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
        print('Iter'+str(epoch)+',Testing Accuracy:'+str(acc))
    
Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
Iter0,Testing Accuracy:0.963



---------------------------------------------------------------------------

KeyboardInterrupt                         Traceback (most recent call last)

<ipython-input-18-1a74dac98566> in <module>
     91             sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.7})
     92 
---> 93         acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
     94         print('Iter'+str(epoch)+',Testing Accuracy:'+str(acc))
     95 


~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    948     try:
    949       result = self._run(None, fetches, feed_dict, options_ptr,
--> 950                          run_metadata_ptr)
    951       if run_metadata:
    952         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)


~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1171     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1172       results = self._do_run(handle, final_targets, final_fetches,
-> 1173                              feed_dict_tensor, options, run_metadata)
   1174     else:
   1175       results = []


~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1348     if handle is None:
   1349       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1350                            run_metadata)
   1351     else:
   1352       return self._do_call(_prun_fn, handle, feeds, fetches)


~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1354   def _do_call(self, fn, *args):
   1355     try:
-> 1356       return fn(*args)
   1357     except errors.OpError as e:
   1358       message = compat.as_text(e.message)


~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1339       self._extend_graph()
   1340       return self._call_tf_sessionrun(
-> 1341           options, feed_dict, fetch_list, target_list, run_metadata)
   1342 
   1343     def _prun_fn(handle, feed_dict, fetch_list):


~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1427     return tf_session.TF_SessionRun_wrapper(
   1428         self._session, options, feed_dict, fetch_list, target_list,
-> 1429         run_metadata)
   1430 
   1431   def _call_tf_sessionprun(self, handle, feed_dict, fetch_list):


KeyboardInterrupt: 

鑑於cpu版跑太慢,所以我就把代碼放在服務器上用GPU跑了。運行結果如下:
在這裏插入圖片描述

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