GeezerVison is showing github.com — In the ReStructuredText (*.txt) files, many of the copies of Python c… · hdubey/DeepLearningTutorials@1d8b9fd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | code/*.pyc | | 2 | +code/tmp* | 2 | 3 | code/midi | 3 | 4 | data/mnist.pkl.gz | 4 | 5 | data/mnist_py3k.pkl.gz | |
Collapse file Copy file name to clipboardExpand all lines: code/SdA.py +51-37Lines changed: 51 additions & 37 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,9 +57,14 @@ class SdA(object): | 57 | 57 | the dAs are only used to initialize the weights. | 58 | 58 | """ | 59 | 59 |
| 60 | | - def __init__(self, numpy_rng, theano_rng=None, n_ins=784, | 61 | | - hidden_layers_sizes=[500, 500], n_outs=10, | 62 | | - corruption_levels=[0.1, 0.1]): | | 60 | + def __init__(self, | | 61 | + numpy_rng, | | 62 | + theano_rng = None, | | 63 | + n_ins = 784, | | 64 | + hidden_layers_sizes = [500, 500], | | 65 | + n_outs = 10, | | 66 | + corruption_levels = [0.1, 0.1] | | 67 | + ): | 63 | 68 | """ This class is made to support a variable number of layers. | 64 | 69 |
| 65 | 70 | :type numpy_rng: numpy.random.RandomState | @@ -241,9 +246,9 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate): | 241 | 246 | (test_set_x, test_set_y) = datasets[2] | 242 | 247 |
| 243 | 248 | # compute number of minibatches for training, validation and testing | 244 | | - n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] | | 249 | + n_valid_batches = valid_set_x.get_value(borrow = True).shape[0] | 245 | 250 | n_valid_batches /= batch_size | 246 | | - n_test_batches = test_set_x.get_value(borrow=True).shape[0] | | 251 | + n_test_batches = test_set_x.get_value(borrow = True).shape[0] | 247 | 252 | n_test_batches /= batch_size | 248 | 253 |
| 249 | 254 | index = T.lscalar('index') # index to a [mini]batch | @@ -252,35 +257,41 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate): | 252 | 257 | gparams = T.grad(self.finetune_cost, self.params) | 253 | 258 |
| 254 | 259 | # compute list of fine-tuning updates | 255 | | - updates = [] | 256 | | - for param, gparam in zip(self.params, gparams): | 257 | | - updates.append((param, param - gparam * learning_rate)) | 258 | | - | 259 | | - train_fn = theano.function(inputs=[index], | 260 | | - outputs=self.finetune_cost, | 261 | | - updates=updates, | 262 | | - givens={ | 263 | | - self.x: train_set_x[index * batch_size: | 264 | | - (index + 1) * batch_size], | 265 | | - self.y: train_set_y[index * batch_size: | 266 | | - (index + 1) * batch_size]}, | 267 | | - name='train') | 268 | | - | 269 | | - test_score_i = theano.function([index], self.errors, | 270 | | - givens={ | 271 | | - self.x: test_set_x[index * batch_size: | 272 | | - (index + 1) * batch_size], | 273 | | - self.y: test_set_y[index * batch_size: | 274 | | - (index + 1) * batch_size]}, | 275 | | - name='test') | 276 | | - | 277 | | - valid_score_i = theano.function([index], self.errors, | 278 | | - givens={ | 279 | | - self.x: valid_set_x[index * batch_size: | 280 | | - (index + 1) * batch_size], | 281 | | - self.y: valid_set_y[index * batch_size: | 282 | | - (index + 1) * batch_size]}, | 283 | | - name='valid') | | 260 | + updates = [ | | 261 | + (param, param - gparam * learning_rate) | | 262 | + for param, gparam in zip(self.params, gparams) | | 263 | + ] | | 264 | + | | 265 | + train_fn = theano.function( | | 266 | + inputs = [index], | | 267 | + outputs = self.finetune_cost, | | 268 | + updates = updates, | | 269 | + givens = { | | 270 | + self.x: train_set_x[index * batch_size : (index + 1) * batch_size], | | 271 | + self.y: train_set_y[index * batch_size : (index + 1) * batch_size] | | 272 | + }, | | 273 | + name = 'train' | | 274 | + ) | | 275 | + | | 276 | + test_score_i = theano.function( | | 277 | + [index], | | 278 | + self.errors, | | 279 | + givens = { | | 280 | + self.x: test_set_x[index * batch_size : (index + 1) * batch_size], | | 281 | + self.y: test_set_y[index * batch_size : (index + 1) * batch_size] | | 282 | + }, | | 283 | + name = 'test' | | 284 | + ) | | 285 | + | | 286 | + valid_score_i = theano.function( | | 287 | + [index], | | 288 | + self.errors, | | 289 | + givens = { | | 290 | + self.x: valid_set_x[index * batch_size : (index + 1) * batch_size], | | 291 | + self.y: valid_set_y[index * batch_size : (index + 1) * batch_size] | | 292 | + }, | | 293 | + name='valid' | | 294 | + ) | 284 | 295 |
| 285 | 296 | # Create a function that scans the entire validation set | 286 | 297 | def valid_score(): | @@ -333,9 +344,12 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15, | 333 | 344 | numpy_rng = numpy.random.RandomState(89677) | 334 | 345 | print '... building the model' | 335 | 346 | # construct the stacked denoising autoencoder class | 336 | | - sda = SdA(numpy_rng=numpy_rng, n_ins=28 * 28, | 337 | | - hidden_layers_sizes=[1000, 1000, 1000], | 338 | | - n_outs=10) | | 347 | + sda = SdA( | | 348 | + numpy_rng = numpy_rng, | | 349 | + n_ins = 28 * 28, | | 350 | + hidden_layers_sizes = [1000, 1000, 1000], | | 351 | + n_outs = 10 | | 352 | + ) | 339 | 353 |
| 340 | 354 | ######################### | 341 | 355 | # PRETRAINING THE MODEL # | |
Collapse file Copy file name to clipboardExpand all lines: code/convolutional_mlp.py +74-37Lines changed: 74 additions & 37 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -76,22 +76,32 @@ def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)): | 76 | 76 | numpy.prod(poolsize)) | 77 | 77 | # initialize weights with random weights | 78 | 78 | W_bound = numpy.sqrt(6. / (fan_in + fan_out)) | 79 | | - self.W = theano.shared(numpy.asarray( | 80 | | - rng.uniform(low=-W_bound, high=W_bound, size=filter_shape), | 81 | | - dtype=theano.config.floatX), | 82 | | - borrow=True) | | 79 | + self.W = theano.shared( | | 80 | + numpy.asarray( | | 81 | + rng.uniform(low = -W_bound, high = W_bound, size = filter_shape), | | 82 | + dtype = theano.config.floatX | | 83 | + ), | | 84 | + borrow = True | | 85 | + ) | 83 | 86 |
| 84 | 87 | # the bias is a 1D tensor -- one bias per output feature map | 85 | 88 | b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX) | 86 | 89 | self.b = theano.shared(value=b_values, borrow=True) | 87 | 90 |
| 88 | 91 | # convolve input feature maps with filters | 89 | | - conv_out = conv.conv2d(input=input, filters=self.W, | 90 | | - filter_shape=filter_shape, image_shape=image_shape) | | 92 | + conv_out = conv.conv2d( | | 93 | + input = input, | | 94 | + filters = self.W, | | 95 | + filter_shape = filter_shape, | | 96 | + image_shape = image_shape | | 97 | + ) | 91 | 98 |
| 92 | 99 | # downsample each feature map individually, using maxpooling | 93 | | - pooled_out = downsample.max_pool_2d(input=conv_out, | 94 | | - ds=poolsize, ignore_border=True) | | 100 | + pooled_out = downsample.max_pool_2d( | | 101 | + input = conv_out, | | 102 | + ds = poolsize, | | 103 | + ignore_border = True | | 104 | + ) | 95 | 105 |
| 96 | 106 | # add the bias term. Since the bias is a vector (1D array), we first | 97 | 107 | # reshape it to a tensor of shape (1,n_filters,1,1). Each bias will | @@ -131,9 +141,9 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 131 | 141 | test_set_x, test_set_y = datasets[2] | 132 | 142 |
| 133 | 143 | # compute number of minibatches for training, validation and testing | 134 | | - n_train_batches = train_set_x.get_value(borrow=True).shape[0] | 135 | | - n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] | 136 | | - n_test_batches = test_set_x.get_value(borrow=True).shape[0] | | 144 | + n_train_batches = train_set_x.get_value(borrow = True).shape[0] | | 145 | + n_valid_batches = valid_set_x.get_value(borrow = True).shape[0] | | 146 | + n_test_batches = test_set_x.get_value(borrow = True).shape[0] | 137 | 147 | n_train_batches /= batch_size | 138 | 148 | n_valid_batches /= batch_size | 139 | 149 | n_test_batches /= batch_size | @@ -159,43 +169,64 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 159 | 169 | # filtering reduces the image size to (28-5+1,28-5+1)=(24,24) | 160 | 170 | # maxpooling reduces this further to (24/2,24/2) = (12,12) | 161 | 171 | # 4D output tensor is thus of shape (batch_size,nkerns[0],12,12) | 162 | | - layer0 = LeNetConvPoolLayer(rng, input=layer0_input, | 163 | | - image_shape=(batch_size, 1, 28, 28), | 164 | | - filter_shape=(nkerns[0], 1, 5, 5), poolsize=(2, 2)) | | 172 | + layer0 = LeNetConvPoolLayer( | | 173 | + rng, | | 174 | + input = layer0_input, | | 175 | + image_shape = (batch_size, 1, 28, 28), | | 176 | + filter_shape = (nkerns[0], 1, 5, 5), | | 177 | + poolsize = (2, 2) | | 178 | + ) | 165 | 179 |
| 166 | 180 | # Construct the second convolutional pooling layer | 167 | 181 | # filtering reduces the image size to (12-5+1,12-5+1)=(8,8) | 168 | 182 | # maxpooling reduces this further to (8/2,8/2) = (4,4) | 169 | 183 | # 4D output tensor is thus of shape (nkerns[0],nkerns[1],4,4) | 170 | | - layer1 = LeNetConvPoolLayer(rng, input=layer0.output, | 171 | | - image_shape=(batch_size, nkerns[0], 12, 12), | 172 | | - filter_shape=(nkerns[1], nkerns[0], 5, 5), poolsize=(2, 2)) | | 184 | + layer1 = LeNetConvPoolLayer( | | 185 | + rng, | | 186 | + input = layer0.output, | | 187 | + image_shape = (batch_size, nkerns[0], 12, 12), | | 188 | + filter_shape = (nkerns[1], nkerns[0], 5, 5), | | 189 | + poolsize = (2, 2) | | 190 | + ) | 173 | 191 |
| 174 | 192 | # the HiddenLayer being fully-connected, it operates on 2D matrices of | 175 | 193 | # shape (batch_size,num_pixels) (i.e matrix of rasterized images). | 176 | 194 | # This will generate a matrix of shape (20,32*4*4) = (20,512) | 177 | 195 | layer2_input = layer1.output.flatten(2) | 178 | 196 |
| 179 | 197 | # construct a fully-connected sigmoidal layer | 180 | | - layer2 = HiddenLayer(rng, input=layer2_input, n_in=nkerns[1] * 4 * 4, | 181 | | - n_out=500, activation=T.tanh) | | 198 | + layer2 = HiddenLayer( | | 199 | + rng, | | 200 | + input = layer2_input, | | 201 | + n_in = nkerns[1] * 4 * 4, | | 202 | + n_out = 500, | | 203 | + activation = T.tanh | | 204 | + ) | 182 | 205 |
| 183 | 206 | # classify the values of the fully-connected sigmoidal layer | 184 | | - layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=10) | | 207 | + layer3 = LogisticRegression(input = layer2.output, n_in = 500, n_out = 10) | 185 | 208 |
| 186 | 209 | # the cost we minimize during training is the NLL of the model | 187 | 210 | cost = layer3.negative_log_likelihood(y) | 188 | 211 |
| 189 | 212 | # create a function to compute the mistakes that are made by the model | 190 | | - test_model = theano.function([index], layer3.errors(y), | 191 | | - givens={ | 192 | | - x: test_set_x[index * batch_size: (index + 1) * batch_size], | 193 | | - y: test_set_y[index * batch_size: (index + 1) * batch_size]}) | 194 | | - | 195 | | - validate_model = theano.function([index], layer3.errors(y), | 196 | | - givens={ | 197 | | - x: valid_set_x[index * batch_size: (index + 1) * batch_size], | 198 | | - y: valid_set_y[index * batch_size: (index + 1) * batch_size]}) | | 213 | + test_model = theano.function( | | 214 | + [index], | | 215 | + layer3.errors(y), | | 216 | + givens = { | | 217 | + x: test_set_x[index * batch_size : (index + 1) * batch_size], | | 218 | + y: test_set_y[index * batch_size : (index + 1) * batch_size] | | 219 | + } | | 220 | + ) | | 221 | + | | 222 | + validate_model = theano.function( | | 223 | + [index], | | 224 | + layer3.errors(y), | | 225 | + givens = { | | 226 | + x: valid_set_x[index * batch_size : (index + 1) * batch_size], | | 227 | + y: valid_set_y[index * batch_size : (index + 1) * batch_size] | | 228 | + } | | 229 | + ) | 199 | 230 |
| 200 | 231 | # create a list of all model parameters to be fit by gradient descent | 201 | 232 | params = layer3.params + layer2.params + layer1.params + layer0.params | @@ -208,14 +239,20 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 208 | 239 | # manually create an update rule for each model parameter. We thus | 209 | 240 | # create the updates list by automatically looping over all | 210 | 241 | # (params[i],grads[i]) pairs. | 211 | | - updates = [] | 212 | | - for param_i, grad_i in zip(params, grads): | 213 | | - updates.append((param_i, param_i - learning_rate * grad_i)) | 214 | | - | 215 | | - train_model = theano.function([index], cost, updates=updates, | 216 | | - givens={ | 217 | | - x: train_set_x[index * batch_size: (index + 1) * batch_size], | 218 | | - y: train_set_y[index * batch_size: (index + 1) * batch_size]}) | | 242 | + updates = [ | | 243 | + (param_i, param_i - learning_rate * grad_i) | | 244 | + for param_i, grad_i in zip(params, grads) | | 245 | + ] | | 246 | + | | 247 | + train_model = theano.function( | | 248 | + [index], | | 249 | + cost, | | 250 | + updates = updates, | | 251 | + givens = { | | 252 | + x: train_set_x[index * batch_size : (index + 1) * batch_size], | | 253 | + y: train_set_y[index * batch_size : (index + 1) * batch_size] | | 254 | + } | | 255 | + ) | 219 | 256 |
| 220 | 257 | ############### | 221 | 258 | # TRAIN MODEL # | |
Collapse file Copy file name to clipboardExpand all lines: code/dA.py +48-20Lines changed: 48 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,9 +75,17 @@ class dA(object): | 75 | 75 |
| 76 | 76 | """ | 77 | 77 |
| 78 | | - def __init__(self, numpy_rng, theano_rng=None, input=None, | 79 | | - n_visible=784, n_hidden=500, | 80 | | - W=None, bhid=None, bvis=None): | | 78 | + def __init__( | | 79 | + self, | | 80 | + numpy_rng, | | 81 | + theano_rng = None, | | 82 | + input = None, | | 83 | + n_visible = 784, | | 84 | + n_hidden = 500, | | 85 | + W = None, | | 86 | + bhid = None, | | 87 | + bvis = None | | 88 | + ): | 81 | 89 | """ | 82 | 90 | Initialize the dA class by specifying the number of visible units (the | 83 | 91 | dimension d of the input ), the number of hidden units ( the dimension | @@ -232,9 +240,10 @@ def get_cost_updates(self, corruption_level, learning_rate): | 232 | 240 | # to its parameters | 233 | 241 | gparams = T.grad(cost, self.params) | 234 | 242 | # generate the list of updates | 235 | | - updates = [] | 236 | | - for param, gparam in zip(self.params, gparams): | 237 | | - updates.append((param, param - learning_rate * gparam)) | | 243 | + updates = [ | | 244 | + (param, param - learning_rate * gparam) | | 245 | + for param, gparam in zip(self.params, gparams) | | 246 | + ] | 238 | 247 |
| 239 | 248 | return (cost, updates) | 240 | 249 |
| @@ -277,15 +286,27 @@ def test_dA(learning_rate=0.1, training_epochs=15, | 277 | 286 | rng = numpy.random.RandomState(123) | 278 | 287 | theano_rng = RandomStreams(rng.randint(2 ** 30)) | 279 | 288 |
| 280 | | - da = dA(numpy_rng=rng, theano_rng=theano_rng, input=x, | 281 | | - n_visible=28 * 28, n_hidden=500) | 282 | | - | 283 | | - cost, updates = da.get_cost_updates(corruption_level=0., | 284 | | - learning_rate=learning_rate) | 285 | | - | 286 | | - train_da = theano.function([index], cost, updates=updates, | 287 | | - givens={x: train_set_x[index * batch_size: | 288 | | - (index + 1) * batch_size]}) | | 289 | + da = dA( | | 290 | + numpy_rng = rng, | | 291 | + theano_rng = theano_rng, | | 292 | + input = x, | | 293 | + n_visible = 28 * 28, | | 294 | + n_hidden = 500 | | 295 | + ) | | 296 | + | | 297 | + cost, updates = da.get_cost_updates( | | 298 | + corruption_level=0., | | 299 | + learning_rate=learning_rate | | 300 | + ) | | 301 | + | | 302 | + train_da = theano.function( | | 303 | + [index], | | 304 | + cost, | | 305 | + updates = updates, | | 306 | + givens = { | | 307 | + x: train_set_x[index * batch_size : (index + 1) * batch_size] | | 308 | + } | | 309 | + ) | 289 | 310 |
| 290 | 311 | start_time = time.clock() | 291 | 312 |
| @@ -322,11 +343,18 @@ def test_dA(learning_rate=0.1, training_epochs=15, | 322 | 343 | rng = numpy.random.RandomState(123) | 323 | 344 | theano_rng = RandomStreams(rng.randint(2 ** 30)) | 324 | 345 |
| 325 | | - da = dA(numpy_rng=rng, theano_rng=theano_rng, input=x, | 326 | | - n_visible=28 * 28, n_hidden=500) | 327 | | - | 328 | | - cost, updates = da.get_cost_updates(corruption_level=0.3, | 329 | | - learning_rate=learning_rate) | | 346 | + da = dA( | | 347 | + numpy_rng = rng, | | 348 | + theano_rng = theano_rng, | | 349 | + input = x, | | 350 | + n_visible = 28 * 28, | | 351 | + n_hidden = 500 | | 352 | + ) | | 353 | + | | 354 | + cost, updates = da.get_cost_updates( | | 355 | + corruption_level = 0.3, | | 356 | + learning_rate=learning_rate | | 357 | + ) | 330 | 358 |
| 331 | 359 | train_da = theano.function([index], cost, updates=updates, | 332 | 360 | givens={x: train_set_x[index * batch_size: | |
|