예제 #1
0
    def __init__(self, np_rng, theano_rng=None, n_ins=784,
        hidden_layer_sizes=[500, 500],
        n_outs=10,
        corruption_rates=[0.1, 0.1]):
        self.sigmoid_layers = []
        self.dA_layers = []
        self.params = []
        self.n_layers = len(hidden_layer_sizes)

        if theano_rng is None:
            theano_rng = RandomStreams(np_rng.randint(2**30))

        self.x = T.matrix('x')
        self.y = T.ivector('y')

        for i in range(self.n_layers):
            if i == 0:
                input_size = n_ins
                layer_input = self.x
            else:
                input_size = hidden_layer_sizes[i-1]
                layer_input = self.sigmoid_layers[-1].output

            hidden_layer = HiddenLayer(
                np_rng, layer_input, input_size, hidden_layer_sizes[i], sigmoid)

            self.sigmoid_layers.append(hidden_layer)
            self.params.extend(hidden_layer.params)

            # construc the denoising autoencoder layer
            da = dA(np_rng, theano_rng, layer_input, 
                n_visible=input_size, n_hidden=hidden_layer_sizes[i],
                W=hidden_layer.W, bhid=hidden_layer.b)
            self.dA_layers.append(da)

        # LogisticRegression Layer for classification
        self.logLayer = LogisticRegressionLayer(self.sigmoid_layers[-1].output, n_in=hidden_layer_sizes[-1],
            n_out=n_outs)
        
        self.params.extend(self.logLayer.params)
        self.finetune_cost = self.logLayer.negative_log_likelihood(self.y)
        self.errors = self.logLayer.errors(self.y)
예제 #2
0
 def iterative_algorithm(self,
                         name,
                         pop_size=100,
                         genome_length=20,
                         lim_percentage=20,
                         corruption_level=0.2,
                         num_epochs=50,
                         lr=0.1,
                         max_evaluations=200000,
                         unique_training=False,
                         hiddens=40,
                         rtr=True):
     self.mask = np.random.binomial(1, 0.5, genome_length)
     trials = max_evaluations / pop_size
     population_limit = int(pop_size * (lim_percentage / 100.0))
     self.dA = dA(n_visible=genome_length, n_hidden=hiddens)
     self.dA.build_dA(corruption_level)
     self.build_sample_dA()
     new_population = np.random.binomial(1, 0.5, (pop_size, genome_length))
     self.population_fitnesses = self.fitness_many(new_population)
     for iteration in range(0, trials):
         print "iteration:", iteration
         population = new_population
         self.population = new_population
         rw = self.tournament_selection_replacement(population)
         good_strings, good_strings_fitnesses = self.get_good_strings(
             population,
             population_limit,
             unique=unique_training,
             fitnesses=self.population_fitnesses)
         print "training A/E"
         training_data = np.array(good_strings)
         self.train_dA(training_data, num_epochs=num_epochs, lr=lr)
         print "sampling..."
         sampled_population = np.array(self.sample_dA(rw), "b")
         self.sample_fitnesses = self.fitness_many(sampled_population)
         if rtr:
             new_population = self.RTR(
                 population,
                 sampled_population,
                 population_fitnesses=self.population_fitnesses,
                 sample_fitnesses=self.sample_fitnesses,
                 w=pop_size / 20)
         else:
             new_population = sampled_population
             new_population[0:1] = good_strings[0:1]
             self.population_fitnesses = self.sample_fitnesses
             self.population_fitnesses[0:1] = good_strings_fitnesses[0:1]
         print "{0},{1},{2}\n".format(np.mean(self.population_fitnesses),
                                      np.min(self.population_fitnesses),
                                      np.max(self.population_fitnesses))
         print "best from previous:", (self.fitness(
             new_population[np.argmax(self.population_fitnesses)]))
         if np.max(self.population_fitnesses) == self.optimum:
             pickle.dump(
                 {
                     "pop": self.population,
                     "fitnesses": self.population_fitnesses,
                     "iteration": iteration
                 }, open("final_shit_ae.pkl", "w"))
             break
     return new_population
