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

2021-08-20 20:30:57 字數 3321 閱讀 3119

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=100#批次大小

n_batch=mnist.train.num_examples//batch_size#批次數量

#初始化權重

def weight_variable(shape):

initial=tf.truncated_normal(shape,stddev=0.1)

return tf.variable(initial)

def bias_variable(shape):

initial=tf.constant(0.1,shape=shape)

return tf.variable(initial)

#卷積層

def conv2d(x,w):

#x是輸入【batch,輸入高度,輸入寬度,輸入通道】

#w是過濾器【高度,寬度,輸入通道數,輸出通道數】

#stride=stride,stride代表x方向步長,stride代表y方向的步長

#padding:「same」,"valid"

return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding='same')

#池化層

def max_pool2_2(x):

return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='same')

x=tf.placeholder(tf.float32,[none,784])

y=tf.placeholder(tf.float32,[none,10])

x_image=tf.reshape(x,[-1,28,28,1])

#初始化第一層偏差與權重

w_conv1=weight_variable([5,5,1,32])

b_conv1=bias_variable([32])

#把x_image和權值進行卷積,再加上偏置,再用啟用函式

h_conv1=tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1)

h_pool1=max_pool2_2(h_conv1)

#第二層

#初始化

w_conv2=weight_variable([5,5,32,64])

b_conv2=bias_variable([64])

#卷積h_conv2=tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)

h_pool2=max_pool2_2(h_conv2)

28*28第一次卷積以後是28*28,第一次池化後為14*14,

第二次卷積為14*14,池化後為7*7,

最後得到乙個7*7的64通路的tensor

#初始化全連線層

w_fc1=weight_variable([7*7*64,1024])

b_fc1=bias_variable([1024])

h_pool2_flat=tf.reshape(h_pool2,[-1,7*7*64])

h_fcl=tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1)+b_fc1)

#keep_prob輸出概率

keep_prob=tf.placeholder(tf.float32)

h_fc1_drop=tf.nn.dropout(h_fcl,keep_prob)

#第二個全連線層

w_fc2=weight_variable([1024,10])

b_fc2=bias_variable([10])

predction=tf.nn.softmax(tf.matmul(h_fc1_drop,w_fc2)+b_fc2)

cross_entropy=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=predction))

train_step=tf.train.adamoptimizer(1e-4).minimize(cross_entropy)

correct_prediction=tf.equal(tf.arg_max(predction,1),tf.arg_max(y,1))

accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.session() as sess:

sess.run(tf.global_variables_initializer())

for epoch in range(21):

for batch in range(n_batch):

batch_xs,batch_ys=mnist.train.next_batch(batch_size)

sess.run(train_step,feed_dict=)

acc=sess.run(accuracy,feed_dict=)

print("iter"+str(epoch)+" testing accuracy= "+str(acc))

執行結果如下:

iter0 testing accuracy= 0.9472

iter1 testing accuracy= 0.971

iter2 testing accuracy= 0.9751

iter3 testing accuracy= 0.9796

iter4 testing accuracy= 0.9833

iter5 testing accuracy= 0.985

iter6 testing accuracy= 0.987

iter7 testing accuracy= 0.9864

iter8 testing accuracy= 0.9876

iter9 testing accuracy= 0.9872

iter10 testing accuracy= 0.9878

Tensorflow實現乙個簡單的卷積神經網路

今天對照tensorflow的書,實現了乙個簡單的卷積神經網路。基於mnist資料集。在神經網路還未出現之前,在處理影象的問題上都是使用的特徵工程 如sift 特徵工程是有侷限性的。cnn被用於影象處理,不需要將特徵提取和分類訓練兩個過程分開,它在訓練時就自動提取了最有效的特徵。卷積神經網路由多個卷...

反卷積實現 tensorflow 實現

deconv解卷積,實際是叫做conv transpose,conv transpose實際是卷積的乙個逆向過程,tf中,編寫conv transpose 的時候,心中想著乙個正向的卷積過程會很有幫助。想象一下我們有乙個正向卷積 input shape 1,5,5,3 kernel shape 2,...

從零開始用TensorFlow搭建卷積神經網路

by 蔣思源 2017年8月29日 14 50 機器之心基於 ahmet taspinar 的博文使用 tensorflow 手動搭建卷積神經網路,並提供所有 和注釋的 jupyter notebook 文件。我們將不僅描述訓練情況,同時還將提供各種背景知識和分析。所有的 和執行結果都已上傳至 gi...