基於muist數據集的maxout網絡實現分類 ----代碼分享

運行環境:windows,tensorflow - gpu-1.13.1

#---------------------------------理解mnist數據集
#導入mnist數據集
from tensorflow.examples.tutorials.mnist import input_data #從網上下載mnist數據集的模塊
mnist = input_data.read_data_sets('MNIST_data/',one_hot = False) #從指定文件夾導入數據集的數據
##分析mnist數據集
#print('輸入訓練數據集數據:',mnist.train.images) #打引導如數據集的數據
#print('輸入訓練數據集shape:',mnist.train.images.shape) #打印訓練數據集的形狀
#print('輸入測試數據集shape:',mnist.test.images.shape) #用於評估訓練過程中的準確度
#print('輸入驗證數據集shape:',mnist.validation.images.shape) #用於評估最終模型的準確度
#print('輸入標籤的shape:',mnist.train.labels.shape)
#展示mnist數據集
#import pylab 
#im = mnist.test.images[6] #train中的第六張圖
#im = im.reshape(-1,28)
#pylab.imshow(im)
#pylab.show()


#-----------------------------------------------

#-------------------------------正向傳播結構
import tensorflow as tf
tf.reset_default_graph()
#分析圖片特點定義變量
#define placeholder
x = tf.placeholder(tf.float32,[None, 784]) #mnist data have 784 value
#y = tf.placeholder(tf.float32,[None,10]) #labels have 10 value
y = tf.placeholder(tf.int32,[None]) 
#定義學習參數
W = tf.Variable(tf.random_normal([784,10])) #Normally,we set weight as random
b = tf.Variable(tf.zeros([10]))#Normally,we set base as zero
#print(b)
#with tf.Session() as sess:
#    print(sess.run(b))
#定義輸出節點
#pred = tf.nn.softmax(tf.matmul(x,W) + b) #sotfmax分類
z = tf.matmul(x,W) + b
maxout = tf.reduce_max(z,axis=1,keep_dims=True)
#設置學習參數
W2 = tf.Variable(tf.truncated_normal([1,10],stddev=0.1))
b2 = tf.Variable(tf.zeros([10]))

pred = tf.nn.softmax(tf.matmul(maxout,W2)+b2) # Softmax分類
#-------------------------------------------

#-------------------------------------定義反向結構及傳播參數
#損失函數
#cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1)) #生成的pred與樣本標籤y進行交叉熵運算,然後取平均值
#cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y,logits = z))
cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=z))
#定義參數
learning_rate = 0.3
#使用梯度下降優化器
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 
#----------------------------------------------------------

#--------------------------------訓練模型並輸出中間狀態參數
training_epochs = 200
batch_size = 500
display_step = 1

saver = tf.train.Saver()
model_path = 'log/mnist_model.ckpt'

#啓動session
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer()) #初始化OP
    
    #啓動循環開始訓練
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(mnist.train.num_examples/batch_size)
        #循環所有數據集
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            #運行優化器
            _,c = sess.run([optimizer, cost], feed_dict = {x:batch_xs,y:batch_ys})
            #計算平均loss值
            avg_cost += c / total_batch
        #顯示訓練中的詳細信息
        if (epoch+1) % display_step == 0:
            print('Epoch:','%04d' % (epoch+1),'cost','{:.9f}'.format(avg_cost))
    print('Finish!')
發佈了36 篇原創文章 · 獲贊 12 · 訪問量 5077
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章