记录MNIST采用卷积方式实现与理解
從時(shí)間上來(lái)說(shuō),這篇文章寫的完了,因?yàn)檫@個(gè)實(shí)驗(yàn)早就做完了;但從能力上來(lái)說(shuō),這篇文章出現(xiàn)的早了,因?yàn)楹芏嗟胤轿叶歼€沒(méi)有理解。如果不現(xiàn)在寫,不知道什么時(shí)候會(huì)有時(shí)間是其一,另外一個(gè)原因是怕自己過(guò)段時(shí)間忘記。
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 # @Author : mario 5 # @File : mnist_faltung.py 6 # @Project : base 7 # @Time : 2018-12-19 14:11:38 8 # @Desc : File is ... 9 10 import tensorflow as tf 11 from tensorflow.examples.tutorials.mnist import input_data 12 13 mnist = input_data.read_data_sets("data/", one_hot=True) 14 15 16 def init_weight_variable(shape): 17 initial = tf.truncated_normal(shape, stddev=0.1) 18 return tf.Variable(initial) 19 20 21 def init_bias_variable(shape): 22 initial = tf.constant(0.1, shape=shape) 23 return tf.Variable(initial) 24 25 26 def conv2d(x, W): 27 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME") 28 29 30 def max_pool_2x2(x): 31 return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") 32 33 34 x = tf.placeholder(tf.float32, [None, 784]) 35 y_ = tf.placeholder(tf.float32, [None, 10]) 36 37 W_conv_1 = init_weight_variable([5, 5, 1, 32]) 38 b_conv_1 = init_bias_variable([32]) 39 40 x_image = tf.reshape(x, [-1, 28, 28, 1]) 41 42 h_conv_1 = tf.nn.relu(conv2d(x_image, W_conv_1) + b_conv_1) 43 h_pool_1 = max_pool_2x2(h_conv_1) 44 45 W_conv_2 = init_weight_variable([5, 5, 32, 64]) 46 b_conv_2 = init_bias_variable([64]) 47 48 h_conv_2 = tf.nn.relu(conv2d(h_pool_1, W_conv_2) + b_conv_2) 49 h_pool_2 = max_pool_2x2(h_conv_2) 50 51 W_fc_1 = init_weight_variable([7 * 7 * 64, 1024]) 52 b_fc_1 = init_bias_variable([1024]) 53 54 h_pool_flat = tf.reshape(h_pool_2, [-1, 7 * 7 * 64]) 55 h_fc_1 = tf.nn.relu(tf.matmul(h_pool_flat, W_fc_1) + b_fc_1) 56 57 keep_prob = tf.placeholder(tf.float32) 58 h_fc1_drop = tf.nn.dropout(h_fc_1, keep_prob) 59 60 W_fc_2 = init_weight_variable([1024, 10]) 61 b_fc_2 = init_bias_variable([10]) 62 63 y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc_2) + b_fc_2) 64 65 cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) 66 train_step = tf.train.AdagradOptimizer(1e-4).minimize(cross_entropy) 67 68 sess = tf.InteractiveSession() 69 init = tf.global_variables_initializer() 70 sess.run(init) 71 72 for _ in range(20000): 73 batch = mnist.train.next_batch(50) 74 train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) 75 76 correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) 77 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 78 79 print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))python版本:Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31) ;tensor flow版本:1.12.0
10、11行:導(dǎo)入必要模塊
13行:加載本地的數(shù)據(jù)
16~18行:定義初始化權(quán)重變量函數(shù)
21~23行:定義初始化偏置變量函數(shù)
26~27行:定義一個(gè)步長(zhǎng)為1,邊距為0的2x2的卷積函數(shù)
30~31行:定義一個(gè)2x2的池化函數(shù)
34、35行:定義占位符x和y_,其中x是為了接收原始數(shù)據(jù),y_是為了接受原始數(shù)據(jù)標(biāo)簽
37、38行:初始化第一層卷積權(quán)重和偏置量
40行:重塑原始數(shù)據(jù)結(jié)構(gòu),我們要把[n,784]這樣的數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換成卷積需要的[n,28,28,1],這里的n是指數(shù)據(jù)量,原始數(shù)據(jù)是將28x28像素的圖片展開(kāi)為784,卷積相當(dāng)于我們先將數(shù)據(jù)還原為28x28,最后的1是指通道數(shù)
42、43行:進(jìn)行卷積操作,并將卷積結(jié)果池化
45~49行:進(jìn)行第二次卷積操作,同樣也是將卷積結(jié)果池化
51~61行:使用ReLU,其中57行和58行是為了防止過(guò)擬合
63行:使用softmax算法確定其分類
65行:計(jì)算交叉熵
66行:利用交叉熵,調(diào)用AdagradOptimizer算法,訓(xùn)練模型
68~70行:啟用session,初始化變量
72~74行:每次50個(gè)訓(xùn)練20000次
76~79行:評(píng)估模型識(shí)別率
也是遇到了很多的問(wèn)題,但幾乎都是因?yàn)椴焕斫獯a造成的,雖然現(xiàn)在代碼是改對(duì)了,但是不理解的地方還是有很多,而且很多概念也是不理解,并且不知道實(shí)際上是做了什么操作,比如說(shuō)卷積、池化等,倒是做了什么?感覺(jué)這個(gè)還是需要后續(xù)了解的。“路漫漫其修遠(yuǎn)兮,吾將上下而求索”用在這里再合適不過(guò)了。
?
轉(zhuǎn)載于:https://www.cnblogs.com/ben-mario/p/10180978.html
總結(jié)
以上是生活随笔為你收集整理的记录MNIST采用卷积方式实现与理解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于在本地idea当中提交spark代码
- 下一篇: 爬取小说 spider