forked from Wo1v3r/trollaganda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
160 lines (140 loc) · 5.6 KB
/
model.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
150
151
152
153
154
155
156
157
158
159
160
"""
This module contains the model features
"""
import matplotlib.pyplot as plt
from keras.callbacks import EarlyStopping
from keras.layers import Conv1D, MaxPooling1D, Embedding
from keras.layers import Dense, Input, Flatten, Dropout, Concatenate
from keras.models import Model
from keras.models import model_from_json
class TheModel(object):
"""
Model class
"""
def __init__(self, model=None):
self.model = model
self.history = None
def conv_net(
self,
embeddings,
max_sequence_length,
num_words,
embedding_dim,
trainable=False,
extra_conv=True
):
embedding_layer = Embedding(num_words,
embedding_dim,
weights=[embeddings],
input_length=max_sequence_length,
trainable=trainable)
sequence_input = Input(shape=(max_sequence_length,), dtype="int32")
embedded_sequences = embedding_layer(sequence_input)
# Yoon Kim model (https://arxiv.org/abs/1408.5882)
convs = []
filter_sizes = [3, 4, 5]
for filter_size in filter_sizes:
l_conv = Conv1D(filters=128, kernel_size=filter_size, activation="relu")(embedded_sequences)
l_pool = MaxPooling1D(pool_size=3)(l_conv)
convs.append(l_pool)
l_merge = Concatenate(axis=1)(convs)
# add a 1D convnet with global maxpooling, instead of Yoon Kim model
conv = Conv1D(filters=128, kernel_size=3, activation="relu")(embedded_sequences)
pool = MaxPooling1D(pool_size=3)(conv)
if extra_conv:
x = Dropout(0.5)(l_merge)
else:
# Original Yoon Kim model
x = Dropout(0.5)(pool)
x = Flatten()(x)
x = Dense(128, activation="relu")(x)
x = Dropout(0.5)(x)
# Finally, we feed the output into a Sigmoid layer.
# The reason why sigmoid is used is because we are trying to achieve a binary classification(1,0)
# for each of the 6 labels, and the sigmoid function will squash the output between the bounds of 0 and 1.
preds = Dense(1, activation="sigmoid")(x)
self.model = Model(sequence_input, preds)
self.model.compile(loss="binary_crossentropy",
optimizer="adam",
metrics=["acc"])
self.model.summary()
def train_model(self, traincnndata, Y_train, epochs, batch_size, validation_split=0.1):
try:
if not self.model:
raise Exception("The model didn't initiate.")
except Exception as e:
print(e)
return
# Defining callbacks
earlystopping = EarlyStopping(monitor="val_loss", min_delta=0.01, patience=4, verbose=1)
callbackslist = [earlystopping]
print("Training the Model")
self.history = self.model.fit(
traincnndata, Y_train, epochs=epochs, callbacks=callbackslist,
validation_split=validation_split, shuffle=True, batch_size=batch_size
)
def evaluate_model(self, test_cnn_data, Y_test):
try:
if not self.model:
raise Exception("The model didn't initiate.")
except Exception as e:
print(e)
return
yresults = self.model.evaluate(test_cnn_data, Y_test)
print("Test set\n Loss: {:0.3f}\n Accuracy: {:0.3f}".format(yresults[0], yresults[1]))
def load_model(self, path):
json_file = open(path + ".json", "r")
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights(path + ".h5")
loaded_model.compile(loss="binary_crossentropy",
optimizer="adam",
metrics=["acc"])
self.model = loaded_model
print("Loaded Model from disk")
def save_model(self, path):
try:
if not self.model:
raise Exception("The model didn't initiate.")
except Exception as e:
print(e)
return
print("Saving Model to path/model.json, weights to path/model.h5")
model_json = self.model.to_json()
with open(path + "/model.json", "w") as json_file:
json_file.write(model_json)
self.model.save_weights(path + "/model.h5")
print("Model Saved")
def print_loss_plot(self):
try:
if not self.history:
raise Exception("The model didn't run.")
except Exception as e:
print(e)
return
plt.figure()
plt.plot(self.history.history["loss"], lw=2.0, color="b", label="train")
plt.plot(self.history.history["val_loss"], lw=2.0, color="r", label="val")
plt.title("CNN sentiment")
plt.xlabel("Epochs")
plt.ylabel("Cross-Entropy Loss")
plt.legend(loc="upper right")
plt.show()
def print_accuracy_plot(self):
try:
if not self.history:
raise Exception("The model didn't run.")
except Exception as e:
print(e)
return
plt.figure()
plt.plot(self.history.history["acc"], lw=2.0, color="b", label="train")
plt.plot(self.history.history["val_acc"], lw=2.0, color="r", label="val")
plt.title("CNN sentiment")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.legend(loc="upper left")
plt.show()
def predict(self, data):
return self.model.predict(data)