數字手寫體的卷積神經網路實現

2021-08-29 03:59:59 字數 3418 閱讀 1210

使用卷積神經網路實現手寫體數字識別,在過程中使用了不同的卷積層個數及全連線層的長度,在組建模型的過程中發現卷積層中的影象深度可以調節,在全連線層中,全連線層的長度可以自由定義。例如:對於乙個捲及神經網路模型,

| conv1-5x5x32 | max_pool1-2x2 |

|conv2-5x5x64|max_pool2-2x2|

| fc1-512 | fc2-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=)

print("step %d,training accuracy is %g"%(i,train_accurcy))

train_step.run(feed_dict=)

print("test accurcy %g"%accuray.eval(feed_dict=))

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%,。因此在模型的構建上應該採用合適的深度以及節點個數。

卷積神經網路分類mnist手寫體數字

import tensorflow as tf from tensorflow.examples.tutorials.mnist import input data mnist input data.read data sets mnist data one hot true import matp...

mnist手寫體識別 卷積神經網路

coding utf 8 通過卷積神經網路進行 author elijah 引入資料集 from tensorflow.examples.tutorials.mnist import input data import tensorflow as tf mnist input data.read d...

tensorflow實現卷積神經網路手寫數字識別

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 ...