-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSTM.py
168 lines (132 loc) · 3.74 KB
/
LSTM.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
161
162
163
164
"""
Fake news detection
LSTM model
"""
import numpy as np
from keras.models import Sequential
from keras.models import load_model
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
from collections import Counter
import os
import getEmbeddings
import cleanText
import matplotlib.pyplot as plt
import scikitplot.plotters as skplt
top_words = 5000
epoch_num = 5
batch_size = 64
def plot_cmat(yte, ypred):
'''Plotting confusion matrix'''
skplt.plot_confusion_matrix(yte, ypred)
plt.show()
# Read the text data
if not os.path.isfile('./xtr_shuffled.npy') or \
not os.path.isfile('./xte_shuffled.npy') or \
not os.path.isfile('./ytr_shuffled.npy') or \
not os.path.isfile('./yte_shuffled.npy'):
getEmbeddings.clean_data()
if not os.path.isfile('./xtest.npy'):
cleanText.clean_data()
xtr = np.load('./xtr_shuffled.npy')
xte = np.load('./xte_shuffled.npy')
y_train = np.load('./ytr_shuffled.npy')
y_test = np.load('./yte_shuffled.npy')
new_data = np.load('./xtest.npy')
a=new_data.tolist() #changing datatype to list
"""
data=[]
data.append(new_data)
print(type(data[0]))
print (type(new_data))
print (new_data)"""
data=[]
data = a.split()
data_seq=[]
data_seq.append(data)
cnt = Counter()
x_train = []
for x in xtr:
x_train.append(x.split())
for word in x_train[-1]:
cnt[word] += 1
# Storing most common words
most_common = cnt.most_common(top_words + 1)
word_bank = {}
id_num = 1
for word, freq in most_common:
word_bank[word] = id_num
id_num += 1
# Encode the sentences
for news in x_train:
i = 0
while i < len(news):
if news[i] in word_bank:
news[i] = word_bank[news[i]]
i += 1
else:
del news[i]
y_train = list(y_train)
y_test = list(y_test)`
# Delete the short news
i = 0
while i < len(x_train):
if len(x_train[i]) > 10:
i += 1
else:
del x_train[i]
del y_train[i]
# Generating test data
x_test = []
for x in xte:
x_test.append(x.split())
# Encode the sentences
for news in x_test:
i = 0
while i < len(news):
if news[i] in word_bank:
news[i] = word_bank[news[i]]
i += 1
else:
del news[i]
for news in data_seq:
i = 0
while i < len(news):
if news[i] in word_bank:
news[i] = word_bank[news[i]]
i += 1
else:
del news[i]
# Truncate and pad input sequences
max_review_length = 500
X_pred = sequence.pad_sequences(data_seq, maxlen=max_review_length)
X_train = sequence.pad_sequences(x_train, maxlen=max_review_length)
X_test = sequence.pad_sequences(x_test, maxlen=max_review_length)
print("**********************************************************************")
#print(X_pred[0])
#print(len(X_pred[0]))
# Convert to numpy arrays
y_train = np.array(y_train)
y_test = np.array(y_test)
# Create the model
embedding_vecor_length = 32
model = Sequential()
model.add(Embedding(top_words+2, embedding_vecor_length, input_length=max_review_length))
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=epoch_num, batch_size=batch_size)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy= %.2f%%" % (scores[1]*100))
# Draw the confusion matrix
y_pred = model.predict_classes(X_test)
plot_cmat(y_test, y_pred)
print ("********************************************************************************")
model.save('lstm_model.h5')
model= load_model('lstm_model.h5')
yhat= model.predict_classes(X_pred)
print(yhat)