Keras 3 API 文档 / 层 API / 卷积层 / DepthwiseConv1D 层

DepthwiseConv1D 层

[源代码]

DepthwiseConv1D

keras.layers.DepthwiseConv1D(
    kernel_size,
    strides=1,
    padding="valid",
    depth_multiplier=1,
    data_format=None,
    dilation_rate=1,
    activation=None,
    use_bias=True,
    depthwise_initializer="glorot_uniform",
    bias_initializer="zeros",
    depthwise_regularizer=None,
    bias_regularizer=None,
    activity_regularizer=None,
    depthwise_constraint=None,
    bias_constraint=None,
    **kwargs
)

1D 深度可分离卷积层。

深度可分离卷积是一种卷积类型,其中每个输入通道都与不同的核(称为深度核)进行卷积。你可以将深度可分离卷积理解为深度可分离卷积的第一步。

它通过以下步骤实现

  • 将输入拆分为单独的通道。
  • 将每个通道与具有 depth_multiplier 输出通道的单独深度核进行卷积。
  • 沿通道轴连接卷积后的输出。

与常规 1D 卷积不同,深度可分离卷积不会混合不同输入通道之间的信息。

depth_multiplier 参数确定应用于一个输入通道的滤波器数量。因此,它控制在深度步骤中每个输入通道生成的输出通道数量。

参数

  • kernel_size: int 或 1 个整数的元组/列表,指定深度可分离卷积窗口的大小。
  • strides: int 或 1 个整数的元组/列表,指定卷积的步幅长度。strides > 1dilation_rate > 1 不兼容。
  • padding: 字符串,可以是 "valid""same"(不区分大小写)。"valid" 表示不填充。"same" 导致在输入的左/右或上/下均匀填充。当 padding="same"strides=1 时,输出与输入大小相同。
  • depth_multiplier: 每个输入通道的深度可分离卷积输出通道数。深度可分离卷积输出通道的总数将等于 input_channel * depth_multiplier
  • data_format: 字符串,可以是 "channels_last""channels_first"。输入中维度的顺序。"channels_last" 对应于形状为 (batch, steps, features) 的输入,而 "channels_first" 对应于形状为 (batch, features, steps) 的输入。它默认为在你的 Keras 配置文件 ~/.keras/keras.json 中找到的 image_data_format 值。如果你从未设置过,则默认为 "channels_last"
  • dilation_rate: int 或 1 个整数的元组/列表,指定用于空洞卷积的空洞率。
  • activation: 激活函数。如果为 None,则不应用激活函数。
  • use_bias: bool,如果为 True,则将偏置添加到输出。
  • depthwise_initializer: 卷积核的初始化器。如果为 None,则将使用默认初始化器("glorot_uniform")。
  • bias_initializer: 偏置向量的初始化器。如果为 None,则将使用默认初始化器("zeros")。
  • depthwise_regularizer: 卷积核的可选正则化器。
  • bias_regularizer: 偏置向量的可选正则化器。
  • activity_regularizer: 输出的可选正则化函数。
  • depthwise_constraint: 可选的投影函数,在卷积核被 Optimizer 更新后应用于卷积核(例如,用于实现层权重的范数约束或值约束)。该函数必须将未投影的变量作为输入,并且必须返回投影的变量(其必须具有相同的形状)。在进行异步分布式训练时,约束是不安全的。
  • bias_constraint: 可选的投影函数,在偏置被 Optimizer 更新后应用于偏置。

输入形状

  • 如果 data_format="channels_last":形状为 (batch_shape, steps, channels) 的 3D 张量
  • 如果 data_format="channels_first":形状为 (batch_shape, channels, steps) 的 3D 张量

输出形状

  • 如果 data_format="channels_last":形状为 (batch_shape, new_steps, channels * depth_multiplier) 的 3D 张量
  • 如果 data_format="channels_first":形状为 (batch_shape, channels * depth_multiplier, new_steps) 的 3D 张量

返回值

表示 activation(depthwise_conv1d(inputs, kernel) + bias) 的 3D 张量。

可能引发

  • ValueError: 当 strides > 1dilation_rate > 1 时。

示例

>>> x = np.random.rand(4, 10, 12)
>>> y = keras.layers.DepthwiseConv1D(3, 3, 2, activation='relu')(x)
>>> print(y.shape)
(4, 4, 36)