Keras layers API.
experimental module: Public API for tf.keras.layers.experimental namespace.
class AbstractRNNCell: Abstract object representing an RNN cell.
class Activation: Applies an activation function to an output.
class ActivityRegularization: Layer that applies an update to the cost function based input activity.
class Add: Layer that adds a list of inputs.
class AdditiveAttention: Additive attention layer, a.k.a. Bahdanau-style attention.
class AlphaDropout: Applies Alpha Dropout to the input.
class Attention: Dot-product attention layer, a.k.a. Luong-style attention.
class Average: Layer that averages a list of inputs element-wise.
class AveragePooling1D: Average pooling for temporal data.
class AveragePooling2D: Average pooling operation for spatial data.
class AveragePooling3D: Average pooling operation for 3D data (spatial or spatio-temporal).
class AvgPool1D: Average pooling for temporal data.
class AvgPool2D: Average pooling operation for spatial data.
class AvgPool3D: Average pooling operation for 3D data (spatial or spatio-temporal).
class BatchNormalization: Layer that normalizes its inputs.
class Bidirectional: Bidirectional wrapper for RNNs.
class Concatenate: Layer that concatenates a list of inputs.
class Conv1D: 1D convolution layer (e.g. temporal convolution).
class Conv1DTranspose: Transposed convolution layer (sometimes called Deconvolution).
class Conv2D: 2D convolution layer (e.g. spatial convolution over images).
class Conv2DTranspose: Transposed convolution layer (sometimes called Deconvolution).
class Conv3D: 3D convolution layer (e.g. spatial convolution over volumes).
class Conv3DTranspose: Transposed convolution layer (sometimes called Deconvolution).
class ConvLSTM2D: Convolutional LSTM.
class Convolution1D: 1D convolution layer (e.g. temporal convolution).
class Convolution1DTranspose: Transposed convolution layer (sometimes called Deconvolution).
class Convolution2D: 2D convolution layer (e.g. spatial convolution over images).
class Convolution2DTranspose: Transposed convolution layer (sometimes called Deconvolution).
class Convolution3D: 3D convolution layer (e.g. spatial convolution over volumes).
class Convolution3DTranspose: Transposed convolution layer (sometimes called Deconvolution).
class Cropping1D: Cropping layer for 1D input (e.g. temporal sequence).
class Cropping2D: Cropping layer for 2D input (e.g. picture).
class Cropping3D: Cropping layer for 3D data (e.g. spatial or spatio-temporal).
class Dense: Just your regular densely-connected NN layer.
class DenseFeatures: A layer that produces a dense Tensor based on given feature_columns.
class DepthwiseConv2D: Depthwise separable 2D convolution.
class Dot: Layer that computes a dot product between samples in two tensors.
class Dropout: Applies Dropout to the input.
class ELU: Exponential Linear Unit.
class Embedding: Turns positive integers (indexes) into dense vectors of fixed size.
class Flatten: Flattens the input. Does not affect the batch size.
class GRU: Gated Recurrent Unit - Cho et al. 2014.
class GRUCell: Cell class for the GRU layer.
class GaussianDropout: Apply multiplicative 1-centered Gaussian noise.
class GaussianNoise: Apply additive zero-centered Gaussian noise.
class GlobalAveragePooling1D: Global average pooling operation for temporal data.
class GlobalAveragePooling2D: Global average pooling operation for spatial data.
class GlobalAveragePooling3D: Global Average pooling operation for 3D data.
class GlobalAvgPool1D: Global average pooling operation for temporal data.
class GlobalAvgPool2D: Global average pooling operation for spatial data.
class GlobalAvgPool3D: Global Average pooling operation for 3D data.
class GlobalMaxPool1D: Global max pooling operation for 1D temporal data.
class GlobalMaxPool2D: Global max pooling operation for spatial data.
class GlobalMaxPool3D: Global Max pooling operation for 3D data.
class GlobalMaxPooling1D: Global max pooling operation for 1D temporal data.
class GlobalMaxPooling2D: Global max pooling operation for spatial data.
class GlobalMaxPooling3D: Global Max pooling operation for 3D data.
class InputLayer: Layer to be used as an entry point into a Network (a graph of layers).
class InputSpec: Specifies the rank, dtype and shape of every input to a layer.
class LSTM: Long Short-Term Memory layer - Hochreiter 1997.
class LSTMCell: Cell class for the LSTM layer.
class Lambda: Wraps arbitrary expressions as a Layer object.
class Layer: This is the class from which all layers inherit.
class LayerNormalization: Layer normalization layer (Ba et al., 2016).
class LeakyReLU: Leaky version of a Rectified Linear Unit.
class LocallyConnected1D: Locally-connected layer for 1D inputs.
class LocallyConnected2D: Locally-connected layer for 2D inputs.
class Masking: Masks a sequence by using a mask value to skip timesteps.
class MaxPool1D: Max pooling operation for 1D temporal data.
class MaxPool2D: Max pooling operation for 2D spatial data.
class MaxPool3D: Max pooling operation for 3D data (spatial or spatio-temporal).
class MaxPooling1D: Max pooling operation for 1D temporal data.
class MaxPooling2D: Max pooling operation for 2D spatial data.
class MaxPooling3D: Max pooling operation for 3D data (spatial or spatio-temporal).
class Maximum: Layer that computes the maximum (element-wise) a list of inputs.
class Minimum: Layer that computes the minimum (element-wise) a list of inputs.
class MultiHeadAttention: MultiHeadAttention layer.
class Multiply: Layer that multiplies (element-wise) a list of inputs.
class PReLU: Parametric Rectified Linear Unit.
class Permute: Permutes the dimensions of the input according to a given pattern.
class RNN: Base class for recurrent layers.
class ReLU: Rectified Linear Unit activation function.
class RepeatVector: Repeats the input n times.
class Reshape: Layer that reshapes inputs into the given shape.
class SeparableConv1D: Depthwise separable 1D convolution.
class SeparableConv2D: Depthwise separable 2D convolution.
class SeparableConvolution1D: Depthwise separable 1D convolution.
class SeparableConvolution2D: Depthwise separable 2D convolution.
class SimpleRNN: Fully-connected RNN where the output is to be fed back to input.
class SimpleRNNCell: Cell class for SimpleRNN.
class Softmax: Softmax activation function.
class SpatialDropout1D: Spatial 1D version of Dropout.
class SpatialDropout2D: Spatial 2D version of Dropout.
class SpatialDropout3D: Spatial 3D version of Dropout.
class StackedRNNCells: Wrapper allowing a stack of RNN cells to behave as a single cell.
class Subtract: Layer that subtracts two inputs.
class ThresholdedReLU: Thresholded Rectified Linear Unit.
class TimeDistributed: This wrapper allows to apply a layer to every temporal slice of an input.
class UpSampling1D: Upsampling layer for 1D inputs.
class UpSampling2D: Upsampling layer for 2D inputs.
class UpSampling3D: Upsampling layer for 3D inputs.
class Wrapper: Abstract wrapper base class.
class ZeroPadding1D: Zero-padding layer for 1D input (e.g. temporal sequence).
class ZeroPadding2D: Zero-padding layer for 2D input (e.g. picture).
class ZeroPadding3D: Zero-padding layer for 3D data (spatial or spatio-temporal).
Input(...): Input() is used to instantiate a Keras tensor.
add(...): Functional interface to the tf.keras.layers.Add layer.
average(...): Functional interface to the tf.keras.layers.Average layer.
concatenate(...): Functional interface to the Concatenate layer.
deserialize(...): Instantiates a layer from a config dictionary.
dot(...): Functional interface to the Dot layer.
maximum(...): Functional interface to compute maximum (element-wise) list of inputs.
minimum(...): Functional interface to the Minimum layer.
multiply(...): Functional interface to the Multiply layer.
serialize(...)
subtract(...): Functional interface to the Subtract layer.


Abstract object representing an RNN cell.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.AbstractRNNCell
See the Keras RNN API guide for details about the usage of RNN API.
This is the base class for implementing RNN cells with custom behavior.
Every RNNCell must have the properties below and implement call with the signature (output, next_state) = call(input, state).
This definition of cell differs from the definition used in the literature. In the literature, 'cell' refers to an object with a single scalar output. This definition refers to a horizontal array of such units.
An RNN cell, in the most abstract setting, is anything that has a state and performs some operation that takes a matrix of inputs. This operation results in an output matrix with self.output_size columns. If self.state_size is an integer, this operation also results in a new state matrix with self.state_size columns. If self.state_size is a (possibly nested tuple of) TensorShape object(s), then it should return a matching structure of Tensors having shape [batch_size].concatenate(s) for each s in self.batch_size.
It can be represented by an Integer, a TensorShape or a tuple of Integers or TensorShapes.
View source


Applies an activation function to an output.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Activation
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the batch axis) when using this layer as the first layer in a model.
Same shape as input.


Layer that applies an update to the cost function based input activity.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.ActivityRegularization
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.
Same shape as input.


Layer that adds a list of inputs.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Add
It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape).
Used in a functional model:


