-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathcnn_theano.py
164 lines (137 loc) · 5.04 KB
/
cnn_theano.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
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import theano
import theano.tensor as T
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from theano.tensor.nnet import conv2d
from theano.tensor.signal.pool import pool_2d
from util import getImageData, error_rate, init_weight_and_bias, init_filter
from ann_theano import HiddenLayer, rmsprop
class ConvPoolLayer(object):
def __init__(self, mi, mo, fw=5, fh=5, poolsz=(2, 2)):
# mi = input feature map size
# mo = output feature map size
sz = (mo, mi, fw, fh)
W0 = init_filter(sz, poolsz)
self.W = theano.shared(W0)
b0 = np.zeros(mo, dtype=np.float32)
self.b = theano.shared(b0)
self.poolsz = poolsz
self.params = [self.W, self.b]
def forward(self, X):
conv_out = conv2d(input=X, filters=self.W)
pooled_out = pool_2d(
input=conv_out,
ws=self.poolsz,
ignore_border=True,
mode='max',
)
return T.tanh(pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
class CNN(object):
def __init__(self, convpool_layer_sizes, hidden_layer_sizes):
self.convpool_layer_sizes = convpool_layer_sizes
self.hidden_layer_sizes = hidden_layer_sizes
def fit(self, X, Y, Xvalid, Yvalid, lr=1e-3, mu=0.99, reg=1e-3, decay=0.99999, eps=1e-10, batch_sz=30, epochs=3, show_fig=True):
# downcast
lr = np.float32(lr)
mu = np.float32(mu)
reg = np.float32(reg)
decay = np.float32(decay)
eps = np.float32(eps)
X = X.astype(np.float32)
Xvalid = Xvalid.astype(np.float32)
Y = Y.astype(np.int32)
Yvalid = Yvalid.astype(np.int32)
# initialize convpool layers
N, c, width, height = X.shape
mi = c
outw = width
outh = height
self.convpool_layers = []
for mo, fw, fh in self.convpool_layer_sizes:
layer = ConvPoolLayer(mi, mo, fw, fh)
self.convpool_layers.append(layer)
outw = (outw - fw + 1) // 2
outh = (outh - fh + 1) // 2
mi = mo
# initialize mlp layers
K = len(set(Y))
self.hidden_layers = []
M1 = self.convpool_layer_sizes[-1][0]*outw*outh # size must be same as output of last convpool layer
count = 0
for M2 in self.hidden_layer_sizes:
h = HiddenLayer(M1, M2, count)
self.hidden_layers.append(h)
M1 = M2
count += 1
# logistic regression layer
W, b = init_weight_and_bias(M1, K)
self.W = theano.shared(W, 'W_logreg')
self.b = theano.shared(b, 'b_logreg')
# collect params for later use
self.params = [self.W, self.b]
for c in self.convpool_layers:
self.params += c.params
for h in self.hidden_layers:
self.params += h.params
# set up theano functions and variables
thX = T.tensor4('X', dtype='float32')
thY = T.ivector('Y')
pY = self.forward(thX)
rcost = reg*T.sum([(p*p).sum() for p in self.params])
cost = -T.mean(T.log(pY[T.arange(thY.shape[0]), thY])) + rcost
prediction = self.th_predict(thX)
cost_predict_op = theano.function(inputs=[thX, thY], outputs=[cost, prediction])
updates = rmsprop(cost, self.params, lr, mu, decay, eps)
train_op = theano.function(
inputs=[thX, thY],
outputs=cost,
updates=updates
)
n_batches = N // batch_sz
costs = []
for i in range(epochs):
X, Y = shuffle(X, Y)
for j in range(n_batches):
Xbatch = X[j*batch_sz:(j*batch_sz+batch_sz)]
Ybatch = Y[j*batch_sz:(j*batch_sz+batch_sz)]
train_c = train_op(Xbatch, Ybatch)
if j % 20 == 0:
c, p = cost_predict_op(Xvalid, Yvalid)
costs.append(c)
e = error_rate(Yvalid, p)
print(
"i:", i,
"j:", j,
"nb:", n_batches,
"train cost:", train_c,
"cost:", c,
"error rate:", e
)
if show_fig:
plt.plot(costs)
plt.show()
def forward(self, X):
Z = X
for c in self.convpool_layers:
Z = c.forward(Z)
Z = Z.flatten(ndim=2)
for h in self.hidden_layers:
Z = h.forward(Z)
return T.nnet.softmax(Z.dot(self.W) + self.b)
def th_predict(self, X):
pY = self.forward(X)
return T.argmax(pY, axis=1)
def main():
Xtrain, Ytrain, Xvalid, Yvalid = getImageData()
model = CNN(
convpool_layer_sizes=[(20, 5, 5), (20, 5, 5)],
hidden_layer_sizes=[500, 300],
)
model.fit(Xtrain, Ytrain, Xvalid, Yvalid)
if __name__ == '__main__':
main()