卷積神經網路 Mnist資料集測試

2021-09-16 21:14:17 字數 4501 閱讀 7061

卷積神經網路

mnist是在機器學習領域中的乙個經典問題。該問題解決的是把28x28畫素的灰度手寫數字識別為相應的數字,其中數字的範圍從0到9.

print("第%d步正確率%f" % (i, accuracy.eval(feed_dict=)))

通過只有乙個全連線層來實現的網路的準確率大概只有91%,這樣的

訓練結果並不是很理想

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):

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')

sess = tf.interactivesession()

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

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

# 第一次卷積

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

# 5*5的取樣視窗,32個卷積核從1個平面抽取特徵

b_conv1 = bias_variable([32]) # 每乙個卷積核乙個偏置值

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

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

h_pool1 = max_pool_2x2(h_conv1)

# 第二層卷積

# 5*5的取樣視窗,64個卷積核從32個平面抽取特徵

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

b_conv2 = weight_variable([64])

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

h_pool2 = max_pool_2x2(h_conv2)

# 28*28的第一次卷積後還是28*28(陣列變小了,但是影象大小不變),第一次池化後變為14*14

# 第二次卷積後為14*14(卷積不會改變平面的大小),第二次池化後變為了7*7

# 進過上面操作後得到64張7*7的平面

# 密集連線層

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_fc = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)

# dropout 防止過擬合

keep_prob = tf.placeholder("float")

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

# 輸出層

w_fc2 = weight_variable([1024, 10])

b_fc2 = bias_variable([10])

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

cross_entry = -tf.reduce_sum(y_ * tf.log(y_conv))

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

correct_predict = tf.equal(tf.arg_max(y_conv, 1), tf.arg_max(y_, 1))

accuracy = tf.reduce_mean(tf.cast(correct_predict, "float"))

sess.run(tf.initialize_all_variables())

for i in range(1000):

batch = mnist.train.next_batch(50)

if i % 100 == 0:

train_accuracy = accuracy.eval(feed_dict=)

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

train_step.run(feed_dict=)

tf.nn.max_pool引數含義和用法

max pooling是cnn當中的最大值池化操作,其實用法和卷積很類似

tf.nn.max_pool(value, ksize, strides, padding, name=none)

引數是四個,和卷積很類似:

第乙個引數value:需要池化的輸入,一般池化層接在卷積層後面,所以輸入通常是feature map,依然是[batch, height, width, channels]這樣的shape

第二個引數ksize:池化視窗的大小,取乙個四維向量,一般是[1, height, width, 1],因為我們不想在batch和channels上做池化,所以這兩個維度設為了1``

第三個引數strides:和卷積類似,視窗在每乙個維度上滑動的步長,一般也是[1, stride,stride, 1]

第四個引數padding:和卷積類似,可以取'valid' 或者'same'

返回乙個tensor,型別不變,shape仍然是[batch, height, width, channels]這種形式

tf.nn.conv2d

tf.nn.conv2d是tensorflow裡面實現卷積的函式,參考文件對它的介紹並不是很詳細,實際上這是搭建卷積神經網路比較核心的乙個方法,非常重要

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=none, name=none)

除去name引數用以指定該操作的name,與方法有關的一共五個引數:

卷積神經網路應用於MNIST資料集分類

import tensorflow as tf from tensorflow.examples.tutorials.mnist import input data mnist input data.read data sets mnist data one hot true 每個批次的大小 bat...

用簡單卷積神經網路實現MNIST資料集識別

第一次寫部落格,記錄一下最近的學習經歷吧,最近在學卷積神經網路,自己就寫了乙個比較簡單的卷積神經網路實現了mnist資料集的識別,本來是想用lenet5來實現的,感覺lenet5太老了,所以就寫了乙個差不多的卷積神經網路來實現mnist資料集的識別。希望可以幫助一些剛學習卷積神經網路的朋友,也可以根...

神經網路實現Mnist資料集簡單分類

import tensorflow as tf from tensorflow.examples.tutorials.mnist import input data 首先要載入資料集 載入資料集 mnist input data.read data sets mnist data one hot t...