def initialize(self, learning_rate=0.1, corruption_level = 0.0): """docstring for build_model_0""" minibatch_index = Tensor.lscalar('minibatch_index') inputs = Tensor.matrix('denoising_autoencoder_inputs') rng = numpy.random.RandomState(123) theano_rng = RandomStreams(rng.randint(2 ** 30)) self.classifier = DenoisingAutoencoder( numpy_rng=rng, theano_rng=theano_rng, n_visible=28 * 28, n_hidden=500 ) self.training_function = self.compiled_training_function( self.classifier, minibatch_index, inputs, learning_rate, corruption_level ) image = Image.fromarray(tile_raster_images(X=self.classifier.W.get_value(borrow=True).T, img_shape=(28, 28), tile_shape=(10, 10), tile_spacing=(1, 1))) image.save('filters_corruption_0.png')
def visualize_data(data, iteration, patchdim, image_shape=None): # Use function we wrote previously out_image = utilities.tile_raster_images(data.T, patchdim, image_shape) plt.imshow(out_image, cmap=cm.Greys_r) savepath_image = "vis_data" + "_iterations_" + str(iteration) + ".png" plt.savefig(savepath_image) plt.close() return
def visualize_data(data,iteration,patchdim,image_shape=None): #Use function we wrote previously out_image = utilities.tile_raster_images(data.T,patchdim,image_shape,tile_spacing=(2,2)) plt.imshow(out_image) plt.colorbar() savepath_image= 'vis_data'+ '_iterations_' + str(iteration) + '.png' plt.savefig(savepath_image) plt.close() return
def visualize_basis(self,iteration,image_shape=None): #Use function we wrote previously tmp = self.basis.get_value() out_image = utilities.tile_raster_images(tmp.T,self.patchdim,image_shape) plt.imshow(out_image,cmap=cm.Greys_r) savepath_image= 'vis_basis'+ '_iterations_' + str(iteration) + '.png' plt.savefig(savepath_image) plt.close() return
def sample(self): """docstring for sample""" ################################# # Sampling from the RBM # ################################# # find out the number of test samples number_of_test_samples = self.dataset.test_set_input.get_value(borrow=True).shape[0] # pick random test examples, with which to initialize the persistent chain test_idx = rng.randint(number_of_test_samples - n_chains) persistent_vis_chain = theano.shared(numpy.asarray( self.dataset.test_set_input.get_value(borrow=True)[test_idx:test_idx + n_chains], dtype=theano.config.floatX)) plot_every = 1000 # define one step of Gibbs sampling (mf = mean-field) define a # function that does `plot_every` steps before returning the # sample for plotting [presig_hids, hid_mfs, hid_samples, presig_vis, vis_mfs, vis_samples], updates = \ theano.scan(self.classifier.gibbs_vhv, outputs_info=[None, None, None, None, None, persistent_vis_chain], n_steps=plot_every) # add to updates the shared variable that takes care of our persistent # chain :. updates.update({persistent_vis_chain: vis_samples[-1]}) # construct the function that implements our persistent chain. # we generate the "mean field" activations for plotting and the actual # samples for reinitializing the state of our persistent chain sample_fn = theano.function([], [vis_mfs[-1], vis_samples[-1]], updates=updates, name='sample_fn') # create a space to store the image for plotting ( we need to leave # room for the tile_spacing as well) image_data = numpy.zeros((29 * n_samples + 1, 29 * n_chains - 1), dtype='uint8') for idx in xrange(n_samples): # generate `plot_every` intermediate samples that we discard, # because successive samples in the chain are too correlated vis_mf, vis_sample = sample_fn() image_data[29 * idx:29 * idx + 28, :] = tile_raster_images( X=vis_mf, img_shape=(28, 28), tile_shape=(1, n_chains), tile_spacing=(1, 1)) # construct image image = Image.fromarray(image_data) image.save('samples.png') os.chdir('../')
dataset.load() rbm = RestrictedBoltzmannMachineTrainer(dataset) if not os.path.isdir('rbm_plots'): os.makedirs('rbm_plots') os.chdir('rbm_plots') rbm.initialize() start_time = time.clock() plotting_time = 0 state = rbm.start_training() while rbm.continue_training(state): plotting_start = time.clock() image = Image.fromarray( tile_raster_images( X=state.classifier.weights.get_value(borrow = True).T, img_shape=(28, 28), tile_shape=(10, 10), tile_spacing=(1, 1) ) ) image.save('filters_at_epoch_%i.png' % state.epoch) plotting_stop = time.clock() plotting_time += (plotting_stop - plotting_start) end_time = time.clock() pretraining_time = (end_time - start_time) - plotting_time print ('Training took %f minutes' % (pretraining_time / 60.))