0%

Tensorflow-Coding-Notes:dropout

Notes

Note1 tf.nn.dropout()

1
tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None,name=None)
1
keep_prob = tf.placeholder(tf.float32)

Codes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import tensorflow as tf
import numpy as np

from tensorflow.examples.tutorials.mnist import input_data

#载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#每个批次的大小
batch_size = 64
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size


#定义placeholder,x为输入,y为label
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

#定义占位符,dropout的比例,神经元工作的比例
keep_prob = tf.placeholder(tf.float32)

#神经网络参数
L1_size = 200
L2_size = 200
L3_size = 200

#定义神经网络
W1 = tf.Variable(tf.truncated_normal([784,L1_size],stddev = 0.1))
b1 = tf.Variable(tf.zeros([1,L1_size]) + 0.01)
z1 = tf.matmul(x,W1) + b1
a1 = tf.nn.tanh(z1)
L1_drop = tf.nn.dropout(a1,keep_prob)

W2 = tf.Variable(tf.truncated_normal([L1_size,L2_size],stddev=0.1))
b2 = tf.Variable(tf.zeros([1,L2_size]) + 0.01)
z2 = tf.matmul(L1_drop,W2) + b2
a2 = tf.nn.tanh(z2)
L2_drop = tf.nn.dropout(a2,keep_prob)


W3 = tf.Variable(tf.truncated_normal([L2_size,L3_size],stddev=0.1))
b3 = tf.Variable(tf.zeros([1,L3_size]) + 0.01)
z3 = tf.matmul(L2_drop,W3) + b3
a3 = tf.nn.tanh(z3)
L3_drop = tf.nn.dropout(a3,keep_prob)

Wout = tf.Variable(tf.truncated_normal([L3_size,10],stddev=0.1))
bout = tf.Variable(tf.zeros([1,10]) + 0.01)
zout = tf.matmul(L3_drop,Wout) + bout
prediction = tf.nn.softmax(zout)

#代价函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y,logits = prediction))

#梯度下降法的优化器
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#变量初试化
init = tf.global_variables_initializer()

#求准确度
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

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

for epoch in range(100):
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.7})




test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1.0})
print("Iter" + str(epoch) + ",Testing Accuracy:" + str(test_acc) + " | Trainung Accuracy:" + str(train_acc))