Keras常用层

Keras基于Tensorflow框架实现,具有更高的集成度,相比tensorflow,其代码简洁易读、更加模块化、易于算法实现。Github的Keras仓库中有许多经典网络和算法的实现源码,同样便于学习。

使用Keras前常需要导入一些构成网络结构的基本层模块,这些模块的用法在Keras文档中已明确介绍,这里整理如下:

Dense:全连接层

from keras.layers import Dense 
keras.layers.core.Dense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)

实现的计算是:

output = activation(dot(input, kernel)+bias)

其中的activation是逐元素计算的激活函数,kernel是该层的权值矩阵。当输入的维度大于2时,会先压缩成与Kernel相同维度后再计算。
units是一个大于0的整数,表示FC层的输出维度。
输入:(batch_size, input_dim)的2D张量
输出:(batch_size, units)的2D张量

Activation:激活层

keras.layers.core.Activation(activation)

输入activation为预定义激活函数名或一个Tensorflow/Theano中的函数。
输入:任意
输出:与输入的shape相同

Dropout层

keras.layers.core.Dropout(rate, noise_shape=None, seed=None)

Dropout层为输入数据施加Dropout,即在训练过程中每次更新参数时按一定概率(rate)随机断开输入神经元,Dropout层用于防止过拟合。
rate:0~1的浮点数,控制需要断开的神经元的比例

Flatten:连接展平层

keras.layers.core.Flatten()

Flatten层用来将输入“压平”,即把多维的输入一维化,常用在从卷积层到全连接层的过渡。
官方文档示例如下:

model = Sequential()
model.add(Convolution2D(64, 3, 3,
            border_mode='same',
            input_shape=(3, 32, 32)))
# now: model.output_shape == (None, 64, 32, 32)

model.add(Flatten())
# now: model.output_shape == (None, 65536)

Reshape层

keras.layers.core.Reshape(target_shape)

Reshape层用来将输入shape转换为特定的shape
target_shape:目标shape,为整数的tuple
输入shape:任意
输出shape: (batch_size,)+target_shape

文档示例:

# as first layer in a Sequential model
model = Sequential()
model.add(Reshape((3, 4), input_shape=(12,)))
# now: model.output_shape == (None, 3, 4)
# note: `None` is the batch dimension

# as intermediate layer in a Sequential model
model.add(Reshape((6, 2)))
# now: model.output_shape == (None, 6, 2)

# also supports shape inference using `-1` as dimension
model.add(Reshape((-1, 2, 2)))
# now: model.output_shape == (None, 3, 2, 2)

Permute层

keras.layers.core.Permute(dims)

Permute层将输入的维度按照给定模式进行重排
dims:整数tuple,指定重排的模式,不包含样本数的维度。重拍模式的下标从1开始。例如(2,1)代表将输入的第二个维度重拍到输出的第一个维度,而将输入的第一个维度重排到第二个维度

文档示例:

model = Sequential()
model.add(Permute((2, 1), input_shape=(10, 64)))
# now: model.output_shape == (None, 64, 10)
# note: `None` is the batch dimension

输入shape: 任意
输出shape: 与输入相同,但是其维度按照指定的模式重新排列

Conv2D层

keras.layers.convolutional.Conv2D(filters, kernel_size, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1), activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)

Conv2D为二维卷积层,即对图像的空域卷积。该层对二维输入进行滑动窗卷积

filters:卷积核的数目(即输出的维度)

kernel_size:单个整数或由两个整数构成的list/tuple,卷积核的宽度和长度。如为单个整数,则表示在各个空间维度的相同长度。

strides:单个整数或由两个整数构成的list/tuple,为卷积的步长。如为单个整数,则表示在各个空间维度的相同步长。任何不为1的strides均与任何不为1的dilation_rate均不兼容

padding:补0策略,为“valid”, “same” 。“valid”代表只进行有效的卷积,即对边 界数据不处理。“same”代表保留边界处的卷积结果,通常会导致输出shape与输入shape相同。

activation:激活函数,为预定义的激活函数名(参考激活函数),或逐元素(element-wise)的Theano函数。如果不指定该参数,将不会使用任何激活函数(即使用线性激活函数:a(x)=x)

Cropping2D层

keras.layers.convolutional.Cropping2D(cropping=((0, 0), (0, 0)), data_format=None)

对2D输入(图像)进行裁剪,将在空域维度,即宽和高的方向上裁剪
cropping:长为2的整数tuple,分别为宽和高方向上头部与尾部需要裁剪掉的元素数

ZeroPadding2D层

keras.layers.convolutional.ZeroPadding2D(padding=(1, 1), data_format=None)

对2D输入(如图片)的边界填充0,以控制卷积以后特征图的大小
padding:整数tuple,表示在要填充的轴的起始和结束处填充0的数目

输入shape:
‘channels_first’模式:
(samples,channels,first_axis_to_pad,second_axis_to_pad)的4D张量
‘channels_last’模式:
(samples,first_axis_to_pad,second_axis_to_pad, channels)的4D张量

输出shape:
‘channels_first’模式:
(samples,channels,first_paded_axis,second_paded_axis)的4D张量
‘channels_last’模式:
(samples,first_paded_axis,second_paded_axis, channels)的4D张量

MaxPooling2D层

keras.layers.pooling.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)

pool_size:整数或长为2的整数tuple,代表在两个方向(竖直,水平)上的下采样因子,如取(2,2)将使图片在两个维度上均变为原长的一半。为整数意为各个维度值相同且为该数字。

输入shape:
‘channels_first’模式下,为形如(samples,channels, rows,cols)的4D张量
‘channels_last’模式下,为形如(samples,rows, cols,channels)的4D张量

输出shape:
‘channels_first’模式:(samples,channels, pooled_rows, pooled_cols)
‘channels_last’模式:(samples,pooled_rows, pooled_cols,channels)

AveragePooling2D层

keras.layers.pooling.AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)

GlobalMaxPooling2D层

keras.layers.pooling.GlobalMaxPooling2D(dim_ordering='default')

输出shape: (nb_samples, channels)的2D张量

GlobalAveragePooling2D层

keras.layers.pooling.GlobalAveragePooling2D(dim_ordering='default')

BatchNormalization层

keras.layers.normalization.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001, center=True, scale=True, beta_initializer='zeros', gamma_initializer='ones', moving_mean_initializer='zeros', moving_variance_initializer='ones', beta_regularizer=None, gamma_regularizer=None, beta_constraint=None, gamma_constraint=None)

该层在每个batch上将前一层的激活值重新规范化,即使得其输出数据的均值接近0,其标准差接近1
axis: 整数,指定要规范化的轴,通常为特征轴。例如在进行 data_format=”channels_first的2D卷积后,一般会设axis=1。
输入shape: 任意
输出shape: 与输入相同

BN层作用:(1)加速收敛 (2)控制过拟合,可以少用或不用Dropout和正则 化(3)降低网络对初始化权重不敏感 (4)允许使用较大的学习率

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值