Additive attention layer, a.k.a. Bahdanau-style attention.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.AdditiveAttention
Inputs are query tensor of shape [batch_size, Tq, dim], value tensor of shape [batch_size, Tv, dim] and key tensor of shape [batch_size, Tv, dim]. The calculation follows the steps:
Attention outputs of shape [batch_size, Tq, dim]. [Optional] Attention scores after masking and softmax with shape [batch_size, Tq, Tv].
The meaning of query, value and key depend on the application. In the case of text similarity, for example, query is the sequence embeddings of the first piece of text and value is the sequence embeddings of the second piece of text. key is usually the same tensor as value.
Here is a code example for using AdditiveAttention in a CNN+Attention network:


Applies Alpha Dropout to the input.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.AlphaDropout
Alpha Dropout is a Dropout that keeps mean and variance of inputs to their original values, in order to ensure the self-normalizing property even after this dropout. Alpha Dropout fits well to Scaled Exponential Linear Units by randomly setting activations to the negative saturation value.
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.
Same shape as input.


Dot-product attention layer, a.k.a. Luong-style attention.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Attention
Inputs are query tensor of shape [batch_size, Tq, dim], value tensor of shape [batch_size, Tv, dim] and key tensor of shape [batch_size, Tv, dim]. The calculation follows the steps:
Attention outputs of shape [batch_size, Tq, dim]. [Optional] Attention scores after masking and softmax with shape [batch_size, Tq, Tv].
The meaning of query, value and key depend on the application. In the case of text similarity, for example, query is the sequence embeddings of the first piece of text and value is the sequence embeddings of the second piece of text. key is usually the same tensor as value.
Here is a code example for using Attention in a CNN+Attention network:


Layer that averages a list of inputs element-wise.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Average
It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape).
Usage in a functional model:


Average pooling for temporal data.
Inherits From: Layer, Module
Main aliases
tf.keras.layers.AvgPool1D
See Migration guide for more details.
tf.compat.v1.keras.layers.AveragePooling1D, tf.compat.v1.keras.layers.AvgPool1D


Average pooling operation for spatial data.
Inherits From: Layer, Module
Main aliases
tf.keras.layers.AvgPool2D
See Migration guide for more details.
tf.compat.v1.keras.layers.AveragePooling2D, tf.compat.v1.keras.layers.AvgPool2D


Average pooling operation for 3D data (spatial or spatio-temporal).
Inherits From: Layer, Module
Main aliases
tf.keras.layers.AvgPool3D
See Migration guide for more details.
tf.compat.v1.keras.layers.AveragePooling3D, tf.compat.v1.keras.layers.AvgPool3D


Layer that normalizes its inputs.
Inherits From: Layer, Module
Batch normalization applies a transformation that maintains the mean output close to 0 and the output standard deviation close to 1.
Importantly, batch normalization works differently during training and during inference.
During training (i.e. when using fit() or when calling the layer/model with the argument training=True), the layer normalizes its output using the mean and standard deviation of the current batch of inputs. That is to say, for each channel being normalized, the layer returns (batch - mean(batch)) / (var(batch) + epsilon) * gamma + beta, where:
During inference (i.e. when using evaluate() or predict() or when calling the layer/model with the argument training=False (which is the default), the layer normalizes its output using a moving average of the mean and standard deviation of the batches it has seen during training. That is to say, it returns (batch - self.moving_mean) / (self.moving_var + epsilon) * gamma + beta.
self.moving_mean and self.moving_var are non-trainable variables that are updated each time the layer in called in training mode, as such:
As such, the layer will only normalize its inputs during inference after having been trained on data that has similar statistics as the inference data.
Input shape: Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.
Output shape: Same shape as input. About setting layer.trainable = False on a BatchNormalization layer:
The meaning of setting layer.trainable = False is to freeze the layer, i.e. its internal state will not change during training: its trainable weights will not be updated during fit() or train_on_batch(), and its state updates will not be run.
Usually, this does not necessarily mean that the layer is run in inference mode (which is normally controlled by the training argument that can be passed when calling a layer). "Frozen state" and "inference mode" are two separate concepts.
However, in the case of the BatchNormalization layer, setting trainable = False on the layer means that the layer will be subsequently run in inference mode (meaning that it will use the moving mean and the moving variance to normalize the current batch, rather than using the mean and variance of the current batch).
This behavior has been introduced in TensorFlow 2.0, in order to enable layer.trainable = False to produce the most commonly expected behavior in the convnet fine-tuning use case.


Bidirectional wrapper for RNNs.
Inherits From: Wrapper, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Bidirectional
The call arguments for this layer are the same as those of the wrapped RNN layer. Beware that when passing the initial_state argument during the call of this layer, the first half in the list of elements in the initial_state list will be passed to the forward RNN call and the last half in the list of elements will be passed to the backward RNN call.
View source


Layer that concatenates a list of inputs.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Concatenate
It takes as input a list of tensors, all of the same shape except for the concatenation axis, and returns a single tensor that is the concatenation of all inputs.


1D convolution layer (e.g. temporal convolution).
Inherits From: Layer, Module
Main aliases
tf.keras.layers.Convolution1D
See Migration guide for more details.
tf.compat.v1.keras.layers.Conv1D, tf.compat.v1.keras.layers.Convolution1D
This layer creates a convolution kernel that is convolved with the layer input over a single spatial (or temporal) dimension to produce a tensor of outputs. If use_bias is True, a bias vector is created and added to the outputs. Finally, if activation is not None, it is applied to the outputs as well.
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.
3+D tensor with shape: batch_shape + (steps, input_dim)
3+D tensor with shape: batch_shape + (new_steps, filters) steps value might have changed due to padding or strides.


Transposed convolution layer (sometimes called Deconvolution).
Inherits From: Conv1D, Layer, Module
Main aliases
tf.keras.layers.Convolution1DTranspose
See Migration guide for more details.
tf.compat.v1.keras.layers.Conv1DTranspose, tf.compat.v1.keras.layers.Convolution1DTranspose
The need for transposed convolutions generally arises from the desire to use a transformation going in the opposite direction of a normal convolution, i.e., from something that has the shape of the output of some convolution to something that has the shape of its input while maintaining a connectivity pattern that is compatible with said convolution.
When using this layer as the first layer in a model, provide the keyword argument input_shape (tuple of integers, does not include the sample axis), e.g. input_shape=(128, 3) for data with 128 time steps and 3 channels.
3D tensor with shape: (batch_size, steps, channels)
3D tensor with shape: (batch_size, new_steps, filters) If output_padding is specified:


2D convolution layer (e.g. spatial convolution over images).
Inherits From: Layer, Module
Main aliases
tf.keras.layers.Convolution2D
See Migration guide for more details.
tf.compat.v1.keras.layers.Conv2D, tf.compat.v1.keras.layers.Convolution2D
This layer creates a convolution kernel that is convolved with the layer input to produce a tensor of outputs. If use_bias is True, a bias vector is created and added to the outputs. Finally, if activation is not None, it is applied to the outputs as well.
When using this layer as the first layer in a model, provide the keyword argument input_shape (tuple of integers, does not include the sample axis), e.g. input_shape=(128, 128, 3) for 128x128 RGB pictures in data_format="channels_last".
4+D tensor with shape: batch_shape + (channels, rows, cols) if data_format='channels_first' or 4+D tensor with shape: batch_shape + (rows, cols, channels) if data_format='channels_last'.
4+D tensor with shape: batch_shape + (filters, new_rows, new_cols) if data_format='channels_first' or 4+D tensor with shape: batch_shape + (new_rows, new_cols, filters) if data_format='channels_last'. rows and cols values might have changed due to padding.


Transposed convolution layer (sometimes called Deconvolution).
Inherits From: Conv2D, Layer, Module
Main aliases
tf.keras.layers.Convolution2DTranspose
See Migration guide for more details.
tf.compat.v1.keras.layers.Conv2DTranspose, tf.compat.v1.keras.layers.Convolution2DTranspose
The need for transposed convolutions generally arises from the desire to use a transformation going in the opposite direction of a normal convolution, i.e., from something that has the shape of the output of some convolution to something that has the shape of its input while maintaining a connectivity pattern that is compatible with said convolution.
When using this layer as the first layer in a model, provide the keyword argument input_shape (tuple of integers, does not include the sample axis), e.g. input_shape=(128, 128, 3) for 128x128 RGB pictures in data_format="channels_last".
4D tensor with shape: (batch_size, channels, rows, cols) if data_format='channels_first' or 4D tensor with shape: (batch_size, rows, cols, channels) if data_format='channels_last'.
4D tensor with shape: (batch_size, filters, new_rows, new_cols) if data_format='channels_first' or 4D tensor with shape: (batch_size, new_rows, new_cols, filters) if data_format='channels_last'. rows and cols values might have changed due to padding. If output_padding is specified:


3D convolution layer (e.g. spatial convolution over volumes).
Inherits From: Layer, Module
Main aliases
tf.keras.layers.Convolution3D
See Migration guide for more details.
tf.compat.v1.keras.layers.Conv3D, tf.compat.v1.keras.layers.Convolution3D
This layer creates a convolution kernel that is convolved with the layer input to produce a tensor of outputs. If use_bias is True, a bias vector is created and added to the outputs. Finally, if activation is not None, it is applied to the outputs as well.
When using this layer as the first layer in a model, provide the keyword argument input_shape (tuple of integers, does not include the sample axis), e.g. input_shape=(128, 128, 128, 1) for 128x128x128 volumes with a single channel, in data_format="channels_last".
5+D tensor with shape: batch_shape + (channels, conv_dim1, conv_dim2, conv_dim3) if data_format='channels_first' or 5+D tensor with shape: batch_shape + (conv_dim1, conv_dim2, conv_dim3, channels) if data_format='channels_last'.
5+D tensor with shape: batch_shape + (filters, new_conv_dim1, new_conv_dim2, new_conv_dim3) if data_format='channels_first' or 5+D tensor with shape: batch_shape + (new_conv_dim1, new_conv_dim2, new_conv_dim3, filters) if data_format='channels_last'. new_conv_dim1, new_conv_dim2 and new_conv_dim3 values might have changed due to padding.


Transposed convolution layer (sometimes called Deconvolution).
Inherits From: Conv3D, Layer, Module
Main aliases
tf.keras.layers.Convolution3DTranspose
See Migration guide for more details.
tf.compat.v1.keras.layers.Conv3DTranspose, tf.compat.v1.keras.layers.Convolution3DTranspose
The need for transposed convolutions generally arises from the desire to use a transformation going in the opposite direction of a normal convolution, i.e., from something that has the shape of the output of some convolution to something that has the shape of its input while maintaining a connectivity pattern that is compatible with said convolution.
When using this layer as the first layer in a model, provide the keyword argument input_shape (tuple of integers, does not include the sample axis), e.g. input_shape=(128, 128, 128, 3) for a 128x128x128 volume with 3 channels if data_format="channels_last".
5D tensor with shape: (batch_size, channels, depth, rows, cols) if data_format='channels_first' or 5D tensor with shape: (batch_size, depth, rows, cols, channels) if data_format='channels_last'.
5D tensor with shape: (batch_size, filters, new_depth, new_rows, new_cols) if data_format='channels_first' or 5D tensor with shape: (batch_size, new_depth, new_rows, new_cols, filters) if data_format='channels_last'. depth and rows and cols values might have changed due to padding. If output_padding is specified::


Convolutional LSTM.
Inherits From: RNN, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.ConvLSTM2D
It is similar to an LSTM layer, but the input transformations and recurrent transformations are both convolutional.
View source
Reset the recorded states for the stateful RNN layer.
Can only be used when RNN layer is constructed with stateful = True. Args: states: Numpy arrays that contains the value for the initial state, which will be feed to cell at the first time step. When the value is None, zero filled numpy array will be created based on the cell state size.


Cropping layer for 1D input (e.g. temporal sequence).
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Cropping1D
It crops along the time dimension (axis 1).
3D tensor with shape (batch_size, axis_to_crop, features)
3D tensor with shape (batch_size, cropped_axis, features)


Cropping layer for 2D input (e.g. picture).
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Cropping2D
It crops along spatial dimensions, i.e. height and width.
4D tensor with shape:
4D tensor with shape:


Cropping layer for 3D data (e.g. spatial or spatio-temporal).
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Cropping3D
Examples:
5D tensor with shape:
5D tensor with shape:


Just your regular densely-connected NN layer.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Dense
Dense implements the operation: output = activation(dot(input, kernel) + bias) where activation is the element-wise activation function passed as the activation argument, kernel is a weights matrix created by the layer, and bias is a bias vector created by the layer (only applicable if use_bias is True).
Besides, layer attributes cannot be modified after the layer has been called once (except the trainable attribute).
N-D tensor with shape: (batch_size, ..., input_dim). The most common situation would be a 2D input with shape (batch_size, input_dim).
N-D tensor with shape: (batch_size, ..., units). For instance, for a 2D input with shape (batch_size, input_dim), the output would have shape (batch_size, units).


A layer that produces a dense Tensor based on given feature_columns.
Inherits From: DenseFeatures, Layer, Module
Generally a single example in training data is described with FeatureColumns. At the first layer of the model, this column oriented data should be converted to a single Tensor.
This layer can be called multiple times with different features.
This is the V2 version of this layer that uses name_scopes to create variables instead of variable_scopes. But this approach currently lacks support for partitioned variables. In that case, use the V1 version instead.


Depthwise separable 2D convolution.
Inherits From: Conv2D, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.DepthwiseConv2D
Depthwise Separable convolutions consist of performing just the first step in a depthwise spatial convolution (which acts on each input channel separately). The depth_multiplier argument controls how many output channels are generated per input channel in the depthwise step.
4D tensor with shape: [batch_size, channels, rows, cols] if data_format='channels_first' or 4D tensor with shape: [batch_size, rows, cols, channels] if data_format='channels_last'.
4D tensor with shape: [batch_size, filters, new_rows, new_cols] if data_format='channels_first' or 4D tensor with shape: [batch_size, new_rows, new_cols, filters] if data_format='channels_last'. rows and cols values might have changed due to padding.


Instantiates a layer from a config dictionary.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.deserialize


Layer that computes a dot product between samples in two tensors.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Dot
E.g. if applied to a list of two tensors a and b of shape (batch_size, n), the output will be a tensor of shape (batch_size, 1) where each entry i will be the dot product between a[i] and b[i].


Applies Dropout to the input.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Dropout
The Dropout layer randomly sets input units to 0 with a frequency of rate at each step during training time, which helps prevent overfitting. Inputs not set to 0 are scaled up by 1/(1 - rate) such that the sum over all inputs is unchanged.
Note that the Dropout layer only applies when training is set to True such that no values are dropped during inference. When using model.fit, training will be appropriately set to True automatically, and in other contexts, you can set the kwarg explicitly to True when calling the layer.
(This is in contrast to setting trainable=False for a Dropout layer. trainable does not affect the layer's behavior, as Dropout does not have any variables/weights that can be frozen during training.)


Exponential Linear Unit.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.ELU
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.
Same shape as the input.


Turns positive integers (indexes) into dense vectors of fixed size.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Embedding
e.g. [[4], [20]] -> [[0.25, 0.1], [0.6, -0.2]]
This layer can only be used as the first layer in a model.
2D tensor with shape: (batch_size, input_length).
3D tensor with shape: (batch_size, input_length, output_dim).


Public API for tf.keras.layers.experimental namespace.
preprocessing module: Public API for tf.keras.layers.experimental.preprocessing namespace.
class EinsumDense: A layer that uses tf.einsum as the backing computation.
class RandomFourierFeatures: Layer that projects its inputs into a random feature space.
class SyncBatchNormalization: Normalize and scale inputs or activations synchronously across replicas.


A layer that uses tf.einsum as the backing computation.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.EinsumDense
This layer can perform einsum calculations of arbitrary dimensionality.
Biased dense layer with einsums
This example shows how to instantiate a standard Keras dense layer using einsum operations. This example is equivalent to tf.keras.layers.Dense(64, use_bias=True).
Applying a dense layer to a sequence
This example shows how to instantiate a layer that applies the same dense operation to every element in a sequence. Here, the 'output_shape' has two values (since there are two non-batch dimensions in the output); the first dimension in the output_shape is None, because the sequence dimension b has an unknown shape.
Applying a dense layer to a sequence using ellipses
This example shows how to instantiate a layer that applies the same dense operation to every element in a sequence, but uses the ellipsis notation instead of specifying the batch and sequence dimensions.
Because we are using ellipsis notation and have specified only one axis, the output_shape arg is a single value. When instantiated in this way, the layer can handle any number of sequence dimensions - including the case where no sequence dimension exists.


Public API for tf.keras.layers.experimental.preprocessing namespace.
class CategoryCrossing: Category crossing layer.
class CategoryEncoding: Category encoding layer.
class CenterCrop: Crop the central portion of the images to target height and width.
class Discretization: Buckets data into discrete ranges.
class Hashing: Implements categorical feature hashing, also known as "hashing trick".
class IntegerLookup: Maps integers from a vocabulary to integer indices.
class Normalization: Feature-wise normalization of the data.
class PreprocessingLayer: Base class for PreprocessingLayers.
class RandomContrast: Adjust the contrast of an image or images by a random factor.
class RandomCrop: Randomly crop the images to target height and width.
class RandomFlip: Randomly flip each image horizontally and vertically.
class RandomHeight: Randomly vary the height of a batch of images during training.
class RandomRotation: Randomly rotate each image.
class RandomTranslation: Randomly translate each image during training.
class RandomWidth: Randomly vary the width of a batch of images during training.
class RandomZoom: Randomly zoom each image during training.
class Rescaling: Multiply inputs by scale and adds offset.
class Resizing: Image resizing layer.
class StringLookup: Maps strings from a vocabulary to integer indices.
class TextVectorization: Text vectorization layer.


Category crossing layer.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.CategoryCrossing
This layer concatenates multiple categorical inputs into a single categorical output (similar to Cartesian product). The output dtype is string.
Input shape: a list of string or int tensors or sparse tensors of shape [batch_size, d1, ..., dm]
Output shape: a single string or int tensor or sparse tensor of shape [batch_size, d1, ..., dm]
Example: (depth=None) If the layer receives three inputs: a=[[1], [4]], b=[[2], [5]], c=[[3], [6]] the output will be a string tensor: [[b'1_X_2_X_3'], [b'4_X_5_X_6']]
Example: (depth is an integer) With the same input above, and if depth=2, the output will be a list of 6 string tensors: [[b'1'], [b'4']] [[b'2'], [b'5']] [[b'3'], [b'6']] [[b'1_X_2'], [b'4_X_5']], [[b'2_X_3'], [b'5_X_6']], [[b'3_X_1'], [b'6_X_4']]
Example: (depth is a tuple/list of integers) With the same input above, and if depth=(2, 3) the output will be a list of 4 string tensors: [[b'1_X_2'], [b'4_X_5']], [[b'2_X_3'], [b'5_X_6']], [[b'3_X_1'], [b'6_X_4']], [[b'1_X_2_X_3'], [b'4_X_5_X_6']]
View source
Fits the state of the preprocessing layer to the data being passed.
View source
Gets the crossed output from a partial list/tuple of inputs.


Category encoding layer.
Inherits From: PreprocessingLayer, Layer, Module
This layer provides options for condensing data into a categorical encoding. It accepts integer values as inputs and outputs a dense representation (one sample = 1-index tensor of float values representing data about the sample's tokens) of those inputs.
Examples with weighted inputs:
View source
Fits the state of the preprocessing layer to the dataset.
Overrides the default adapt method to apply relevant preprocessing to the inputs before passing to the combiner.
View source
View source


Crop the central portion of the images to target height and width.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.CenterCrop
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
4D tensor with shape: (samples, target_height, target_width, channels).
If the input height/width is even and the target height/width is odd (or inversely), the input image is left-padded by 1 pixel.
View source
Fits the state of the preprocessing layer to the data being passed.


Buckets data into discrete ranges.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.Discretization
This layer will place each element of its input data into one of several contiguous ranges and output an integer index indicating which range each element was placed in.
Any tf.Tensor or tf.RaggedTensor of dimension 2 or higher.
Same as input shape.
Bucketize float values based on provided buckets.
View source
Fits the state of the preprocessing layer to the data being passed.


Implements categorical feature hashing, also known as "hashing trick".
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.Hashing
This layer transforms single or multiple categorical inputs to hashed output. It converts a sequence of int or string to a sequence of int. The stable hash function uses tensorflow::ops::Fingerprint to produce universal output that is consistent across platforms.
This layer uses FarmHash64 by default, which provides a consistent hashed output across different platforms and is stable across invocations, regardless of device and context, by mixing the input bits thoroughly.
If you want to obfuscate the hashed output, you can also pass a random salt argument in the constructor. In that case, the layer will use the SipHash64 hash function, with the salt value serving as additional input to the hash function.
Example (FarmHash64):
Example (FarmHash64) with list of inputs:
Example (SipHash64):
Example (Siphash64 with a single integer, same as salt=[133, 133]
Reference: SipHash with salt
Input shape: A single or list of string, int32 or int64 Tensor, SparseTensor or RaggedTensor of shape [batch_size, ...,]
Output shape: An int64 Tensor, SparseTensor or RaggedTensor of shape [batch_size, ...]. If any input is RaggedTensor then output is RaggedTensor, otherwise if any input is SparseTensor then output is SparseTensor, otherwise the output is Tensor.
View source
Fits the state of the preprocessing layer to the data being passed.


Maps integers from a vocabulary to integer indices.
Inherits From: PreprocessingLayer, Layer, Module
This layer translates a set of arbitrary integers into an integer output via a table-based lookup, with optional out-of-vocabulary handling.
If desired, the user can call this layer's adapt() method on a data set, which will analyze the data set, determine the frequency of individual string values, and create a vocabulary from them. This vocabulary can have unlimited size or be capped, depending on the configuration options for this layer; if there are more unique values in the input than the maximum vocabulary size, the most frequent terms will be used to create the vocabulary.
Creating a lookup layer with a known vocabulary
This example creates a lookup layer with a pre-existing vocabulary.
Creating a lookup layer with an adapted vocabulary
This example creates a lookup layer and generates the vocabulary by analyzing the dataset.
Note how the mask value 0 and the OOV value -1 have been added to the vocabulary. The remaining values are sorted by frequency (1138, which has 2 occurrences, is first) then by inverse sort order.
Lookups with multiple OOV tokens.
This example demonstrates how to use a lookup layer with multiple OOV tokens. When a layer is created with more than one OOV token, any OOV values are hashed into the number of OOV buckets, distributing OOV values in a deterministic fashion across the set.
Note that the output for OOV value 37 is 2, while the output for OOV value 1000 is 1. The in-vocab terms have their output index increased by 1 from earlier examples (12 maps to 3, etc) in order to make space for the extra OOV value.
Inverse lookup
This example demonstrates how to map indices to values using this layer. (You can also use adapt() with inverse=True, but for simplicity we'll pass the vocab in this example.)
Note that the integer 5, which is out of the vocabulary space, returns an OOV token.
Forward and inverse lookup pairs
This example demonstrates how to use the vocabulary of a standard lookup layer to create an inverse lookup layer.
In this example, the input value 1000 resulted in an output of -1, since 1000 was not in the vocabulary - it got represented as an OOV, and all OOV values are returned as -1 in the inverse layer. Also, note that for the inverse to work, you must have already set the forward layer vocabulary either directly or via fit() before calling get_vocabulary().
View source
Fits the state of the preprocessing layer to the dataset.
Overrides the default adapt method to apply relevant preprocessing to the inputs before passing to the combiner.
View source
View source
Sets vocabulary data for this layer with inverse=False.
This method sets the vocabulary for this layer directly, instead of analyzing a dataset through 'adapt'. It should be used whenever the vocab information is already known. If vocabulary data is already present in the layer, this method will either replace it
View source


Feature-wise normalization of the data.
Inherits From: PreprocessingLayer, Layer, Module
This layer will coerce its inputs into a distribution centered around 0 with standard deviation 1. It accomplishes this by precomputing the mean and variance of the data, and calling (input-mean)/sqrt(var) at runtime.
What happens in adapt: Compute mean and variance of the data and store them as the layer's weights. adapt should be called before fit, evaluate, or predict.
Calculate the mean and variance by analyzing the dataset in adapt.
Pass the mean and variance directly.
View source
Fits the state of the preprocessing layer to the data being passed.


Base class for PreprocessingLayers.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.PreprocessingLayer
View source
Fits the state of the preprocessing layer to the data being passed.


Adjust the contrast of an image or images by a random factor.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.RandomContrast
Contrast is adjusted independently for each channel of each image during training.
For each channel, this layer computes the mean of the image pixels in the channel and then adjusts each component x of each pixel to (x - mean) * contrast_factor + mean.
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
View source
Fits the state of the preprocessing layer to the data being passed.


Randomly crop the images to target height and width.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.RandomCrop
This layer will crop all the images in the same batch to the same cropping location. By default, random cropping is only applied during training. At inference time, the images will be first rescaled to preserve the shorter side, and center cropped. If you need to apply random cropping at inference time, set training to True when calling the layer.
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
4D tensor with shape: (samples, target_height, target_width, channels).
View source
Fits the state of the preprocessing layer to the data being passed.


Randomly flip each image horizontally and vertically.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.RandomFlip
This layer will flip the images based on the mode attribute. During inference time, the output will be identical to input. Call the layer with training=True to flip the input.
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
View source
Fits the state of the preprocessing layer to the data being passed.


Randomly vary the height of a batch of images during training.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.RandomHeight
Adjusts the height of a batch of images by a random factor. The input should be a 4-D tensor in the "channels_last" image data format.
By default, this layer is inactive during inference.
4D tensor with shape: (samples, height, width, channels) (data_format='channels_last').
4D tensor with shape: (samples, random_height, width, channels).
View source
Fits the state of the preprocessing layer to the data being passed.


Randomly rotate each image.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.RandomRotation
By default, random rotations are only applied during training. At inference time, the layer does nothing. If you need to apply random rotations at inference time, set training to True when calling the layer.
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
View source
Fits the state of the preprocessing layer to the data being passed.


Randomly translate each image during training.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.RandomTranslation
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
View source
Fits the state of the preprocessing layer to the data being passed.


Randomly vary the width of a batch of images during training.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.RandomWidth
Adjusts the width of a batch of images by a random factor. The input should be a 4-D tensor in the "channels_last" image data format.
By default, this layer is inactive during inference.
4D tensor with shape: (samples, height, width, channels) (data_format='channels_last').
4D tensor with shape: (samples, height, random_width, channels).
View source
Fits the state of the preprocessing layer to the data being passed.


Randomly zoom each image during training.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.RandomZoom
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
4D tensor with shape: (samples, height, width, channels), data_format='channels_last'.
View source
Fits the state of the preprocessing layer to the data being passed.


Multiply inputs by scale and adds offset.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.Rescaling
To rescale an input in the [0, 255] range to be in the [0, 1] range, you would pass scale=1./255.
To rescale an input in the [0, 255] range to be in the [-1, 1] range, you would pass scale=1./127.5, offset=-1.
The rescaling is applied both during training and inference.
Arbitrary.
Same as input.
View source
Fits the state of the preprocessing layer to the data being passed.


Image resizing layer.
Inherits From: PreprocessingLayer, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.preprocessing.Resizing
Resize the batched image input to target height and width. The input should be a 4-D tensor in the format of NHWC.
View source
Fits the state of the preprocessing layer to the data being passed.


Maps strings from a vocabulary to integer indices.
Inherits From: PreprocessingLayer, Layer, Module
This layer translates a set of arbitrary strings into an integer output via a table-based lookup, with optional out-of-vocabulary handling.
If desired, the user can call this layer's adapt() method on a data set, which will analyze the data set, determine the frequency of individual string values, and create a vocabulary from them. This vocabulary can have unlimited size or be capped, depending on the configuration options for this layer; if there are more unique values in the input than the maximum vocabulary size, the most frequent terms will be used to create the vocabulary.
Creating a lookup layer with a known vocabulary
This example creates a lookup layer with a pre-existing vocabulary.
Creating a lookup layer with an adapted vocabulary
This example creates a lookup layer and generates the vocabulary by analyzing the dataset.
Note how the mask token '' and the OOV token [UNK] have been added to the vocabulary. The remaining tokens are sorted by frequency ('d', which has 2 occurrences, is first) then by inverse sort order.
Lookups with multiple OOV tokens.
This example demonstrates how to use a lookup layer with multiple OOV tokens. When a layer is created with more than one OOV token, any OOV values are hashed into the number of OOV buckets, distributing OOV values in a deterministic fashion across the set.
Note that the output for OOV value 'm' is 1, while the output for OOV value 'z' is 2. The in-vocab terms have their output index increased by 1 from earlier examples (a maps to 3, etc) in order to make space for the extra OOV value.
Inverse lookup
This example demonstrates how to map indices to strings using this layer. (You can also use adapt() with inverse=True, but for simplicity we'll pass the vocab in this example.)
Note that the integer 5, which is out of the vocabulary space, returns an OOV token.
Forward and inverse lookup pairs
This example demonstrates how to use the vocabulary of a standard lookup layer to create an inverse lookup layer.
In this example, the input value 'z' resulted in an output of '[UNK]', since 1000 was not in the vocabulary - it got represented as an OOV, and all OOV values are returned as '[OOV}' in the inverse layer. Also, note that for the inverse to work, you must have already set the forward layer vocabulary either directly or via fit() before calling get_vocabulary().
View source
Fits the state of the preprocessing layer to the dataset.
Overrides the default adapt method to apply relevant preprocessing to the inputs before passing to the combiner.
View source
View source
Sets vocabulary data for this layer with inverse=False.
This method sets the vocabulary for this layer directly, instead of analyzing a dataset through 'adapt'. It should be used whenever the vocab information is already known. If vocabulary data is already present in the layer, this method will either replace it
View source


Text vectorization layer.
Inherits From: PreprocessingLayer, Layer, Module
This layer has basic options for managing text in a Keras model. It transforms a batch of strings (one sample = one string) into either a list of token indices (one sample = 1D tensor of integer token indices) or a dense representation (one sample = 1D tensor of float values representing data about the sample's tokens).
If desired, the user can call this layer's adapt() method on a dataset. When this layer is adapted, it will analyze the dataset, determine the frequency of individual string values, and create a 'vocabulary' from them. This vocabulary can have unlimited size or be capped, depending on the configuration options for this layer; if there are more unique values in the input than the maximum vocabulary size, the most frequent terms will be used to create the vocabulary.
The processing of each sample contains the following steps:
Some notes on passing Callables to customize splitting and normalization for this layer:
This example instantiates a TextVectorization layer that lowercases text, splits on whitespace, strips punctuation, and outputs integer vocab indices.
This example instantiates a TextVectorization layer by passing a list of vocabulary terms to the layer's init method.
input_array = np.array([["earth", "wind", "and", "fire"], ["fire", "and", "earth", "michigan"]]) expected_output = [[2, 3, 4, 5], [5, 4, 2, 1]]
input_data = keras.Input(shape=(None,), dtype=dtypes.string) layer = get_layer_class()( max_tokens=None, standardize=None, split=None, output_mode=text_vectorization.INT, vocabulary=vocab_data) int_data = layer(input_data) model = keras.Model(inputs=input_data, outputs=int_data)
output_dataset = model.predict(input_array)
View source
Fits the state of the preprocessing layer to the dataset.
Overrides the default adapt method to apply relevant preprocessing to the inputs before passing to the combiner.
View source
View source
Sets vocabulary (and optionally document frequency) data for this layer.
This method sets the vocabulary and DF data for this layer directly, instead of analyzing a dataset through 'adapt'. It should be used whenever the vocab (and optionally document frequency) information is already known. If vocabulary data is already present in the layer, this method will replace it.


Layer that projects its inputs into a random feature space.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.experimental.RandomFourierFeatures
This layer implements a mapping from input space to a space with output_dim dimensions, which approximates shift-invariant kernels. A kernel function K(x, y) is shift-invariant if K(x, y) == k(x - y) for some function k. Many popular Radial Basis Functions (RBF), including Gaussian and Laplacian kernels, are shift-invariant.
The implementation of this layer is based on the following paper: "Random Features for Large-Scale Kernel Machines" by Ali Rahimi and Ben Recht.
The distribution from which the parameters of the random features map (layer) are sampled determines which shift-invariant kernel the layer approximates (see paper for more details). You can use the distribution of your choice. The layer supports out-of-the-box approximation sof the following two RBF kernels:
Usage: Typically, this layer is used to "kernelize" linear models by applying a non-linear transformation (this layer) to the input features and then training a linear model on top of the transformed features. Depending on the loss function of the linear model, the composition of this layer and the linear model results to models that are equivalent (up to approximation) to kernel SVMs (for hinge loss), kernel logistic regression (for logistic loss), kernel linear regression (for squared loss), etc.
A kernel multinomial logistic regression model with Gaussian kernel for MNIST:
A quasi-SVM classifier for MNIST:
To use another kernel, just replace the layer creation line with:


Normalize and scale inputs or activations synchronously across replicas.
Inherits From: Layer, Module
Applies batch normalization to activations of the previous layer at each batch by synchronizing the global batch statistics across all devices that are training the model. For specific details about batch normalization please refer to the tf.keras.layers.BatchNormalization layer docs.
If this layer is used when using tf.distribute strategy to train models across devices/workers, there will be an allreduce call to aggregate batch statistics across all replicas at every training step. Without tf.distribute strategy, this layer behaves as a regular tf.keras.layers.BatchNormalization layer.
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.
Same shape as input.


Flattens the input. Does not affect the batch size.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Flatten


Apply multiplicative 1-centered Gaussian noise.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.GaussianDropout
As it is a regularization layer, it is only active at training time.
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.
Same shape as input.


Apply additive zero-centered Gaussian noise.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.GaussianNoise
This is useful to mitigate overfitting (you could see it as a form of random data augmentation). Gaussian Noise (GS) is a natural choice as corruption process for real valued inputs.
As it is a regularization layer, it is only active at training time.
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.
Same shape as input.


Global average pooling operation for temporal data.
Inherits From: Layer, Module
Main aliases
tf.keras.layers.GlobalAvgPool1D
See Migration guide for more details.
tf.compat.v1.keras.layers.GlobalAveragePooling1D, tf.compat.v1.keras.layers.GlobalAvgPool1D
2D tensor with shape (batch_size, features).


Global average pooling operation for spatial data.
Inherits From: Layer, Module
Main aliases
tf.keras.layers.GlobalAvgPool2D
See Migration guide for more details.
tf.compat.v1.keras.layers.GlobalAveragePooling2D, tf.compat.v1.keras.layers.GlobalAvgPool2D
2D tensor with shape (batch_size, channels).


Global Average pooling operation for 3D data.
Inherits From: Layer, Module
Main aliases
tf.keras.layers.GlobalAvgPool3D
See Migration guide for more details.
tf.compat.v1.keras.layers.GlobalAveragePooling3D, tf.compat.v1.keras.layers.GlobalAvgPool3D
2D tensor with shape (batch_size, channels).


Global max pooling operation for 1D temporal data.
Inherits From: Layer, Module
Main aliases
tf.keras.layers.GlobalMaxPooling1D
See Migration guide for more details.
tf.compat.v1.keras.layers.GlobalMaxPool1D, tf.compat.v1.keras.layers.GlobalMaxPooling1D
Downsamples the input representation by taking the maximum value over the time dimension.
2D tensor with shape (batch_size, features).


Global max pooling operation for spatial data.
Inherits From: Layer, Module
Main aliases
tf.keras.layers.GlobalMaxPooling2D
See Migration guide for more details.
tf.compat.v1.keras.layers.GlobalMaxPool2D, tf.compat.v1.keras.layers.GlobalMaxPooling2D
2D tensor with shape (batch_size, channels).


Global Max pooling operation for 3D data.
Inherits From: Layer, Module
Main aliases
tf.keras.layers.GlobalMaxPooling3D
See Migration guide for more details.
tf.compat.v1.keras.layers.GlobalMaxPool3D, tf.compat.v1.keras.layers.GlobalMaxPooling3D
2D tensor with shape (batch_size, channels).


Gated Recurrent Unit - Cho et al. 2014.
Inherits From: GRU, RNN, Layer, Module
See the Keras RNN API guide for details about the usage of RNN API.
Based on available runtime hardware and constraints, this layer will choose different implementations (cuDNN-based or pure-TensorFlow) to maximize the performance. If a GPU is available and all the arguments to the layer meet the requirement of the CuDNN kernel (see below for details), the layer will use a fast cuDNN implementation.
The requirements to use the cuDNN implementation are:
There are two variants of the GRU implementation. The default one is based on v3 and has reset gate applied to hidden state before matrix multiplication. The other one is based on original and has the order reversed.
The second variant is compatible with CuDNNGRU (GPU-only) and allows inference on CPU. Thus it has separate biases for kernel and recurrent_kernel. To use this variant, set 'reset_after'=True and recurrent_activation='sigmoid'.
View source
Get the dropout mask for RNN cell's input.
It will create mask based on context if there isn't any existing cached mask. If a new mask is generated, it will update the cache in the cell.
View source
Get the recurrent dropout mask for RNN cell.
It will create mask based on context if there isn't any existing cached mask. If a new mask is generated, it will update the cache in the cell.
View source
Reset the cached dropout masks if any.
This is important for the RNN layer to invoke this in it call() method so that the cached mask is cleared before calling the cell.call(). The mask should be cached across the timestep within the same batch, but shouldn't be cached between batches. Otherwise it will introduce unreasonable bias against certain index of data within the batch.
View source
Reset the cached recurrent dropout masks if any.
This is important for the RNN layer to invoke this in it call() method so that the cached mask is cleared before calling the cell.call(). The mask should be cached across the timestep within the same batch, but shouldn't be cached between batches. Otherwise it will introduce unreasonable bias against certain index of data within the batch.
View source
Reset the recorded states for the stateful RNN layer.
Can only be used when RNN layer is constructed with stateful = True. Args: states: Numpy arrays that contains the value for the initial state, which will be feed to cell at the first time step. When the value is None, zero filled numpy array will be created based on the cell state size.


Cell class for the GRU layer.
Inherits From: GRUCell, Layer, Module
See the Keras RNN API guide for details about the usage of RNN API.
This class processes one step within the whole time sequence input, whereas tf.keras.layer.GRU processes the whole sequence.
View source
Get the dropout mask for RNN cell's input.
It will create mask based on context if there isn't any existing cached mask. If a new mask is generated, it will update the cache in the cell.
View source
View source
Get the recurrent dropout mask for RNN cell.
It will create mask based on context if there isn't any existing cached mask. If a new mask is generated, it will update the cache in the cell.
View source
Reset the cached dropout masks if any.
This is important for the RNN layer to invoke this in it call() method so that the cached mask is cleared before calling the cell.call(). The mask should be cached across the timestep within the same batch, but shouldn't be cached between batches. Otherwise it will introduce unreasonable bias against certain index of data within the batch.
View source
Reset the cached recurrent dropout masks if any.
This is important for the RNN layer to invoke this in it call() method so that the cached mask is cleared before calling the cell.call(). The mask should be cached across the timestep within the same batch, but shouldn't be cached between batches. Otherwise it will introduce unreasonable bias against certain index of data within the batch.


Layer to be used as an entry point into a Network (a graph of layers).
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.InputLayer
It can either wrap an existing tensor (pass an input_tensor argument) or create a placeholder tensor (pass arguments input_shape, and optionally, dtype).
It is generally recommend to use the functional layer API via Input, (which creates an InputLayer) without directly using InputLayer.
When using InputLayer with Keras Sequential model, it can be skipped by moving the input_shape parameter to the first layer after the InputLayer.
This class can create placeholders for tf.Tensors, tf.SparseTensors, and tf.RaggedTensors by choosing 'sparse=True' or 'ragged=True'. Note that 'sparse' and 'ragged' can't be configured to True at same time. Usage:


Specifies the rank, dtype and shape of every input to a layer.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.InputSpec, tf.compat.v1.layers.InputSpec
Layers can expose (if appropriate) an input_spec attribute: an instance of InputSpec, or a nested structure of InputSpec instances (one per input tensor). These objects enable the layer to run input compatibility checks for input structure, input rank, input shape, and input dtype.
A None entry in a shape is compatible with any dimension, a None shape is compatible with any shape.
View source
View source


Wraps arbitrary expressions as a Layer object.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Lambda
The Lambda layer exists so that arbitrary TensorFlow functions can be used when constructing Sequential and Functional API models. Lambda layers are best suited for simple operations or quick experimentation. For more advanced use cases, follow this guide for subclassing tf.keras.layers.Layer.
The main reason to subclass tf.keras.layers.Layer instead of using a Lambda layer is saving and inspecting a Model. Lambda layers are saved by serializing the Python bytecode, which is fundamentally non-portable. They should only be loaded in the same environment where they were saved. Subclassed layers can be saved in a more portable way by overriding their get_config method. Models that rely on subclassed Layers are also often easier to visualize and reason about.
While it is possible to use Variables with Lambda layers, this practice is discouraged as it can easily lead to bugs. For instance, consider the following layer:
Because scale_layer does not directly track the scale variable, it will not appear in scale_layer.trainable_weights and will therefore not be trained if scale_layer is used in a Model.
A better pattern is to write a subclassed Layer:
In general, Lambda layers can be convenient for simple stateless computation, but anything more complex should use a subclass Layer instead.
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.
Specified by output_shape argument


This is the class from which all layers inherit.
Inherits From: Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Layer
A layer is a callable object that takes as input one or more tensors and that outputs one or more tensors. It involves computation, defined in the call() method, and a state (weight variables), defined either in the constructor __init__() or in the build() method.
Users will just instantiate a layer and then treat it as a callable.
We recommend that descendants of Layer implement the following methods:
Here's a basic example: a layer with two variables, w and b, that returns y = w . x + b. It shows how to implement build() and call(). Variables set as attributes of a layer are tracked as weights of the layers (in layer.weights).
Note that the method add_weight() offers a shortcut to create weights:
Besides trainable weights, updated via backpropagation during training, layers can also have non-trainable weights. These weights are meant to be updated manually during call(). Here's a example layer that computes the running sum of its inputs:
For more information about creating layers, see the guide Writing custom layers and models with Keras
Only applicable if the layer has exactly one input, i.e. if it is connected to one incoming layer.
Variable regularization tensors are created when this property is accessed, so it is eager safe: accessing losses under a tf.GradientTape will propagate gradients back to the corresponding variables.
Only applicable if the layer has exactly one output, i.e. if it is connected to one incoming layer.
View source
Add loss tensor(s), potentially dependent on layer inputs.
Some losses (for instance, activity regularization losses) may be dependent on the inputs passed when calling a layer. Hence, when reusing the same layer on different inputs a and b, some entries in layer.losses may be dependent on a and some on b. This method automatically keeps track of dependencies.
This method can be used inside a subclassed layer or model's call function, in which case losses should be a Tensor or list of Tensors.
This method can also be called directly on a Functional Model during construction. In this case, any loss Tensors passed to this Model must be symbolic and be able to be traced back to the model's Inputs. These losses become part of the model's topology and are tracked in get_config.
If this is not the case for your loss (if, for example, your loss references a Variable of one of the model's layers), you can wrap your loss in a zero-argument lambda. These losses are not tracked as part of the model's topology since they can't be serialized.
View source
Adds metric tensor to the layer.
This method can be used inside the call() method of a subclassed layer or model.
This method can also be called directly on a Functional Model during construction. In this case, any tensor passed to this Model must be symbolic and be able to be traced back to the model's Inputs. These metrics become part of the model's topology and are tracked when you save the model via save().
View source
Adds a new variable to the layer.
View source
Creates the variables of the layer (optional, for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call.
This is typically used to create the weights of Layer subclasses.
View source
This is where the layer's logic lives.
Note here that call() method in tf.keras is little bit different from keras API. In keras API, you can pass support masking for layers as additional arguments. Whereas tf.keras has compute_mask() method to support masking.
View source
Computes an output mask tensor.
View source
Computes the output shape of the layer.
If the layer has not been built, this method will call build on the layer. This assumes that the layer will later be used with inputs that match the input shape provided here.
View source
Compute the output tensor signature of the layer based on the inputs.
Unlike a TensorShape object, a TensorSpec object contains both shape and dtype information for a tensor. This method allows layers to provide output dtype information if it is different from the input dtype. For any layer that doesn't implement this function, the framework will fall back to use compute_output_shape, and will assume that the output dtype matches the input dtype.
View source
Count the total number of scalars composing the weights.
View source
Creates a layer from its config.
This method is the reverse of get_config, capable of instantiating the same layer from the config dictionary. It does not handle layer connectivity (handled by Network), nor weights (handled by set_weights).
View source
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
View source
Returns the current weights of the layer.
The weights of a layer represent the state of the layer. This function returns both trainable and non-trainable weight values associated with this layer as a list of Numpy arrays, which can in turn be used to load state into similarly parameterized layers.
For example, a Dense layer returns a list of two values-- per-output weights and the bias value. These can be used to set the weights of another Dense layer:
View source
Sets the weights of the layer, from Numpy arrays.
The weights of a layer represent the state of the layer. This function sets the weight values from numpy arrays. The weight values should be passed in the order they are created by the layer. Note that the layer's weights must be instantiated before calling this function by calling the layer.
For example, a Dense layer returns a list of two values-- per-output weights and the bias value. These can be used to set the weights of another Dense layer:
View source
Wraps call, applying pre- and post-processing steps.


Layer normalization layer (Ba et al., 2016).
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.LayerNormalization
Normalize the activations of the previous layer for each given example in a batch independently, rather than across a batch like Batch Normalization. i.e. applies a transformation that maintains the mean activation within each example close to 0 and the activation standard deviation close to 1.
Given a tensor inputs, moments are calculated and normalization is performed across the axes specified in axis.
Notice that with Layer Normalization the normalization happens across the axes within each example, rather than across different examples in the batch.
If scale or center are enabled, the layer will scale the normalized outputs by broadcasting them with a trainable variable gamma, and center the outputs by broadcasting with a trainable variable beta. gamma will default to a ones tensor and beta will default to a zeros tensor, so that centering and scaling are no-ops before training has begun.
So, with scaling and centering enabled the normalization equations are as follows: Let the intermediate activations for a mini-batch to be the inputs.
For each sample x_i in inputs with k features, we compute the mean and variance of the sample:
and then compute a normalized x_i_normalized, including a small factor epsilon for numerical stability.
And finally x_i_normalized is linearly transformed by gamma and beta, which are learned parameters:
gamma and beta will span the axes of inputs specified in axis, and this part of the inputs' shape must be fully defined.
Note that other implementations of layer normalization may choose to define gamma and beta over a separate set of axes from the axes being normalized across. For example, Group Normalization (Wu et al. 2018) with group size of 1 corresponds to a Layer Normalization that normalizes across height, width, and channel and has gamma and beta span only the channel dimension. So, this Layer Normalization implementation will not match a Group Normalization layer with group size set to 1.
Input shape: Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model. Output shape: Same shape as input. Reference:


Leaky version of a Rectified Linear Unit.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.LeakyReLU
It allows a small gradient when the unit is not active:
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the batch axis) when using this layer as the first layer in a model.
Same shape as the input.


Locally-connected layer for 1D inputs.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.LocallyConnected1D
The LocallyConnected1D layer works similarly to the Conv1D layer, except that weights are unshared, that is, a different set of filters is applied at each different patch of the input.
2 stores layer weights in a dense but sparsely-populated 2D matrix and implements the forward pass as a single matrix-multiply. It uses a lot of RAM but performs few (large) ops.
3 stores layer weights in a sparse tensor and implements the forward pass as a single sparse matrix-multiply.
How to choose:
1: large, dense models, 2: small models, 3: large, sparse models,
where "large" stands for large input/output activations (i.e. many filters, input_filters, large input_size, output_size), and "sparse" stands for few connections between inputs and outputs, i.e. small ratio filters * input_filters * kernel_size / (input_size * strides), where inputs to and outputs of the layer are assumed to have shapes (input_size, input_filters), (output_size, filters) respectively.
It is recommended to benchmark each in the setting of interest to pick the most efficient one (in terms of speed and memory usage). Correct choice of implementation can lead to dramatic speed improvements (e.g. 50X), potentially at the expense of RAM.
Also, only padding="valid" is supported by implementation=1.
3D tensor with shape: (batch_size, steps, input_dim)
3D tensor with shape: (batch_size, new_steps, filters) steps value might have changed due to padding or strides.


Locally-connected layer for 2D inputs.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.LocallyConnected2D
The LocallyConnected2D layer works similarly to the Conv2D layer, except that weights are unshared, that is, a different set of filters is applied at each different patch of the input.
2 stores layer weights in a dense but sparsely-populated 2D matrix and implements the forward pass as a single matrix-multiply. It uses a lot of RAM but performs few (large) ops.
3 stores layer weights in a sparse tensor and implements the forward pass as a single sparse matrix-multiply.
How to choose:
1: large, dense models, 2: small models, 3: large, sparse models,
where "large" stands for large input/output activations (i.e. many filters, input_filters, large np.prod(input_size), np.prod(output_size)), and "sparse" stands for few connections between inputs and outputs, i.e. small ratio `filters * input_filters * np.prod(kernel_size) / (np.prod(input_size)
It is recommended to benchmark each in the setting of interest to pick the most efficient one (in terms of speed and memory usage). Correct choice of implementation can lead to dramatic speed improvements (e.g. 50X), potentially at the expense of RAM.
Also, only padding="valid" is supported by implementation=1.
4D tensor with shape: (samples, channels, rows, cols) if data_format='channels_first' or 4D tensor with shape: (samples, rows, cols, channels) if data_format='channels_last'.
4D tensor with shape: (samples, filters, new_rows, new_cols) if data_format='channels_first' or 4D tensor with shape: (samples, new_rows, new_cols, filters) if data_format='channels_last'. rows and cols values might have changed due to padding.


Long Short-Term Memory layer - Hochreiter 1997.
Inherits From: LSTM, RNN, Layer, Module
See the Keras RNN API guide for details about the usage of RNN API.
Based on available runtime hardware and constraints, this layer will choose different implementations (cuDNN-based or pure-TensorFlow) to maximize the performance. If a GPU is available and all the arguments to the layer meet the requirement of the CuDNN kernel (see below for details), the layer will use a fast cuDNN implementation.
The requirements to use the cuDNN implementation are:
View source
Get the dropout mask for RNN cell's input.
It will create mask based on context if there isn't any existing cached mask. If a new mask is generated, it will update the cache in the cell.
View source
Get the recurrent dropout mask for RNN cell.
It will create mask based on context if there isn't any existing cached mask. If a new mask is generated, it will update the cache in the cell.
View source
Reset the cached dropout masks if any.
This is important for the RNN layer to invoke this in it call() method so that the cached mask is cleared before calling the cell.call(). The mask should be cached across the timestep within the same batch, but shouldn't be cached between batches. Otherwise it will introduce unreasonable bias against certain index of data within the batch.
View source
Reset the cached recurrent dropout masks if any.
This is important for the RNN layer to invoke this in it call() method so that the cached mask is cleared before calling the cell.call(). The mask should be cached across the timestep within the same batch, but shouldn't be cached between batches. Otherwise it will introduce unreasonable bias against certain index of data within the batch.
View source
Reset the recorded states for the stateful RNN layer.
Can only be used when RNN layer is constructed with stateful = True. Args: states: Numpy arrays that contains the value for the initial state, which will be feed to cell at the first time step. When the value is None, zero filled numpy array will be created based on the cell state size.


Cell class for the LSTM layer.
Inherits From: LSTMCell, Layer, Module
See the Keras RNN API guide for details about the usage of RNN API.
This class processes one step within the whole time sequence input, whereas tf.keras.layer.LSTM processes the whole sequence.
View source
Get the dropout mask for RNN cell's input.
It will create mask based on context if there isn't any existing cached mask. If a new mask is generated, it will update the cache in the cell.
View source
View source
Get the recurrent dropout mask for RNN cell.
It will create mask based on context if there isn't any existing cached mask. If a new mask is generated, it will update the cache in the cell.
View source
Reset the cached dropout masks if any.
This is important for the RNN layer to invoke this in it call() method so that the cached mask is cleared before calling the cell.call(). The mask should be cached across the timestep within the same batch, but shouldn't be cached between batches. Otherwise it will introduce unreasonable bias against certain index of data within the batch.
View source
Reset the cached recurrent dropout masks if any.
This is important for the RNN layer to invoke this in it call() method so that the cached mask is cleared before calling the cell.call(). The mask should be cached across the timestep within the same batch, but shouldn't be cached between batches. Otherwise it will introduce unreasonable bias against certain index of data within the batch.


Masks a sequence by using a mask value to skip timesteps.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Masking
For each timestep in the input tensor (dimension #1 in the tensor), if all values in the input tensor at that timestep are equal to mask_value, then the timestep will be masked (skipped) in all downstream layers (as long as they support masking).
If any downstream layer does not support masking yet receives such an input mask, an exception will be raised.
Consider a Numpy data array x of shape (samples, timesteps, features), to be fed to an LSTM layer. You want to mask timestep #3 and #5 because you lack data for these timesteps. You can:
See the masking and padding guide for more details.


Layer that computes the maximum (element-wise) a list of inputs.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Maximum
It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape).


Max pooling operation for 1D temporal data.
Inherits From: Layer, Module
Main aliases
tf.keras.layers.MaxPooling1D
See Migration guide for more details.
tf.compat.v1.keras.layers.MaxPool1D, tf.compat.v1.keras.layers.MaxPooling1D
Downsamples the input representation by taking the maximum value over the window defined by pool_size. The window is shifted by strides. The resulting output when using "valid" padding option has a shape of: output_shape = (input_shape - pool_size + 1) / strides)
The resulting output shape when using the "same" padding option is: output_shape = input_shape / strides
For example, for strides=1 and padding="valid":
For example, for strides=2 and padding="valid":
For example, for strides=1 and padding="same":


Max pooling operation for 2D spatial data.
Inherits From: Layer, Module
Main aliases
tf.keras.layers.MaxPooling2D
See Migration guide for more details.
tf.compat.v1.keras.layers.MaxPool2D, tf.compat.v1.keras.layers.MaxPooling2D
Downsamples the input representation by taking the maximum value over the window defined by pool_size for each dimension along the features axis. The window is shifted by strides in each dimension. The resulting output when using "valid" padding option has a shape(number of rows or columns) of: output_shape = (input_shape - pool_size + 1) / strides)
The resulting output shape when using the "same" padding option is: output_shape = input_shape / strides
For example, for stride=(1,1) and padding="valid":
For example, for stride=(2,2) and padding="valid":
For example, for stride=(1,1) and padding="same":


Max pooling operation for 3D data (spatial or spatio-temporal).
Inherits From: Layer, Module
Main aliases
tf.keras.layers.MaxPooling3D
See Migration guide for more details.
tf.compat.v1.keras.layers.MaxPool3D, tf.compat.v1.keras.layers.MaxPooling3D


Layer that computes the minimum (element-wise) a list of inputs.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Minimum
It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape).


MultiHeadAttention layer.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.MultiHeadAttention
This is an implementation of multi-headed attention based on "Attention is all you Need". If query, key, value are the same, then this is self-attention. Each timestep in query attends to the corresponding sequence in key, and returns a fixed-width vector.
This layer first projects query, key and value. These are (effectively) a list of tensors of length num_attention_heads, where the corresponding shapes are [batch_size, , key_dim], [batch_size, , key_dim], [batch_size, , value_dim].
Then, the query and key tensors are dot-producted and scaled. These are softmaxed to obtain attention probabilities. The value tensors are then interpolated by these probabilities, then concatenated back to a single tensor.
Finally, the result tensor with the last dimension as value_dim can take an linear projection and return.
Performs 1D cross-attention over two sequence inputs with an attention mask. Returns the additional attention weights over heads.
Performs 2D self-attention over a 5D input tensor on axes 2 and 3.


Layer that multiplies (element-wise) a list of inputs.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Multiply
It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape).


Permutes the dimensions of the input according to a given pattern.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Permute
Useful e.g. connecting RNNs and convnets.
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.
Same as the input shape, but with the dimensions re-ordered according to the specified pattern.


Parametric Rectified Linear Unit.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.PReLU
where alpha is a learned array with the same shape as x.
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.
Same shape as the input.


Rectified Linear Unit activation function.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.ReLU
With default values, it returns element-wise max(x, 0).
Otherwise, it follows:
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the batch axis) when using this layer as the first layer in a model.
Same shape as the input.


Repeats the input n times.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.RepeatVector
2D tensor of shape (num_samples, features).
3D tensor of shape (num_samples, n, features).


Layer that reshapes inputs into the given shape.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Reshape
Arbitrary, although all dimensions in the input shape must be known/fixed. Use the keyword argument input_shape (tuple of integers, does not include the samples/batch size axis) when using this layer as the first layer in a model.
(batch_size,) + target_shape


Base class for recurrent layers.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.RNN
See the Keras RNN API guide for details about the usage of RNN API.
N-D tensor with shape [batch_size, timesteps, ...] or [timesteps, batch_size, ...] when time_major is True.
This layer supports masking for input data with a variable number of timesteps. To introduce masks to your data, use an [tf.keras.layers.Embedding] layer with the mask_zero parameter set to True.
Note on using statefulness in RNNs: You can set RNN layers to be 'stateful', which means that the states computed for the samples in one batch will be reused as initial states for the samples in the next batch. This assumes a one-to-one mapping between samples in different successive batches.
To enable statefulness:
To reset the states of your model, call .reset_states() on either a specific layer, or on your entire model.
Note on specifying the initial state of RNNs: You can specify the initial state of RNN layers symbolically by calling them with the keyword argument initial_state. The value of initial_state should be a tensor or list of tensors representing the initial state of the RNN layer.
You can specify the initial state of RNN layers numerically by calling reset_states with the keyword argument states. The value of states should be a numpy array or list of numpy arrays representing the initial state of the RNN layer.
Note on passing external constants to RNNs: You can pass "external" constants to the cell using the constants keyword argument of RNN.call (as well as RNN.call) method. This requires that the cell.call method accepts the same keyword argument constants. Such constants can be used to condition the cell transformation on additional static inputs (not changing over time), a.k.a. an attention mechanism.
View source
Reset the recorded states for the stateful RNN layer.
Can only be used when RNN layer is constructed with stateful = True. Args: states: Numpy arrays that contains the value for the initial state, which will be feed to cell at the first time step. When the value is None, zero filled numpy array will be created based on the cell state size.


Depthwise separable 1D convolution.
Inherits From: Layer, Module
Main aliases
tf.keras.layers.SeparableConvolution1D
See Migration guide for more details.
tf.compat.v1.keras.layers.SeparableConv1D, tf.compat.v1.keras.layers.SeparableConvolution1D
This layer performs a depthwise convolution that acts separately on channels, followed by a pointwise convolution that mixes channels. If use_bias is True and a bias initializer is provided, it adds a bias vector to the output. It then optionally applies an activation function to produce the final output.
3D tensor with shape: (batch_size, channels, steps) if data_format='channels_first' or 5D tensor with shape: (batch_size, steps, channels) if data_format='channels_last'.
3D tensor with shape: (batch_size, filters, new_steps) if data_format='channels_first' or 3D tensor with shape: (batch_size, new_steps, filters) if data_format='channels_last'. new_steps value might have changed due to padding or strides.


Depthwise separable 2D convolution.
Inherits From: Layer, Module
Main aliases
tf.keras.layers.SeparableConvolution2D
See Migration guide for more details.
tf.compat.v1.keras.layers.SeparableConv2D, tf.compat.v1.keras.layers.SeparableConvolution2D
Separable convolutions consist of first performing a depthwise spatial convolution (which acts on each input channel separately) followed by a pointwise convolution which mixes the resulting output channels. The depth_multiplier argument controls how many output channels are generated per input channel in the depthwise step.
Intuitively, separable convolutions can be understood as a way to factorize a convolution kernel into two smaller kernels, or as an extreme version of an Inception block.
4D tensor with shape: (batch_size, channels, rows, cols) if data_format='channels_first' or 4D tensor with shape: (batch_size, rows, cols, channels) if data_format='channels_last'.
4D tensor with shape: (batch_size, filters, new_rows, new_cols) if data_format='channels_first' or 4D tensor with shape: (batch_size, new_rows, new_cols, filters) if data_format='channels_last'. rows and cols values might have changed due to padding.


Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.serialize


Fully-connected RNN where the output is to be fed back to input.
Inherits From: RNN, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.SimpleRNN
See the Keras RNN API guide for details about the usage of RNN API.
View source
Reset the recorded states for the stateful RNN layer.
Can only be used when RNN layer is constructed with stateful = True. Args: states: Numpy arrays that contains the value for the initial state, which will be feed to cell at the first time step. When the value is None, zero filled numpy array will be created based on the cell state size.


Cell class for SimpleRNN.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.SimpleRNNCell
See the Keras RNN API guide for details about the usage of RNN API.
This class processes one step within the whole time sequence input, whereas tf.keras.layer.SimpleRNN processes the whole sequence.
View source
Get the dropout mask for RNN cell's input.
It will create mask based on context if there isn't any existing cached mask. If a new mask is generated, it will update the cache in the cell.
View source
View source
Get the recurrent dropout mask for RNN cell.
It will create mask based on context if there isn't any existing cached mask. If a new mask is generated, it will update the cache in the cell.
View source
Reset the cached dropout masks if any.
This is important for the RNN layer to invoke this in it call() method so that the cached mask is cleared before calling the cell.call(). The mask should be cached across the timestep within the same batch, but shouldn't be cached between batches. Otherwise it will introduce unreasonable bias against certain index of data within the batch.
View source
Reset the cached recurrent dropout masks if any.
This is important for the RNN layer to invoke this in it call() method so that the cached mask is cleared before calling the cell.call(). The mask should be cached across the timestep within the same batch, but shouldn't be cached between batches. Otherwise it will introduce unreasonable bias against certain index of data within the batch.


Softmax activation function.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Softmax
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.
Same shape as the input.


Spatial 1D version of Dropout.
Inherits From: Dropout, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.SpatialDropout1D
This version performs the same function as Dropout, however, it drops entire 1D feature maps instead of individual elements. If adjacent frames within feature maps are strongly correlated (as is normally the case in early convolution layers) then regular dropout will not regularize the activations and will otherwise just result in an effective learning rate decrease. In this case, SpatialDropout1D will help promote independence between feature maps and should be used instead.
3D tensor with shape: (samples, timesteps, channels)
Same as input.


Spatial 2D version of Dropout.
Inherits From: Dropout, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.SpatialDropout2D
This version performs the same function as Dropout, however, it drops entire 2D feature maps instead of individual elements. If adjacent pixels within feature maps are strongly correlated (as is normally the case in early convolution layers) then regular dropout will not regularize the activations and will otherwise just result in an effective learning rate decrease. In this case, SpatialDropout2D will help promote independence between feature maps and should be used instead.
4D tensor with shape: (samples, channels, rows, cols) if data_format='channels_first' or 4D tensor with shape: (samples, rows, cols, channels) if data_format='channels_last'.
Same as input.


Spatial 3D version of Dropout.
Inherits From: Dropout, Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.SpatialDropout3D
This version performs the same function as Dropout, however, it drops entire 3D feature maps instead of individual elements. If adjacent voxels within feature maps are strongly correlated (as is normally the case in early convolution layers) then regular dropout will not regularize the activations and will otherwise just result in an effective learning rate decrease. In this case, SpatialDropout3D will help promote independence between feature maps and should be used instead.
5D tensor with shape: (samples, channels, dim1, dim2, dim3) if data_format='channels_first' or 5D tensor with shape: (samples, dim1, dim2, dim3, channels) if data_format='channels_last'.
Same as input.


Wrapper allowing a stack of RNN cells to behave as a single cell.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.StackedRNNCells
Used to implement efficient stacked RNNs.
View source


Layer that subtracts two inputs.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.Subtract
It takes as input a list of tensors of size 2, both of the same shape, and returns a single tensor, (inputs[0] - inputs[1]), also of the same shape.


Thresholded Rectified Linear Unit.
Inherits From: Layer, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.layers.ThresholdedReLU
Arbitrary. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.
Same shape as the input.