1. 程式人生 > >使用tensorflow實現mnist手寫識別(單層神經網絡實現)

使用tensorflow實現mnist手寫識別(單層神經網絡實現)

for 神經網絡 評估 手寫識別 抽取 display mnist 一個 next

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
import numpy as np
import matplotlib.pyplot as plt
mnist =  input_data.read_data_sets("data/",one_hot = True)
#導入Tensorflwo和mnist數據集

#構建輸入層
x = tf.placeholder(tf.float32,[None,784],name=X)
y = tf.placeholder(tf.float32,[None,10],name=
Y) #隱藏層神經元數量 H1_NN = 256 #第一層神經元數量 W1 = tf.Variable(tf.random_normal([784,H1_NN])) #權重 b1 = tf.Variable(tf.zeros([H1_NN])) #偏置項 Y1 = tf.nn.relu(tf.matmul(x,W1)+b1) #第一層輸出 W2 = tf.Variable(tf.random_normal([H1_NN,10]))#權重 b2 = tf.Variable(tf.zeros(10))#偏置項 forward = tf.matmul(Y1,W2)+b2 #定義前向傳播
pred = tf.nn.softmax(forward) #激活函數輸出 #損失函數 #loss_function = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), # reduction_indices=1)) #(log(0))超出範圍報錯 loss_function = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=forward,labels=y))
#訓練參數 train_epochs = 50 #訓練次數 batch_size = 50 #每次訓練多少個樣本 total_batch = int(mnist.train.num_examples/batch_size) #隨機抽取樣本 display_step = 1 #訓練情況輸出 learning_rate = 0.01 #學習率 #優化器 opimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss_function) #準確率函數 correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(pred,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #記錄開始訓練時間 from time import time startTime = time() #初始化變量 sess =tf.Session() init = tf.global_variables_initializer() sess.run(init) #訓練 for epoch in range(train_epochs): for batch in range(total_batch): xs,ys = mnist.train.next_batch(batch_size)#讀取批次數據 sess.run(opimizer,feed_dict={x:xs,y:ys})#執行批次數據訓練 #total_batch個批次訓練完成後,使用驗證數據計算誤差與準確率 loss,acc=sess.run([loss_function,accuracy], feed_dict={x:mnist.validation.images, y:mnist.validation.labels}) #輸出訓練情況 if(epoch+1) % display_step == 0: print("Train Epoch:",%02d % (epoch + 1), "Loss=","{:.9f}".format(loss),"Accuracy=","{:.4f}".format(acc)) duration = time()-startTime print("Trian Finshed takes:","{:.2f}".format(duration))#顯示預測耗時 #由於pred預測結果是one_hot編碼格式,所以需要轉換0~9數字 prediction_resul = sess.run(tf.argmax(pred,1),feed_dict={x:mnist.test.images}) prediction_resul[0:10] #模型評估 accu_test = sess.run(accuracy, feed_dict={x:mnist.test.images,y:mnist.test.labels}) print("Accuray:",accu_test) compare_lists = prediction_resul == np.argmax(mnist.test.labels,1) print(compare_lists) err_lists = [i for i in range(len(mnist.test.labels)) if compare_lists[i] == False] print(err_lists,len(err_lists)) index_list = [] def print_predct_errs(labels,#標簽列表 perdiction):#預測值列表 count = 0 compare_lists = (perdiction == np.argmax(labels,1)) err_lists = [i for i in range(len(labels)) if compare_lists[i] == False] for x in err_lists: index_list.append(x) print("index="+str(x)+ "標簽值=",np.argmax(labels[x]), "預測值=",perdiction[x]) count = count+1 print("總計:",count) return index_list print_predct_errs(mnist.test.labels,prediction_resul) def plot_images_labels_prediction(images,labels,prediction,index,num=25): fig = plt.gcf() # 獲取當前圖片 fig.set_size_inches(10,12) if num>=25: num=25 #最多顯示25張圖片 for i in range(0,num): ax = plt.subplot(5,5, i+1) #獲取當前要處理的子圖 ax.imshow(np.reshape(images[index],(28,28)),cmap=binary)#顯示第index個圖像 title = label= + str(np.argmax(labels[index]))#構建該圖上要顯示的title if len(prediction)>0: title += predict= +str(prediction[index]) ax.set_title(title,fontsize=10) ax.set_xticks([]) ax.set_yticks([]) index += 1 plt.show() plot_images_labels_prediction(mnist.test.images,mnist.test.labels,prediction_resul,index=index_list[100])

單純記錄一下個人代碼,很基礎的一個MNIST手寫識別使用Tensorflwo實現,算是入門的Hello world 了,有些奇怪的問題暫時沒有解決 訓練次數調成40 在訓練到第35次左右發生了梯度爆炸,原因未知,損失函數要使用帶softmax那個,不然也會發生梯度爆炸

使用tensorflow實現mnist手寫識別(單層神經網絡實現)