使用卷積神經網絡實作手寫體數字識别,在過程中使用了不同的卷積層個數及全連接配接層的長度,在組模組化型的過程中發現卷積層中的圖像深度可以調節,在全連接配接層中,全連接配接層的長度可以自由定義。例如:對于一個卷及神經網絡模型,
| conv1-5x5x32 | max_pool1-2x2 |
|conv2-5x5x64|max_pool2-2x2|
| fc1-512 | fc2-10 |
對此模型,我們可以進行更改,将此模型改為
- conv1-5x5x16
- max_pool1-2x2
- conv2-5x5x64
- conv3-5x5x128
- max-pool2-2x2
- fc1-3000
- fc2-1024
-
fc3-10
模型更改後對模型進行訓練,發現模型訓練時間變長,且在增加模型卷積層的深度以及全連接配接層的節點數後模型訓練的正确率反而降低。
下面為修改後的模型代碼:
#coding:utf-8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("MNIST_data",one_hot=True)
x=tf.placeholder(tf.float32,shape=[None,784])
y_=tf.placeholder(tf.float32,shape=[None,10])
x_image=tf.reshape(x,[-1,28,28,1])
#定義權重
def weight_variable(shape):
initializer=tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(initializer)
def biase_variable(shape):
initializer=tf.constant(0.1,shape=shape)
return tf.Variable(initializer)
def conv2d(x,w):
return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding="SAME")
def max_pool_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
#第一層卷積
w_conv1=weight_variable([5,5,1,16])
b_conv1=biase_variable([16])
h_conv1=tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1)
#第一層池化
h_pool1=max_pool_2x2(h_conv1)
#第二層卷積
w_conv2=weight_variable([5,5,16,64])
b_conv2=weight_variable([64])
h_conv2=tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)
#第三層卷積
w_conv3=weight_variable([5,5,64,128])
b_conv3=weight_variable([128])
h_conv3=tf.nn.relu(conv2d(h_conv2,w_conv3)+b_conv3)
#第二層池化
h_pool2=max_pool_2x2(h_conv3)
#第一層全連接配接
w_fc1=weight_variable([7*7*128,3000])
b_fc1=biase_variable([3000])
h_pool2_flat=tf.reshape(h_pool2,[-1,7*7*128])
h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1)+b_fc1)
keep_prob=tf.placeholder(tf.float32)
h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)
#第二層全連接配接層
w_fc2=weight_variable([3000,1024])
b_fc2=biase_variable([1024])
y_conv2=tf.matmul(h_fc1_drop,w_fc2)+b_fc2
#第三層全連接配接層
w_fc3=weight_variable([1024,10])
b_fc3=weight_variable([10])
y_conv=tf.matmul(y_conv2,w_fc3)+b_fc3
cross_entropy=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits=y_conv))
train_step=tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
corrent_validation=tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
accuray=tf.reduce_mean(tf.cast(corrent_validation,dtype=tf.float32))
sess=tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
for i in range(2000):
batch=mnist.train.next_batch(50)
if i %100==0:
train_accurcy=accuray.eval(feed_dict={x:batch[0],y_:batch[1],keep_prob:1.0})
print("step %d,training accuracy is %g"%(i,train_accurcy))
train_step.run(feed_dict={x:batch[0],y_:batch[1],keep_prob:0.5})
print("test accurcy %g"%accuray.eval(feed_dict={x:mnist.test.images,y_:mnist.test.labels,keep_prob:0.5}))
step 1400,training accuracy is 0.96
step 1500,training accuracy is 0.96
step 1600,training accuracy is 0.96
step 1700,training accuracy is 0.98
step 1800,training accuracy is 0.96
step 1900,training accuracy is 1
2018-10-16 17:00:04.703214:#########
2018-10-16 17:00:05.853831: ########
2018-10-16 17:00:07.134570: ########
test accurcy 0.957
可以看出在訓練次數為2000次的情況下,最終正确率能夠達到95.7%,而采用第一個模型正确率可以達到98%,。是以在模型的建構上應該采用合适的深度以及節點個數。