예제 #3
0
 def iterative_algorithm(
     self,
     name,
     pop_size=100,
     genome_length=20,
     lim_percentage=20,
     lim=20,
     trials=10,
     corruption_level=0.2,
     num_epochs=50,
     lr = 0.1,
     online_training=False,
     pickle_data=False,
     max_evaluations=200000,
     save_data=False):
     results_path = "results/autoencoder/{0}/".format(name)
     ensure_dir(results_path)
     trials = max_evaluations/pop_size
     population_limit = lim
     if lim_percentage > 0:
         population_limit = int(pop_size*(lim_percentage/100.0))
         print "population_limit:",population_limit
         print "{0}*({1}/100.0) = {2}".format(pop_size,lim_percentage,int(pop_size*(lim_percentage/100.0)))
     fitfile = open("{0}fitnesses.dat".format(results_path),"w")
     self.dA = dA(n_visible=genome_length,n_hidden=50)
     self.dA.build_dA(corruption_level)
     self.build_sample_dA()
     all_strings,good_strings=self.generate_good_strings(pop_size,genome_length,population_limit)
     self.train_dA(ar(good_strings),corruption_level=corruption_level,num_epochs=num_epochs,lr=lr,output_folder=results_path,iteration=0)
     # sampled_population = [self.sample_dA([i]) for i in self.get_new_population_rw(all_strings)]
     sampled_population = self.sample_dA(self.get_new_population_rw(all_strings))
     print "s:",sampled_population
     original_fitnesses,sample_fitnesses,differences,distances = self.get_statistics(all_strings,sampled_population)
     data = {
     "original":original_fitnesses,
     "fitnesses_sampled":sample_fitnesses,
     "differences_in_fitness":differences,
     "distances":distances
     }
     if pickle_data:
         pickle.dump(data,open("results/autoencoder/{0}_0.pkl".format(name),"wb"))
     if save_data:
         self.save_population(sampled_population,0)
         self.save_training_data(good_strings,0)
         random_pop = [self.generate_random_string(genome_length) for z in range(1000)]
         print random_pop[0]
         sampled_r_pop = self.sample_dA(random_pop)
         self.save_sampled_random_population([r for r in sampled_r_pop],0)
         self.save_random_population(random_pop,0)
         self.save_pop_fitnesses(sample_fitnesses,"fitness_pop",0)
         self.save_pop_fitnesses(self.fitness_many(good_strings),"fitness_training_data",0)
     print "writing..."
     fitfile.write("{0},{1},{2},{3}\n".format(np.mean(original_fitnesses),np.min(original_fitnesses),np.max(original_fitnesses),np.std(original_fitnesses)))
     fitfile.write("{0},{1},{2},{3}\n".format(np.mean(sample_fitnesses),np.min(sample_fitnesses),np.max(sample_fitnesses),np.std(original_fitnesses)))
     print "{0},{1},{2}\n".format(np.mean(original_fitnesses),np.min(original_fitnesses),np.max(original_fitnesses))
     print "{0},{1},{2}\n".format(np.mean(sample_fitnesses),np.min(sample_fitnesses),np.max(sample_fitnesses))
     print "writing over"
     for iteration in range(0,trials):
         print "choosing pop:"
         population = self.get_new_population_rw([z for z in sampled_population])
         print "choosing pop over"
         if online_training == False:
             print "building model..."
             self.dA = dA(n_visible=genome_length,n_hidden=50)
             self.dA.build_dA(corruption_level)
             self.build_sample_dA()
         good_strings,good_strings_fitnesses=self.get_good_strings(population,population_limit)
         for f in good_strings_fitnesses:
             print "good_strings_fitnesses:",f
         self.train_dA(ar(good_strings),corruption_level=corruption_level,num_epochs=num_epochs,lr=lr,output_folder=results_path,iteration=iteration+1)
         print "sampling..."
         sampled_population = self.sample_dA(population)
         print "sampling over"
         original_fitnesses,sample_fitnesses,differences,distances = self.get_statistics(population,sampled_population)
         data = {
         "original":original_fitnesses,
         "fitnesses_sampled":sample_fitnesses,
         "differences_in_fitness":differences,
         "distances":distances
         }
         if pickle_data:
             pickle.dump(data,open("results/autoencoder/{0}_{1}.pkl".format(name,iteration+1),"wb"))
         fitfile.write("{0},{1},{2},{3}\n".format(np.mean(sample_fitnesses),np.min(sample_fitnesses),np.max(sample_fitnesses),np.std(original_fitnesses)))
         fitfile.flush()
         print "{0},{1},{2}\n".format(np.mean(sample_fitnesses),np.min(sample_fitnesses),np.max(sample_fitnesses))
         if save_data:
             self.save_population(sampled_population,iteration+1)
             self.save_training_data(good_strings,iteration+1)
             random_pop = [self.generate_random_string(genome_length) for z in range(1000)]
             print random_pop[0]
             sampled_r_pop = self.sample_dA(random_pop)
             self.save_sampled_random_population([r for r in sampled_r_pop],iteration+1)
             self.save_random_population(random_pop,iteration+1)
             self.save_pop_fitnesses(sample_fitnesses,"fitness_pop",iteration+1)
             self.save_pop_fitnesses(good_strings_fitnesses,"fitness_training_data",iteration+1)
     fitfile.close()
    def __init__(
        self,
        numpy_rng,
        theano_rng=None,
        n_ins=784,
        hidden_layers_sizes=[500, 500],
        n_outs=10,
        corruption_levels=[0.1, 0.1],
    ):
        """ This class is made to support a variable number of layers.

        :type numpy_rng: numpy.random.RandomState
        :param numpy_rng: numpy random number generator used to draw initial
                    weights

        :type theano_rng: theano.tensor.shared_randomstreams.RandomStreams
        :param theano_rng: Theano random generator; if None is given one is
                           generated based on a seed drawn from `rng`

        :type n_ins: int
        :param n_ins: dimension of the input to the sdA

        :type n_layers_sizes: list of ints
        :param n_layers_sizes: intermediate layers size, must contain
                               at least one value

        :type n_outs: int
        :param n_outs: dimension of the output of the network

        :type corruption_levels: list of float
        :param corruption_levels: amount of corruption to use for each
                                  layer
        """

        self.sigmoid_layers = []
        self.dA_layers = []
        self.params = []
        self.n_layers = len(hidden_layers_sizes)

        assert self.n_layers > 0

        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
        # allocate symbolic variables for the data
        self.x = T.matrix("x")  # the data is presented as rasterized images
        self.y = T.ivector("y")  # the labels are presented as 1D vector of
        # [int] labels
        # end-snippet-1

        # The SdA is an MLP, for which all weights of intermediate layers
        # are shared with a different denoising autoencoders
        # We will first construct the SdA as a deep multilayer perceptron,
        # and when constructing each sigmoidal layer we also construct a
        # denoising autoencoder that shares weights with that layer
        # During pretraining we will train these autoencoders (which will
        # lead to chainging the weights of the MLP as well)
        # During finetunining we will finish training the SdA by doing
        # stochastich gradient descent on the MLP

        # start-snippet-2
        for i in xrange(self.n_layers):
            # construct the sigmoidal layer

            # the size of the input is either the number of hidden units of
            # the layer below or the input size if we are on the first layer
            if i == 0:
                input_size = n_ins
            else:
                input_size = hidden_layers_sizes[i - 1]

            # the input to this layer is either the activation of the hidden
            # layer below or the input of the SdA if you are on the first
            # layer
            if i == 0:
                layer_input = self.x
            else:
                layer_input = self.sigmoid_layers[-1].output

            sigmoid_layer = HiddenLayer(
                rng=numpy_rng,
                input=layer_input,
                n_in=input_size,
                n_out=hidden_layers_sizes[i],
                activation=T.nnet.sigmoid,
            )
            # add the layer to our list of layers
            self.sigmoid_layers.append(sigmoid_layer)
            # its arguably a philosophical question...
            # but we are going to only declare that the parameters of the
            # sigmoid_layers are parameters of the StackedDAA
            # the visible biases in the dA are parameters of those
            # dA, but not the SdA
            self.params.extend(sigmoid_layer.params)

            # Construct a denoising autoencoder that shared weights with this
            # layer
            dA_layer = dA(
                numpy_rng=numpy_rng,
                theano_rng=theano_rng,
                input=layer_input,
                n_visible=input_size,
                n_hidden=hidden_layers_sizes[i],
                W=sigmoid_layer.W,
                bhid=sigmoid_layer.b,
            )
            self.dA_layers.append(dA_layer)
        # end-snippet-2
        # We now need to add a logistic layer on top of the MLP
        self.logLayer = LogisticRegression(
            input=self.sigmoid_layers[-1].output, n_in=hidden_layers_sizes[-1], n_out=n_outs
        )

        self.params.extend(self.logLayer.params)
        # construct a function that implements one step of finetunining

        # compute the cost for second phase of training,
        # defined as the negative log likelihood
        self.finetune_cost = self.logLayer.negative_log_likelihood(self.y)
        # compute the gradients with respect to the model parameters
        # symbolic variable that points to the number of errors made on the
        # minibatch given by self.x and self.y
        self.errors = self.logLayer.errors(self.y)

        self.example = T.dvector("ex")
        self.use_fn = theano.function(inputs=[self.x], outputs=self.logLayer.p_y_given_x, name="use")
