-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlstm_module.py
149 lines (131 loc) · 3.54 KB
/
lstm_module.py
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
## Setup
import numpy as np
from tensorflow import keras
import tensorflow as tf
from tensorflow.keras import models
from tensorflow.keras import layers
# import optuna
# from optuna.integration import KerasPruningCallback
# from optuna.trial import TrialState
import os
import sys
my_lib_path = os.path.abspath("./")
sys.path.append(my_lib_path)
## Text preprocessing
import preprocessing_module
(
pad_X_train,
pad_X_val,
pad_X_test,
y_train,
y_val,
y_test,
num_unique_words,
max_length,
) = preprocessing_module.get_data()
def print_data():
print(pad_X_train.shape)
print(y_train.shape)
print(pad_X_val.shape)
print(y_val.shape)
print(pad_X_test.shape)
print(y_test.shape)
print(num_unique_words)
print(max_length)
# LSTM model
def LSTM(
embedding_dim=32,
num_units=64,
num_classes=25,
num_epochs=20,
batch_size=32,
verbosity=0,
loss_function="sparse_categorical_crossentropy",
optimizer="adam",
trial=None,
):
# create initial model
if os.path.isdir("LSTM"):
model = models.load_model("LSTM")
else:
model = keras.models.Sequential()
model.add(
layers.Embedding(num_unique_words, embedding_dim, input_length=max_length)
)
model.save("LSTM")
if trial is not None:
## Optuna
num_units = trial.suggest_int("Units number", 8, 128, log=True)
model.add(
layers.LSTM(
num_units,
dropout=0.2,
activation="tanh",
recurrent_activation="sigmoid",
recurrent_dropout=0,
unroll=False,
use_bias=True,
)
)
model.add(layers.Dense(num_classes, activation="softmax"))
# compile model
model.compile(loss=loss_function, optimizer=optimizer, metrics=["accuracy"])
# fit the model
if trial is not None:
## Optuna
num_epochs = trial.suggest_int("Epochs number", 10, 50, log=True)
model.fit(
pad_X_train,
y_train,
epochs=num_epochs,
batch_size=batch_size,
verbose=verbosity,
validation_data=(pad_X_val, y_val),
)
return model
# LSTM model
def cnn(
embedding_dim=32,
num_units=64,
num_classes=25,
num_epochs=20,
batch_size=32,
verbosity=0,
loss_function="sparse_categorical_crossentropy",
optimizer="adam",
trial=None,
):
# create initial model
if os.path.isdir("CNN"):
model = models.load_model("CNN")
else:
model = keras.models.Sequential()
model.add(
layers.Embedding(num_unique_words, embedding_dim, input_length=max_length)
)
model.save("CNN")
# if trial is not None:
# ## Optuna
# num_units = trial.suggest_int("Units number", 8, 128, log=True)
model.add(layers.Conv1D(filters=num_units, kernel_size=3, activation="relu"))
model.add(layers.Flatten())
model.add(layers.Dense(num_classes, activation="softmax"))
# compile model
model.compile(loss=loss_function, optimizer=optimizer, metrics=["accuracy"])
# fit the model
# if trial is not None:
# ## Optuna
# num_epochs = trial.suggest_int("Epochs number", 10, 50, log=True)
model.fit(
pad_X_train,
y_train,
epochs=num_epochs,
batch_size=batch_size,
verbose=verbosity,
validation_data=(pad_X_val, y_val),
)
return model
def evaluate_model(model):
# evaluate the model on test data
scores = model.evaluate(pad_X_test, y_test, verbose=0)
return scores[1]