卷積神經網絡三大特點
1.局部連接,局部連接會大大減少網絡的參數。在處理圖像這樣的高維度輸入時,讓每個神經元都與前一層中的所有神經元進行全連接是不現實的。讓每個神經元只與輸入數據的一個局部區域連接,該連接的空間大小叫做神經元的感受野,它的尺寸是一個超參數,其實就是濾波器的空間尺寸。
2.權值共享,在卷積層中使用參數共享是用來控制參數的數量。每個濾波器與上一層局部連接,同時每個濾波器的所有局部連接都使用同樣的參數,此舉會同樣大大減少網絡的參數。
3.空間或時間上的下采樣,它的作用是逐漸降低數據的空間尺寸,這樣的話就能減少網絡中參數的數量,使得計算資源耗費變少,也能有效控制過擬合。
卷積神經網絡代碼實現
卷積神經網絡【Convolutional Neural Networks,CNN】是一類包含卷積計算且具有深度結構的前饋神經網絡【Feedforward Neural Networks】是深度學習的代表算法之一。卷積神經網絡具有表征學習【representation learning】能力,能夠按其階層結構對輸入信息進行平移不變分類。
神經網絡實質上是多層函數嵌套形成的數學模型。1998年Yann LeCun等人推出了LeNet-5架構,廣泛用于手寫字體識別,包含全連接層和sigmoid激活函數,還有卷積層和池化層。
1 # -*- coding: utf-8 -*-
2 “”“
3 Created on Wed Nov 21 17:32:28 2018
4
5 @author: zhen
6 ”“”
7
8 import tensorflow as tf
9 from tensorflow.examples.tutorials.mnist import input_data
10
11 mnist = input_data.read_data_sets(‘C:/Users/zhen/MNIST_data_bak/’, one_hot=True)
12 sess = tf.InteractiveSession()
13
14 def weight_variable(shape):
15 initial = tf.truncated_normal(shape, stddev=0.1)
16 return tf.Variable(initial)
17
18 def bias_variable(shape):
19 initial = tf.constant(0.1, shape=shape)
20 return tf.Variable(initial)
21
22 def conv2d(x, W):
23 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding=‘SAME’)
24
25 def max_pool_2x2(x):
26 return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding=‘SAME’)
27
28 x = tf.placeholder(tf.float32, [None, 784])
29 y = tf.placeholder(tf.float32, [None, 10])
30 x_image = tf.reshape(x, [-1, 28, 28, 1])
31
32 # 第一層卷積核
33 W_conv = weight_variable([5, 5, 1, 16])
34 b_conv = bias_variable([16])
35 h_conv = tf.nn.relu(conv2d(x_image, W_conv) + b_conv)
36 h_pool = max_pool_2x2(h_conv)
37
38 # 第二層卷積核
39 W_conv2 = weight_variable([5, 5, 16, 32])
40 b_conv2 = bias_variable([32])
41 h_conv2 = tf.nn.relu(conv2d(h_pool, W_conv2) + b_conv2)
42 h_pool2 = max_pool_2x2(h_conv2)
43
44 # 全連接層
45 W_fc = weight_variable([7 * 7 * 32, 512])
46 b_fc = bias_variable([512])
47 h_pool_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 32])
48 h_fc = tf.nn.relu(tf.matmul(h_pool_flat, W_fc) + b_fc)
49
50 # 防止過擬合,使用Dropout層
51 keep_prob = tf.placeholder(tf.float32)
52 h_fc_drop = tf.nn.dropout(h_fc, keep_prob)
53
54 # Softmax分類
55 W_fc2 = weight_variable([512, 10])
56 b_fc2 = bias_variable([10])
57 y_conv = tf.nn.softmax(tf.matmul(h_fc_drop, W_fc2) + b_fc2)
58
59 # 定義損失函數
60 cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y_conv), reduction_indices=[1]))
61 train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
62 correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))
63 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
64
65 # 訓練
66 tf.global_variables_initializer().run()
67 for i in range(20):
68 batch = mnist.train.next_batch(50)
69 train_step.run(feed_dict={x:batch[0], y:batch[1], keep_prob:0.5})
70
71 print(“test accuracy %g” % accuracy.eval(feed_dict={x:mnist.test.images, y:mnist.test.labels, keep_prob:1.0}))
結果:
1.算法模型不變,增大訓練集數據【隱藏一層16個卷積核,隱藏二層32個卷積核,全連接層512,10分類】:
2.訓練集數據不變,增大卷積核數【數據集為10000,全連接層512,10分類】:
責任編輯:YYX
-
cnn
+關注
關注
3文章
354瀏覽量
22619 -
卷積神經網絡
+關注
關注
4文章
369瀏覽量
12170
發布評論請先 登錄
評論