Keras 简明教程

Keras - Time Series Prediction using LSTM RNN

在本章中,我们将编写一个基于简单长短期记忆 (LSTM) 的 RNN 来执行序列分析。序列是一组值,其中每个值对应于时间的一个特定实例。我们考虑一个简单的阅读句子示例。阅读理解一个句子包括按给定顺序阅读单词并尝试理解给定语境中的每个单词及其含义,最后以积极或消极的情感理解句子。

In this chapter, let us write a simple Long Short Term Memory (LSTM) based RNN to do sequence analysis. A sequence is a set of values where each value corresponds to a particular instance of time. Let us consider a simple example of reading a sentence. Reading and understanding a sentence involves reading the word in the given order and trying to understand each word and its meaning in the given context and finally understanding the sentence in a positive or negative sentiment.

在此处,单词被视为值,并且第一个值对应于第一个单词,第二个值对应于第二个单词,等等,并严格保持顺序。 Sequence Analysis 在自然语言处理中经常用于寻找给定文本的情感分析。

Here, the words are considered as values, and first value corresponds to first word, second value corresponds to second word, etc., and the order will be strictly maintained. Sequence Analysis is used frequently in natural language processing to find the sentiment analysis of the given text.

让我们创建一个 LSTM 模型来分析 IMDB 电影评论并找出其正面/负面情绪。

Let us create a LSTM model to analyze the IMDB movie reviews and find its positive/negative sentiment.

用于序列分析的模型可以表示为以下内容 −

The model for the sequence analysis can be represented as below −

sequence analysis

该模型的核心功能如下:

The core features of the model are as follows −

  1. Input layer using Embedding layer with 128 features.

  2. First layer, Dense consists of 128 units with normal dropout and recurrent dropout set to 0.2.

  3. Output layer, Dense consists of 1 unit and ‘sigmoid’ activation function.

  4. Use binary_crossentropy as loss function.

  5. Use adam as Optimizer.

  6. Use accuracy as metrics.

  7. Use 32 as batch size.

  8. Use 15 as epochs.

  9. Use 80 as the maximum length of the word.

  10. Use 2000 as the maximum number of word in a given sentence.

Step 1: Import the modules

让我们导入必需的模块。

Let us import the necessary modules.

from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Embedding
from keras.layers import LSTM
from keras.datasets import imdb

Step 2: Load data

我们导入 imdb 数据集。

Let us import the imdb dataset.

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words = 2000)

在此,

Here,

  1. imdb is a dataset provided by Keras. It represents a collection of movies and its reviews.

  2. num_words represent the maximum number of words in the review.

Step 3: Process the data

让我们根据模型更改数据集,以便可以将其输入到模型中。可以使用以下代码更改数据 −

Let us change the dataset according to our model, so that it can be fed into our model. The data can be changed using the below code −

x_train = sequence.pad_sequences(x_train, maxlen=80)
x_test = sequence.pad_sequences(x_test, maxlen=80)

在此,

Here,

sequence.pad_sequences 将形状为 (data) 的输入数据列表转换为形状为 (data, timesteps) 的 2D NumPy 数组。它基本上将时间步概念添加到给定数据中。它生成长度为 maxlen 的时间步。

sequence.pad_sequences convert the list of input data with shape, (data) into 2D NumPy array of shape (data, timesteps). Basically, it adds timesteps concept into the given data. It generates the timesteps of length, maxlen.

Step 4: Create the model

让我们创建实际模型。

Let us create the actual model.

model = Sequential()
model.add(Embedding(2000, 128))
model.add(LSTM(128, dropout = 0.2, recurrent_dropout = 0.2))
model.add(Dense(1, activation = 'sigmoid'))

在此,

Here,

我们使用 Embedding layer 作为输入层,然后添加 LSTM 层。最后,使用 Dense layer 作为输出层。

We have used Embedding layer as input layer and then added the LSTM layer. Finally, a Dense layer is used as output layer.

Step 5: Compile the model

