forked from LiKev12/CSE544T-Project-TextBugger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline_models.py
200 lines (156 loc) · 6.46 KB
/
baseline_models.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from __future__ import unicode_literals, print_function
from spacy.lang.en import English # updated
import json
import pickle
import numpy as np
import random
import matplotlib.pyplot as plt
import pprint
import nltk
import time
from keras.models import Sequential
from keras.layers import Dense, LSTM, Embedding
from keras.datasets import imdb
from keras.preprocessing import sequence
from keras.layers import Dense, Dropout, Activation
from keras.layers import Embedding, LSTM
from keras.layers import Conv1D, Flatten, MaxPooling1D
from keras import Input
# import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tf.compat.v1.enable_eager_execution()
from keras import backend as k
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from nltk.tokenize.treebank import TreebankWordDetokenizer
def make_LSTM(dataset, num_epochs):
data = pickle.load( open( "datasets/{}/{}_embeddings.p".format(dataset, dataset), "rb" ) )
emb_map = pickle.load( open( "datasets/{}/{}_embed_map.p".format(dataset, dataset), "rb" ) )
vocab_size = len(list(emb_map['w2i'].keys()))
print('Vocab size is {}'.format(vocab_size))
## Train
x_train_pos = data['train']['pos']
x_train_neg = data['train']['neg'][0:len(x_train_pos)]
print("POS: {}".format(len(x_train_pos)))
print("NEG: {}".format(len(x_train_neg)))
x_train = []
x_train.extend(x_train_pos)
x_train.extend(x_train_neg)
y_train = [1 for i in range(len(x_train_pos))]
y_train.extend([0 for i in range(len(x_train_neg))])
x_train = np.array(x_train, dtype='float')
y_train = np.array(y_train, dtype='float')
## Test
x_test_pos = data['test']['pos']
x_test_neg = data['test']['neg'][0:len(x_test_pos)]
x_test = []
x_test.extend(x_test_pos)
x_test.extend(x_test_neg)
y_test = [1 for i in range(len(x_test_pos))]
y_test.extend([0 for i in range(len(x_test_neg))])
x_test = np.array(x_test, dtype='float')
y_test = np.array(y_test, dtype='float')
## Shuffle
x_train,y_train = shuffle(x_train, y_train)
x_test,y_test = shuffle(x_test,y_test)
## Model
hidden_size = 32
sl_model = Sequential()
sl_model.add(Embedding(vocab_size, hidden_size))
sl_model.add(LSTM(hidden_size, activation='tanh', dropout=0.2, recurrent_dropout=0.2))
sl_model.add(Dense(1, activation='sigmoid'))
sl_model.compile(optimizer='adam',loss='binary_crossentropy', metrics=['accuracy'])
sl_model.fit(x_train, y_train, batch_size = 128, epochs = num_epochs, validation_data = (x_test, y_test), shuffle=True)
loss, acc = sl_model.evaluate(x_test, y_test)
print('Single layer model -- ACC {} -- LOSS {}'.format(acc,loss))
print('{} model done!'.format(dataset))
pickle.dump(sl_model, open( "models/LSTM_TCD_{}.p".format(dataset), "wb" ))
return
def get_cnn(dataset, epochs):
data = pickle.load( open( "datasets/{}/{}_embeddings.p".format(dataset, dataset), "rb" ) )
emb_map = pickle.load( open( "datasets/{}/{}_embed_map.p".format(dataset, dataset), "rb" ) )
vocab_size = len(list(emb_map['w2i'].keys()))
print('Vocab size is {}'.format(vocab_size))
## Train
x_train_pos = data['train']['pos']
x_train_neg = data['train']['neg'][0:len(x_train_pos)]
x_train = []
x_train.extend(x_train_pos)
x_train.extend(x_train_neg)
y_train = [1 for i in range(len(x_train_pos))]
y_train.extend([0 for i in range(len(x_train_neg))])
x_train = np.array(x_train, dtype='float')
y_train = np.array(y_train, dtype='float')
## Test
x_test_pos = data['test']['pos']
x_test_neg = data['test']['neg'][0:len(x_test_pos)]
x_test = []
x_test.extend(x_test_pos)
x_test.extend(x_test_neg)
y_test = [1 for i in range(len(x_test_pos))]
y_test.extend([0 for i in range(len(x_test_neg))])
x_test = np.array(x_test, dtype='float')
y_test = np.array(y_test, dtype='float')
## Shuffle
x_train,y_train = shuffle(x_train, y_train)
x_test,y_test = shuffle(x_test,y_test)
## Model
max_len = x_train.shape[1]
batch_size = 32
embedding_dims=10
filters=16
kernel_size=3
hidden_dims=250
model = Sequential()
model.add(Embedding(vocab_size, embedding_dims, input_length=max_len))
model.add(Dropout(0.5))
model.add(Conv1D(filters,kernel_size,padding='valid',activation='relu'))
model.add(MaxPooling1D())
model.add(Conv1D(filters, kernel_size, padding='valid', activation='relu'))
model.add(MaxPooling1D())
model.add(Flatten())
model.add(Dense(hidden_dims, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(x_train, y_train, batch_size = batch_size, epochs = epochs, validation_data=(x_test, y_test))
loss, acc = model.evaluate(x_test, y_test)
print('CNN Model -- ACC {} -- LOSS {}'.format(acc,loss))
print('{} model done!'.format(dataset))
pickle.dump(model, open( "models/CNN/CNN_TCD_{}.p".format(dataset), "wb" ))
def make_LR(dataset):
data = pickle.load( open( "datasets/{}/{}_vectors.p".format(dataset, dataset), "rb" ) )
## Train
x_train_pos = data['train']['pos']
x_train_neg = data['train']['neg'][0:len(x_train_pos)]
x_train = []
x_train.extend(x_train_pos)
x_train.extend(x_train_neg)
y_train = [1 for i in range(len(x_train_pos))]
y_train.extend([0 for i in range(len(x_train_neg))])
x_train = np.array(x_train, dtype='float')
y_train = np.array(y_train, dtype='float')
## Test
x_test_pos = data['test']['pos']
x_test_neg = data['test']['neg'][0:len(x_test_pos)]
x_test = []
x_test.extend(x_test_pos)
x_test.extend(x_test_neg)
y_test = [1 for i in range(len(x_test_pos))]
y_test.extend([0 for i in range(len(x_test_neg))])
x_test = np.array(x_test, dtype='float')
y_test = np.array(y_test, dtype='float')
## Shuffle
x_train,y_train = shuffle(x_train, y_train)
x_test,y_test = shuffle(x_test,y_test)
model = LogisticRegression(random_state=42).fit(x_train,y_train)
acc = model.score(x_test,y_test)
print("ACCURACY: {}".format(acc))
pickle.dump(model, open( "models/LR/LR_TCD_{}.p".format(dataset), "wb" ))
# make_LSTM('Kaggle',2) # 86.53%
# get_cnn('Kaggle',2) # 87.40%
# make_LR('Kaggle') # 87.51