日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

cnn_mnist_案例详解

發布時間:2025/3/19 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cnn_mnist_案例详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)# 權重初始化 def weight_variable(shape):# 從截斷的正態分布中輸出隨機值。 # 生成的值服從具有指定平均值和標準偏差的正態分布,如果生成的值大于平均值2個標準偏差的值則丟棄重新選擇。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: 指需要做卷積的輸入圖像,它要求是一個Tensor,具有[batch, in_height, in_width, in_channels]這樣的shape# W: 相當于CNN中的卷積核,它要求是一個Tensor,具有[filter_height, filter_width, in_channels, out_channels]這樣的shape,具體含義是[卷積核的高度,卷積核的寬度,圖像通道數,卷積核個數]# strides: 卷積時在圖像每一維的步長,這是一個一維的向量,長度4# padding: string類型的量,只能是"SAME","VALID"其中之一,這個值決定了不同的卷積方式return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='VALID')# mp2*2池化 def max_pool_2x2(x):# x: 需要池化的輸入,一般池化層接在卷積層后面,所以輸入通常是feature map,依然是[batch, height, width, channels]這樣的shape# ksize: 池化窗口的大小,取一個四維向量,一般是[1, height, width, 1],因為我們不想在batch和channels上做池化,所以這兩個維度設為了1# strides: 和卷積類似,窗口在每一個維度上滑動的步長,一般也是[1, stride,stride, 1]# padding: 和卷積類似,可以取'VALID' 或者'SAME'return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='VALID')import tensorflow as tf sess = tf.InteractiveSession() x = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder("float", [None,10])# 第一層卷積 W_conv1 = weight_variable([5, 5, 1, 32]) 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)# 第二層卷積 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_pool_2x2(h_conv2)# 密集連接層 W_fc1 = weight_variable([4 * 4 * 64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 4*4*64]) h_fc1 = 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_fc1, 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_entropy = -tf.reduce_sum(y_*tf.log(y_conv)) # Adam 優化器根據損失函數對每個參數的梯度的一階矩估計和二階矩估計動態調整針對于每個參數的學習速率。Adam 也是基于梯度下降的方法,但是每次迭代參數的學習步長都有一個確定的范圍,不會因為很大的梯度導致很大的學習步長,參數的值比較穩定。 詳細的話:http://www.cnblogs.com/xinchrome/p/4964930.html train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) sess.run(tf.global_variables_initializer()) with sess.as_default():for i in range(1000):batch = mnist.train.next_batch(50)if i%100 == 0:train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0}, session=sess)print("step {}, training accuracy {}".format(i, train_accuracy))train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})print("test accuracy {}".format(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}, session=sess)))

個人理解:?

?輸入圖片大小為28*28 單通道

5*5*1*32? 5*5卷積核,單通道,32個卷積核

經過第一層卷積層后,輸出大小為? 28-5+1=24? ?24*24*32

池化層移動窗口為 1*2*2*1? 行列stride均為2

經過第一層池化后,輸出大小為? ?24/2 =12? ? ? 12*12*32

?

第二層卷積核? 5*5*32*64? 卷積窗口仍是5*5 通道為32? 64個卷積核

經過第二層卷積后 輸出大小為12-5+1=8? ?8*8*64

經過第二層池化(參數同第一層) , 輸出大小為4*4*64

最后全連接層:將池化輸出平鋪成一維的向量reshape[-1,4*4*64]? ?即1*1024的維度;?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的cnn_mnist_案例详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。