예제 #5
0
    def __init__(
        self,
        numpy_rng,
        theano_rng=None,
        n_ins=784,  # 28 * 28
        hidden_layers_sizes=[500, 500],  # 两个 denoising autoencoding,后面简称 dA
        n_outs=10,  # 最上层的 MLP 的输出
        corruption_levels=[0.1, 0.1]  # 每个 dA 的 Denoising Level
    ):
        """ This class is made to support a variable number of layers.

        :type numpy_rng: numpy.random.RandomState
        :param numpy_rng: numpy random number generator used to draw initial
                    weights

        :type theano_rng: theano.tensor.shared_randomstreams.RandomStreams
        :param theano_rng: Theano random generator; if None is given one is
                           generated based on a seed drawn from `rng`

        :type n_ins: int
        :param n_ins: dimension of the input to the sdA

        :type n_layers_sizes: list of ints
        :param n_layers_sizes: intermediate layers size, must contain
                               at least one value

        :type n_outs: int
        :param n_outs: dimension of the output of the network

        :type corruption_levels: list of float
        :param corruption_levels: amount of corruption to use for each
                                  layer
        """

        self.sigmoid_layers = []  # 只是用于构建 dA,而不是指上层的 MLP
        self.dA_layers = []
        self.params = []
        self.n_layers = len(hidden_layers_sizes)

        assert self.n_layers > 0

        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2**30))
        # allocate symbolic variables for the data
        self.x = T.matrix('x')  # the data is presented as rasterized images
        self.y = T.ivector('y')  # the labels are presented as 1D vector of
        # [int] labels
        # end-snippet-1

        # The SdA is an MLP, for which all weights of intermediate layers
        # are shared with a different denoising autoencoders
        # We will first construct the SdA as a deep multilayer perceptron,
        # and when constructing each sigmoidal layer we also construct a
        # denoising autoencoder that shares weights with that layer
        # During pretraining we will train these autoencoders (which will
        # lead to chainging the weights of the MLP as well)
        # During finetunining we will finish training the SdA by doing
        # stochastich gradient descent on the MLP

        # start-snippet-2
        for i in xrange(self.n_layers):
            # construct the sigmoidal layer

            # the size of the input is either the number of hidden units of
            # the layer below or the input size if we are on the first layer
            if i == 0:
                input_size = n_ins
            else:
                input_size = hidden_layers_sizes[i - 1]

            # the input to this layer is either the activation of the hidden
            # layer below or the input of the SdA if you are on the first
            # layer
            if i == 0:
                layer_input = self.x
            else:
                layer_input = self.sigmoid_layers[
                    -1].output  # 这就是借用 HiddenLayer 的原因

            sigmoid_layer = HiddenLayer(rng=numpy_rng,
                                        input=layer_input,
                                        n_in=input_size,
                                        n_out=hidden_layers_sizes[i],
                                        activation=T.nnet.sigmoid)
            # add the layer to our list of layers
            self.sigmoid_layers.append(sigmoid_layer)
            # its arguably a philosophical question...
            # but we are going to only declare that the parameters of the
            # sigmoid_layers are parameters of the StackedDAA
            # the visible biases in the dA are parameters of those
            # dA, but not the SdA
            self.params.extend(sigmoid_layer.params)

            # Construct a denoising autoencoder that shared weights with this
            # layer
            dA_layer = dA(
                numpy_rng=numpy_rng,
                theano_rng=theano_rng,
                input=layer_input,
                n_visible=input_size,
                n_hidden=hidden_layers_sizes[i],
                W=sigmoid_layer.W,  # 借用 W 参数
                bhid=sigmoid_layer.b)  # 借用 b 参数
            self.dA_layers.append(dA_layer)
        # end-snippet-2
        # We now need to add a logistic layer on top of the MLP, 直接接在第二个 dA 层的后面
        self.logLayer = LogReg(input=self.sigmoid_layers[-1].output,
                               n_in=hidden_layers_sizes[-1],
                               n_out=n_outs)

        self.params.extend(self.logLayer.params)
        # construct a function that implements one step of finetunining

        # compute the cost for second phase of training,
        # defined as the negative log likelihood
        self.finetune_cost = self.logLayer.negative_log_likelihood(self.y)
        # compute the gradients with respect to the model parameters
        # symbolic variable that points to the number of errors made on the
        # minibatch given by self.x and self.y
        self.errors = self.logLayer.errors(self.y)
