LeNet-5


LeNet-5是一個較簡單的卷積神經網絡。下圖顯示了其結構:輸入的二維圖像,先經過兩次卷積層到池化層,再經過全連接層,最后使用softmax分類作為輸出層

 

模型結構:

LeNet-5共有7層(不包含輸入層),每層都包含可訓練參數;每個層有多個Feature Map,每個FeatureMap通過一種卷積濾波器提取輸入的一種特征,然后每個FeatureMap有多個神經元。

  C1層是一個卷積層
  輸入圖片:32 * 32
  卷積核大小:5 * 5 卷積核種類:6
  輸出featuremap大小:28 * 28 (32-5+1)
  神經元數量:28 * 28 * 6
  可訓練參數:(5 * 5+1) * 6(每個濾波器5 * 5=25個unit參數和一個bias參數,一共6個濾波器)
  連接數:(5 * 5+1) * 6 * 28 * 28

 

  S2層是一個下采樣層

  輸入:28 * 28
  采樣區域:2 * 2
  采樣方式:4個輸入相加,乘以一個可訓練參數,再加上一個可訓練偏置。結果通過sigmoid
  采樣種類:6
  輸出featureMap大小:14 * 14(28/2)
  神經元數量:14 * 14 * 6
  可訓練參數:2 * 6(和的權+偏置)
  連接數:(2 * 2+1) * 6 * 14 * 14
  S2中每個特征圖的大小是C1中特征圖大小的1/4

  C3層也是一個卷積層
  輸入:S2中所有6個或者幾個特征map組合
  卷積核大小:5 * 5
  卷積核種類:16
  輸出featureMap大小:10 * 10
  C3中的每個特征map是連接到S2中的所有6個或者幾個特征map的,表示本層的特征map是上一層提取到的特征map的不同組合
  存在的一個方式是:C3的前6個特征圖以S2中3個相鄰的特征圖子集為輸入。接下來6個特征圖以S2中4個相鄰特征圖子集為輸入。然后的3個以不相鄰的4個特征圖子集為輸入。  最后一個將S2中所有特征圖為輸入。 則:可訓練參數:6 * (3 * 25+1)+6 * (4 * 25+1)+3 * (4 * 25+1)+(25 * 6+1)=1516
  連接數:10 * 10 * 1516=151600

 

  S4層是一個下采樣層
  輸入:10 * 10
  采樣區域:2 * 2
  采樣方式:4個輸入相加,乘以一個可訓練參數,再加上一個可訓練偏置。結果通過sigmoid
  采樣種類:16
  輸出featureMap大小:5 * 5(10/2)
  神經元數量:5 * 5 * 16=400
  可訓練參數:2 * 16=32(和的權+偏置)
  連接數:16 * (2 * 2+1) * 5 * 5=2000
  S4中每個特征圖的大小是C3中特征圖大小的1/4

  C5層是一個卷積層
  輸入:S4層的全部16個單元特征map(與s4全相連)
  卷積核大小:5 * 5 卷積核種類:120
  輸出featureMap大小:1 * 1(5-5+1)
  可訓練參數/連接:120 * (16 * 5 * 5+1)=48120

  F6層全連接層
  輸入:c5 120維向量
  計算方式:計算輸入向量和權重向量之間的點積,再加上一個偏置,結果通過sigmoid函數
  可訓練參數:84 * (120+1)=10164

模型特性:

  • 卷積網絡使用一個3層的序列:卷積、池化、非線性——這可能是自這篇論文以來面向圖像的深度學習的關鍵特性!
  • 使用卷積提取空間特征
  • 使用映射的空間均值進行降采樣
  • tanh或sigmoids非線性
  • 多層神經網絡(MLP)作為最終的分類器
  • 層間的稀疏連接矩陣以避免巨大的計算開銷

 

import tensorflow.examples.tutorials.mnist.input_data as input_data
import tensorflow as tf

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)


def compute_accuracy(v_xs,v_ys):
    global prediction
    y_pre = sess.run(prediction,feed_dict={xs:v_xs})
    correct_prediction = tf.equal(tf.argmax(y_pre,1),tf.argmax(v_ys,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    result = sess.run(accuracy,feed_dict={xs:v_xs,ys:v_ys})

    return result
xs = tf.placeholder(tf.float32,[None,784])
ys = tf.placeholder(tf.float32,[None,10])

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

def weights(shape):
    weight = tf.truncated_normal(shape,stddev=0.1)
    return tf.Variable(weight)

def biases(shape):
    bias = tf.constant(0.1,shape=shape)
    return tf.Variable(bias)

def conv2d(x,W):
    return tf.nn.conv2d(input=x,filter=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')

# 1st layer: conv+relu+max_pool
w_conv1 = weights([5,5,1,6])
b_conv1 = biases([6])
h_conv1 = tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

# 2nd layer: conv+relu+max_pool
w_conv2 = weights([5,5,6,16])
b_conv2 = biases([16])
h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2)+b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*16])

# 3rd layer: 3*full connection
w_fc1 = weights([7*7*16,120])
b_fc1 = biases([120])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1)+b_fc1)

w_fc2 = weights([120, 84])
b_fc2 = biases([84])
h_fc2 = tf.nn.relu(tf.matmul(h_fc1, w_fc2)+b_fc2)

w_fc3 = weights([84, 10])
b_fc3 = biases([10])
prediction = tf.nn.softmax(tf.matmul(h_fc2, w_fc3)+b_fc3)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)



with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for i in range(2000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys})
        if i % 100 == 0:
            print(compute_accuracy(mnist.test.images, mnist.test.labels))

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM