1. 程式人生 > >卷積神經網路的卷積核的每個通道是否相同?

卷積神經網路的卷積核的每個通道是否相同?

假設輸入資料的格式是[?,28,28,16],卷積核的尺寸是[3,3,16,32]

輸入資料的格式的含義是:

                                                  ?:batchsize

                                                  28,28:feature map單個通道的尺寸(高,寬)

                                                 16:feature map的通道數

卷積核格式的含義:

                                            3,3:卷積核的高與寬

                                           16:   卷積核的通道數

                                            32:卷積核的個數

[3,3,16,32]的含義是指:卷積核的尺寸是3*3*16,寬為3,高為3,通道數為16(對應被卷積的張量的通道數),共有32個卷積核

卷積的過程是:對於單個卷積核,有16個通道,每個通道的分量分別與對應的被卷積張量的對應通道卷積,得到16個通道的卷積結果,然後這16個通道的卷積結果按元素疊加,生成一個通道的卷積結果,然後該卷積結果再經過啟用函式,得到最終的卷積結果。(至此,一個16通道的張量,經過一個16通道的卷積核後,得到了一個單通道的張量)                                    

                 一個卷積核得到一個單通道的張量,共有32個卷積核,可得到32個單通道的張量,最後將這些張量連線起來,得到一個32通道的張量結果。

                 備註:卷積的實現過程分兩種情況 1)pointwise卷積 2)depthwise卷積

                 1)pointwise卷積    同時對featuremap的所有通道進行卷積,直接生成最後的卷積結果

               2)depthwise卷積(深度可分離卷積)各個通道分別卷積完之後,再疊加生成最後的featuremap

值得說明的是,卷積核的尺寸是3*3*16,有16個通道,這16個通道的卷積核內容並不是共享的,它們各不相同。所以,一個卷積核產生的變數數目是 3*3*16*32個。

實驗測試如下:

#-*-coding:utf-8-*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)

##準備資料
X  = tf.placeholder(tf.float32,[None,784])
Y_ = tf.placeholder(tf.float32,[None,10])

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

#定義卷積層和偏置層


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):
    conv_result = tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
    return conv_result
def max_pooling_2x2(x):
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')


##第一層卷積
W_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)#-1*28*28*32
h_pool1 = max_pooling_2x2(h_conv1)

##第2層卷積
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_pooling_2x2(h_conv2)
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])

##全連線1
w_fc1 = weight_variable([7*7*64,1024])
b_fc1 = bias_variable([1024])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1)+b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)

##全連線2
w_fc2 = weight_variable([1024,10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop,w_fc2)+b_fc2

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=Y_,logits=y_conv))

##準確率
correct_predict = tf.equal(tf.argmax(y_conv,1),tf.argmax(Y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_predict,tf.float32))

##定義訓練過程
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)



########

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

for i in range(5000):
    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})
        print("step %d,training accuracy %g"%(i,train_accuracy))

    train_step.run(feed_dict={ X:batch[0],Y_:batch[1],keep_prob:0.5})

print("test accuracy %g"%accuracy.eval(feed_dict = {
    X:mnist.test.images,Y_:mnist.test.labels,keep_prob:1.0}))

print("W_conv2[:,:,0,0]")
print(W_conv2[:,:,0,0].eval())

print("W_conv2[:,:,1,0]")
print(W_conv2[:,:,1,0].eval())

print("W_conv2[:,:,2,0]")
print(W_conv2[:,:,2,0].eval())

print("W_conv2[:,:,0,1]")
print(W_conv2[:,:,0,1].eval())



 

 

實驗結果:

可見,每個通道的卷積引數各不相同。