Skip to content

Commit 590892c

Browse files
committedMar 4, 2012
Merge pull request jaberg#5 from nouiz/conv
Conv (Thanks @nouiz)
2 parents 0cd33f9 + 53d23ea commit 590892c

File tree

1 file changed

+70
-43
lines changed

1 file changed

+70
-43
lines changed
 

‎theano/convnet.py

+70-43
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,168 @@
1-
import time, socket
2-
from theano.tensor import lscalar, lvector, matrix, tanh, dot, grad, log, arange
1+
import socket
2+
import time
3+
4+
import numpy
5+
from numpy import asarray, random
6+
7+
from theano.tensor import lscalar, tanh, dot, grad, log, arange
38
from theano.tensor.nnet import softmax
49
from theano.tensor.nnet.conv import conv2d
5-
from theano.tensor.signal.downsample import max_pool2D
10+
from theano.tensor.signal.downsample import max_pool_2d
611
from theano import shared, function, config
7-
import numpy, theano
8-
from numpy import asarray, random
12+
913
random.seed(2344)
1014

15+
1116
def rand(*size):
1217
return asarray(random.rand(*size), dtype=config.floatX)
18+
19+
1320
def randn(*size):
1421
return asarray(random.randn(*size), dtype=config.floatX)
22+
23+
1524
def randint(size, high):
1625
return asarray(random.randint(size=size, low=0, high=high), dtype='int32')
26+
27+
1728
def zeros(*size):
1829
return numpy.zeros(size, dtype=config.floatX)
1930

20-
n_examples=1000
21-
outputs=10
22-
lr=numpy.asarray(0.01, dtype=config.floatX)
31+
32+
n_examples = 1000
33+
outputs = 10
34+
lr = numpy.asarray(0.01, dtype=config.floatX)
2335

2436
data_x = shared(randn(n_examples, 1, 32, 32))
2537
data_y = shared(randint((n_examples,), outputs))
2638

2739
si = lscalar()
2840
nsi = lscalar()
29-
sx = data_x[si:si+nsi]
30-
sy = data_y[si:si+nsi]
41+
sx = data_x[si:si + nsi]
42+
sy = data_y[si:si + nsi]
3143

32-
bmark = open("%s_convnet_%s_%s.bmark"% (socket.gethostname(), config.device, config.floatX), 'w')
44+
bmark = open("%s_convnet_%s_%s.bmark" % (socket.gethostname(),
45+
config.device, config.floatX), 'w')
3346

3447
if config.floatX == 'float32':
3548
prec = 'float'
3649
else:
3750
prec = 'double'
3851

52+
3953
def reportmodel(model, batchsize, v):
4054
bmark.write("%s\t" % model)
4155
bmark.write("theano{%s/%s/%i}\t" % (
4256
config.device[0], prec, batchsize))
43-
bmark.write("%.2f\n"%v)
57+
bmark.write("%.2f\n" % v)
58+
4459

4560
def eval_and_report(train, name, batchsizes, N=n_examples):
4661
for bs in batchsizes:
47-
assert N % bs == 0 # can't be cheatin now...
62+
assert N % bs == 0 # can't be cheatin now...
4863
t = time.time()
49-
for i in xrange(N/bs):
50-
cost = train(i*bs, bs)
51-
if not (i % (1000/bs)):
52-
print i*bs, cost
53-
reportmodel(name, bs, N/(time.time()-t))
64+
for i in xrange(N / bs):
65+
cost = train(i * bs, bs)
66+
if not (i % (1000 / bs)):
67+
print i * bs, cost
68+
reportmodel(name, bs, N / (time.time() - t))
69+
5470

5571
def bench_ConvSmall(batchsize):
56-
data_x.value = randn(n_examples, 1, 32, 32)
72+
data_x.set_value(randn(n_examples, 1, 32, 32))
5773
w0 = shared(rand(6, 1, 5, 5) * numpy.sqrt(6 / (25.)))
5874
b0 = shared(zeros(6))
5975
w1 = shared(rand(16, 6, 5, 5) * numpy.sqrt(6 / (25.)))
6076
b1 = shared(zeros(16))
61-
vv = shared(rand(16*5*5, 120) * numpy.sqrt(6.0/16./25))
77+
vv = shared(rand(16 * 5 * 5, 120) * numpy.sqrt(6.0 / 16. / 25))
6278
cc = shared(zeros(120))
6379
v = shared(zeros(120, outputs))
6480
c = shared(zeros(outputs))
6581
params = [w0, b0, w1, b1, v, c, vv, cc]
6682

