随着神经网络的发展,现在有大量的神经网络层类型。 在本文中,我将介绍几个神经网络层,解释它们的作用和工作方式,并在Python流行的机器学习库Keras中展示它们的应用。
Core Layers
Dense
Dense层是标准的神经网络层,可输出-
-其中“activation”是通过“激活参数”传递的激活函数,“kernel”是由图层创建的权重矩阵,而“bias”是由图层创建的偏差矢量。 可以使用参数“ use_bias”进行调整,该参数可以设置为False。
# as first layer in a sequential model: model = Sequential() model.add(Dense(32, input_shape=(16,))) # now the model will take as input arrays of shape (*, 16) # and output arrays of shape (*, 32) # after the first layer, you don't need to specify # the size of the input anymore: model.add(Dense(32))
Activation Layer
激活层将激活功能应用于输出。 可以使用以下方法在Keras中完成…
keras.layers.Activation(activation)
…在参数“activation”中填写要使用的激活函数的名称,例如“ Sigmoid”或“ relu”。
Dropout Layer
Dropout Layer用于防止神经网络过度拟合,在训练期间每次更新时,将输入单元的分数“比率”随机设置为0。
这简化了神经网络并减少了训练时间。 可以在Keras中使用...实现Dropout层。
keras.layers.Dropout(rate, noise_shape=None, seed=None)
…其中rate表示介于0和1之间的浮点数,表示要下降的输入单位的分数,noise_shape表示一维整数张量,表示与输入相乘的二进制丢包掩码的形状,seed表示要使用的Python整数 作为随机种子。
Flatten Layer
Flatten layer将多维数据转换为单个向量进行处理。 例如,在应用Flatten层后,28 x 28的图像将成为784位长的矢量。 Keras的实现是…
keras.layers.Flatten()
Convolutional Layers
Conv1D
一维卷积层将卷积应用于两个一维数据类型(矢量,信号)。 长度为n的输入向量f和长度为m的核g的卷积为…
考虑此操作的一种方法是在输入图像上滑动内核-对于内核的每个位置,将内核和图像的重叠值相乘,然后求和。 它只是图像的“窗口平均值”。 一维卷积层是数据的概括,可以在Keras中实现为:
keras.layers.Conv1D(filters, kernel_size, strides=1)
参数如下:
filters — an integer representing the dimensionality of the output space, or the number of output filters in the convolutionkernel_size — an integer specifying the length of the 1D convolution windowstrides — an integer specifying the stride length of the convolutionConv2D
与Conv1D相似,在图像上进行二维卷积意味着使用移动窗口对图像(二维数据)进行泛化。 图像和内核的每种排列后面的重叠值将相乘并求和。
Keras中的二维卷积可以实现为
keras.layers.Conv2D(filters, kernel_size, strides=(1, 1))
参数:
filter: An integer describing the dimensionality of the output space (number of output filters in convolution)kernel_size: a tuple/list of 2 integers, specifying the height and width of the 2D convolution windowstrides — a tuple or list of 2 integers specifying the strides of each convolution along the height and width对于Conv3d,只需将“ Conv2D”更改为“ Conv3D”并添加其他尺寸。
Locally Connected Layer
本地连接的层类似于卷积层,但是不是将单个mask应用于每个区域,而是将不同的mask应用于每个区域。 这确实会导致大量的超参数,并且仅应用于需要超参数和随后增加训练时间的高级图像识别问题中。
keras.layers.LocallyConnected1D(filters, kernel_size, strides=1)
与往常一样,将“ LocallyConnected1D”替换为“ LocallyConnected2D”和“ LocallyConnected3D”,并根据需要调整参数以获取其他尺寸。
Pooling Layers
注—在构建神经网络时,通常在卷积层之后是池化层或池化层以及激活函数。
Max Pooling
最大池化是将数据分成几个区域,获取这些区域中单元格的最大值,然后将其插入输出矩阵的过程。
以下是一维最大池化的代码
keras.layers.MaxPooling1D(pool_size=2, strides=None)
参数:
Pool_size is an integer representing the size of the max pooling windowsStrides is an integer representing the factor by which to downscale — for instance, ‘2’ will halve the input对于2D和3D池化,只需将“ MaxPooling1D”替换为适当的名称(“ MaxPooling2D”和“ MaxPooling3D”),然后在必要的参数中添加一个附加的尺寸即可。
Average Pooling
平均池化类似于最大池化,但是不是取每个区域的最大值,而是取平均值。
keras.layers.AveragePooling1D(pool_size=2, strides=None)
参数:
pool_size is an integer representing the size of the max pooling windowsstrides is an integer representing the factor by which to downscale.对于2D和3D平均池化,只需将“ AveragePooling1D”替换为适当的名称(“ AveragePooling2D”和“ AveragePooling3D”),然后在必要的参数中添加其他尺寸即可。