tf.keras.layers.Conv1D和tf.keras.layers.Conv1DTranspose

文章目录

  • tf.keras.layers.Conv1D
  • tf.keras.layers.Conv1DTranspose

tf.keras.layers.Conv1D

tf.keras.layers.Conv1D(
    filters, kernel_size, strides=1, padding='valid', data_format='channels_last',
    dilation_rate=1, groups=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, **kwargs
)

从这个函数原型可以知道各个参数的默认值。

When using this layer as the first layer in a model, provide an input_shape argument (tuple of integers or None, e.g. (10, 128) for sequences of 10 vectors of 128-dimensional vectors, or (None, 128) for variable-length sequences of 128-dimensional vectors.
如果Conv1D是第一层,需要输入input_shape,可以是(10,128),即10个128维的向量,也可以是 (None, 128) ,表示可变个数个128维向量。

# 10个时间步长,每个时间由128维向量表示,batch_size是4
input_shape = (4, 10, 128)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv1D(
32, 3, activation='relu',input_shape=input_shape[1:])(x)
print(y.shape)

输出: (4, 8,32)

# [4, 7]是扩展的batch_size,不管batch_size是不是两维,输入4维的时候,前两维都被认为是batch_size,10是时间,128维向量
input_shape = (4, 7, 10, 128)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv1D(
32, 3, activation='relu', input_shape=input_shape[2:])(x)
print(y.shape)

输出:(4, 7,8, 32)

Input shape:
3+D tensor with shape: batch_shape + (steps, input_dim)
Output shape:
3+D tensor with shape: batch_shape + (new_steps, filters) steps value might have changed due to padding or strides.

kernel_initializer和bias_initializer都是需要初始化方式的,所以默认就有,如果不初始化,那filter就没有值,不能计算。
kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None,层的参数,偏置和激活函数都可以添加惩罚项。

keras.regularizers.l1(0.)
keras.regularizers.l2(0.)
keras.regularizers.l1_l2(l1=0.01, l2=0.01)

参数就是下图中的λ。
tf.keras.layers.Conv1D和tf.keras.layers.Conv1DTranspose_第1张图片
任何输入一个权重矩阵、返回一个损失贡献张量的函数,都可以用作正则化器。例如自定义:

from keras import backend as K

def l1_reg(weight_matrix):
    return 0.01 * K.sum(K.abs(weight_matrix))

model.add(Dense(64, input_dim=64,
                kernel_regularizer=l1_reg))

tf.keras.layers.Conv1DTranspose

函数原型:

tf.keras.layers.Conv1DTranspose(
    filters, kernel_size, strides=1, padding='valid', output_padding=None,
    data_format=None, dilation_rate=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, **kwargs
)

Input shape:
3D tensor with shape: (batch_size, steps, channels)

Output shape:
3D tensor with shape: (batch_size, new_steps, filters) If output_padding is specified:

new_timesteps = ((timesteps - 1) * strides + kernel_size - 2 * padding + output_padding)

你可能感兴趣的:(tensorflow)