예제 #6
0
 def iterative_algorithm(
     self,
     name,
     pop_size=100,
     genome_length=20,
     lim_percentage=20,
     corruption_level=0.2,
     num_epochs=50,
     lr = 0.1,
     max_evaluations=200000,
     unique_training=False,
     hiddens=40,
     rtr = True
     ):
     self.mask = np.random.binomial(1,0.5,genome_length)
     trials = max_evaluations/pop_size
     population_limit = int(pop_size*(lim_percentage/100.0))
     self.dA = dA(n_visible=genome_length,n_hidden=hiddens)
     self.dA.build_dA(corruption_level)
     self.build_sample_dA()
     new_population = np.random.binomial(1,0.5,(pop_size,genome_length))
     self.population_fitnesses = self.fitness_many(new_population)
     for iteration in range(0,trials):
         print "iteration:",iteration
         population = new_population
         self.population = new_population
         rw = self.tournament_selection_replacement(population)
         good_strings,good_strings_fitnesses=self.get_good_strings(
                                       population,
                                       population_limit,
                                       unique=unique_training,
                                       fitnesses=self.population_fitnesses
                                     )
         print "training A/E"
         training_data = np.array(good_strings)
         self.train_dA(training_data,
                       num_epochs=num_epochs,
                       lr=lr)
         print "sampling..."
         sampled_population = np.array(self.sample_dA(rw),"b")
         self.sample_fitnesses = self.fitness_many(sampled_population)
         if rtr:
             new_population = self.RTR(
                           population,
                           sampled_population,
                           population_fitnesses=self.population_fitnesses,
                           sample_fitnesses=self.sample_fitnesses,
                           w=pop_size/20
                           )
         else:
             new_population = sampled_population
             new_population[0:1] = good_strings[0:1]
             self.population_fitnesses = self.sample_fitnesses
             self.population_fitnesses[0:1] = good_strings_fitnesses[0:1]
         print "{0},{1},{2}\n".format(np.mean(self.population_fitnesses),
                                      np.min(self.population_fitnesses),
                                      np.max(self.population_fitnesses))
         print "best from previous:",(
           self.fitness(new_population[np.argmax(self.population_fitnesses)])
             )
         if np.max(self.population_fitnesses) == self.optimum:
             pickle.dump({"pop":self.population,"fitnesses":self.population_fitnesses,"iteration":iteration},open("final_shit_ae.pkl","w"))
             break
     return new_population
