forked from PacktPublishing/Advanced-Deep-Learning-with-R
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCh-11: Text classification using Long Short-Term Memory Network
149 lines (117 loc) · 3.77 KB
/
Ch-11: Text classification using Long Short-Term Memory Network
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
# IMDB data
library(keras)
imdb <- dataset_imdb(num_words = 500)
c(c(train_x, train_y), c(test_x, test_y)) %<-% imdb
train_x <- pad_sequences(train_x, maxlen = 200)
test_x <- pad_sequences(test_x, maxlen = 200)
# Model architecture
model <- keras_model_sequential() %>%
layer_embedding(input_dim = 500, output_dim = 32) %>%
layer_lstm(units = 32) %>%
layer_dense(units = 1, activation = "sigmoid")
model
# Compile
model %>% compile(optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = c("acc"))
# Fit model
model_one <- model %>% fit(train_x, train_y,
epochs = 10,
batch_size = 128,
validation_split = 0.2)
plot(model_one)
# Evaluate
model %>% evaluate(train_x, train_y)
# Confusion Matrix
pred <- model %>% predict_classes(train_x)
table(Predicted=pred, Actual=imdb$train$y)
# Evaluate
model %>% evaluate(test_x, test_y)
# Confusion Matrix
pred1 <- model %>$ predict_classes(text_x)
table(Predicted=pred1, Actual=imdb$test$y)
# Model architecture
model <- keras_model_sequential() %>%
layer_embedding(input_dim = 500, output_dim = 32) %>%
layer_lstm(units = 32) %>%
layer_dense(units = 1, activation = "sigmoid")
# Compile
model %>% compile(optimizer = "adam",
loss = "binary_crossentropy",
metrics = c("acc"))
# Fit model
model_two <- model %>% fit(train_x, train_y,
epochs = 10,
batch_size = 128,
validation_split = 0.2)
plot(model_two)
# Loss and accuracy
model %>% evaluate(train_x, train_y)
# Confusion Matrix
pred <- model %>% predict_classes(train_x)
table(Predicted=pred, Actual=imdb$train$y)
# Loss and accuracy
model %>% evaluate(test_x, test_y)
# Confusion Matrix
pred1 <- model %>% predict_classes(test_x)
table(Predicted=pred1, Actual=imdb$test$y)
# Number of positive and negative reviews in the train data
table(train_y)
# Number of positive and negative review in the test data
table(test_y)
# Model architecture
model <- keras_model_sequential() %>%
layer_embedding(input_dim = 500, output_dim = 32) %>%
layer_lstm(units = 32,
return_sequences = TRUE) %>%
layer_lstm(units = 32) %>%
layer_dense(units = 1, activation = "sigmoid")
# Compiling model
model %>% compile(optimizer = "adam",
loss = "binary_crossentropy",
metrics = c("acc"))
# Fitting model
model_three <- model %>% fit(train_x, train_y,
epochs = 10,
batch_size = 128,
validation_split = 0.2)
# Loss and accuracy plot
plot(model_three)
# Loss and accuracy
model %>% evaluate(train_x, train_y)
# Confusion Matrix
pred <- model %>% predict_classes(train_x)
table(Predicted=pred, Actual=imdb$train$y)
# Loss and accuracy
model %>% evaluate(test_x, test_y)
# Confusion Matrix
pred1 <- model %>% predict_classes(test_x)
table(Predicted=pred1, Actual=imdb$test$y)
# Model architecture
model <- keras_model_sequential() %>%
layer_embedding(input_dim = 500, output_dim = 32) %>%
bidirectional(layer_lstm(units = 32)) %>%
layer_dense(units = 1, activation = "sigmoid")
# Model summary
summary(model)
# Compiling model
model %>% compile(optimizer = "adam",
loss = "binary_crossentropy",
metrics = c("acc"))
# Fitting model
model_four <- model %>% fit(train_x, train_y,
epochs = 10,
batch_size = 128,
validation_split = 0.2)
# Loss and accuracy plot
plot(model_four)
# Loss and accuracy
model %>% evaluate(train_x, train_y)
# Confusion Matrix
pred <- model %>% predict_classes(train_x)
table(Predicted=pred, Actual=imdb$train$y)
# Loss and accuracy
model %>% evaluate(test_x, test_y)
#Confusion Matrix
pred1 <- model %>% predict_classes(test_x)
table(Predicted=pred1, Actual=imdb$test$y)