Keras Common Layer Types
Keras is a high-level neural network API that provides a rich set of layer types for building deep learning models.
A layer (Layer) is the basic building block of Keras; each layer receives input data, applies a specific transformation, and outputs the result.
This article introduces in detail the most commonly used layer types in Keras and how to use them.
Core Layer Types
Dense Fully Connected Layer
The fully connected layer is the most basic neural network layer, where every input node is connected to every output node.
Example
from
keras.
layers
import
Dense
Create a fully connected layer with 64 neurons using the ReLU activation function
dense_layer
=
Dense
(
units
=
64
,
activation
=
'relu'
)
Parameter Description:
units: positive integer, the dimension of the output spaceactivation: activation function, such as 'relu', 'sigmoid', 'tanh', 'softmax', etc.use_bias: boolean, whether to use a bias vector (default True)kernel_initializer: initialization method for the weight matrix
Application scenarios:
- Used for multilayer perceptrons (MLP)
- As the final layer of a classifier
- Feature transformation and nonlinear mapping
Conv2D Two-Dimensional Convolution Layer
A convolution operation mainly used for image processing, able to extract local features.
Example
from
keras.
layers
import
Conv2D
Create a convolution layer with 32 3x3 kernels
conv_layer
=
Conv2D
(
filters
=
32
,
kernel_size
=
(
3
,
3
)
,
activation
=
'relu'
)
Parameter Description:
filters: integer, the dimension of the output space (the number of convolution kernels)kernel_size: integer or tuple, the width and height of the convolution windowstrides: convolution stride, default (1, 1)padding: 'valid' (no padding) or 'same' (padding so that the output has the same size as the input)
Application scenarios:
- Image classification
- Object detection
- Image segmentation
LSTM Long Short-Term Memory Network Layer
A recurrent neural network layer for processing sequential data, able to learn long-term dependencies.
Example
from
keras.
layers
import
LSTM
Create an LSTM layer with 128 units
lstm_layer
=
LSTM
(
units
=
128
,
return_sequences
=
True
)
Parameter Description:
units: positive integer, the dimension of the output spacereturn_sequences: boolean, whether to return the full sequence (default False)dropout: a float between 0 and 1, the dropout rate of the input linear transformationrecurrent_dropout: a float between 0 and 1, the dropout rate of the recurrent state
Application scenarios:
- Natural Language Processing
- Time Series Forecasting
- Speech recognition
Dropout Random Deactivation Layer
Randomly set some neuron outputs to 0 during training to prevent overfitting.
Example
from
keras.
layers
import
Dropout
Create a Dropout layer with a dropout rate of 0.5
dropout_layer
=
Dropout
(
rate
=
0.5
)
Parameter Description:
rate: a float between 0 and 1, the dropout rationoise_shape: an integer tensor representing the shape of the binary dropout mask to be multiplied with the inputseed: random seed
Application scenarios:
- Prevent neural networks from overfitting
- Improve model generalization
- Usually used after fully connected layers
Other Important Layer Types
BatchNormalization Batch Normalization Layer
Apply batch normalization to the output of the previous layer, speeding up training and improving model stability.
Example
from
keras.
layers
import
BatchNormalization
Create a batch normalization layer
bn_layer
=
BatchNormalization
(
)
MaxPooling2D Two-Dimensional Max Pooling Layer
Downsample feature maps by taking the maximum value within a window.
Example
from
keras.
layers
import
MaxPooling2D
Create a 2x2 max pooling layer
pool_layer
=
MaxPooling2D
(
pool_size
=
(
2
,
2
)
)
Flatten Flattening Layer
Flatten multi-dimensional input into one dimension, commonly used when transitioning from convolution layers to fully connected layers.
Example
from
keras.
layers
import
Flatten
Create a flattening layer
flatten_layer
=
Flatten
(
)
Embedding Embedding Layer
Convert positive integers (indices) into fixed-size dense vectors.
Example
from
keras.
layers
import
Embedding
Create an embedding layer with a vocabulary size of 1000 and an output dimension of 64
embedding_layer
=
Embedding
(
input_dim
=
1000
,
output_dim
=
64
)
Layer Combination Example
Example
from
keras.
models
import
Sequential
from
keras.
layers
import
Dense
,
Dropout
,
Conv2D
,
MaxPooling2D
,
Flatten
Create a simple CNN model
model
=
Sequential
(
[
Conv2D
(
32
,
(
3
,
3
)
,
activation
=
'relu'
,
input_shape
=
(
28
,
28
,
1
)
)
,
MaxPooling2D
(
(
2
,
2
)
)
,
Conv2D
(
64
,
(
3
,
3
)
,
activation
=
'relu'
)
,
MaxPooling2D
(
(
2
,
2
)
)
,
Flatten
(
)
,
Dense
(
64
,
activation
=
'relu'
)
,
Dropout
(
0.5
)
,
Dense
(
10
,
activation
=
'softmax'
)
]
)
Guidelines for Choosing Layers
-
Input Data Types:
-
Model Depth:
-
Task Type:
-
Computational Resources: