forked from kiryor/nnPUlearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
178 lines (158 loc) · 5.48 KB
/
model.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
import chainer
import chainer.functions as F
import chainer.links as L
import numpy as np
from chainer import Chain, cuda
class MyClassifier(Chain):
prior = 0
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):
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
chainer.reporter.report({'error': result}, 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()
size = n_p + n_n
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
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 t_p, t_n, f_p, 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 CNN(MyClassifier, Chain):
def __init__(self, prior, dim):
super(CNN, self).__init__(
conv1=L.Convolution2D(3, 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