def sgd_da(corruption_level): # Initialize RNGs rng = numpy.random.RandomState(123) theano_rng = RandomStreams(rng.randint(2 ** 30)) # Build the logistic regression class # Images in MNIST are 28*28, there are 10 output classes da = AutoEncoder( numpy_rng=rng, theano_rng=theano_rng, input=x, n_visible=28 * 28, n_hidden=500 ) # Cost to minimize cost = da.loss(corruption_level=corruption_level) # Stochastic Gradient descent updates = simple_sgd(cost, da.params, learning_rate) train_da = theano.function( inputs=[index], outputs=cost, updates=updates, givens=[ (x, train_set_x[index * batch_size: (index + 1) * batch_size]), ] ) ################ # TRAIN MODEL # ################ start_time = timeit.default_timer() print("... Training the model") for epoch in range(n_epochs): c = [] for batch_index in range(n_train_batches): c.append(train_da(batch_index)) print('Training epoch {0}, cost {1}'.format(epoch, numpy.mean(c))) end_time = timeit.default_timer() training_time = (end_time - start_time) print(('The {0} corruption code for file ' + os.path.split(__file__)[1] + ' ran for {1:.2f}m').format(corruption_level * 100, (training_time) / 60.)) image = Image.fromarray( tile_raster_images(X=da.W.get_value(borrow=True).T, img_shape=(28, 28), tile_shape=(10, 10), tile_spacing=(1, 1)) ) image.save('filters_corrpution_' + str(int(corruption_level * 100)) + '.png')
def sgd_da(corruption_level): # Initialize RNGs rng = numpy.random.RandomState(123) theano_rng = RandomStreams(rng.randint(2**30)) # Build the logistic regression class # Images in MNIST are 28*28, there are 10 output classes da = AutoEncoder(numpy_rng=rng, theano_rng=theano_rng, input=x, n_visible=28 * 28, n_hidden=500) # Cost to minimize cost = da.loss(corruption_level=corruption_level) # Stochastic Gradient descent updates = simple_sgd(cost, da.params, learning_rate) train_da = theano.function( inputs=[index], outputs=cost, updates=updates, givens=[ (x, train_set_x[index * batch_size:(index + 1) * batch_size]), ]) ################ # TRAIN MODEL # ################ start_time = timeit.default_timer() print("... Training the model") for epoch in range(n_epochs): c = [] for batch_index in range(n_train_batches): c.append(train_da(batch_index)) print('Training epoch {0}, cost {1}'.format(epoch, numpy.mean(c))) end_time = timeit.default_timer() training_time = (end_time - start_time) print( ('The {0} corruption code for file ' + os.path.split(__file__)[1] + ' ran for {1:.2f}m').format(corruption_level * 100, (training_time) / 60.)) image = Image.fromarray( tile_raster_images(X=da.W.get_value(borrow=True).T, img_shape=(28, 28), tile_shape=(10, 10), tile_spacing=(1, 1))) image.save('filters_corrpution_' + str(int(corruption_level * 100)) + '.png')