67-
c0 = tanh(conv2d(sx, w0, image_shape=(batchsize, 1, 32, 32), filter_shape=(6, 1, 5, 5)) + b0.dimshuffle(0, 'x', 'x'))
68-
s0 = tanh(max_pool2D(c0, (2,2))) # this is not the correct leNet5 model, but it's closer to
83+
c0 = tanh(conv2d(sx, w0, image_shape=(batchsize, 1, 32, 32),
84+
filter_shape=(6, 1, 5, 5)) + b0.dimshuffle(0, 'x', 'x'))
85+
# this is not the correct leNet5 model, but it's closer to
86+
s0 = tanh(max_pool_2d(c0, (2, 2)))
6987

70-
c1 = tanh(conv2d(s0, w1, image_shape=(batchsize, 6, 14, 14), filter_shape=(16,6,5,5)) + b1.dimshuffle(0, 'x', 'x'))
71-
s1 = tanh(max_pool2D(c1, (2,2)))
88+
c1 = tanh(conv2d(s0, w1, image_shape=(batchsize, 6, 14, 14),
89+
filter_shape=(16, 6, 5, 5)) +
90+
b1.dimshuffle(0, 'x', 'x'))
91+
s1 = tanh(max_pool_2d(c1, (2, 2)))
7292

73-
p_y_given_x = softmax(dot(tanh(dot(s1.flatten(2), vv)+cc), v)+c)
93+
p_y_given_x = softmax(dot(tanh(dot(s1.flatten(2), vv) + cc), v) + c)
7494
nll = -log(p_y_given_x)[arange(sy.shape[0]), sy]
7595
cost = nll.mean()
7696

7797
gparams = grad(cost, params)
7898

7999
train = function([si, nsi], cost,
80-
updates=[(p,p-lr*gp) for p,gp in zip(params, gparams)])
100+
updates=[(p, p - lr * gp) for p, gp in zip(params, gparams)])
81101

82102
eval_and_report(train, "ConvSmall", [batchsize], N=600)
83103

104+
84105
def bench_ConvMed(batchsize):
85-
data_x.value = randn(n_examples, 1, 96, 96)
106+
data_x.set_value(randn(n_examples, 1, 96, 96))
86107
w0 = shared(rand(6, 1, 7, 7) * numpy.sqrt(6 / (25.)))
87108
b0 = shared(zeros(6))
88109
w1 = shared(rand(16, 6, 7, 7) * numpy.sqrt(6 / (25.)))
89110
b1 = shared(zeros(16))
90-
vv = shared(rand(16*8*8, 120) * numpy.sqrt(6.0/16./25))
111+
vv = shared(rand(16 * 8 * 8, 120) * numpy.sqrt(6.0 / 16. / 25))
91112
cc = shared(zeros(120))
92113
v = shared(zeros(120, outputs))
93114
c = shared(zeros(outputs))
94115
params = [w0, b0, w1, b1, v, c, vv, cc]
95116

96-
c0 = tanh(conv2d(sx, w0, image_shape=(batchsize, 1, 96, 96), filter_shape=(6,1,7,7)) + b0.dimshuffle(0, 'x', 'x'))
97-
s0 = tanh(max_pool2D(c0, (3,3))) # this is not the correct leNet5 model, but it's closer to
117+
c0 = tanh(conv2d(sx, w0, image_shape=(batchsize, 1, 96, 96),
118+
filter_shape=(6, 1, 7, 7)) + b0.dimshuffle(0, 'x', 'x'))
119+
# this is not the correct leNet5 model, but it's closer to
120+
s0 = tanh(max_pool_2d(c0, (3, 3)))
98121

