GeezerVison is showing github.com — Minimized lines > 79 characters, trailing whitespace, and whitespace … · mpage/DeepLearningTutorials@e3f9cd2
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 +25-13Lines changed: 25 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,11 +58,11 @@ class SdA(object): | 58 | 58 | """ | 59 | 59 |
| 60 | 60 | def __init__( | 61 | | - self, | 62 | | - numpy_rng, | 63 | | - theano_rng=None, | | 61 | + self, | | 62 | + numpy_rng, | | 63 | + theano_rng=None, | 64 | 64 | n_ins=784, | 65 | | - hidden_layers_sizes=[500, 500], | | 65 | + hidden_layers_sizes=[500, 500], | 66 | 66 | n_outs=10, | 67 | 67 | corruption_levels=[0.1, 0.1] | 68 | 68 | ): | @@ -268,28 +268,40 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate): | 268 | 268 | outputs=self.finetune_cost, | 269 | 269 | updates=updates, | 270 | 270 | givens={ | 271 | | - self.x: train_set_x[index * batch_size : (index + 1) * batch_size], | 272 | | - self.y: train_set_y[index * batch_size : (index + 1) * batch_size] | | 271 | + self.x: train_set_x[ | | 272 | + index * batch_size: (index + 1) * batch_size | | 273 | + ], | | 274 | + self.y: train_set_y[ | | 275 | + index * batch_size: (index + 1) * batch_size | | 276 | + ] | 273 | 277 | }, | 274 | 278 | name='train' | 275 | 279 | ) | 276 | 280 |
| 277 | 281 | test_score_i = theano.function( | 278 | | - [index], | | 282 | + [index], | 279 | 283 | self.errors, | 280 | 284 | givens={ | 281 | | - self.x: test_set_x[index * batch_size : (index + 1) * batch_size], | 282 | | - self.y: test_set_y[index * batch_size : (index + 1) * batch_size] | | 285 | + self.x: test_set_x[ | | 286 | + index * batch_size: (index + 1) * batch_size | | 287 | + ], | | 288 | + self.y: test_set_y[ | | 289 | + index * batch_size: (index + 1) * batch_size | | 290 | + ] | 283 | 291 | }, | 284 | 292 | name='test' | 285 | 293 | ) | 286 | 294 |
| 287 | 295 | valid_score_i = theano.function( | 288 | | - [index], | | 296 | + [index], | 289 | 297 | self.errors, | 290 | 298 | givens={ | 291 | | - self.x: valid_set_x[index * batch_size : (index + 1) * batch_size], | 292 | | - self.y: valid_set_y[index * batch_size : (index + 1) * batch_size] | | 299 | + self.x: valid_set_x[ | | 300 | + index * batch_size: (index + 1) * batch_size | | 301 | + ], | | 302 | + self.y: valid_set_y[ | | 303 | + index * batch_size: (index + 1) * batch_size | | 304 | + ] | 293 | 305 | }, | 294 | 306 | name='valid' | 295 | 307 | ) | @@ -346,7 +358,7 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15, | 346 | 358 | print '... building the model' | 347 | 359 | # construct the stacked denoising autoencoder class | 348 | 360 | sda = SdA( | 349 | | - numpy_rng=numpy_rng, | | 361 | + numpy_rng=numpy_rng, | 350 | 362 | n_ins=28 * 28, | 351 | 363 | hidden_layers_sizes=[1000, 1000, 1000], | 352 | 364 | n_outs=10 | |
Collapse file Copy file name to clipboardExpand all lines: code/cA.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | squared Frobenius norm of the Jacobian of the hidden mapping h with | 13 | 13 | respect to the visible units yields the contractive auto-encoder: | 14 | 14 |
| 15 | | - - \sum_{k=1}^d[ x_k \log z_k + (1-x_k) \log( 1-z_k)] + \| \frac{\partial h(x)}{\partial x} \|^2 | | 15 | + - \sum_{k=1}^d[ x_k \log z_k + (1-x_k) \log( 1-z_k)] + \| \frac{\partial h(x)}{\partial x} \|^2 | 16 | 16 |
| 17 | 17 | References : | 18 | 18 | - S. Rifai, P. Vincent, X. Muller, X. Glorot, Y. Bengio: Contractive | |
Collapse file Copy file name to clipboardExpand all lines: code/convolutional_mlp.py +25-22Lines changed: 25 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -90,16 +90,16 @@ 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, | | 93 | + input=input, | 94 | 94 | filters=self.W, | 95 | | - filter_shape=filter_shape, | | 95 | + filter_shape=filter_shape, | 96 | 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 | 101 | input=conv_out, | 102 | | - ds=poolsize, | | 102 | + ds=poolsize, | 103 | 103 | ignore_border=True | 104 | 104 | ) | 105 | 105 |
| @@ -170,7 +170,7 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 170 | 170 | # maxpooling reduces this further to (24/2,24/2) = (12,12) | 171 | 171 | # 4D output tensor is thus of shape (batch_size,nkerns[0],12,12) | 172 | 172 | layer0 = LeNetConvPoolLayer( | 173 | | - rng, | | 173 | + rng, | 174 | 174 | input=layer0_input, | 175 | 175 | image_shape=(batch_size, 1, 28, 28), | 176 | 176 | filter_shape=(nkerns[0], 1, 5, 5), | @@ -182,10 +182,10 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 182 | 182 | # maxpooling reduces this further to (8/2,8/2) = (4,4) | 183 | 183 | # 4D output tensor is thus of shape (nkerns[0],nkerns[1],4,4) | 184 | 184 | layer1 = LeNetConvPoolLayer( | 185 | | - rng, | | 185 | + rng, | 186 | 186 | input=layer0.output, | 187 | 187 | image_shape=(batch_size, nkerns[0], 12, 12), | 188 | | - filter_shape=(nkerns[1], nkerns[0], 5, 5), | | 188 | + filter_shape=(nkerns[1], nkerns[0], 5, 5), | 189 | 189 | poolsize=(2, 2) | 190 | 190 | ) | 191 | 191 |
| @@ -196,10 +196,10 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 196 | 196 |
| 197 | 197 | # construct a fully-connected sigmoidal layer | 198 | 198 | layer2 = HiddenLayer( | 199 | | - rng, | 200 | | - input=layer2_input, | | 199 | + rng, | | 200 | + input=layer2_input, | 201 | 201 | n_in=nkerns[1] * 4 * 4, | 202 | | - n_out=500, | | 202 | + n_out=500, | 203 | 203 | activation=T.tanh | 204 | 204 | ) | 205 | 205 |
| @@ -211,20 +211,20 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 211 | 211 |
| 212 | 212 | # create a function to compute the mistakes that are made by the model | 213 | 213 | test_model = theano.function( | 214 | | - [index], | | 214 | + [index], | 215 | 215 | layer3.errors(y), | 216 | 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] | | 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 | 219 | } | 220 | 220 | ) | 221 | 221 |
| 222 | 222 | validate_model = theano.function( | 223 | | - [index], | | 223 | + [index], | 224 | 224 | layer3.errors(y), | 225 | 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] | | 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 | 228 | } | 229 | 229 | ) | 230 | 230 |
| @@ -245,12 +245,12 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 245 | 245 | ] | 246 | 246 |
| 247 | 247 | train_model = theano.function( | 248 | | - [index], | 249 | | - cost, | | 248 | + [index], | | 249 | + cost, | 250 | 250 | updates=updates, | 251 | 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] | | 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 | 254 | } | 255 | 255 | ) | 256 | 256 |
| @@ -312,10 +312,13 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 312 | 312 | best_iter = iter | 313 | 313 |
| 314 | 314 | # test it on the test set | 315 | | - test_losses = [test_model(i) for i in xrange(n_test_batches)] | | 315 | + test_losses = [ | | 316 | + test_model(i) | | 317 | + for i in xrange(n_test_batches) | | 318 | + ] | 316 | 319 | test_score = numpy.mean(test_losses) | 317 | | - print((' epoch %i, minibatch %i/%i, test error of best ' | 318 | | - 'model %f %%') % | | 320 | + print((' epoch %i, minibatch %i/%i, test error of ' | | 321 | + 'best model %f %%') % | 319 | 322 | (epoch, minibatch_index + 1, n_train_batches, | 320 | 323 | test_score * 100.)) | 321 | 324 |
| |
Collapse file Copy file name to clipboardExpand all lines: code/dA.py +15-15Lines changed: 15 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -76,14 +76,14 @@ class dA(object): | 76 | 76 | """ | 77 | 77 |
| 78 | 78 | def __init__( | 79 | | - self, | 80 | | - numpy_rng, | 81 | | - theano_rng=None, | | 79 | + self, | | 80 | + numpy_rng, | | 81 | + theano_rng=None, | 82 | 82 | input=None, | 83 | | - n_visible=784, | | 83 | + n_visible=784, | 84 | 84 | n_hidden=500, | 85 | | - W=None, | 86 | | - bhid=None, | | 85 | + W=None, | | 86 | + bhid=None, | 87 | 87 | bvis=None | 88 | 88 | ): | 89 | 89 | """ | @@ -287,10 +287,10 @@ 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, | | 290 | + numpy_rng=rng, | | 291 | + theano_rng=theano_rng, | 292 | 292 | input=x, | 293 | | - n_visible=28 * 28, | | 293 | + n_visible=28 * 28, | 294 | 294 | n_hidden=500 | 295 | 295 | ) | 296 | 296 |
| @@ -300,11 +300,11 @@ def test_dA(learning_rate=0.1, training_epochs=15, | 300 | 300 | ) | 301 | 301 |
| 302 | 302 | train_da = theano.function( | 303 | | - [index], | 304 | | - cost, | | 303 | + [index], | | 304 | + cost, | 305 | 305 | updates=updates, | 306 | 306 | givens={ | 307 | | - x: train_set_x[index * batch_size : (index + 1) * batch_size] | | 307 | + x: train_set_x[index * batch_size: (index + 1) * batch_size] | 308 | 308 | } | 309 | 309 | ) | 310 | 310 |
| @@ -344,10 +344,10 @@ 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, | | 347 | + numpy_rng=rng, | | 348 | + theano_rng=theano_rng, | 349 | 349 | input=x, | 350 | | - n_visible=28 * 28, | | 350 | + n_visible=28 * 28, | 351 | 351 | n_hidden=500 | 352 | 352 | ) | 353 | 353 |
| |
Collapse file Copy file name to clipboardExpand all lines: code/logistic_sgd.py +25-15Lines changed: 25 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -79,7 +79,7 @@ def __init__(self, input, n_in, n_out): | 79 | 79 | (n_in, n_out), | 80 | 80 | dtype=theano.config.floatX | 81 | 81 | ), | 82 | | - name='W', | | 82 | + name='W', | 83 | 83 | borrow=True | 84 | 84 | ) | 85 | 85 | # initialize the baises b as a vector of n_out 0s | @@ -88,19 +88,22 @@ def __init__(self, input, n_in, n_out): | 88 | 88 | (n_out,), | 89 | 89 | dtype=theano.config.floatX | 90 | 90 | ), | 91 | | - name='b', | | 91 | + name='b', | 92 | 92 | borrow=True | 93 | 93 | ) | 94 | 94 |
| 95 | | - # symbolic expression for computing the matrix of class-membership probabilities | | 95 | + # symbolic expression for computing the matrix of class-membership | | 96 | + # probabilities | 96 | 97 | # Where: | 97 | | - # W is a matrix where column-k represent the separation hyper plain for class-k | | 98 | + # W is a matrix where column-k represent the separation hyper plain for | | 99 | + # class-k | 98 | 100 | # x is a matrix where row-j represents input training sample-j | 99 | | - # b is a vector where element-k represent the free parameter of hyper plain-k | | 101 | + # b is a vector where element-k represent the free parameter of hyper | | 102 | + # plain-k | 100 | 103 | self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b) | 101 | 104 |
| 102 | | - # symbolic description of how to compute prediction as class whose probability | 103 | | - # is maximal | | 105 | + # symbolic description of how to compute prediction as class whose | | 106 | + # probability is maximal | 104 | 107 | self.y_pred = T.argmax(self.p_y_given_x, axis=1) | 105 | 108 |
| 106 | 109 | # parameters of the model | @@ -173,13 +176,20 @@ def load_data(dataset): | 173 | 176 | data_dir, data_file = os.path.split(dataset) | 174 | 177 | if data_dir == "" and not os.path.isfile(dataset): | 175 | 178 | # Check if dataset is in the data directory. | 176 | | - new_path = os.path.join(os.path.split(__file__)[0], "..", "data", dataset) | | 179 | + new_path = os.path.join( | | 180 | + os.path.split(__file__)[0], | | 181 | + "..", | | 182 | + "data", | | 183 | + dataset | | 184 | + ) | 177 | 185 | if os.path.isfile(new_path) or data_file == 'mnist.pkl.gz': | 178 | 186 | dataset = new_path | 179 | 187 |
| 180 | 188 | if (not os.path.isfile(dataset)) and data_file == 'mnist.pkl.gz': | 181 | 189 | import urllib | 182 | | - origin = 'http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz' | | 190 | + origin = ( | | 191 | + 'http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz' | | 192 | + ) | 183 | 193 | print 'Downloading data from %s' % origin | 184 | 194 | urllib.urlretrieve(origin, dataset) | 185 | 195 |
| @@ -289,17 +299,17 @@ def sgd_optimization_mnist(learning_rate=0.13, n_epochs=1000, | 289 | 299 | inputs=[index], | 290 | 300 | outputs=classifier.errors(y), | 291 | 301 | givens={ | 292 | | - x: test_set_x[index * batch_size : (index + 1) * batch_size], | 293 | | - y: test_set_y[index * batch_size : (index + 1) * batch_size] | | 302 | + x: test_set_x[index * batch_size: (index + 1) * batch_size], | | 303 | + y: test_set_y[index * batch_size: (index + 1) * batch_size] | 294 | 304 | } | 295 | 305 | ) | 296 | 306 |
| 297 | 307 | validate_model = theano.function( | 298 | 308 | inputs=[index], | 299 | 309 | outputs=classifier.errors(y), | 300 | 310 | givens={ | 301 | | - x: valid_set_x[index * batch_size : (index + 1) * batch_size], | 302 | | - y: valid_set_y[index * batch_size : (index + 1) * batch_size] | | 311 | + x: valid_set_x[index * batch_size: (index + 1) * batch_size], | | 312 | + y: valid_set_y[index * batch_size: (index + 1) * batch_size] | 303 | 313 | } | 304 | 314 | ) | 305 | 315 |
| @@ -320,8 +330,8 @@ def sgd_optimization_mnist(learning_rate=0.13, n_epochs=1000, | 320 | 330 | outputs=cost, | 321 | 331 | updates=updates, | 322 | 332 | givens={ | 323 | | - x: train_set_x[index * batch_size : (index + 1) * batch_size], | 324 | | - y: train_set_y[index * batch_size : (index + 1) * batch_size] | | 333 | + x: train_set_x[index * batch_size: (index + 1) * batch_size], | | 334 | + y: train_set_y[index * batch_size: (index + 1) * batch_size] | 325 | 335 | } | 326 | 336 | ) | 327 | 337 |
| |
|