一、简单的一层神经网络
import tensorflow as tf
#下载MNIST数据集(28*28,输入维度为784)
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
#构建简单的一层神经网络,包括常见参数weights、biases和activation_function
#首先定义添加神经层的函数def add_layer(),它有四个参数:输入值、输入的大小、输出的大小和激励函数
def add_layer(inputs,in_size,out_size,activation_function = None):
#定义weights和biases,weight为随机变量(variable)
Weights = tf.Variable(tf.random_normal([in_size,out_size]))#矩阵大小为in_size*out_size
biases = tf.Variable(tf.zeros([1,out_size])+0.1)#biases不为0,加上任意一个小数值
#定义Wx_plus_b, 即神经网络未激活的值
Wx_plus_b = tf.matmul(inputs,Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
#定义一个计算准确率的函数
def compute_accuracy(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict={xs: v_xs})
correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys})
return result
# 定义placeholder存放数据
xs = tf.placeholder(tf.float32, [None, 784])
ys = tf.placeholder(tf.float32, [None, 10])
#用add_layer函数搭建一个最简单的训练网络结构,只有输入层和输出层
prediction = add_layer(xs, 784, 10, activation_function=tf.nn.softmax)
#损失函数(cross_entropy)和优化方法
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),
reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
#session会话控制
sess = tf.Session()
sess.run(tf.global_variables_initializer())
#训练1000次,每训练50次输出测试数据的训练精度
for i in range(1000):
#开始训练,训练集中每次取100个数据(batch_xs, batch_ys)
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys})#placeholder和feed_dict同时出现
if i%50 == 0:
print(compute_accuracy( mnist.test.images, mnist.test.labels))
结果为:
二、Dropout的使用
1.Dropout指部分神经元的激活值以一定的概率p暂停工作,在这次训练过程中不更新权值,但它的权值仍保留。
2. tf.nn.dropout
tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None,name=None)
keep_prob为保留概率,即我们要保留的结果所占比例
它作为一个placeholder,在run时传入,一般用于全连接层。
下面在前面的代码的基础上加上dropout,需要改动的地方如下
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
在def layer时增加一行 Wx_plus_b = tf.nn.dropout(Wx_plus_b, keep_prob)
其余不变
def add_layer(inputs,in_size,out_size,activation_function = None):
Weights = tf.Variable(tf.random_normal([in_size,out_size]))
biases = tf.Variable(tf.zeros([1,out_size])+0.1)
Wx_plus_b = tf.matmul(inputs,Weights) + biases
#这里增加一行dropout
Wx_plus_b = tf.nn.dropout(Wx_plus_b, keep_prob)
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
注意:因为keep_prob作为一个placeholder,所以在feed_dict中传入!
keep_prob: 1(保留全部神经元) keep_prob: 0.5(保留50%神经元)
dropout仅在train中使用,在验证和test中keep_prob为1
#定义一个计算准确率的函数
def compute_accuracy(v_xs, v_ys):
global prediction
#keep_prob为1
y_pre = sess.run(prediction, feed_dict={xs: v_xs,keep_prob: 1})
correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys,keep_prob: 1})
return result
keep_prob作为一个placeholder,在run时传入
增加一行:keep_prob = tf.placeholder(tf.float32)
#keep_prob作为一个placeholder,在run时传入
keep_prob = tf.placeholder(tf.float32)
# 定义placeholder存放数据
xs = tf.placeholder(tf.float32, [None, 784])
ys = tf.placeholder(tf.float32, [None, 10])
#用add_layer函数搭建一个简单的训练网络结构
layer1 = add_layer(xs, 784, 50, activation_function=tf.nn.tanh)
prediction = add_layer(layer1, 50, 10, activation_function=tf.nn.softmax)
#损失函数(cross_entropy)和优化方法
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),
reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
在run时传入keep_prob
#session会话控制
sess = tf.Session()
sess.run(tf.global_variables_initializer())
#训练1000次,每训练50次输出测试数据的训练精度
for i in range(1000):
#开始训练,训练集中每次取100个数据(batch_xs, batch_ys)
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5})#placeholder和feed_dict同时出现
if i%50 == 0:
print(compute_accuracy( mnist.test.images, mnist.test.labels))
结果:(变差了,我只是随便试一试)