forked from kiryor/nnPUlearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
313 lines (279 loc) · 10.2 KB
/
Copy pathmodel.py
File metadata and controls
313 lines (279 loc) · 10.2 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import warnings
warnings.filterwarnings('ignore')
import chainer
import chainer.functions as F
import chainer.links as L
import numpy as np
from chainer import Chain
from chainer.backends import cuda
from sklearn.metrics import recall_score
from functools import partial
class MyClassifier(Chain):
prior = 0
it_position = None
def __call__(self, x, t, loss_func):
self.clear()
h = self.calculate(x)
self.loss = loss_func(h, t)
chainer.reporter.report({'loss': self.loss}, self)
return self.loss
def clear(self):
self.loss = None
def calculate(self, x):
return None
def call_reporter(self, dictionary):
chainer.reporter.report(dictionary, self)
def error(self, x, t):
warnings.filterwarnings("ignore")
xp = cuda.get_array_module(x, False)
size = len(t)
with chainer.no_backprop_mode():
with chainer.using_config("train", False):
h = xp.reshape(xp.sign(self.calculate(x).data), size)
if isinstance(h, chainer.Variable):
h = h.data
if isinstance(t, chainer.Variable):
t = t.data
result = (h != t).sum() / size
t, h = t.get(), h.get()
h_separated = ','.join([str(x) for x in h]) + '\n'
h_separated = str(self.it_position) + ',' + h_separated
with open('result/preds.csv', 'a') as f:
f.write(h_separated)
assert h.shape[0] == t.shape[0]
# Calculate partial recall
recall = recall_score(t, h)
# Calculate perc pos and perc pos non fake
h_pos_idx = np.where(h == 1)[0]
perc_pos = h_pos_idx.shape[0]/h.shape[0] if h.shape[0] > 0 else 0.
if len(h_pos_idx) > 0:
perc_pos_nf = np.unique(t[h_pos_idx], return_counts=True)[1]/h_pos_idx.shape[0]
if len(perc_pos_nf) > 0:
perc_pos_nf = perc_pos_nf[-1]
else:
perc_pos_nf = 0.
else:
perc_pos_nf = 0.
chainer.reporter.report({'error': result}, self)
chainer.reporter.report({'recall': recall}, self)
chainer.reporter.report({'percPos': perc_pos}, self)
chainer.reporter.report({'percPosNF': perc_pos_nf}, self)
return cuda.to_cpu(result) if xp != np else result
def compute_prediction_summary(self, x, t):
xp = cuda.get_array_module(x, False)
if isinstance(t, chainer.Variable):
t = t.data
n_p = (t == 1).sum()
n_n = (t == -1).sum()
with chainer.no_backprop_mode():
with chainer.using_config("train", False):
h = xp.ravel(xp.sign(self.calculate(x).data))
if isinstance(h, chainer.Variable):
h = h.data
t_p = ((h == 1) * (t == 1)).sum()
t_n = ((h == -1) * (t == -1)).sum()
f_p = n_n - t_n
f_n = n_p - t_p
return int(t_p), int(t_n), int(f_p), int(f_n)
class LinearClassifier(MyClassifier, Chain):
def __init__(self, prior, dim):
super(LinearClassifier, self).__init__(
l=L.Linear(dim, 1)
)
self.prior = prior
def calculate(self, x):
h = self.l(x)
return h
class ThreeLayerPerceptron(MyClassifier, Chain):
def __init__(self, prior, dim):
super(ThreeLayerPerceptron, self).__init__(l1=L.Linear(dim, 100),
l2=L.Linear(100, 1))
self.af = F.relu
self.prior = prior
def calculate(self, x):
h = self.l1(x)
h = self.af(h)
h = self.l2(h)
return h
#
# class MultiLayerPerceptron(MyClassifier, Chain):
# def __init__(self, prior, dim):
# super(MultiLayerPerceptron, self).__init__(l1=L.Linear(dim, 300, nobias=True),
# b1=L.BatchNormalization(300),
# l2=L.Linear(300, 300, nobias=True),
# b2=L.BatchNormalization(300),
# l3=L.Linear(300, 300, nobias=True),
# b3=L.BatchNormalization(300),
# l4=L.Linear(300, 300, nobias=True),
# b4=L.BatchNormalization(300),
# l5=L.Linear(300, 1))
# self.af = F.relu
# self.prior = prior
#
# def calculate(self, x):
# h = self.l1(x)
# h = self.b1(h)
# h = self.af(h)
# h = self.l2(h)
# h = self.b2(h)
# h = self.af(h)
# h = self.l3(h)
# h = self.b3(h)
# h = self.af(h)
# h = self.l4(h)
# h = self.b4(h)
# h = self.af(h)
# h = self.l5(h)
# return h
class MultiLayerPerceptron(MyClassifier, Chain):
def __init__(self, prior, dim):
super(MultiLayerPerceptron, self).__init__(l1=L.Linear(dim, 300, nobias=True),
b1=L.BatchNormalization(300),
l2=L.Linear(300, 300, nobias=True),
b2=L.BatchNormalization(300),
l3=L.Linear(300, 300, nobias=True),
b3=L.BatchNormalization(300),
l4=L.Linear(300, 300, nobias=True),
b4=L.BatchNormalization(300),
l5=L.Linear(300, 300, nobias=True),
b5=L.BatchNormalization(300),
l6=L.Linear(300, 300, nobias=True),
b6=L.BatchNormalization(300),
l7=L.Linear(300, 300, nobias=True),
b7=L.BatchNormalization(300),
l8=L.Linear(300, 300, nobias=True),
b8=L.BatchNormalization(300),
l9=L.Linear(300, 1))
self.af = F.relu
self.dr = F.dropout
self.prior = prior
def calculate(self, x):
h = self.l1(x)
h = self.b1(h)
h = self.af(h)
h = self.l2(h)
h = self.b2(h)
h = self.af(h)
h = self.l3(h)
h = self.b3(h)
h = self.af(h)
h = self.l4(h)
h = self.b4(h)
h = self.af(h)
h = self.l5(h)
h = self.b5(h)
h = self.af(h)
h = self.l6(h)
# h = self.dr(h)
h = self.b6(h)
h = self.af(h)
h = self.l7(h)
# h = self.dr(h)
h = self.b7(h)
h = self.af(h)
h = self.l8(h)
# h = self.dr(h)
h = self.b8(h)
h = self.af(h)
h = self.l9(h)
return h
class CNN(MyClassifier, Chain):
def __init__(self, prior, dim):
super(CNN, self).__init__(
conv1=L.Convolution2D(None, 16, 3, pad=1),
conv2=L.Convolution2D(None, 16, 3, pad=1),
conv3=L.Convolution2D(None, 16, 3, pad=1),
conv4=L.Convolution2D(None, 16, 3, pad=1),
b1=L.BatchNormalization(16),
b2=L.BatchNormalization(16),
fc1=L.Linear(None, 128),
fc2=L.Linear(128, 128),
fc3=L.Linear(128, 1),
)
self.mpool=partial(F.max_pooling_2d, ksize=2, stride=2)
self.af = F.relu
self.prior = prior
def calculate(self, x):
h = self.conv1(x)
h = self.af(h)
h = self.mpool(x)
# h = self.b1(h)
h = self.conv2(h)
h = self.af(h)
h = self.mpool(x)
h = self.conv3(x)
h = self.af(h)
h = self.mpool(x)
h = self.conv4(x)
h = self.af(h)
h = self.mpool(x)
# h = self.b2(h)
# h = self.af(h)
h = self.fc1(h)
h = self.af(h)
# h = self.fc2(h)
# h = self.af(h)
h = self.fc3(h)
return h
# class CNN(MyClassifier, Chain):
# def __init__(self, prior, dim):
# super(CNN, self).__init__(
# conv1=L.Convolution2D(None, 96, 3, pad=1),
# conv2=L.Convolution2D(96, 96, 3, pad=1),
# conv3=L.Convolution2D(96, 96, 3, pad=1, stride=2),
# conv4=L.Convolution2D(96, 192, 3, pad=1),
# conv5=L.Convolution2D(192, 192, 3, pad=1),
# conv6=L.Convolution2D(192, 192, 3, pad=1, stride=2),
# conv7=L.Convolution2D(192, 192, 3, pad=1),
# conv8=L.Convolution2D(192, 192, 1),
# conv9=L.Convolution2D(192, 10, 1),
# b1=L.BatchNormalization(96),
# b2=L.BatchNormalization(96),
# b3=L.BatchNormalization(96),
# b4=L.BatchNormalization(192),
# b5=L.BatchNormalization(192),
# b6=L.BatchNormalization(192),
# b7=L.BatchNormalization(192),
# b8=L.BatchNormalization(192),
# b9=L.BatchNormalization(10),
# fc1=L.Linear(None, 1000),
# fc2=L.Linear(1000, 1000),
# fc3=L.Linear(1000, 1),
# )
# self.af = F.relu
# self.prior = prior
#
# def calculate(self, x):
# h = self.conv1(x)
# h = self.b1(h)
# h = self.af(h)
# h = self.conv2(h)
# h = self.b2(h)
# h = self.af(h)
# h = self.conv3(h)
# h = self.b3(h)
# h = self.af(h)
# h = self.conv4(h)
# h = self.b4(h)
# h = self.af(h)
# h = self.conv5(h)
# h = self.b5(h)
# h = self.af(h)
# h = self.conv6(h)
# h = self.b6(h)
# h = self.af(h)
# h = self.conv7(h)
# h = self.b7(h)
# h = self.af(h)
# h = self.conv8(h)
# h = self.b8(h)
# h = self.af(h)
# h = self.conv9(h)
# h = self.b9(h)
# h = self.af(h)
# h = self.fc1(h)
# h = self.af(h)
# h = self.fc2(h)
# h = self.af(h)
# h = self.fc3(h)
# return h