Tensorflow三

CNN厉害的地方在于感受野和权值共享减少神经网络的训练参数.类比于人,不需要每个神经元对全局图像做感受,每个神经元只感受局部信息,然后在高层组合这些局部信息得到全局信息。相比于全连接层,CNN局部连接大量减少了参数。总的来说CNN能够自己做特征提取。

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
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('/tmp/data',one_hot=True)

#超参数
learning_rate=0.01
num_step = 201
batch_size = 128
hidden_size = 1024
num_input = 784
num_class = 10
#选择激活部分神经元
dropout = 0.75

X = tf.placeholder(tf.float32,[None,num_input])
Y = tf.placeholder(tf.float32,[None,num_class])
keep_prob = tf.placeholder(tf.float32)

#卷积层
def conv2d(x,W,b,strides=1):
#第一个参数和第四个参数都是1
x = tf.nn.conv2d(x,W,strides=[1,strides,strides,1],padding='SAME')
x = tf.nn.bias_add(x,b)
#图像中激活函数一般relu用的比较多
return tf.nn.relu(x)
#池化层
def maxpool2d(x,k=2):
return tf.nn.max_pool(x,ksize=[1,k,k,1],strides=[1,k,k,1],padding='SAME')
#网络模型
def netWork(x,weights,biases,dropout):
x = tf.reshape(x,shape=[-1,28,28,1])
#一个卷积层加一个最大池化层
conv1 = conv2d(x,weights['wc1'],biases['bc1'])
conv1 = maxpool2d(conv1,k=2)
#再来一个卷积层加一个最大池化层
conv2 = conv2d(conv1,weights['wc2'],biases['bc2'])
conv2 = maxpool2d(conv2,k=2)
#一个全连接层,reshape一定要对应
fc1 = tf.reshape(conv2,[-1,weights['fc1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1,weights['fc1']),biases['bd1'])
fc1 = tf.nn.relu(fc1)
#矩阵中一部分置0
fc1 = tf.nn.dropout(fc1,dropout)
out = tf.add(tf.matmul(fc1,weights['out']),biases['out'])
return out

weights = {
'wc1':tf.Variable(tf.random_normal([5,5,1,32])),
'wc2':tf.Variable(tf.random_normal([5,5,32,64])),
'fc1':tf.Variable(tf.random_normal([7*7*64,hidden_size])),
'out':tf.Variable(tf.random_normal([hidden_size,num_class]))
}
biases = {
'bc1': tf.Variable(tf.random_normal([32])),
'bc2': tf.Variable(tf.random_normal([64])),
'bd1': tf.Variable(tf.random_normal([hidden_size])),
'out': tf.Variable(tf.random_normal([num_class]))
}
pred = netWork(X,weights,biases,dropout)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

#计算准确率
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(num_step):
batch_x , batch_y = mnist.train.next_batch(batch_size)
sess.run(optimizer,feed_dict={X:batch_x,Y:batch_y,keep_prob:0.8})
if epoch%5 ==0:
loss,acc = sess.run([cost,accuracy],feed_dict={X:batch_x,Y:batch_y,keep_prob:1.0})
print("Step " + str(epoch) + ", Minibatch Loss= " + "{:.4f}".format(loss) + ", Training Accuracy= " + "{:.3f}".format(acc))

print("Optimization Finished!")
# Calculate accuracy for 256 MNIST test images
print("Testing Accuracy:", sess.run(accuracy, feed_dict={X: mnist.test.images[:256],Y: mnist.test.labels[:256],keep_prob: 1.0}))