forked from PacktPublishing/Advanced-Deep-Learning-with-R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCh-10: RNN
42 lines (34 loc) · 1.04 KB
/
Ch-10: RNN
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
# Libraries required
library(keras)
#imdb data
imdb <- dataset_imdb(num_words = 500)
c(c(train_x, train_y), c(test_x, test_y)) %<-% imdb
length(train_x); length(test_x)
table(train_y)
table(test_y)
# Padding sequences
train_x <- pad_sequences(train_x, maxlen = 100)
test_x <- pad_sequences(test_x, maxlen = 100)
# Model
model <- keras_model_sequential()
model %>%
layer_embedding(input_dim = 500, output_dim = 32) %>%
layer_simple_rnn(units = 8) %>%
layer_dense(units = 1, activation = "sigmoid")
# Compile
model %>% compile(optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = c("acc"))
# Fit model
history <- model %>% fit(train_x, train_y,
epochs = 10,
batch_size = 128,
validation_split = 0.2)
plot(history)
# Prediction
model %>% evaluate(train_x, train_y)
pred <- model %>% predict_classes(train_x)
table(Predicted=pred, Actual=imdb$train$y)
model %>% evaluate(test_x, test_y)
pred1 <- model %>% predict_classes(test_x)
table(Predicted=pred1, Actual=imdb$test$y)