9951 explained code solutions for 126 technologies


python-tensorflowHow do I use a Long Short-Term Memory (LSTM) network with Python and TensorFlow?


A Long Short-Term Memory (LSTM) network is a type of recurrent neural network (RNN) that is capable of learning long-term dependencies. It can be used with Python and TensorFlow to create deep learning models for a variety of tasks, such as natural language processing (NLP) and time series forecasting.

The following example code block uses an LSTM network with Python and TensorFlow to create a model that predicts the next word in a sentence given the previous words:

import tensorflow as tf
from tensorflow.keras.layers import LSTM, Embedding

# Define the model
model = tf.keras.Sequential()
model.add(Embedding(vocab_size, 64, input_length=max_length))
model.add(LSTM(128))
model.add(tf.keras.layers.Dense(vocab_size, activation='softmax'))

# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Train the model
model.fit(X, y, epochs=10)

The code block does the following:

  1. Imports the necessary libraries.
  2. Defines the model using an Embedding layer, an LSTM layer, and a Dense layer.
  3. Compiles the model using the Adam optimizer and categorical cross-entropy loss.
  4. Trains the model using the training data.

Helpful links

Edit this code on GitHub