-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRNN.py
executable file
·122 lines (94 loc) · 4.44 KB
/
RNN.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
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelEncoder
########### LSTM ##################################################
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Embedding
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
def LSTM_fakenews():
df = pd.read_csv('data/updated.csv', encoding="utf-8")
df = df.dropna()
#
# uncomment this line if your computer does not have suffienct RAM(at least 16GB)
# df = df.sample(10000)
# Preparing the target and predictors for modeling
X_body_text = df.text.values
X_headline_text = df.title.values
y = df.label.values
X_headline_train, X_headline_test, y_headline_train, y_headline_test = train_test_split(X_headline_text, y, test_size=0.2, random_state=0)
X_body_train, X_body_test, y_body_train, y_body_test = train_test_split(X_body_text, y, test_size=0.2, random_state=0)
X_headline_list = list(X_headline_train)
tokenizer = Tokenizer()
tokenizer.fit_on_texts(X_headline_list)
print(len(tokenizer.word_index))
sequences = tokenizer.texts_to_sequences(X_headline_list)
l = len(max(sequences,key = lambda x : len(x)))
padded_headline_sequences = pad_sequences(sequences, maxlen = 1000) #padded_sequencies is the tokenized and padded data
#padded_sequences
model = Sequential()
model.add(Embedding(len(tokenizer.word_index)+1, 128, input_length=1000)) #maxlen of tokenizerwordindex
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2)) #128 depends on no of words in a row
model.add(Dense(2, activation='sigmoid')) #2 because of one hot enc
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
from keras.utils import to_categorical
y_train_LSTM = []
y_test_LSTM = []
for x in y_headline_train:
if x == 'REAL':
y_train_LSTM.append(1)
else:
y_train_LSTM.append(0)
for x in y_headline_test:
if x == 'REAL':
y_test_LSTM.append(1)
else:
y_test_LSTM.append(0)
y_train_LSTM = to_categorical(y_train_LSTM, num_classes = 2)
y_test_LSTM = to_categorical(y_test_LSTM, num_classes = 2)
model.fit(padded_headline_sequences, y_train_LSTM, validation_split=0.2, epochs=3)
test = list(X_headline_test)
print(len(tokenizer.word_index))
sequences = tokenizer.texts_to_sequences(test)
#Sequenized and padded data
pad_test = pad_sequences(sequences, maxlen = 1000)
scores = model.evaluate(pad_test,y_test_LSTM,verbose=0)
print("Accuracy using only headlines: %.2f%%" % (scores[1]*100))
from keras.utils import to_categorical
y_train_LSTM = []
y_test_LSTM = []
for x in y_body_train:
if x == 'REAL':
y_train_LSTM.append(1)
else:
y_train_LSTM.append(0)
for x in y_body_test:
if x == 'REAL':
y_test_LSTM.append(1)
else:
y_test_LSTM.append(0)
y_train_LSTM = to_categorical(y_train_LSTM, num_classes = 2)
y_test_LSTM = to_categorical(y_test_LSTM, num_classes = 2)
X_body_list = list(X_body_train)
tokenizer = Tokenizer()
tokenizer.fit_on_texts(X_body_list)
print(len(tokenizer.word_index))
sequences = tokenizer.texts_to_sequences(X_body_list)
l = len(max(sequences,key = lambda x : len(x)))
padded_body_sequences = pad_sequences(sequences, maxlen = 1000) #padded_sequencies is the tokenized and padded data
#padded_sequences
model = Sequential()
model.add(Embedding(len(tokenizer.word_index)+1, 128, input_length=1000)) #maxlen of tokenizerwordindex
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2)) #128 depends on no of words in a row
model.add(Dense(2, activation='sigmoid')) #2 because of one hot enc
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(padded_body_sequences, y_train_LSTM, validation_split=0.2, epochs=3)
test = list(X_body_test)
print(len(tokenizer.word_index))
sequences = tokenizer.texts_to_sequences(test)
#Sequenized and padded data
pad_test = pad_sequences(sequences, maxlen = 1000)
scores = model.evaluate(pad_test,y_test_LSTM,verbose=0)
print("Accuracy using only body: %.2f%%" % (scores[1]*100))