GeezerVison is showing github.com — Ran code through pep8 and fixed all occurrences of E251 unexpected sp… · hdubey/DeepLearningTutorials@3f46135
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: code/SdA.py +19-19Lines changed: 19 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,11 +59,11 @@ class SdA(object): | 59 | 59 |
| 60 | 60 | def __init__(self, | 61 | 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] | | 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 | 67 | ): | 68 | 68 | """ This class is made to support a variable number of layers. | 69 | 69 |
| @@ -246,9 +246,9 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate): | 246 | 246 | (test_set_x, test_set_y) = datasets[2] | 247 | 247 |
| 248 | 248 | # compute number of minibatches for training, validation and testing | 249 | | - 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] | 250 | 250 | n_valid_batches /= batch_size | 251 | | - 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] | 252 | 252 | n_test_batches /= batch_size | 253 | 253 |
| 254 | 254 | index = T.lscalar('index') # index to a [mini]batch | @@ -263,30 +263,30 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate): | 263 | 263 | ] | 264 | 264 |
| 265 | 265 | train_fn = theano.function( | 266 | | - inputs = [index], | 267 | | - outputs = self.finetune_cost, | 268 | | - updates = updates, | 269 | | - givens = { | | 266 | + inputs=[index], | | 267 | + outputs=self.finetune_cost, | | 268 | + updates=updates, | | 269 | + givens={ | 270 | 270 | self.x: train_set_x[index * batch_size : (index + 1) * batch_size], | 271 | 271 | self.y: train_set_y[index * batch_size : (index + 1) * batch_size] | 272 | 272 | }, | 273 | | - name = 'train' | | 273 | + name='train' | 274 | 274 | ) | 275 | 275 |
| 276 | 276 | test_score_i = theano.function( | 277 | 277 | [index], | 278 | 278 | self.errors, | 279 | | - givens = { | | 279 | + givens={ | 280 | 280 | self.x: test_set_x[index * batch_size : (index + 1) * batch_size], | 281 | 281 | self.y: test_set_y[index * batch_size : (index + 1) * batch_size] | 282 | 282 | }, | 283 | | - name = 'test' | | 283 | + name='test' | 284 | 284 | ) | 285 | 285 |
| 286 | 286 | valid_score_i = theano.function( | 287 | 287 | [index], | 288 | 288 | self.errors, | 289 | | - givens = { | | 289 | + givens={ | 290 | 290 | self.x: valid_set_x[index * batch_size : (index + 1) * batch_size], | 291 | 291 | self.y: valid_set_y[index * batch_size : (index + 1) * batch_size] | 292 | 292 | }, | @@ -345,10 +345,10 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15, | 345 | 345 | print '... building the model' | 346 | 346 | # construct the stacked denoising autoencoder class | 347 | 347 | sda = SdA( | 348 | | - numpy_rng = numpy_rng, | 349 | | - n_ins = 28 * 28, | 350 | | - hidden_layers_sizes = [1000, 1000, 1000], | 351 | | - n_outs = 10 | | 348 | + numpy_rng=numpy_rng, | | 349 | + n_ins=28 * 28, | | 350 | + hidden_layers_sizes=[1000, 1000, 1000], | | 351 | + n_outs=10 | 352 | 352 | ) | 353 | 353 |
| 354 | 354 | ######################### | |
Collapse file Copy file name to clipboardExpand all lines: code/convolutional_mlp.py +30-30Lines changed: 30 additions & 30 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -78,10 +78,10 @@ def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)): | 78 | 78 | W_bound = numpy.sqrt(6. / (fan_in + fan_out)) | 79 | 79 | self.W = theano.shared( | 80 | 80 | numpy.asarray( | 81 | | - rng.uniform(low = -W_bound, high = W_bound, size = filter_shape), | 82 | | - dtype = theano.config.floatX | | 81 | + rng.uniform(low=-W_bound, high=W_bound, size=filter_shape), | | 82 | + dtype=theano.config.floatX | 83 | 83 | ), | 84 | | - borrow = True | | 84 | + borrow=True | 85 | 85 | ) | 86 | 86 |
| 87 | 87 | # the bias is a 1D tensor -- one bias per output feature map | @@ -90,17 +90,17 @@ def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)): | 90 | 90 |
| 91 | 91 | # convolve input feature maps with filters | 92 | 92 | conv_out = conv.conv2d( | 93 | | - input = input, | 94 | | - filters = self.W, | 95 | | - filter_shape = filter_shape, | 96 | | - image_shape = image_shape | | 93 | + input=input, | | 94 | + filters=self.W, | | 95 | + filter_shape=filter_shape, | | 96 | + image_shape=image_shape | 97 | 97 | ) | 98 | 98 |
| 99 | 99 | # downsample each feature map individually, using maxpooling | 100 | 100 | pooled_out = downsample.max_pool_2d( | 101 | | - input = conv_out, | 102 | | - ds = poolsize, | 103 | | - ignore_border = True | | 101 | + input=conv_out, | | 102 | + ds=poolsize, | | 103 | + ignore_border=True | 104 | 104 | ) | 105 | 105 |
| 106 | 106 | # add the bias term. Since the bias is a vector (1D array), we first | @@ -141,9 +141,9 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 141 | 141 | test_set_x, test_set_y = datasets[2] | 142 | 142 |
| 143 | 143 | # compute number of minibatches for training, validation and testing | 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] | | 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] | 147 | 147 | n_train_batches /= batch_size | 148 | 148 | n_valid_batches /= batch_size | 149 | 149 | n_test_batches /= batch_size | @@ -171,10 +171,10 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 171 | 171 | # 4D output tensor is thus of shape (batch_size,nkerns[0],12,12) | 172 | 172 | layer0 = LeNetConvPoolLayer( | 173 | 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) | | 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 | 178 | ) | 179 | 179 |
| 180 | 180 | # Construct the second convolutional pooling layer | @@ -183,10 +183,10 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 183 | 183 | # 4D output tensor is thus of shape (nkerns[0],nkerns[1],4,4) | 184 | 184 | layer1 = LeNetConvPoolLayer( | 185 | 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) | | 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 | 190 | ) | 191 | 191 |
| 192 | 192 | # the HiddenLayer being fully-connected, it operates on 2D matrices of | @@ -197,14 +197,14 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 197 | 197 | # construct a fully-connected sigmoidal layer | 198 | 198 | layer2 = HiddenLayer( | 199 | 199 | rng, | 200 | | - input = layer2_input, | 201 | | - n_in = nkerns[1] * 4 * 4, | 202 | | - n_out = 500, | 203 | | - activation = T.tanh | | 200 | + input=layer2_input, | | 201 | + n_in=nkerns[1] * 4 * 4, | | 202 | + n_out=500, | | 203 | + activation=T.tanh | 204 | 204 | ) | 205 | 205 |
| 206 | 206 | # classify the values of the fully-connected sigmoidal layer | 207 | | - layer3 = LogisticRegression(input = layer2.output, n_in = 500, n_out = 10) | | 207 | + layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=10) | 208 | 208 |
| 209 | 209 | # the cost we minimize during training is the NLL of the model | 210 | 210 | cost = layer3.negative_log_likelihood(y) | @@ -213,7 +213,7 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 213 | 213 | test_model = theano.function( | 214 | 214 | [index], | 215 | 215 | layer3.errors(y), | 216 | | - givens = { | | 216 | + givens={ | 217 | 217 | x: test_set_x[index * batch_size : (index + 1) * batch_size], | 218 | 218 | y: test_set_y[index * batch_size : (index + 1) * batch_size] | 219 | 219 | } | @@ -222,7 +222,7 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 222 | 222 | validate_model = theano.function( | 223 | 223 | [index], | 224 | 224 | layer3.errors(y), | 225 | | - givens = { | | 225 | + givens={ | 226 | 226 | x: valid_set_x[index * batch_size : (index + 1) * batch_size], | 227 | 227 | y: valid_set_y[index * batch_size : (index + 1) * batch_size] | 228 | 228 | } | @@ -247,8 +247,8 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 247 | 247 | train_model = theano.function( | 248 | 248 | [index], | 249 | 249 | cost, | 250 | | - updates = updates, | 251 | | - givens = { | | 250 | + updates=updates, | | 251 | + givens={ | 252 | 252 | x: train_set_x[index * batch_size : (index + 1) * batch_size], | 253 | 253 | y: train_set_y[index * batch_size : (index + 1) * batch_size] | 254 | 254 | } | |
Collapse file Copy file name to clipboardExpand all lines: code/dA.py +20-20Lines changed: 20 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -78,13 +78,13 @@ class dA(object): | 78 | 78 | def __init__( | 79 | 79 | self, | 80 | 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 | | 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 | 88 | ): | 89 | 89 | """ | 90 | 90 | Initialize the dA class by specifying the number of visible units (the | @@ -287,11 +287,11 @@ def test_dA(learning_rate=0.1, training_epochs=15, | 287 | 287 | theano_rng = RandomStreams(rng.randint(2 ** 30)) | 288 | 288 |
| 289 | 289 | da = dA( | 290 | | - numpy_rng = rng, | 291 | | - theano_rng = theano_rng, | 292 | | - input = x, | 293 | | - n_visible = 28 * 28, | 294 | | - n_hidden = 500 | | 290 | + numpy_rng=rng, | | 291 | + theano_rng=theano_rng, | | 292 | + input=x, | | 293 | + n_visible=28 * 28, | | 294 | + n_hidden=500 | 295 | 295 | ) | 296 | 296 |
| 297 | 297 | cost, updates = da.get_cost_updates( | @@ -302,8 +302,8 @@ def test_dA(learning_rate=0.1, training_epochs=15, | 302 | 302 | train_da = theano.function( | 303 | 303 | [index], | 304 | 304 | cost, | 305 | | - updates = updates, | 306 | | - givens = { | | 305 | + updates=updates, | | 306 | + givens={ | 307 | 307 | x: train_set_x[index * batch_size : (index + 1) * batch_size] | 308 | 308 | } | 309 | 309 | ) | @@ -344,15 +344,15 @@ def test_dA(learning_rate=0.1, training_epochs=15, | 344 | 344 | theano_rng = RandomStreams(rng.randint(2 ** 30)) | 345 | 345 |
| 346 | 346 | da = dA( | 347 | | - numpy_rng = rng, | 348 | | - theano_rng = theano_rng, | 349 | | - input = x, | 350 | | - n_visible = 28 * 28, | 351 | | - n_hidden = 500 | | 347 | + numpy_rng=rng, | | 348 | + theano_rng=theano_rng, | | 349 | + input=x, | | 350 | + n_visible=28 * 28, | | 351 | + n_hidden=500 | 352 | 352 | ) | 353 | 353 |
| 354 | 354 | cost, updates = da.get_cost_updates( | 355 | | - corruption_level = 0.3, | | 355 | + corruption_level=0.3, | 356 | 356 | learning_rate=learning_rate | 357 | 357 | ) | 358 | 358 |
| |
Collapse file Copy file name to clipboardExpand all lines: code/logistic_sgd.py +22-22Lines changed: 22 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,21 +75,21 @@ def __init__(self, input, n_in, n_out): | 75 | 75 |
| 76 | 76 | # initialize with 0 the weights W as a matrix of shape (n_in, n_out) | 77 | 77 | self.W = theano.shared( | 78 | | - value = numpy.zeros( | | 78 | + value=numpy.zeros( | 79 | 79 | (n_in, n_out), | 80 | | - dtype = theano.config.floatX | | 80 | + dtype=theano.config.floatX | 81 | 81 | ), | 82 | | - name = 'W', | 83 | | - borrow = True | | 82 | + name='W', | | 83 | + borrow=True | 84 | 84 | ) | 85 | 85 | # initialize the baises b as a vector of n_out 0s | 86 | 86 | self.b = theano.shared( | 87 | | - value = numpy.zeros( | | 87 | + value=numpy.zeros( | 88 | 88 | (n_out,), | 89 | | - dtype = theano.config.floatX | | 89 | + dtype=theano.config.floatX | 90 | 90 | ), | 91 | | - name = 'b', | 92 | | - borrow = True | | 91 | + name='b', | | 92 | + borrow=True | 93 | 93 | ) | 94 | 94 |
| 95 | 95 | # symbolic expression for computing the matrix of class-membership probabilities | @@ -101,7 +101,7 @@ def __init__(self, input, n_in, n_out): | 101 | 101 |
| 102 | 102 | # symbolic description of how to compute prediction as class whose probability | 103 | 103 | # is maximal | 104 | | - self.y_pred = T.argmax(self.p_y_given_x, axis = 1) | | 104 | + self.y_pred = T.argmax(self.p_y_given_x, axis=1) | 105 | 105 |
| 106 | 106 | # parameters of the model | 107 | 107 | self.params = [self.W, self.b] | @@ -277,7 +277,7 @@ def sgd_optimization_mnist(learning_rate=0.13, n_epochs=1000, | 277 | 277 |
| 278 | 278 | # construct the logistic regression class | 279 | 279 | # Each MNIST image has size 28*28 | 280 | | - classifier = LogisticRegression(input = x, n_in = 28 * 28, n_out = 10) | | 280 | + classifier = LogisticRegression(input=x, n_in=28 * 28, n_out=10) | 281 | 281 |
| 282 | 282 | # the cost we minimize during training is the negative log likelihood of | 283 | 283 | # the model in symbolic format | @@ -286,26 +286,26 @@ def sgd_optimization_mnist(learning_rate=0.13, n_epochs=1000, | 286 | 286 | # compiling a Theano function that computes the mistakes that are made by | 287 | 287 | # the model on a minibatch | 288 | 288 | test_model = theano.function( | 289 | | - inputs = [index], | 290 | | - outputs = classifier.errors(y), | 291 | | - givens = { | | 289 | + inputs=[index], | | 290 | + outputs=classifier.errors(y), | | 291 | + givens={ | 292 | 292 | x: test_set_x[index * batch_size : (index + 1) * batch_size], | 293 | 293 | y: test_set_y[index * batch_size : (index + 1) * batch_size] | 294 | 294 | } | 295 | 295 | ) | 296 | 296 |
| 297 | 297 | validate_model = theano.function( | 298 | | - inputs = [index], | 299 | | - outputs = classifier.errors(y), | 300 | | - givens = { | | 298 | + inputs=[index], | | 299 | + outputs=classifier.errors(y), | | 300 | + givens={ | 301 | 301 | x: valid_set_x[index * batch_size : (index + 1) * batch_size], | 302 | 302 | y: valid_set_y[index * batch_size : (index + 1) * batch_size] | 303 | 303 | } | 304 | 304 | ) | 305 | 305 |
| 306 | 306 | # compute the gradient of cost with respect to theta = (W,b) | 307 | | - g_W = T.grad(cost = cost, wrt = classifier.W) | 308 | | - g_b = T.grad(cost = cost, wrt = classifier.b) | | 307 | + g_W = T.grad(cost=cost, wrt=classifier.W) | | 308 | + g_b = T.grad(cost=cost, wrt=classifier.b) | 309 | 309 |
| 310 | 310 | # specify how to update the parameters of the model as a list of | 311 | 311 | # (variable, update expression) pairs. | @@ -316,10 +316,10 @@ def sgd_optimization_mnist(learning_rate=0.13, n_epochs=1000, | 316 | 316 | # the same time updates the parameter of the model based on the rules | 317 | 317 | # defined in `updates` | 318 | 318 | train_model = theano.function( | 319 | | - inputs = [index], | 320 | | - outputs = cost, | 321 | | - updates = updates, | 322 | | - givens = { | | 319 | + inputs=[index], | | 320 | + outputs=cost, | | 321 | + updates=updates, | | 322 | + givens={ | 323 | 323 | x: train_set_x[index * batch_size : (index + 1) * batch_size], | 324 | 324 | y: train_set_y[index * batch_size : (index + 1) * batch_size] | 325 | 325 | } | |
|