卷积层

     在图像处理中,往往将图像转化为像素,如讲1000*1000的图像转化为1000000的向量。如果假设隐藏层也是为1000000时,则权值参数诶1000000000000个,这种权值参数太多了,无法进行训练,所以需要减少权值参数的个数。一般有两种方法进行减少:

      第一种称为局部感知野。一般认为人对外界的认知时从局部到全局的,而图像的空间联系也是局部像素联系较为紧密。所以每个神经元没有必要对全局进行感知,只需要对局部进行感知,然后由更高层将局部的信息综合起来就得到了全局的信息。假如每个神经元只和10*10个像素值相连,那么权值个数为1000000*100,减少了万分之一,而10*10个像素值对应的10*10个参数,就是卷积操作的卷积核。

      第二种称为参数共享。虽然采用局部感知的方式减少了权值,但是还是太多,这里通过权值共享,即这1000000个神经元的100个参数都是相同的,那么权值参数就变成了100了。

一个实例:

import tensorflow as tf
import numpy as np

image = np.array([[1,1,1,0,0],
                  [0,1,1,1,0],
                  [0,0,1,1,1],
                  [0,0,1,1,0],
                  [0,1,1,0,0]])

weight = np.array([[1,0,1],
                    [0,1,0],
                    [1,0,1]])
bias = 0
input = tf.constant(image, dtype=tf.float32)
filter = tf.constant(weight, dtype=tf.float32)
input = tf.reshape(input, [1,5,5,1])
filter = tf.reshape(filter, [3,3,1,1])
result = tf.nn.conv2d(input, filter, strides=[1,1,1,1], padding="VALID")
with tf.Session() as sess:
    idata = sess.run(input)
    filt = sess.run(filter)
    res = sess.run(result)
    print(res)

输出为1*3*3*1矩阵:

4  3  4

2  4  3

2  3  4


此时卷积的padding="VALID", 输出大小计算公式为((width - filter + 2*padding)/stride)+1,  对于VALID,为width/stride  向下取整, 本例中(5-3+0)+1=3,如果收入的width=6,stride=2,则输出等于2.5,实际输出为2.实例如下:

import tensorflow as tf
import numpy as np

image = np.array([[1,1,1,0,0,1],
                  [0,1,1,1,0,1],
                  [0,0,1,1,1,1],
                  [0,0,1,1,0,1],
                  [0,1,1,0,0,1],
                  [1,1,1,1,1,1]])

weight = np.array([[1,0,1],
                    [0,1,0],
                    [1,0,1]])
bias = 0
input = tf.constant(image, dtype=tf.float32)
filter = tf.constant(weight, dtype=tf.float32)
input = tf.reshape(input, [1,6,6,1])
filter = tf.reshape(filter, [3,3,1,1])
result = tf.nn.conv2d(input, filter, strides=[1,2,2,1], padding="VALID")
with tf.Session() as sess:
    idata = sess.run(input)
    filt = sess.run(filter)
    res = sess.run(result)
    print(res)

输出为1*2*2*1矩阵:

4    4

2    4


如果padding设置为SAME,此时输出为width/stride上取整, 则输出为:

import tensorflow as tf
import numpy as np
#convolution padding="SAME"
image = np.array([[1,1,1,0,0],
                  [0,1,1,1,0],
                  [0,0,1,1,1],
                  [0,0,1,1,0],
                  [0,1,1,0,0]])

weight = np.array([[1,0,1],
                    [0,1,0],
                    [1,0,1]])
bias = 0
input = tf.constant(image, dtype=tf.float32)
filter = tf.constant(weight, dtype=tf.float32)
input = tf.reshape(input, [1,5,5,1])
filter = tf.reshape(filter, [3,3,1,1])
result = tf.nn.conv2d(input, filter, strides=[1,1,1,1], padding="SAME")
with tf.Session() as sess:
    idata = sess.run(input)
    filt = sess.run(filter)
    res = sess.run(result)
    print(res)

输出为:1*5*5*1

2  2  3  1  1

1  4  3  4  1

1  2 4  3  1

1  2 3  4  1

0  2  2  1  1


stride=2时:

import tensorflow as tf
import numpy as np
#convolution padding="SAME"
image = np.array([[1,1,1,0,0],
                  [0,1,1,1,0],
                  [0,0,1,1,1],
                  [0,0,1,1,0],
                  [0,1,1,0,0]])

weight = np.array([[1,0,1],
                    [0,1,0],
                    [1,0,1]])
bias = 0
input = tf.constant(image, dtype=tf.float32)
filter = tf.constant(weight, dtype=tf.float32)
input = tf.reshape(input, [1,5,5,1])
filter = tf.reshape(filter, [3,3,1,1])
result = tf.nn.conv2d(input, filter, strides=[1,2,2,1], padding="SAME")
with tf.Session() as sess:
    idata = sess.run(input)
    filt = sess.run(filter)
    res = sess.run(result)
    print(res)

输出1*3*3*1:

2 3 1

1 4 3

0 2 1




PyTorch中的卷积层是神经网络中常用的一种层类型,用于处理图像和其他多维数据。卷积层通过对输入数据进行卷积操作来提取特征,并生成输出特征图。卷积操作是指将一个滤波器(也称为卷积核)与输入数据进行逐元素相乘,并将结果相加得到输出特征图的过程。[1] 在PyTorch中,卷积层的相关参数包括输入通道数、输出通道数、卷积核大小、步长和边缘填充等。输入通道数指的是输入数据的通道数,例如RGB图像的通道数为3。输出通道数指的是卷积层输出的特征图的通道数,通常可以理解为卷积核的个数。卷积核大小指的是卷积核的尺寸,例如3x3的卷积核。步长指的是卷积操作在输入数据上滑动的步长,用于控制输出特征图的尺寸。边缘填充是指在输入数据的边缘周围填充额外的像素,以保持输出特征图的尺寸与输入数据相同。[2] 在使用PyTorch的卷积层时,可以通过定义一个继承自`nn.Module`的神经网络类,并在其中定义卷积层的相关参数来创建卷积层。例如,可以使用`nn.Conv2d`类来定义一个二维卷积层,并指定输入通道数、输出通道数、卷积核大小、步长和边缘填充等参数。然后,在神经网络的前向传播方法中,可以通过调用卷积层的`forward`方法来进行卷积操作,并返回输出特征图。[3] 总结起来,PyTorch中的卷积层是用于处理图像和其他多维数据的一种神经网络层类型。它通过卷积操作来提取特征,并生成输出特征图。在使用PyTorch的卷积层时,需要定义相关参数,并在神经网络的前向传播方法中调用卷积层进行卷积操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值