python-kerasHow do I use a Python Keras LSTM for a specific example?
The following example code shows how to use a Python Keras LSTM for a specific example. This example uses a simple dataset of 1000 samples consisting of two features and a label. The label is binary, and the features are randomly generated.
# import libraries
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM
# generate dataset
data_dim = 2
timesteps = 8
num_classes = 2
# expected input data shape: (batch_size, timesteps, data_dim)
x_train = np.random.random((1000, timesteps, data_dim))
y_train = np.random.randint(2, size=(1000, 1))
# build model
model = Sequential()
model.add(LSTM(32, return_sequences=True,
input_shape=(timesteps, data_dim)))
model.add(LSTM(32, return_sequences=True))
model.add(LSTM(32))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
# train model
model.fit(x_train, y_train,
batch_size=64, epochs=5)
The code above builds a LSTM model with three LSTM layers and a Dense layer. The input shape is set to (batch_size, timesteps, data_dim). The model is then compiled with the categorical_crossentropy
loss function and the rmsprop
optimizer. Finally, the model is trained on the generated dataset.
Code explanation
import numpy as np
: imports the NumPy library asnp
from keras.models import Sequential
: imports theSequential
model from Kerasfrom keras.layers import Dense, LSTM
: imports theDense
andLSTM
layers from Kerasdata_dim = 2
: sets the dimension of the data to 2timesteps = 8
: sets the number of timesteps to 8num_classes = 2
: sets the number of classes to 2x_train = np.random.random((1000, timesteps, data_dim))
: generates a dataset of 1000 samples with two features and a labely_train = np.random.randint(2, size=(1000, 1))
: generates a binary label for the datasetmodel = Sequential()
: creates a sequential modelmodel.add(LSTM(32, return_sequences=True, input_shape=(timesteps, data_dim)))
: adds a LSTM layer to the model with 32 units,return_sequences=True
to return the output of each timestep, andinput_shape
set to the data shapemodel.add(LSTM(32, return_sequences=True))
: adds another LSTM layer to the model with 32 units andreturn_sequences=True
model.add(LSTM(32))
: adds another LSTM layer to the model with 32 unitsmodel.add(Dense(2, activation='softmax'))
: adds a Dense layer with 2 units and thesoftmax
activation functionmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
: compiles the model with thecategorical_crossentropy
loss function, thermsprop
optimizer, and accuracy as the metricmodel.fit(x_train, y_train, batch_size=64, epochs=5)
: trains the model on the dataset with a batch size of 64 and 5 epochs
Helpful links
More of Python Keras
- How can I use Python and Keras together?
- How do I use the to_categorical function in Python Keras?
- How can I use the Adam optimizer in TensorFlow?
- What is Python Keras and how is it used?
- How can I use Python and Keras to create an Optical Character Recognition (OCR) example?
- How do I create a neural network using Python and Keras?
- How do I use the Keras layers.dense function in Python?
- How to load a model in Python Keras?
- How do I use validation_data when creating a Keras model in Python?
See more codes...