Keras layers are the fundamental building blocks for constructing neural networks. They can be used to create both simple and complex models, and are designed to be highly modular and easy to use.
Overview
Keras provides a wide variety of layers that can be used for different purposes in your neural network. These layers include:
- Dense: Fully connected layer.
- Convolutional: Convolutional layer, used for image data.
- Recurrent: Recurrent layer, used for sequence data.
- Embedding: Embedding layer, used for converting categorical data into dense vectors.
- Activation: Activation layer, used to introduce non-linearities into the network.
For more information on each type of layer, please refer to the following sections.
Dense Layer
The Dense
layer is a fully connected neural network layer. It is typically used as the main layer in a neural network.
- Input shape:
(n_samples, n_features)
- Output shape:
(n_samples, n_units)
Example Usage
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(32,)))
model.add(Dense(10, activation='softmax'))
Convolutional Layer
The Conv2D
layer is a convolutional layer, used for processing image data. It applies various filters to the input image and extracts features.
- Input shape:
(n_samples, height, width, channels)
- Output shape:
(n_samples, height, width, filters)
Example Usage
from keras.models import Sequential
from keras.layers import Conv2D
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)))
Recurrent Layer
The LSTM
layer is a recurrent layer, used for processing sequence data. It is designed to remember information over time.
- Input shape:
(n_samples, timesteps, n_features)
- Output shape:
(n_samples, timesteps, n_units)
Example Usage
from keras.models import Sequential
from keras.layers import LSTM
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(100, 1)))
Embedding Layer
The Embedding
layer is used to convert categorical data into dense vectors. It is commonly used in natural language processing tasks.
- Input shape:
(n_samples, n_features)
- Output shape:
(n_samples, n_units)
Example Usage
from keras.models import Sequential
from keras.layers import Embedding
model = Sequential()
model.add(Embedding(input_dim=10000, output_dim=32, input_length=10))
For more information on Keras layers, please visit the Keras Layers API Documentation page.