tf.keras.Layers中常见层的API接口

TensorFlow的官网API:https://tensorflow.google.cn/api_docs/python/tf

keras的中文网站API:https://keras-zh.readthedocs.io/

这里只是记录一下常用的一些API

tf.keras.Layers

Input层

tf.keras.Input(
    shape=None, batch_size=None, name=None, dtype=None, sparse=False, tensor=None,
    ragged=False, **kwargs
)
Arguments
shapeA shape tuple (integers), not including the batch size. For instance, shape=(32,) indicates that the expected input will be batches of 32-dimensional vectors. Elements of this tuple can be None; ‘None’ elements represent dimensions where the shape is not known.
batch_sizeoptional static batch size (integer).
nameAn optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn’t provided.
dtypeThe data type expected by the input, as a string (float32, float64, int32…)
sparseA boolean specifying whether the placeholder to be created is sparse. Only one of ‘ragged’ and ‘sparse’ can be True. Note that, if sparse is False, sparse tensors can still be passed into the input - they will be densified with a default value of 0.
tensorOptional existing tensor to wrap into the Input layer. If set, the layer will use the tf.TypeSpec of this tensor rather than creating a new placeholder tensor.
raggedA boolean specifying whether the placeholder to be created is ragged. Only one of ‘ragged’ and ‘sparse’ can be True. In this case, values of ‘None’ in the ‘shape’ argument represent ragged dimensions. For more information about RaggedTensors, see this guide. 指定要创建的占位符是否参差不齐的布尔值。
**kwargsdeprecated arguments support. Supports batch_shape and batch_input_shape.
Returns
A tensor.

参数

  • shape: 一个尺寸元组(整数),不包含批量大小。 例如,shape=(32,) 表明期望的输入是按批次的 32 维向量。
  • batch_shape: 一个尺寸元组(整数),包含批量大小。 例如,batch_shape=(10, 32) 表明期望的输入是 10 个 32 维向量。 batch_shape=(None, 32) 表明任意批次大小的 32 维向量。
  • name: 一个可选的层的名称的字符串。 在一个模型中应该是唯一的(不可以重用一个名字两次)。 如未提供,将自动生成。
  • dtype: 输入所期望的数据类型,字符串表示 (float32, float64, int32…)
  • sparse: 一个布尔值,指明需要创建的占位符是否是稀疏的。
  • tensor: 可选的可封装到 Input 层的现有张量。 如果设定了,那么这个层将不会创建占位符张量。

返回

一个张量。

Activation层

tf.keras.layers.Activation(
    activation, **kwargs
)
Arguments
activationActivation function, such as tf.nn.relu, or string name of built-in activation function, such as “relu”.
  • 例子
layer = tf.keras.layers.Activation('relu')
output = layer([-3.0, -1.0, 0.0, 2.0])
list(output.numpy())

layer = tf.keras.layers.Activation(tf.nn.relu)
output = layer([-3.0, -1.0, 0.0, 2.0])
list(output.numpy())

Dense层

tf.keras.layers.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, **kwargs
)
Arguments
unitsPositive integer, dimensionality of the output space. 正整数,输出空间的维数。
activationActivation function to use. If you don’t specify anything, no activation is applied (ie. “linear” activation: a(x) = x). 指定激活函数的使用,如果不指定任何函数,将不会使用任何激活函数(即线性激活)
use_biasBoolean, whether the layer uses a bias vector. 布尔值,该层是否使用偏置向量。
kernel_initializerInitializer for the kernel weights matrix. 初始化核权重矩阵
bias_initializerInitializer for the bias vector. 初始化偏置向量
kernel_regularizerRegularizer function applied to the kernel weights matrix. 对核权重矩阵使用正则化
bias_regularizerRegularizer function applied to the bias vector. 对偏置向量使用正则化
activity_regularizerRegularizer function applied to the output of the layer (its “activation”).
kernel_constraintConstraint function applied to the kernel weights matrix. 应用于核权重矩阵上的约束函数
bias_constraintConstraint function applied to the bias vector. 应用于偏置向量上的约束函数
  • 例子
# Create a `Sequential` model and add a Dense layer as the first layer.
model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(16,)))
model.add(tf.keras.layers.Dense(32, activation='relu'))
# Now the model will take as input arrays of shape (None, 16)
# and output arrays of shape (None, 32).
# Note that after the first layer, you don't need to specify
# the size of the input anymore:
model.add(tf.keras.layers.Dense(32))
model.output_shape

Flatten层

tf.keras.layers.Flatten(
    data_format=None, **kwargs
)
Arguments
data_formatA string, one of channels_last (default) or channels_first. The ordering of the dimensions in the inputs. channels_last corresponds to inputs with shape (batch, ..., channels) while channels_first corresponds to inputs with shape (batch, channels, ...). It defaults to the image_data_format value found in your Keras config file at ~/.keras/keras.json. If you never set it, then it will be “channels_last”.

参数

  • data_format:一个字符串,其值为 channels_last(默认值)或者 channels_first。它表明输入的维度的顺序。此参数的目的是当模型从一种数据格式切换到另一种数据格式时保留权重顺序。channels_last 对应着尺寸为 (batch, ..., channels) 的输入,而 channels_first 对应着尺寸为 (batch, channels, ...) 的输入。默认为 image_data_format 的值,你可以在 Keras 的配置文件 ~/.keras/keras.json 中找到它。如果你从未设置过它,那么它将是 channels_last
  • 例子
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(64, 3, 3, input_shape=(3, 32, 32)))
model.output_shape
(None, 1, 10, 64)

model.add(Flatten())
model.output_shape
(None, 640)

Dropout层

tf.keras.layers.Dropout(
    rate, noise_shape=None, seed=None, **kwargs
)
Arguments
rateFloat between 0 and 1. Fraction of the input units to drop.
noise_shape1D integer tensor representing the shape of the binary dropout mask that will be multiplied with the input. For instance, if your inputs have shape (batch_size, timesteps, features) and you want the dropout mask to be the same for all timesteps, you can use noise_shape=(batch_size, 1, features). noise_shape某维度为1,代表着所有样本在此维度dropout的方式一致。
seedA Python integer to use as random seed.

Inoput层

tf.keras.layers.InputLayer(
    input_shape=None, batch_size=None, dtype=None, input_tensor=None, sparse=False,
    name=None, ragged=False, **kwargs
)
Arguments
input_shapeShape tuple 元组(not including the batch axis), or TensorShape instance (not including the batch axis).
batch_sizeOptional input batch size (integer or None).
dtypeOptional datatype of the input. When not provided, the Keras default float type will be used.
input_tensorOptional tensor to use as layer input. If set, the layer will use the tf.TypeSpec of this tensor rather than creating a new placeholder tensor.
sparseBoolean, whether the placeholder created is meant to be sparse. Default to False.
raggedBoolean, whether the placeholder created is meant to be ragged. In this case, values of ‘None’ in the ‘shape’ argument represent ragged dimensions. For more information about RaggedTensors, see this guide. Default to False.
nameOptional name of the layer (string).

Embedding层

tf.keras.layers.Embedding(
    input_dim, output_dim, embeddings_initializer='uniform',
    embeddings_regularizer=None, activity_regularizer=None,
    embeddings_constraint=None, mask_zero=False, input_length=None, **kwargs
)
Arguments
input_dimInteger. Size of the vocabulary, i.e. maximum integer index + 1. 输入的向量的最大元素+1
output_dimInteger. Dimension of the dense embedding. 稠密层的维数
embeddings_initializerInitializer for the embeddings matrix (see keras.initializers). 初始化embedding矩阵的方式
embeddings_regularizerRegularizer function applied to the embeddings matrix (see keras.regularizers). 给embedding矩阵施加正则项
embeddings_constraintConstraint function applied to the embeddings matrix (see keras.constraints). 给embedding矩阵施加约束函数
mask_zeroBoolean, whether or not the input value 0 is a special “padding” value that should be masked out. This is useful when using recurrent layers which may take variable length input. If this is True, then all subsequent layers in the model need to support masking or an exception will be raised. If mask_zero is set to True, as a consequence, index 0 cannot be used in the vocabulary (input_dim should equal size of vocabulary + 1).
input_lengthLength of input sequences, when it is constant. This argument is required if you are going to connect Flatten then Dense layers upstream (without it, the shape of the dense outputs cannot be computed). 输入序列的长度,当它是常数时。如果要在该层后连接Flatten层,然后连接Dense层,则需要此参数(如果没有此参数,则无法计算dense层输出的形状)。

