基于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!')
发布了34 篇原创文章 · 获赞 9 · 访问量 3024

猜你喜欢

转载自blog.csdn.net/RObot_123/article/details/103786235