让我们使用选定的损失函数、优化器和指标来编译模型。

Let us compile the model using selected loss function, optimizer and metrics.

model.compile(loss = 'binary_crossentropy',
   optimizer = 'adam', metrics = ['accuracy'])

Step 6: Train the model

让我们使用 fit() 方法训练模型。

LLet us train the model using fit() method.

model.fit(
   x_train, y_train,
   batch_size = 32,
   epochs = 15,
   validation_data = (x_test, y_test)
)

执行该应用程序将输出以下信息 −

Executing the application will output the below information −

Epoch 1/15 2019-09-24 01:19:01.151247: I
tensorflow/core/platform/cpu_feature_guard.cc:142]
Your CPU supports instructions that this
TensorFlow binary was not co mpiled to use: AVX2
25000/25000 [==============================] - 101s 4ms/step - loss: 0.4707
- acc: 0.7716 - val_loss: 0.3769 - val_acc: 0.8349 Epoch 2/15
25000/25000 [==============================] - 95s 4ms/step - loss: 0.3058
- acc: 0.8756 - val_loss: 0.3763 - val_acc: 0.8350 Epoch 3/15
25000/25000 [==============================] - 91s 4ms/step - loss: 0.2100
- acc: 0.9178 - val_loss: 0.5065 - val_acc: 0.8110 Epoch 4/15
25000/25000 [==============================] - 90s 4ms/step - loss: 0.1394
- acc: 0.9495 - val_loss: 0.6046 - val_acc: 0.8146 Epoch 5/15
25000/25000 [==============================] - 90s 4ms/step - loss: 0.0973
- acc: 0.9652 - val_loss: 0.5969 - val_acc: 0.8147 Epoch 6/15
25000/25000 [==============================] - 98s 4ms/step - loss: 0.0759
- acc: 0.9730 - val_loss: 0.6368 - val_acc: 0.8208 Epoch 7/15
25000/25000 [==============================] - 95s 4ms/step - loss: 0.0578
- acc: 0.9811 - val_loss: 0.6657 - val_acc: 0.8184 Epoch 8/15
25000/25000 [==============================] - 97s 4ms/step - loss: 0.0448
- acc: 0.9850 - val_loss: 0.7452 - val_acc: 0.8136 Epoch 9/15
25000/25000 [==============================] - 95s 4ms/step - loss: 0.0324
- acc: 0.9894 - val_loss: 0.7616 - val_acc: 0.8162Epoch 10/15
25000/25000 [==============================] - 100s 4ms/step - loss: 0.0247
- acc: 0.9922 - val_loss: 0.9654 - val_acc: 0.8148 Epoch 11/15
25000/25000 [==============================] - 99s 4ms/step - loss: 0.0169
- acc: 0.9946 - val_loss: 1.0013 - val_acc: 0.8104 Epoch 12/15
25000/25000 [==============================] - 90s 4ms/step - loss: 0.0154
- acc: 0.9948 - val_loss: 1.0316 - val_acc: 0.8100 Epoch 13/15
25000/25000 [==============================] - 89s 4ms/step - loss: 0.0113
- acc: 0.9963 - val_loss: 1.1138 - val_acc: 0.8108 Epoch 14/15
25000/25000 [==============================] - 89s 4ms/step - loss: 0.0106
- acc: 0.9971 - val_loss: 1.0538 - val_acc: 0.8102 Epoch 15/15
25000/25000 [==============================] - 89s 4ms/step - loss: 0.0090
- acc: 0.9972 - val_loss: 1.1453 - val_acc: 0.8129
25000/25000 [==============================] - 10s 390us/step

Step 7 − Evaluate the model

让我们使用测试数据评估模型。

Let us evaluate the model using test data.

score, acc = model.evaluate(x_test, y_test, batch_size = 32)

print('Test score:', score)
print('Test accuracy:', acc)

执行以上代码将会输出以下信息−

Executing the above code will output the below information −

Test score: 1.145306069601178
Test accuracy: 0.81292