forked from Sachin19/adversarial-classify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline.py
242 lines (204 loc) · 7.15 KB
/
baseline.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from sklearn.feature_extraction.text import CountVectorizer
import pickle
import numpy as np
import argparse
parser = argparse.ArgumentParser(description='Remove topical words')
parser.add_argument('--base_path', type=str, required=True,
help='path of base folder')
parser.add_argument('--suffix', type=str, default="",
help='suffix like _10, _5, _2 or empty string')
parser.add_argument('--extra_suffix', type=str, default="",
help='suffix like .logoddstop50r empty string')
parser.add_argument('--char', action='store_true')
parser.add_argument('--lex', action='store_true')
parser.add_argument('--brown', action='store_true')
parser.add_argument('--output_proba', action='store_true')
parser.add_argument('--predict_ood', action='store_false')
parser.add_argument('--train_prob_path', type=str, help='path of file to write output probabilites for train set')
parser.add_argument('--dev_prob_path', type=str, help='path of file to write output probabilites for valid set')
parser.add_argument('--test_prob_path', type=str, help='path of file to write output probabilites for test set')
parser.add_argument("--save_model")
args = parser.parse_args()
base_path = args.base_path
ftrain = open(base_path+"/train"+args.suffix+args.extra_suffix+".txt")
fdev = open(base_path+"/valid"+args.suffix+args.extra_suffix+".txt")
ftest = open(base_path+"/test"+args.suffix+args.extra_suffix+".txt")
fouttest = open(base_path+"/oodtest"+args.suffix+args.extra_suffix+".txt")
brownc = pickle.load(open(base_path+"/brownclusters"+args.suffix+".pkl","rb"))
texts = []
trainY = []
char_cv = CountVectorizer(analyzer='char_wb', ngram_range=(1,3), max_features=5000)
cv = CountVectorizer(max_features=5000)
for l in ftrain:
p = l.strip().split("\t")
texts.append(p[0])
trainY.append(p[1])
trainfeatures = []
if args.lex:
trainlexX = cv.fit_transform(texts).todense()
print ("Found lexical features")
trainfeatures.append(trainlexX)
if args.char:
traincharX = char_cv.fit_transform(texts).todense()
print ("Found char n-gram features")
trainfeatures.append(traincharX)
if args.brown:
trainbrownX = []
for text in texts:
feat = [0 for i in range(100)]
words = text.split()
for word in words:
if word in brownc:
feat[brownc[word]-1]+=1.0
sumfeat = sum(feat)+1e-6
feat = [f/sumfeat for f in feat]
trainbrownX.append(feat)
print ("Found brown cluster features")
trainbrownX = np.array(trainbrownX)
trainfeatures.append(trainbrownX)
trainX = np.concatenate(trainfeatures, axis=1)
print ("Train features computed")
###test#####
test_texts = []
testY = []
for l in ftest:
p = l.strip().split("\t")
test_texts.append(p[0])
testY.append(p[1])
testfeatures = []
if args.lex:
testlexX = cv.transform(test_texts).todense()
testfeatures.append(testlexX)
if args.char:
testcharX = char_cv.transform(test_texts).todense()
testfeatures.append(testcharX)
if args.brown:
testbrownX = []
for text in test_texts:
feat = [0 for i in range(100)]
words = text.split()
for word in words:
if word in brownc:
feat[brownc[word]-1]+=1.0
sumfeat = sum(feat)+1e-10
feat = [f/sumfeat for f in feat]
testbrownX.append(feat)
print ("Found brown cluster features for the test set")
testbrownX = np.array(testbrownX)
testfeatures.append(testbrownX)
testX = np.concatenate(testfeatures, axis=1)
print ("Test features computed")
###test#####
oodtest_texts = []
oodtestY = []
for l in fouttest:
p = l.strip().split("\t")
oodtest_texts.append(p[0])
oodtestY.append(p[1])
oodtestfeatures = []
if args.lex:
oodtestlexX = cv.transform(oodtest_texts).todense()
oodtestfeatures.append(oodtestlexX)
if args.char:
oodtestcharX = char_cv.transform(oodtest_texts).todense()
oodtestfeatures.append(oodtestcharX)
if args.brown:
oodtestbrownX = []
for text in oodtest_texts:
feat = [0 for i in range(100)]
words = text.split()
for word in words:
if word in brownc:
feat[brownc[word]-1]+=1.0
sumfeat = sum(feat)+1e-10
feat = [f/sumfeat for f in feat]
oodtestbrownX.append(feat)
print ("Found brown cluster features for the oodtest set")
oodtestbrownX = np.array(oodtestbrownX)
oodtestfeatures.append(oodtestbrownX)
oodtestX = np.concatenate(oodtestfeatures, axis=1)
print ("Test features computed")
if args.output_proba:
###dev#####
dev_texts = []
devY = []
for l in fdev:
p = l.strip().split("\t")
dev_texts.append(p[0])
devY.append(p[1])
devfeatures = []
if args.lex:
devlexX = cv.transform(dev_texts).todense()
devfeatures.append(devlexX)
if args.char:
devcharX = char_cv.transform(dev_texts).todense()
devfeatures.append(devcharX)
if args.brown:
devbrownX = []
for text in dev_texts:
feat = [0 for i in range(100)]
words = text.split()
for word in words:
if word in brownc:
feat[brownc[word]-1]+=1.0
sumfeat = sum(feat)+1e-10
feat = [f/sumfeat for f in feat]
devbrownX.append(feat)
print ("Found brown cluster features for the dev set")
devbrownX = np.array(devbrownX)
devfeatures.append(devbrownX)
devX = np.concatenate(devfeatures, axis=1)
print ("Dev features computed")
# test_texts = []
# testoutY = []
# for l in fouttest:
# p = l.strip().split("\t")
# test_texts.append(p[0])
# testoutY.append(p[1])
# # testlexX = cv.transform(test_texts).todense()
# testcharX = char_cv.transform(test_texts).todense()
# testbrownX = []
# for text in test_texts:
# feat = [0 for i in range(100)]
# words = text.split()
# for word in words:
# if word in brownc:
# feat[brownc[word]-1]+=1.0
# sumfeat = sum(feat)+1e-10
# feat = [f/sumfeat for f in feat]
# testbrownX.append(feat)
# print ("Found brown cluster features")
# testbrownX = np.array(testbrownX)
# print (testcharX.shape, testbrownX.shape)
# testoutX = np.concatenate([testcharX, testbrownX], axis=1)
###model####
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
lr.fit(trainX, trainY)
if args.save_model != "":
print ("Saving the baseline model")
with open(args.save_model,"wb") as f:
pickle.dump(lr, f)
predY = lr.predict(testX)
print ("Test Accuracy", (1.0*sum(predY==testY))/len(testY))
if args.output_proba:
predTrainY = lr.predict(trainX)
print ("Train Accuracy", (1.0*sum(predTrainY==trainY))/len(trainY))
probTrainY = lr.predict_proba(trainX)
f = open(args.train_prob_path,"w")
for p in probTrainY:
f.write(" ".join([str(t) for t in p])+"\n")
f.close()
probdevY = lr.predict_proba(devX)
f = open(args.dev_prob_path,"w")
for p in probdevY:
f.write(" ".join([str(t) for t in p])+"\n")
f.close()
probtestY = lr.predict_proba(testX)
f = open(args.test_prob_path,"w")
for p in probtestY:
f.write(" ".join([str(t) for t in p])+"\n")
f.close()
if args.predict_ood:
predY = lr.predict(oodtestX)
print ("OOD Test Accuracy", (1.0*sum(predY==oodtestY))/len(oodtestY))