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
3
8
from theano .tensor .nnet import softmax
4
9
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
6
11
from theano import shared , function , config
7
- import numpy , theano
8
- from numpy import asarray , random
12
+
9
13
random .seed (2344 )
10
14
15
+
11
16
def rand (* size ):
12
17
return asarray (random .rand (* size ), dtype = config .floatX )
18
+
19
+
13
20
def randn (* size ):
14
21
return asarray (random .randn (* size ), dtype = config .floatX )
22
+
23
+
15
24
def randint (size , high ):
16
25
return asarray (random .randint (size = size , low = 0 , high = high ), dtype = 'int32' )
26
+
27
+
17
28
def zeros (* size ):
18
29
return numpy .zeros (size , dtype = config .floatX )
19
30
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 )
23
35
24
36
data_x = shared (randn (n_examples , 1 , 32 , 32 ))
25
37
data_y = shared (randint ((n_examples ,), outputs ))
26
38
27
39
si = lscalar ()
28
40
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 ]
31
43
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' )
33
46
34
47
if config .floatX == 'float32' :
35
48
prec = 'float'
36
49
else :
37
50
prec = 'double'
38
51
52
+
39
53
def reportmodel (model , batchsize , v ):
40
54
bmark .write ("%s\t " % model )
41
55
bmark .write ("theano{%s/%s/%i}\t " % (
42
56
config .device [0 ], prec , batchsize ))
43
- bmark .write ("%.2f\n " % v )
57
+ bmark .write ("%.2f\n " % v )
58
+
44
59
45
60
def eval_and_report (train , name , batchsizes , N = n_examples ):
46
61
for bs in batchsizes :
47
- assert N % bs == 0 # can't be cheatin now...
62
+ assert N % bs == 0 # can't be cheatin now...
48
63
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
+
54
70
55
71
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 ) )
57
73
w0 = shared (rand (6 , 1 , 5 , 5 ) * numpy .sqrt (6 / (25. )))
58
74
b0 = shared (zeros (6 ))
59
75
w1 = shared (rand (16 , 6 , 5 , 5 ) * numpy .sqrt (6 / (25. )))
60
76
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 ))
62
78
cc = shared (zeros (120 ))
63
79
v = shared (zeros (120 , outputs ))
64
80
c = shared (zeros (outputs ))
65
81
params = [w0 , b0 , w1 , b1 , v , c , vv , cc ]
66
82
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 )))
69
87
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 )))
72
92
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 )
74
94
nll = - log (p_y_given_x )[arange (sy .shape [0 ]), sy ]
75
95
cost = nll .mean ()
76
96
77
97
gparams = grad (cost , params )
78
98
79
99
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 )])
81
101
82
102
eval_and_report (train , "ConvSmall" , [batchsize ], N = 600 )
83
103
104
+
84
105
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 ) )
86
107
w0 = shared (rand (6 , 1 , 7 , 7 ) * numpy .sqrt (6 / (25. )))
87
108
b0 = shared (zeros (6 ))
88
109
w1 = shared (rand (16 , 6 , 7 , 7 ) * numpy .sqrt (6 / (25. )))
89
110
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 ))
91
112
cc = shared (zeros (120 ))
92
113
v = shared (zeros (120 , outputs ))
93
114
c = shared (zeros (outputs ))
94
115
params = [w0 , b0 , w1 , b1 , v , c , vv , cc ]
95
116
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 )))
98
121
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 )))
101
125
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 )
103
127
nll = - log (p_y_given_x )[arange (sy .shape [0 ]), sy ]
104
128
cost = nll .mean ()
105
129
106
130
gparams = grad (cost , params )
107
131
108
132
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 )])
110
134
eval_and_report (train , "ConvMed" , [batchsize ], N = 120 )
111
135
136
+
112
137
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 ) )
114
139
w0 = shared (rand (6 , 1 , 7 , 7 ) * numpy .sqrt (6 / (25. )))
115
140
b0 = shared (zeros (6 ))
116
141
w1 = shared (rand (16 , 6 , 7 , 7 ) * numpy .sqrt (6 / (25. )))
117
142
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 ))
119
144
cc = shared (zeros (120 ))
120
145
v = shared (zeros (120 , outputs ))
121
146
c = shared (zeros (outputs ))
122
147
params = [w0 , b0 , w1 , b1 , v , c , vv , cc ]
123
148
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 )))
126
153
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 )))
129
157
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 )
131
159
nll = - log (p_y_given_x )[arange (sy .shape [0 ]), sy ]
132
160
cost = nll .mean ()
133
161
134
162
gparams = grad (cost , params )
135
163
136
164
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 )])
138
166
eval_and_report (train , "ConvLarge" , [batchsize ], N = 120 )
139
167
140
168
if __name__ == '__main__' :
@@ -144,4 +172,3 @@ def bench_ConvLarge(batchsize):
144
172
bench_ConvMed (60 )
145
173
bench_ConvLarge (1 )
146
174
bench_ConvLarge (60 )
147
-
0 commit comments