99-
c1 = tanh(conv2d(s0, w1, image_shape=(batchsize, 6, 30, 30), filter_shape=(16,6,7,7)) + b1.dimshuffle(0, 'x', 'x'))
100-
s1 = tanh(max_pool2D(c1, (3,3)))
122+
c1 = tanh(conv2d(s0, w1, image_shape=(batchsize, 6, 30, 30),
123+
filter_shape=(16, 6, 7, 7)) + b1.dimshuffle(0, 'x', 'x'))
124+
s1 = tanh(max_pool_2d(c1, (3, 3)))
101125

102-
p_y_given_x = softmax(dot(tanh(dot(s1.flatten(2), vv)+cc), v)+c)
126+
p_y_given_x = softmax(dot(tanh(dot(s1.flatten(2), vv) + cc), v) + c)
103127
nll = -log(p_y_given_x)[arange(sy.shape[0]), sy]
104128
cost = nll.mean()
105129

106130
gparams = grad(cost, params)
107131

108132
train = function([si, nsi], cost,
109-
updates=[(p,p-lr*gp) for p,gp in zip(params, gparams)])
133+
updates=[(p, p - lr * gp) for p, gp in zip(params, gparams)])
110134
eval_and_report(train, "ConvMed", [batchsize], N=120)
111135

136+
112137
def bench_ConvLarge(batchsize):
113-
data_x.value = randn(n_examples, 1, 256, 256)
138+
data_x.set_value(randn(n_examples, 1, 256, 256))
114139
w0 = shared(rand(6, 1, 7, 7) * numpy.sqrt(6 / (25.)))
115140
b0 = shared(zeros(6))
116141
w1 = shared(rand(16, 6, 7, 7) * numpy.sqrt(6 / (25.)))
117142
b1 = shared(zeros(16))
118-
vv = shared(rand(16*11*11, 120) * numpy.sqrt(6.0/16./25))
143+
vv = shared(rand(16 * 11 * 11, 120) * numpy.sqrt(6.0 / 16. / 25))
119144
cc = shared(zeros(120))
120145
v = shared(zeros(120, outputs))
121146
c = shared(zeros(outputs))
122147
params = [w0, b0, w1, b1, v, c, vv, cc]
123148

124-
c0 = tanh(conv2d(sx, w0, image_shape=(batchsize, 1, 256, 256), filter_shape=(6,1,7,7)) + b0.dimshuffle(0, 'x', 'x'))
125-
s0 = tanh(max_pool2D(c0, (5,5))) # this is not the correct leNet5 model, but it's closer to
149+
c0 = tanh(conv2d(sx, w0, image_shape=(batchsize, 1, 256, 256),
150+
filter_shape=(6, 1, 7, 7)) + b0.dimshuffle(0, 'x', 'x'))
151+
# this is not the correct leNet5 model, but it's closer to
152+
s0 = tanh(max_pool_2d(c0, (5, 5)))
126153

127-
c1 = tanh(conv2d(s0, w1, image_shape=(batchsize, 6, 50, 50), filter_shape=(16,6,7,7)) + b1.dimshuffle(0, 'x', 'x'))
128-
s1 = tanh(max_pool2D(c1, (4,4)))
154+
c1 = tanh(conv2d(s0, w1, image_shape=(batchsize, 6, 50, 50),
155+
filter_shape=(16, 6, 7, 7)) + b1.dimshuffle(0, 'x', 'x'))
156+
s1 = tanh(max_pool_2d(c1, (4, 4)))
129157

130-
p_y_given_x = softmax(dot(tanh(dot(s1.flatten(2), vv)+cc), v)+c)
158+
p_y_given_x = softmax(dot(tanh(dot(s1.flatten(2), vv) + cc), v) + c)
131159
nll = -log(p_y_given_x)[arange(sy.shape[0]), sy]
132160
cost = nll.mean()
133161

134162
gparams = grad(cost, params)
135163

136164
train = function([si, nsi], cost,
137-
updates=[(p,p-lr*gp) for p,gp in zip(params, gparams)])
165+
updates=[(p, p - lr * gp) for p, gp in zip(params, gparams)])
138166
eval_and_report(train, "ConvLarge", [batchsize], N=120)
139167

140168
if __name__ == '__main__':
@@ -144,4 +172,3 @@ def bench_ConvLarge(batchsize):
144172
bench_ConvMed(60)
145173
bench_ConvLarge(1)
146174
bench_ConvLarge(60)
147-

0 commit comments

Comments
 (0)
Please sign in to comment.