-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNN.py
199 lines (182 loc) · 6.04 KB
/
CNN.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
import theano as th
from theano import tensor as T
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
import numpy as np
from theano.tensor.nnet.conv import conv2d
from theano.tensor.signal.pool import pool_2d
from theano.ifelse import ifelse
from compression import compressbatch as cb
from compression import printimage as pi
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
srng = RandomStreams()
def inner_i(data):
result,updates=th.scan(fn=lambda i:ifelse(T.gt(T.cast(i,'float64'),0.),1.,0.),
outputs_info=None,
sequences=data)
return result
def maximizing(data):
result,updates=th.scan(fn=lambda i:inner_i(i),
outputs_info=None,
sequences=data)
return result
def find(data):
result,updates=th.scan(fn=lambda i:maximizing(i),
outputs_info=None,
sequences=data)
return result
def find_val(data):
data2=0.00001+data
data=data*data2
val0=data.shape[0]
val1=data.shape[1]
val2=data.shape[2]
return T.sum(data)/(val0*val1*val2)
def scan(b1):
result,updates=th.scan(fn=lambda past:T.concatenate([past.reshape((-1,1)),past.reshape((-1,1))]),
outputs_info=None,
sequences=b1)
return result.reshape((1,-1))
def expandimage(b1):
result,updates=th.scan(fn=lambda past:T.concatenate([scan(past),scan(past)]),
outputs_info=None,
sequences=b1)
return result.reshape((2*b1.shape[0],-1))
#y=scan(a).reshape((1,10))
def expandlayers(image):
result,updates=th.scan(fn=lambda value:expandimage(value),
outputs_info=None,
sequences=image)
return result
def check(X,i,j,a,b):
k=T.sum(X[:,i:i+a,j:j+b])
return k
def inner(image,ii,a,b):
result,updates=th.scan(fn=lambda j:check(image,ii,j,a,b),
outputs_info=None,
sequences=T.arange(image.shape[2]-b+1))
return result
def outer(image,a,b):
result,updates=th.scan(fn=lambda i:inner(image,i,a,b),
outputs_info=None,
sequences=T.arange(image.shape[1]-a+1))
y=T.argmax(result)
m=y/(image.shape[1]-a+1)
n=y%(image.shape[1]-b+1)
z=image[:,m:m+a,n:n+b]
return z
def change(image,val,d1,d2):
val2=find_val(image)
a=image.shape[1]
b=image.shape[2]
return ifelse(T.lt(T.cast(val,'float64'),T.cast(val2,'float64')),outer(image,d1,d2),pool_2d(image,(2,2)))
def innerbatch(layers):
result,updates=th.scan(fn=lambda i:change(i),
outputs_info=None,
sequences=layers)
return result
def magnify(batch,d1,d2):
val=find_val(batch)
result,updates=th.scan(fn=lambda i:change(i,val,d1,d2),
outputs_info=None,
sequences=batch)
return result
def magnify2(batch,val):
result,updates=th.scan(fn=lambda i:change(i,val),
outputs_info=None,
sequences=batch)
return result
def find_threshold(images):
result,updates=th.scan(fn=lambda i:find_val(i),
outputs_info=None,
sequences=images)
return T.sum(result)/images.shape[0]
def floatX(X):
return np.asarray(X, dtype=th.config.floatX)
def init_weights(shape):
return th.shared(floatX(np.random.randn(*shape) * 0.01))
def rectify(X):
return T.maximum(X, 0.)
def softmax(X):
e_x = T.exp(X - X.max(axis=1).dimshuffle(0, 'x'))
return e_x / e_x.sum(axis=1).dimshuffle(0, 'x')
def dropout(X, p=0.):
if p > 0:
retain_prob = 1 - p
X *= srng.binomial(X.shape, p=retain_prob, dtype=th.config.floatX)
X /= retain_prob
return X
def RMSprop(cost, params, lr=0.001, rho=0.9, epsilon=1e-6):
grads = T.grad(cost=cost, wrt=params)
updates = []
for p, g in zip(params, grads):
acc = th.shared(p.get_value() * 0.)
acc_new = rho * acc + (1 - rho) * g ** 2
gradient_scaling = T.sqrt(acc_new + epsilon)
g = g / gradient_scaling
updates.append((acc, acc_new))
updates.append((p, p - lr * g))
return updates
def find_val_np(data):
data2=0.00001+data
data=data*data2
val0=data.shape[0]
val1=data.shape[1]
val2=data.shape[2]
return np.sum(data)/(val0*val1*val2)
def find_threshold_np(data):
val=0.
for i in data:
val+=find_val_np(i)
return val/data.shape[0]
def model(X, w, w2,w4, p_drop_conv, p_drop_hidden):
l1a = rectify(conv2d(T.cast(X,'float64'), T.cast(w,'float64')))
#l1=magnify(l1a,13,13)
l1 = pool_2d(l1a, (2, 2))
l1 = dropout(l1, p_drop_conv)
l2a = rectify(conv2d(T.cast(l1,'float64'), T.cast(w2,'float64')))
#l2=magnify(l2a,6,6)
l2 = pool_2d(l2a, (2, 2))
l2 = dropout(l2, p_drop_conv)
#l3a = rectify(conv2d(T.cast(l2,'float64'), T.cast(w3,'float64')))
#l3b = pool_2d(l3a, (2, 2))
l3 = T.flatten(l2, outdim=2)
#l3 = dropout(l3, p_drop_conv)
l4 = rectify(T.dot(l3, w4))
l4 = dropout(l4, p_drop_hidden)
pyx = softmax(T.dot(l4, w_o))
return l1, l2, l3, l4, pyx
X = T.ftensor4()
Y = T.fmatrix()
V =T.fscalar()
w = init_weights((32, 1, 3, 3))
w2 = init_weights((64, 32, 3, 3))
#w3 = init_weights((128, 64, 3, 3))
w4 = init_weights((64 * 6 * 6, 625))
w_o = init_weights((625, 10))
noise_l1, noise_l2, noise_l3, noise_l4, noise_py_x = model(X, w, w2,w4, 0.2, 0.5)
l1, l2, l3, l4, py_x = model(X, w, w2, w4, 0., 0.)
y_x = T.argmax(py_x, axis=1)
cost = T.mean(T.nnet.categorical_crossentropy(noise_py_x, Y))
params = [w, w2,w4, w_o]
updates = RMSprop(cost, params, lr=0.01)
train = th.function(inputs=[X, Y], outputs=cost, updates=updates, allow_input_downcast=True)
predict = th.function(inputs=[X], outputs=y_x, allow_input_downcast=True)
print "Started training"
for i in range(10):
print "building data trX"
trX=np.asarray(cb(mnist.train.images),dtype=th.config.floatX)
print "building data teX"
teX=np.asarray(cb(mnist.test.images),dtype=th.config.floatX)
trY=np.asarray(mnist.train.labels,dtype=th.config.floatX)
teY=np.asarray(mnist.test.labels,dtype=th.config.floatX)
print trX.shape
trX = trX.reshape(-1, 1, 28, 28)
teX = teX.reshape(-1, 1, 28, 28)
print i
val1=find_threshold_np(trX)
val2=find_threshold_np(teX)
for start, end in zip(range(0, len(trX), 100), range(100, len(trX), 100)):
print start,end
cost = train(trX[start:end], trY[start:end])
print np.mean(np.argmax(teY, axis=1)== predict(teX))