예제 #7
0
its = 10
genome_l = 100
low = -50
high = 50
mask = np.array(np.loadtxt("mask.dat"),"b")
total_rows = 17

plt.figure(figsize=(7, 12))
gs = gridspec.GridSpec(total_rows, 2,hspace=0.5)
# plt.subplots_adjust( hspace=0.4 )
for iteration in range(1,20):
	solution = np.array(np.loadtxt("rw_population_{0}.dat".format(iteration)),"b")
	params = cPickle.load(open("best_params_{0}.pickle".format(iteration)))
	n_visible = params[0].shape[0]
	n_hiddens = params[0].shape[1]
	ae = denoising_autoencoder.dA(n_visible=n_visible,n_hidden=n_hiddens,W=params[0],hbias=params[1],vbias=params[2])
	ae.build_dA(corruption_level=0.1)
	sample_func = theano.function([ae.input],ae.sample)
	reconstruct_func = theano.function([ae.input],ae.z)

	input_vector = solution[0]
	# output_probabilites = 

	ax = plt.subplot(gs[0:2,:])

	probabilities = []
	for i in range(0,300):
		input_vector=solution[i]
		r=reconstruct_func([input_vector])[0]
		r[np.where(mask==1)]=1-r[np.where(mask==1)]
		probabilities.append(r)