#### 2.1 序列问题

2.1.2 为什幺需要循环神经网络——序列式问题

1对多：图片生成描述

#### 2.2 循环神经网络文本分类

2.2.1 定义一个1层单层单向的rnn，2层全连接层的模型

```embedding_dim = 16
batch_size = 512
single_rnn_model = keras.models.Sequential([
# 1. define matrix: [vocab_size, embedding_dim]
# 2. [1,2,3,4..], max_length * embedding_dim
# 3. batch_size * max_length * embedding_dim
keras.layers.Embedding(vocab_size, embedding_dim,
input_length = max_length),
# return_sequences: 决定你返回的输出（False）是最后一步的输出，还是所有的输出
keras.layers.SimpleRNN(units = 64, return_sequences = False),
keras.layers.Dense(64, activation = 'relu'),
keras.layers.Dense(1, activation='sigmoid'),
])
single_rnn_model.summary()
loss = 'binary_crossentropy',
metrics = ['accuracy'])```

```Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
embedding (Embedding)        (None, 500, 16)           160000
_________________________________________________________________
simple_rnn (SimpleRNN)       (None, 64)                5184
_________________________________________________________________
dense (Dense)                (None, 64)                4160
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 65
=================================================================
Total params: 169,409
Trainable params: 169,409
Non-trainable params: 0```

```history_single_rnn = single_rnn_model.fit(
train_data, train_labels,
epochs = 30,
batch_size = batch_size,
validation_split = 0.2)```

```def plot_learning_curves(history, label, epochs, min_value, max_value):
data = {}
data[label] = history.history[label]
data['val_'+label] = history.history['val_'+label]
pd.DataFrame(data).plot(figsize=(8, 5))
plt.grid(True)
plt.axis([0, epochs, min_value, max_value])
plt.show()

plot_learning_curves(history_single_rnn, 'accuracy', 30, 0, 1)
plot_learning_curves(history_single_rnn, 'loss', 30, 0, 1)```

```single_rnn_model.evaluate(
test_data, test_labels,
batch_size = batch_size,
verbose = 0)```

`[0.7533659934997559, 0.5001599788665771]`

2.2.2 实现一个双层双向的rnn，2层全连接层的模型

```embedding_dim = 16
batch_size = 512
model = keras.models.Sequential([
# 1. define matrix: [vocab_size, embedding_dim]
# 2. [1,2,3,4..], max_length * embedding_dim
# 3. batch_size * max_length * embedding_dim
keras.layers.Embedding(vocab_size, embedding_dim,
input_length = max_length),
keras.layers.Bidirectional(
keras.layers.SimpleRNN(
units = 64, return_sequences = True)),
# return_sequences改为True的原因：下一个RNN的输入是序列，而不是单个的输入
keras.layers.Bidirectional(
keras.layers.SimpleRNN(
units = 64, return_sequences = False)),
keras.layers.Dense(64, activation = 'relu'),
keras.layers.Dense(1, activation='sigmoid'),
])
model.summary()
loss = 'binary_crossentropy',
metrics = ['accuracy'])```

```Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
embedding (Embedding)        (None, 500, 16)           160000
_________________________________________________________________
bidirectional (Bidirectional (None, 500, 128)          10368
_________________________________________________________________
bidirectional_1 (Bidirection (None, 128)               24704
_________________________________________________________________
dense (Dense)                (None, 64)                8256
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 65
=================================================================
Total params: 203,393
Trainable params: 203,393
Non-trainable params: 0
_________________________________________________________________```

```history = model.fit(
train_data, train_labels,
epochs = 30,
batch_size = batch_size,
validation_split = 0.2)```

```plot_learning_curves(history, 'accuracy', 30, 0, 1)
plot_learning_curves(history, 'loss', 30, 0, 3.8)```

2.2.3 实现一个单层双向的rnn，2层全连接层的模型

```embedding_dim = 16
batch_size = 512
bi_rnn_model = keras.models.Sequential([
# 1. define matrix: [vocab_size, embedding_dim]
# 2. [1,2,3,4..], max_length * embedding_dim
# 3. batch_size * max_length * embedding_dim
keras.layers.Embedding(vocab_size, embedding_dim,
input_length = max_length),
keras.layers.Bidirectional(
keras.layers.SimpleRNN(
units = 32, return_sequences = False)),
keras.layers.Dense(32, activation = 'relu'),
keras.layers.Dense(1, activation='sigmoid'),
])
bi_rnn_model.summary()
loss = 'binary_crossentropy',
metrics = ['accuracy'])```

```Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
embedding (Embedding)        (None, 500, 16)           160000
_________________________________________________________________
bidirectional (Bidirectional (None, 64)                3136
_________________________________________________________________
dense (Dense)                (None, 32)                2080
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 33
=================================================================
Total params: 165,249
Trainable params: 165,249
Non-trainable params: 0
_________________________________________________________________```

```history = bi_rnn_model.fit(
train_data, train_labels,
epochs = 30,
batch_size = batch_size,
validation_split = 0.2)```

```plot_learning_curves(history, 'accuracy', 30, 0, 1.1)
plot_learning_curves(history, 'loss', 30, 0, 2.5)```

`bi_rnn_model.evaluate(test_data, test_labels, batch_size = batch_size, verbose = 0)`

`[1.404372215270996, 0.7716000080108643]`