这里补充一下:input_dim不是指输入样本的维度,它的值表示:在原样本中最大的元素值的基础上加1,

ouptput_dim则是该层网络的样本的输入维度。input_length才是指输入样本的原维数。

Conv2d层

tf.keras.layers.Conv2D(
    filters, kernel_size, strides=(1, 1), padding='valid',
    data_format=None, dilation_rate=(1, 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
)
Arguments
filtersInteger, the dimensionality of the output space (i.e. the number of output filters in the convolution).
kernel_sizeAn integer or tuple/list of 2 integers, specifying the height and width of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions.
stridesAn integer or tuple/list of 2 integers, specifying the strides of the convolution along the height and width. Can be a single integer to specify the same value for all spatial dimensions. Specifying any stride value != 1 is incompatible with specifying any dilation_rate value != 1.
paddingone of "valid" or "same" (case-insensitive). "valid" means no padding. "same" results in padding evenly to the left/right or up/down of the input such that output has the same height/width dimension as the input.
data_formatA string, one of channels_last (default) or channels_first. The ordering of the dimensions in the inputs. channels_last corresponds to inputs with shape (batch_size, height, width, channels) while channels_first corresponds to inputs with shape (batch_size, channels,height, width). It defaults to the image_data_format value found in your Keras config file at ~/.keras/keras.json. If you never set it, then it will be channels_last.
dilation_ratean integer or tuple/list of 2 integers, specifying the dilation rate to use for dilated convolution. Can be a single integer to specify the same value for all spatial dimensions. Currently, specifying any dilation_rate value != 1 is incompatible with specifying any stride value != 1.
groupsA positive integer specifying the number of groups in which the input is split along the channel axis. Each group is convolved separately with filters / groups filters. The output is the concatenation of all the groups results along the channel axis. Input channels and filters must both be divisible by groups.
activationActivation function to use. If you don’t specify anything, no activation is applied (see keras.activations).
use_biasBoolean, whether the layer uses a bias vector.
kernel_initializerInitializer for the kernel weights matrix (see keras.initializers).
bias_initializerInitializer for the bias vector (see keras.initializers).
kernel_regularizerRegularizer function applied to the kernel weights matrix (see keras.regularizers).
bias_regularizerRegularizer function applied to the bias vector (see keras.regularizers).
activity_regularizerRegularizer function applied to the output of the layer (its “activation”) (see keras.regularizers).
kernel_constraintConstraint function applied to the kernel matrix (see keras.constraints).
bias_constraintConstraint function applied to the bias vector (see keras.constraints).

参数

  • filters: 整数,输出空间的维度 (即卷积中滤波器的输出数量)。
  • kernel_size: 一个整数,或者 2 个整数表示的元组或列表, 指明 2D 卷积窗口的宽度和高度。 可以是一个整数,为所有空间维度指定相同的值。
  • strides: 一个整数,或者 2 个整数表示的元组或列表, 指明卷积沿宽度和高度方向的步长。 可以是一个整数,为所有空间维度指定相同的值。 指定任何 stride 值 != 1 与指定 dilation_rate 值 != 1 两者不兼容。
  • padding: "valid""same" (大小写敏感)。
  • data_format: 字符串, channels_last (默认) 或 channels_first 之一,表示输入中维度的顺序。 channels_last 对应输入尺寸为 (batch, height, width, channels)channels_first 对应输入尺寸为 (batch, channels, height, width)。 它默认为从 Keras 配置文件 ~/.keras/keras.json 中 找到的 image_data_format 值。 如果你从未设置它,将使用 channels_last
  • dilation_rate: 一个整数或 2 个整数的元组或列表, 指定膨胀卷积的膨胀率。 可以是一个整数,为所有空间维度指定相同的值。 当前,指定任何 dilation_rate 值 != 1 与 指定 stride 值 != 1 两者不兼容。
  • activation: 要使用的激活函数 (详见 activations)。 如果你不指定,则不使用激活函数 (即线性激活: a(x) = x)。
  • use_bias: 布尔值,该层是否使用偏置向量。
  • kernel_initializer: kernel 权值矩阵的初始化器 (详见 initializers)。
  • bias_initializer: 偏置向量的初始化器 (详见 initializers)。
  • kernel_regularizer: 运用到 kernel 权值矩阵的正则化函数 (详见 regularizer)。
  • bias_regularizer: 运用到偏置向量的正则化函数 (详见 regularizer)。
  • activity_regularizer: 运用到层输出(它的激活值)的正则化函数 (详见 regularizer)。
  • kernel_constraint: 运用到 kernel 权值矩阵的约束函数 (详见 constraints)。
  • bias_constraint: 运用到偏置向量的约束函数 (详见 constraints)。

输入尺寸

  • 如果 data_format=‘channels_first’, 输入 4D 张量,尺寸为 (samples, channels, rows, cols)
  • 如果 data_format=‘channels_last’, 输入 4D 张量,尺寸为 (samples, rows, cols, channels)

输出尺寸

  • 如果 data_format=‘channels_first’, 输出 4D 张量,尺寸为 (samples, filters, new_rows, new_cols)
  • 如果 data_format=‘channels_last’, 输出 4D 张量,尺寸为 (samples, new_rows, new_cols, filters)

由于填充的原因, rowscols 值可能已更改。

MaxPool2D层

tf.keras.layers.MaxPool2D(
    pool_size=(2, 2), strides=None, padding='valid', data_format=None,
    **kwargs
)
Arguments
pool_sizeinteger or tuple of 2 integers, window size over which to take the maximum. (2, 2) will take the max value over a 2x2 pooling window. If only one integer is specified, the same window length will be used for both dimensions.
stridesInteger, tuple of 2 integers, or None. Strides values. Specifies how far the pooling window moves for each pooling step. If None, it will default to pool_size.
paddingOne of "valid" or "same" (case-insensitive). "valid" means no padding. "same" results in padding evenly to the left/right or up/down of the input such that output has the same height/width dimension as the input.
data_formatA string, one of channels_last (default) or channels_first. The ordering of the dimensions in the inputs. channels_last corresponds to inputs with shape (batch, height, width, channels) while channels_first corresponds to inputs with shape (batch, channels, height, width). It defaults to the image_data_format value found in your Keras config file at ~/.keras/keras.json. If you never set it, then it will be “channels_last”.

参数

  • pool_size: 整数,或者 2 个整数表示的元组, 沿(垂直,水平)方向缩小比例的因数。 (2,2)会把输入张量的两个维度都缩小一半。 如果只使用一个整数,那么两个维度都会使用同样的窗口长度。
  • strides: 整数,2 个整数表示的元组,或者是 None。 表示步长值。 如果是 None,那么默认值是 pool_size
  • padding: "valid" 或者 "same" (区分大小写)。
  • data_format: 字符串,channels_last (默认)或 channels_first 之一。 表示输入各维度的顺序。 channels_last 代表尺寸是 (batch, height, width, channels) 的输入张量, 而 channels_first 代表尺寸是 (batch, channels, height, width) 的输入张量。 默认值根据 Keras 配置文件 ~/.keras/keras.json 中的 image_data_format 值来设置。 如果还没有设置过,那么默认值就是 “channels_last”。

输入尺寸

  • 如果 data_format='channels_last': 尺寸是 (batch_size, rows, cols, channels) 的 4D 张量
  • 如果 data_format='channels_first': 尺寸是 (batch_size, channels, rows, cols) 的 4D 张量

输出尺寸

  • 如果 data_format='channels_last': 尺寸是 (batch_size, pooled_rows, pooled_cols, channels) 的 4D 张量
  • 如果 data_format='channels_first': 尺寸是 (batch_size, channels, pooled_rows, pooled_cols) 的 4D 张量
  • 1
    点赞
  • 5
    收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论 1

打赏作者

xLyons

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值