-
Notifications
You must be signed in to change notification settings - Fork 82
/
01-intro-to-lstms.Rmd
302 lines (240 loc) · 8.83 KB
/
01-intro-to-lstms.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
---
title: "Introduction to RNNs & LSTMs"
output:
html_notebook:
toc: yes
toc_float: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
ggplot2::theme_set(ggplot2::theme_bw())
```
In this example, we are going to learn about recurrent neural networks (RNNs)
and long short term memory (LSTM) neural networks. These architectures are
designed for sequence data, which can include text, videos, time series, and
more. However, for this workshop we are going to focus on text data as this is
the domain seeing the largest impact from LSTMs.
Learning objectives:
- Understand how an RNN works, how to implement it, and its primary weakness
- Understand how an LSTM works and how to implement it
# Requirements
```{r}
library(keras)
library(tidyverse)
library(glue)
```
# Prepare our data
For our introduction we're going to start with the built-in IMDB data set. Let's
use the 10,000 most frequent words and we will set the max length of each
review to be 500, which will capture the majority of most reviews.
```{r}
# establish our data characteristics
n_features <- 10000
max_len <- 500
# import and prep our data
imdb <- dataset_imdb(num_words = n_features)
c(c(x_train, y_train), c(x_test, y_test)) %<-% imdb
x_train <- pad_sequences(x_train, maxlen = max_len)
x_test <- pad_sequences(x_test, maxlen = max_len)
```
# RNNs
To model our IMDB data, we could use:
1. __one-hot encoding__, which has a problem with only allowing a single
representation of a word. "Seattle" is simply a word but has no relationship
to "Seahawks", "Wilson", "weather", or "Starbucks".
2. __embeddings__, which allow our inputs (i.e. words) to have relationships
with other inputs but does not control for the sequential nature of the
inputs.
Recurrent neural networks were specifically designed to model sequential data.
[ℹ️](http://bit.ly/dl-08)
## Train an RNN
Let's build a model with an RNN layer. First, we still use an embedding layer to
allow for more complex representation of our words but we follow that with
`layer_simple_rnn()`. Everything else remains the same as before.
```{r}
model <- keras_model_sequential() %>%
layer_embedding(
input_dim = n_features,
input_length = max_len,
output_dim = 32,
name = "Embeddings") %>%
layer_simple_rnn(units = 32, name = "RNN") %>%
layer_dense(units = 1, activation = "sigmoid", name = "Prediction")
model %>% compile(
optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = "accuracy"
)
summary(model)
```
```{r}
history <- model %>% fit(
x_train, y_train,
epochs = 10,
batch_size = 128,
validation_split = 0.2,
callbacks = list(callback_early_stopping(patience = 3))
)
```
```{r}
best_epoch <- which(history$metrics$val_loss == min(history$metrics$val_loss))
loss <- history$metrics$val_loss[best_epoch] %>% round(3)
acc <- history$metrics$val_acc[best_epoch] %>% round(3)
glue("The best epoch had a loss of {loss} and an accuracy of {acc}")
```
## Your Turn! (5min)
Spend a few minutes adjusting this model and see how it impacts performance. You
may want to test:
- Does increasing and decreasing the word embedding dimension and/or the number
of RNN layer units impact performance?
- It's sometimes userful to stack several recurrent layers one after the other
in order to increase the representational power of a network. What happens
when you try this?
__Tip__: An RNN will take inputs from different parts of a sequence and produce
a single output. When stacking multiple recurrent layers, you need to set
`return_sequences = TRUE` so that you get the output for each part of the
sequence so that you can supply a sequence into the next RNN layer (see
`?layer_simple_rnn()`).
```{r}
model <- keras_model_sequential() %>%
layer_embedding(
input_dim = n_features,
input_length = max_len,
output_dim = _____,
name = "Embeddings") %>%
layer_simple______(_____) %>%
layer_dense(units = 1, activation = _____, name = "Prediction")
model %>% compile(
optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = "accuracy"
)
summary(model)
```
```{r}
history <- model %>% fit(
x_train, y_train,
epochs = 10,
batch_size = 128,
validation_split = 0.2,
callbacks = list(callback_early_stopping(patience = _____))
)
```
```{r}
best_epoch <- which(history$metrics$val_loss == min(history$metrics$val_loss))
loss <- history$metrics$val_loss[best_epoch] %>% round(3)
acc <- history$metrics$val_acc[best_epoch] %>% round(3)
glue("The best epoch had a loss of {loss} and an accuracy of {acc}")
```
# LSTMs
Unfortunately, a problem with RNNs (and any very deep neural network) is as our
network gets deep, it loses the signal due to the vanishing gradient descent
problem. LSTMs were developed to address this problem. [ℹ️](http://bit.ly/dl-08#12)
## Train an LSTM
To train an LSTM, we simply replace `layer_simple_rnn()` with `layer_lstm()`.
```{r}
model <- keras_model_sequential() %>%
layer_embedding(
input_dim = n_features,
input_length = max_len,
output_dim = 32,
name = "Embeddings") %>%
layer_lstm(units = 32, name = "LSTM") %>%
layer_dense(units = 1, activation = "sigmoid", name = "Prediction")
model %>% compile(
optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = "accuracy"
)
summary(model)
```
```{r}
history <- model %>% fit(
x_train, y_train,
epochs = 10,
batch_size = 128,
validation_split = 0.2,
callbacks = list(callback_early_stopping(patience = 3))
)
```
```{r}
best_epoch <- which(history$metrics$val_loss == min(history$metrics$val_loss))
loss <- history$metrics$val_loss[best_epoch] %>% round(3)
acc <- history$metrics$val_acc[best_epoch] %>% round(3)
glue("The best epoch had a loss of {loss} and an accuracy of {acc}")
```
## Your Turn! (5min)
Spend a few minutes adjusting this model and see how it impacts performance. You
may want to test:
- Does increasing and decreasing the word embedding dimension and/or the number
of LSTM layer units impact performance?
- As we learned in an earlier module, dropout can help reduce overfitting and
improve model performance. See if you can add dropout to your model. Hint,
checkout the docs (`?layer_lstm()`) because adding dropout to LSTMs are a
little unique.
__Tip__: When applying dropout to a recurrent network, it’s important to apply
the same dropout pattern to each timestep within the recurrent layer, rather
than randomly. Consequently, recurrent layers have two dropout parameters:
- `dropout`: the dropout rate for inputs going into the recurrent layer
- `recurrent_dropout`: the dropout rate for the recurrent units within the
recurrent layer
See `?layer_lstm()` for details. Note, you can still apply dropout between the
embedding layer and the dense layer after the LSTM layer by using the regular
`layer_dropout()` layer.
```{r}
model <- keras_model_sequential() %>%
layer_embedding(
input_dim = n_features,
input_length = max_len,
output_dim = _____,
name = "Embeddings") %>%
layer______(_____) %>%
layer_dense(units = _____, activation = _____, name = "Prediction")
model %>% compile(
optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = "accuracy"
)
summary(model)
```
```{r}
history <- model %>% fit(
x_train, y_train,
epochs = 10,
batch_size = 128,
validation_split = 0.2,
callbacks = _____
)
```
```{r}
best_epoch <- which(history$metrics$val_loss == min(history$metrics$val_loss))
loss <- history$metrics$val_loss[best_epoch] %>% round(3)
acc <- history$metrics$val_acc[best_epoch] %>% round(3)
glue("The best epoch had a loss of {loss} and an accuracy of {acc}")
```
# Why the lack of accuracy
So why are our recurrent models not doing much better than our one-hot encoding
and simple word embedding models? For most sentiment predictions we can capture
the essence of the tone (i.e. positive vs. negative) by focusing on a few key
words, which our simpler encoding models can do quite well.
![](images/customer_review.gif)
As your prediction task requires more spatial context, then recurrent networks
should begin to out perform the more simpler encoding models.
# Takeaways
* Recurrent networks help to capture spatial context within text
* RNNs
- are just multiple perceptrons sequentially linked
- pass information through hidden states
- use the Tanh activation to control value magnitudes
- suffer from the vanishing gradient
- implemented with `layer_simple_rnn()`
* LSTMs
- use multiple gates that help to "remember" or "forget" information
- computationally expensive
- implemented with `layer_lstm()`
* Unique characteristics
- must use `return_sequences = TRUE` to stack multiple recurrent layers
- recurrent layers have their own special dropout procedures --> don't use
`layer_dropout()`; rather, use the dropout parameters within the recurrent
layer.
[🏠](https://github.com/rstudio-conf-2020/dl-keras-tf)