欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于tensorflow数据集的dropout优化

程序员文章站 2022-03-06 21:24:10
...

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 #一次20
n_batch = mnist.train.num_examples //batch_size #次数

x = tf.placeholder(tf.float32,[None,784]) #60000,28*28
y = tf.placeholder(tf.float32,[None,10]) #0-9
keep_prob = tf.placeholder(tf.float32) #keep_prob神经元使用率
lr = tf.Variable(0.001,dtype=tf.float32)

#输入层
w1 = tf.Variable(tf.truncated_normal(([784,800]),stddev = 0.1)) #截断正态分布,stddev是标准差
b1 = tf.Variable(tf.zeros([800])+0.1)
L1 = tf.nn.sigmoid((tf.matmul(x,w1)+b1))
L1 = tf.nn.tanh((tf.matmul(x,w1)+b1))
L1_dropout = tf.nn.dropout(L1,keep_prob)

#中间层
w2 = tf.Variable(tf.truncated_normal(([800,400]),stddev = 0.1))
b2 = tf.Variable(tf.zeros([400])+0.1)
L2 = tf.nn.sigmoid((tf.matmul(L1_dropout,w2)+b2))
#L2 = tf.nn.tanh((tf.matmul(L1_dropout,w2)+b2))
L2_dropout = tf.nn.dropout(L2,keep_prob) #防止或减轻过拟合 (输入tensor,keep_prob: float类型,每个元素被保留下来的概率,设置神经元被选中的概率,在初始化时keep_prob是一个占位符, )

#输出层
w3 = tf.Variable(tf.truncated_normal(([400,10]),stddev=0.1))
b3 = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.matmul(L2_dropout,w3)+b3

#loss = tf.reduce_mean(tf.square(y-prediction)) 下面这个训练效果好
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))

#labels:为神经网络期望的输出,logits:为神经网络最后一层的输出
#这个函数内部自动计算softmax,然后再计算交叉熵代价函数,
#也就是说logits必须是没有经过tf.nn.softmax函数处理的数据,
否则导致训练结果有问题。建议编程序时使用这个函数,而不必自己编写交叉熵代价函数。

train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
train_step = tf.train.AdamOptimizer(lr).minimize(loss)

#equal()返回布尔类型的列表,argmax(参数,0表示列,1表示行) 返回一维张量(1维向量)中最大值所在位置的索引
#y是10维向量,prediction2也是10维向量,所以correct_prediction是包含10个True和False的列表
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))

#准确率即由0,1.0求和在求平均值
accurary = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

for step in range(31):
    sess.run(tf.assign(lr, 0.001*(0.95 ** step)))
    for batch in range(n_batch):
        # 图片数据保存在batch_xs,图片标签保存在batch_ys
        batch_xs,batch_ys = mnist.train.next_batch(batch_size)
        sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0})

    learning_rate = sess.run(lr)
    Test_acc = sess.run(accurary,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
    Train_acc = sess.run(accurary, feed_dict={x: mnist.train.images, y: mnist.train.labels, keep_prob: 1.0})
    print("Iter "+str(step)+" Test_acc "+str(Test_acc) + " Train_acc " + str(Train_acc) + " 学习率 "+str(learning_rate))

#Iter 30 Test_acc 0.9819 Train_acc 0.9961636
#Iter 30 Test_acc 0.9819 Train_acc 1.0 学习率 0.00021463877

相关标签: tensorflow dropout