-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter 6.1.Rmd
445 lines (346 loc) · 11 KB
/
Chapter 6.1.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
---
title: "Chapter 6.1"
output: html_notebook
---
## one hot encoding
### word level
### listing 6.1 from book.
create the token index
```{r}
# This is our initial data; one entry per "sample"
# (in this toy example, a "sample" is just a sentence, but
# it could be an entire document).
samples <- c("The cat sat on the mat.", "The dog ate my homework.")
# First, build an index of all tokens in the data.
token_index <- list()
for (sample in samples) {
# Tokenizes the samples via the strsplit function. In real life, you'd also
# strip punctuation and special characters from the samples.
for (word in strsplit(sample, " ")[[1]]) {
#cat("current word is: ", word, "\n")
if (!word %in% names(token_index)) {
# cat("word not in token index\n")
# Assigns a unique index to each unique word. Note that you don't
# attribute index 1 to anything.
token_index[[word]] <- length(token_index) + 2
#cat("updated token index:\n")
#print(token_index)
} #if
} # for word
} # for sample
token_index
```
create the one hot encoding
```{r}
# Vectorizes the samples. You'll only consider the first max_length
# words in each sample.
max_length <- 10
# This is where you store the results.
results <- array(0, dim = c(length(samples),
max_length,
max(as.integer(token_index)))) #3D array
for (i in 1:length(samples)) {
sample <- samples[[i]]
words <- head(strsplit(sample, " ")[[1]], n = max_length)
for (j in 1:length(words)) {
index <- token_index[[words[[j]]]]
results[[i, j, index]] <- 1
}
}
results
```
## Character level one-hot
## listing 6.2
```{r}
samples <- c("The cat sat on the mat.", "The dog ate my homework.")
ascii_tokens <- c("", sapply(as.raw(c(32:126)), rawToChar))
token_index <- c(1:(length(ascii_tokens)))
names(token_index) <- ascii_tokens
max_length <- 50
results <- array(0, dim = c(length(samples), max_length, length(token_index)))
for (i in 1:length(samples)) {
sample <- samples[[i]]
characters <- strsplit(sample, "")[[1]]
for (j in 1:length(characters)) {
character <- characters[[j]]
results[i, j, token_index[[character]]] <- 1
}
}
```
## keras fundctions for one hot encoding
## listing 6.3
```{r}
library(keras)
use_condaenv("r-reticulate")
samples <- c("The cat sat on the mat.", "The dog ate my homework.")
# Creates a tokenizer, configured to only take into account the 1,000
# most common words, then builds the word index.
tokenizer <- text_tokenizer(num_words = 1000) %>%
fit_text_tokenizer(samples)
# Turns strings into lists of integer indices
sequences <- texts_to_sequences(tokenizer, samples)
# You could also directly get the one-hot binary representations. Vectorization
# modes other than one-hot encoding are supported by this tokenizer.
one_hot_results <- texts_to_matrix(tokenizer, samples, mode = "binary")
# How you can recover the word index that was computed
word_index <- tokenizer$word_index
cat("Found", length(word_index), "unique tokens.\n")
```
```{r}
library(hashFunction)
samples <- c("The cat sat on the mat.", "The dog ate my homework.")
# We will store our words as vectors of size 1000.
# Note that if you have close to 1000 words (or more)
# you will start seeing many hash collisions, which
# will decrease the accuracy of this encoding method.
dimensionality <- 1000
max_length <- 10
results <- array(0, dim = c(length(samples), max_length, dimensionality))
for (i in 1:length(samples)) {
sample <- samples[[i]]
words <- head(strsplit(sample, " ")[[1]], n = max_length)
for (j in 1:length(words)) {
# Hash the word into a "random" integer index
# that is between 0 and 1,000
index <- abs(spooky.32(words[[j]])) %% dimensionality
results[[i, j, index]] <- 1
}
}
results[,,1:10]
```
## Layer embedding
Here the model learns how to represent the words in a denser array
### listing 6.6
working with IMDB
```{r}
# Number of words to consider as features
max_features <- 10000
# Cut texts after this number of words
# (among top max_features most common words)
maxlen <- 20
# Load the data as lists of integers.
imdb <- dataset_imdb(num_words = max_features)
c(c(x_train, y_train), c(x_test, y_test)) %<-% imdb
# This turns our lists of integers
# into a 2D integer tensor of shape `(samples, maxlen)`
x_train <- pad_sequences(x_train, maxlen = maxlen)
x_test <- pad_sequences(x_test, maxlen = maxlen)
dim(x_test)
head(x_test) #OK one row per review, 1 column for each of the first 20 words. cell entries represent the integet token of the word
```
### listing 6.7
```{r, echo=TRUE, results='hide'}
model <- keras_model_sequential() %>%
# We specify the maximum input length to our Embedding layer
# so we can later flatten the embedded inputs
layer_embedding(input_dim = 10000, output_dim = 8, #10000 possible words that will be represented in 8 dimensions
input_length = maxlen) %>%
# We flatten the 3D tensor of embeddings
# into a 2D tensor of shape `(samples, maxlen * 8)`
layer_flatten() %>%
# We add the classifier on top
layer_dense(units = 1, activation = "sigmoid")
model %>% compile(
optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = c("acc")
)
history <- model %>% fit(
x_train, y_train,
epochs = 10,
batch_size = 32,
validation_split = 0.2
)
```
```{r}
plot(history)
history
```
## use someone elses embedding (raw text to word embeddings)
### listing 6.8
```{bash, eval=FALSE}
cd input
wget --no-check-certificate https://mng.bz/0tIo
```
```{bash, eval=FALSE}
cd input
unzip -q 0tIo
rm -r __MACOSX
rm 0tIo
```
```{r}
imdb_dir <- "input/aclImdb"
train_dir <- file.path(imdb_dir, "train")
labels <- c()
texts <- c()
for (label_type in c("neg", "pos")) {
label <- switch(label_type, neg = 0, pos = 1)
dir_name <- file.path(train_dir, label_type)
for (fname in list.files(dir_name, pattern = glob2rx("*.txt"),
full.names = TRUE)) {
texts <- c(texts, readChar(fname, file.info(fname)$size))
labels <- c(labels, label)
}
}
```
Tokenize it
```{r}
maxlen <- 100 # We will cut reviews after 100 words
training_samples <- 200 # We will be training on 200 samples
validation_samples <- 10000 # We will be validating on 10000 samples
max_words <- 10000 # We will only consider the top 10,000 words in the dataset
tokenizer <- text_tokenizer(num_words = max_words) %>%
fit_text_tokenizer(texts)
sequences <- texts_to_sequences(tokenizer, texts)
word_index = tokenizer$word_index
cat("Found", length(word_index), "unique tokens.\n")
data <- pad_sequences(sequences, maxlen = maxlen)
labels <- as.array(labels)
cat("Shape of data tensor:", dim(data), "\n")
cat('Shape of label tensor:', dim(labels), "\n")
# Split the data into a training set and a validation set
# But first, shuffle the data, since we started from data
# where sample are ordered (all negative first, then all positive).
indices <- sample(1:nrow(data))
training_indices <- indices[1:training_samples]
validation_indices <- indices[(training_samples + 1):
(training_samples + validation_samples)]
x_train <- data[training_indices,]
y_train <- labels[training_indices]
x_val <- data[validation_indices,]
y_val <- labels[validation_indices]
```
get the glove encoding
```{bash, eval=FALSE}
cd input
wget -nv http://nlp.stanford.edu/data/glove.6B.zip
```
```{bash, eval=FALSE}
cd input
unzip glove.6B.zip
rm glove.6B.zip
```
preprocess the glove encoding to turn words into numbers
```{r}
glove_dir = 'input'
lines <- readLines(file.path(glove_dir, "glove.6B.100d.txt"))
strsplit(lines[1:10], " ") # OK so each line is for a word, and gives the "loadings" for that word in each of 100 dimesnsions
embeddings_index <- new.env(hash = TRUE, parent = emptyenv())
for (i in 1:length(lines)) {
line <- lines[[i]]
values <- strsplit(line, " ")[[1]]
word <- values[[1]] # the word, since this comes first
embeddings_index[[word]] <- as.double(values[-1]) # the loadings
}
cat("Found", length(embeddings_index), "word vectors.\n")
```
```{r}
head(names(embeddings_index))
embeddings_index$house
```
now create an embedding_index that we can use in keras. This takes each word in the imdb reviews (up to 10,000) and adds the embedding.
```{r}
embedding_dim <- 100
embedding_matrix <- array(0, c(max_words, embedding_dim))
for (word in names(word_index)) {
index <- word_index[[word]]
if (index < max_words) {
embedding_vector <- embeddings_index[[word]]
if (!is.null(embedding_vector))
# Words not found in the embedding index will be all zeros.
embedding_matrix[index+1,] <- embedding_vector
}
}
```
```{r}
cat("word index\n")
head(word_index)
cat("embedding matrix\n")
dim(embedding_matrix)
head(embedding_matrix[,1:10])
cat("embeddings_index$the\n")
embeddings_index$the[1:10]
```
So the first data row (row2) in the embeddings matrix corresponds to the first word in the imdb word index and has the embeddings for that
### now define the model:
```{r}
model <- keras_model_sequential() %>%
layer_embedding(input_dim = max_words, output_dim = embedding_dim,
input_length = maxlen) %>%
layer_flatten() %>%
layer_dense(units = 32, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid")
summary(model)
```
add the embedding
```{r}
get_layer(model, index = 1) %>%
set_weights(list(embedding_matrix)) %>%
freeze_weights()
```
```{r, echo=TRUE, results='hide'}
model %>% compile(
optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = c("acc")
)
history <- model %>% fit(
x_train, y_train,
epochs = 20,
batch_size = 32,
validation_data = list(x_val, y_val)
)
save_model_weights_hdf5(model, "pre_trained_glove_model.h5")
```
Let's plot its performance over time:
```{r}
plot(history)
```
without pretrained embeddings
```{r, echo=TRUE, results='hide'}
model <- keras_model_sequential() %>%
layer_embedding(input_dim = max_words, output_dim = embedding_dim,
input_length = maxlen) %>%
layer_flatten() %>%
layer_dense(units = 32, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid")
model %>% compile(
optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = c("acc")
)
history <- model %>% fit(
x_train, y_train,
epochs = 20,
batch_size = 32,
validation_data = list(x_val, y_val)
)
```
```{r}
plot(history)
```
slightly worse
### evaluate test data
```{r}
test_dir <- file.path(imdb_dir, "test")
labels <- c()
texts <- c()
for (label_type in c("neg", "pos")) {
label <- switch(label_type, neg = 0, pos = 1)
dir_name <- file.path(test_dir, label_type)
for (fname in list.files(dir_name, pattern = glob2rx("*.txt"),
full.names = TRUE)) {
texts <- c(texts, readChar(fname, file.info(fname)$size))
labels <- c(labels, label)
}
}
sequences <- texts_to_sequences(tokenizer, texts)
x_test <- pad_sequences(sequences, maxlen = maxlen)
y_test <- as.array(labels)
```
And let's load and evaluate the first model:
```{r}
model %>%
load_model_weights_hdf5("pre_trained_glove_model.h5") %>%
evaluate(x_test, y_test, verbose = 0)
```