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 asnpfrom keras.models import Sequential: imports theSequentialmodel from Kerasfrom keras.layers import Dense, LSTM: imports theDenseandLSTMlayers 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=Trueto return the output of each timestep, andinput_shapeset to the data shapemodel.add(LSTM(32, return_sequences=True)): adds another LSTM layer to the model with 32 units andreturn_sequences=Truemodel.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 thesoftmaxactivation functionmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']): compiles the model with thecategorical_crossentropyloss function, thermspropoptimizer, 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 improve the validation accuracy of my Keras model using Python?
- How do I use Python Keras to zip a file?
- How can I use word2vec and Keras to develop a machine learning model in Python?
- How do I install the Python Keras .whl file?
- How do I use validation_data when creating a Keras model in Python?
- How can I enable verbose mode when using Python Keras?
- How do I check which version of Keras I am using in Python?
- How do I use Python's tf.keras.utils.get_file to retrieve a file?
- How do I use Python Keras to perform Optical Character Recognition (OCR)?
- What is Python Keras and how is it used?
See more codes...