卷积神经网络的卷积核的每个通道是否相同?

假设输入数据的格式是[?,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())



 

 

实验结果:

可见,每个通道的卷积参数各不相同。

 

  • 11
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值