Exemple #1
0
def runAutoencoder():
    ds = StockPrice()
    # print ds.train[0][0]
    data = np.random.randn(10, 5).astype(config.floatX)
    # print data
    print BinomialCorruptor(0.2)
    ae = DenoisingAutoencoder(
        BinomialCorruptor(corruption_level=0.2), 1000, 100, act_enc="sigmoid", act_dec="linear", tied_weights=False
    )
    trainer = sgd.SGD(
        learning_rate=0.005,
        batch_size=5,
        termination_criterion=EpochCounter(3),
        cost=cost_ae.MeanSquaredReconstructionError(),
        monitoring_batches=5,
        monitoring_dataset=ds,
    )
    trainer.setup(ae, ds)
    while True:
        trainer.train(dataset=ds)
        ae.monitor()
        ae.monitor.report_epoch()
        if not trainer.continue_learning(ae):
            break
    # print ds.train[0][0]
    # print ae.reconstruct(ds.train[0][0])

    w = ae.weights.get_value()
    # ae.hidbias.set_value(np.random.randn(1000).astype(config.floatX))
    hb = ae.hidbias.get_value()
    # ae.visbias.set_value(np.random.randn(100).astype(config.floatX))
    vb = ae.visbias.get_value()
    d = tensor.matrix()
    result = np.dot(1.0 / (1 + np.exp(-hb - np.dot(ds.train[0][0], w))), w.T) + vb
Exemple #2
0
def runAutoencoder():
    ds = StockPrice()
    #print ds.train[0][0]
    data = np.random.randn(10, 5).astype(config.floatX)
    #print data
    print BinomialCorruptor(.2)
    ae = DenoisingAutoencoder(BinomialCorruptor(corruption_level=.2), 1000, 100, act_enc='sigmoid', act_dec='linear',
                     tied_weights=False)
    trainer = sgd.SGD(learning_rate=.005, batch_size=5, termination_criterion=EpochCounter(3), cost=cost_ae.MeanSquaredReconstructionError(), monitoring_batches=5, monitoring_dataset=ds)
    trainer.setup(ae, ds)
    while True:
        trainer.train(dataset=ds)
        ae.monitor()
        ae.monitor.report_epoch()
        if not trainer.continue_learning(ae):
            break
    #print ds.train[0][0]
    #print ae.reconstruct(ds.train[0][0])

    w = ae.weights.get_value()
    #ae.hidbias.set_value(np.random.randn(1000).astype(config.floatX))
    hb = ae.hidbias.get_value()
    #ae.visbias.set_value(np.random.randn(100).astype(config.floatX))
    vb = ae.visbias.get_value()
    d = tensor.matrix()
    result = np.dot(1. / (1 + np.exp(-hb - np.dot(ds.train[0][0],  w))), w.T) + vb
def learn_manifold(data_train, model_fname):

    arch = [30]
    learning_rate = 0.005
    max_iters = 1000
    batch_size = 100
    tied_weights = False

    print('Training a denoising autoencoder...')
    monitoring_dataset = {'train': data_train}
    im_dim = data_train.X.shape[1]

    model = DenoisingAutoencoder(nvis=im_dim, nhid=arch[0], irange=0.05,
                                 corruptor=BinomialCorruptor(corruption_level=0.5),
                                 act_enc="tanh",
                                 act_dec="sigmoid", tied_weights=tied_weights)

    algorithm = pylearn2.training_algorithms.sgd.\
                SGD(learning_rate=learning_rate, 
                    batch_size=batch_size,
                    monitoring_dataset=monitoring_dataset,
                    cost=MeanSquaredReconstructionError(),
                    termination_criterion=pylearn2.\
                        termination_criteria.And(criteria=[
                            MonitorBased(channel_name='train_objective', 
                                         prop_decrease=0.001, N=10),
                            EpochCounter(max_epochs=max_iters)])
                )
    extensions = [
            pylearn2.train_extensions.best_params.\
            MonitorBasedSaveBest(channel_name='train_objective', 
                                 save_path=model_fname)]

    trainer = pylearn2.train.Train(dataset=data_train, model=model,
                                   algorithm=algorithm, 
                                   extensions=extensions,
                                   save_freq=0)

    trainer.main_loop()

    if False:
        I1 = data_train.X[np.random.randint(0,data_train.X.shape[0])]
        im_side = np.sqrt(I1.size)
        I1[np.random.rand(I1.size)>0.8] = 0
        I2 = (model.reconstruct(theano.shared(I1)).eval()>0.5).astype(int)
        plt.subplot(121)
        plt.imshow(I1.reshape((im_side, im_side)), cmap='Greys', 
                   interpolation='nearest', vmin=0, vmax=f1)
        plt.subplot(122)
        plt.imshow(I2.reshape((im_side, im_side)), cmap='Greys', 
                   interpolation='nearest', vmin=0, vmax=1)
        plt.show()
    def combine_sublayers(self):
        print "Combining sub-layers"

        # Create a large 2560 unit DAE. The model is considered trained by the concatenation
        # of its 512 unit sub-layers. The 2560 hidden units weights will be initialized
        # by these. 
        nvis = 3072
        nhid = 2560
        irange = 0.05
        corruption = 0.2
        corruptor = BinomialCorruptor(corruption_level=corruption)
        activation_encoder = "tanh"
        activation_decoder = None

        # By default, the DAE initializes the weights at random
        # Since we're using our own already pre-trained weights
        # We will instead change those values.
        large_dae = DenoisingAutoencoder(nvis=nvis, nhid=nhid, corruptor=corruptor, irange=irange, act_enc=activation_encoder, act_dec=activation_decoder)

        # Do not need to change hidden or visible bias. 
        # They are static vars in theory.
        large_dae._params = [
            large_dae.visbias,
            large_dae.hidbias,
            # Here is where we change the weights.
            large_dae.weights
        ]

        numpy_array = np.zeros((3072, 2560))
        # Load sub-layer models and get their weights.
        for i in range (0,5):
            fo = open(self.save_path+str(i)+".pkl", 'rb')

            # 768 Vis, 512 hidden unit DAE.
            small_dae = cPickle.load(fo)
            fo.close()
            
            # TODO: Create numpy array of proper values to set the large_dae. 
            # Get the weights from the small_dae's
            # so that they can be appended together.                                      
            weights = small_dae.weights.get_value()
            large_dae_weights.append(weights)

        print "Successfully combined sub-layers"
Exemple #5
0
 def new_model(self, model_params, dataset):
     corruptor = BinomialCorruptor(
         corruption_level=model_params['noise_level'])
     model = DenoisingAutoencoder(nvis=dataset.X.shape[1],
                                  nhid=model_params['hidden_outputs'],
                                  irange=model_params['irange'],
                                  corruptor=corruptor,
                                  act_enc='tanh',
                                  act_dec=None)
     return model
Exemple #6
0
def get_denoising_autoencoder(structure):
    n_input, n_output = structure
    curruptor = BinomialCorruptor(corruption_level=0.5)
    config = {
        'corruptor': curruptor,
        'nhid': n_output,
        'nvis': n_input,
        'tied_weights': True,
        'act_enc': 'sigmoid',
        'act_dec': 'sigmoid',
        'irange': 0.001,
    }
    return DenoisingAutoencoder(**config)
def get_denoising_autoencoder(structure,corr_val):
    n_input, n_output = structure
    corruptor = BinomialCorruptor(corruption_level=corr_val)
    #corruptor =  GaussianCorruptor(stdev=0.25)
    config = {
        'corruptor': corruptor,
        'nhid': n_output,
        'nvis': n_input,
        'tied_weights': True,
        'act_enc': 'sigmoid',
        'act_dec': 'sigmoid',
        'irange': 4*np.sqrt(6. / (n_input + n_output)),
    }
    return DenoisingAutoencoder(**config)
    def create_layer_one(self):
        
        which_set = "train"
        one_hot = True
        start = 0
        # Creating 5 random patch layers based on 8,000 samples (Saturation point where the objective no longer improves.
        stop = 800
        # GridPatchCIFAR10 Randomly selects 5 16x16 patches from each image, and we do this 5 times. This helps increase training time and captures more information. Similar to how the neurons in the eye are attached to a specific region in the image.
        dataset = GridPatchCIFAR10(which_set=which_set, one_hot=one_hot, start=start, stop=stop)

        # Denoising autoencoder model hyper-parameters
        nvis = 768
        nhid = 512 
        irange = 0.05
        corruption_lvl = 0.2
        corruptor = BinomialCorruptor(corruption_level=corruption_lvl)
        activation_encoder = "tanh"
        # Linear activation
        activation_decoder = None 

        # Creating the denoising autoencoder
        model = DenoisingAutoencoder(nvis=nvis, nhid=nhid, irange=irange, corruptor=corruptor, act_enc=activation_encoder, act_dec=activation_decoder)

        # Parameters for SGD learning algorithm instantiated below
        learning_rate = 0.001
        batch_size = 100
        monitoring_batches = 5
        monitoring_dataset = dataset
        cost = MeanSquaredReconstructionError()
        max_epochs = 10
        termination_criterion = EpochCounter(max_epochs=max_epochs)
        

        # SGD Learning algorithm
        algorithm = SGD(learning_rate=learning_rate, batch_size=batch_size, monitoring_batches=monitoring_batches, monitoring_dataset=dataset, cost=cost, termination_criterion=termination_criterion)


        processes = []
        for i in range(0,5):
            print "Training DAE Sub-Layer: ", i
            save_path = self.save_path+str(i)+".pkl"
            save_freq = 1
            train = Train(dataset=dataset,model=model,algorithm=algorithm, save_path=save_path, save_freq=save_freq)
            p = Process(target=train.main_loop, args=())
            p.start()
            processes.append(p)

        for process in processes:
            process.join()
Exemple #9
0
def main():

    # Only the trainset is processed by this function.
    print 'getting preprocessed data to train model'
    pp_trainset, testset = get_processed_dataset()
    # remember to change here when changing datasets
    print 'loading unprocessed data for input displays'
    trainset = cifar10.CIFAR10(which_set="train")

    dmat = trainset.get_design_matrix()
    nvis = dmat.shape[1]

    model = DenoisingAutoencoder(
        corruptor=BinomialCorruptor(corruption_level=0.5),
        nhid=nhid,
        nvis=nvis,
        act_enc='sigmoid',
        act_dec='sigmoid',
        irange=.01)

    algorithm = SGD(
        learning_rate=0.1,
        cost=MeanSquaredReconstructionError(),
        batch_size=1000,
        monitoring_batches=10,
        monitoring_dataset=pp_trainset,
        termination_criterion=EpochCounter(max_epochs=MAX_EPOCHS_UNSUPERVISED),
        update_callbacks=None)

    extensions = None

    trainer = Train(model=model,
                    algorithm=algorithm,
                    save_path='testrun.pkl',
                    save_freq=1,
                    extensions=extensions,
                    dataset=pp_trainset)

    trainer.main_loop()
Exemple #10
0
def main():

    # the data isn't going to be fully processed, so we may have to
    # do some stuff to the testset still to make it work.
    trainset, testset = get_processed_dataset()

    # Creating the patch-pairs:
    design_matrix = trainset.get_design_matrix()
    processed_patch_size = design_matrix.shape[1]

    num_images = train_size

    examples_per_image = patches_per_image * (patches_per_image - 1)
    num_examples = examples_per_image * num_images

    stamps = trainset.stamps
    max_stamp = input_width - patch_width
    d_size = (2 * max_stamp + 1)**input_dim

    patch_pairs = np.zeros((num_examples, 2 * processed_patch_size))
    distances = np.zeros((num_examples, input_dim))
    distances_onehot = np.zeros((num_examples, d_size))
    examples = np.zeros((num_examples, 2 * processed_patch_size + d_size))

    nvis = 2 * processed_patch_size + d_size

    def flatten_encoding(encoding, max_stamp):
        dims = len(encoding)
        flat_encoding = 0
        for i in xrange(dims - 1):
            flat_encoding += encoding[i]
            flat_encoding *= max_stamp
        flat_encoding += encoding[-1]

    # Can be done without (or with less) for loops?
    print 'begin for loop'
    for i in xrange(num_images):
        if (i % 1000 == 0):
            print i, '-th outer loop...'
        for j in xrange(patches_per_image):
            patch1_num = i * patches_per_image + j
            patch1_pos = stamps[patch1_num, :]
            for k in xrange(patches_per_image):
                example_num = i * examples_per_image + j * (patches_per_image -
                                                            1) + k
                if (k > j):
                    example_num -= 1
                if (k != j):
                    patch2_num = i * patches_per_image + k
                    patch2_pos = stamps[patch2_num, :]
                    distance = patch1_pos - patch2_pos
                    distances[example_num] = distance
                    distance_encoding = distance + max_stamp
                    distance_encoding = flatten_encoding(
                        distance_encoding, max_stamp)
                    distances_onehot[example_num, distance_encoding] = 1
                    p1 = design_matrix[patch1_num]
                    p2 = design_matrix[patch2_num]
                    patch_pairs[example_num] = np.hstack((p1, p2))
                    examples[example_num] = np.hstack(
                        (patch_pairs[example_num],
                         distances_onehot[example_num]))
    print 'end for loop'

    trainset.set_design_matrix(examples)

    model = DenoisingAutoencoder(
        corruptor=BinomialCorruptor(corruption_level=0.5),
        nhid=nhid,
        nvis=nvis,
        act_enc='sigmoid',
        act_dec='sigmoid',
        irange=.01)

    algorithm = SGD(
        learning_rate=0.1,
        cost=MeanSquaredReconstructionError(),
        batch_size=100,
        monitoring_batches=10,
        monitoring_dataset=trainset,
        termination_criterion=EpochCounter(max_epochs=MAX_EPOCHS_UNSUPERVISED),
        update_callbacks=None)

    extensions = None

    trainer = Train(model=model,
                    algorithm=algorithm,
                    save_path='run.pkl',
                    save_freq=1,
                    extensions=extensions,
                    dataset=trainset)

    trainer.main_loop()
Exemple #11
0
def main():

    # Only the trainset is processed by this function.
    print 'getting preprocessed data for training model'
    pp_trainset, testset = get_processed_dataset()
    # remember to change here when changing datasets
    print 'loading unprocessed data for input displays'
    trainset = cifar10.CIFAR10(which_set="train")

    dmat = pp_trainset.get_design_matrix()
    nvis = dmat.shape[1]

    model = DenoisingAutoencoder(
        corruptor=BinomialCorruptor(corruption_level=0.3),
        nhid=nhid,
        nvis=nvis,
        act_enc='sigmoid',
        act_dec='sigmoid',
        irange=.01)

    algorithm = SGD(
        learning_rate=learning_rate,
        cost=MeanSquaredReconstructionError(),
        batch_size=100,
        monitoring_batches=10,
        monitoring_dataset=pp_trainset,
        termination_criterion=EpochCounter(max_epochs=MAX_EPOCHS_UNSUPERVISED),
        update_callbacks=None)

    extensions = None

    trainer = Train(model=model,
                    algorithm=algorithm,
                    save_path='run.pkl',
                    save_freq=1,
                    extensions=extensions,
                    dataset=pp_trainset)

    trainer.main_loop()

    ####################
    # Plot and Save:

    # choose random patch-pairs to plot
    stamps = pp_trainset.stamps
    num_examples = stamps.shape[0]
    to_plot = np.random.randint(0, num_examples, num2plot)

    # use to_plot indices to extract data
    stamps_data = stamps[to_plot]
    image_numbers = stamps[to_plot, 0].astype(int)
    X = trainset.X
    images_data = trainset.get_topological_view(X[image_numbers])
    p1x = stamps_data[:, 1]
    p1y = stamps_data[:, 2]
    p2x = stamps_data[:, 3]
    p2y = stamps_data[:, 4]

    # For input ppd's, once we've identified the patches, we just outline them and draw an arrow for d
    # This might mess with original trainset (I dunno), in which case, we should make a copy
    add_outlines(images_data, p1x, p1y, patch_width)
    add_outlines(images_data, p2x, p2y, patch_width)

    ##################################################
    # translating outputs back into things we can plot
    dataset = pp_trainset
    Xout = dataset.X.astype('float32')
    max_stamp = input_width - patch_width
    d_size = (2 * max_stamp + 1)**input_dim
    # displacement
    d_enc = Xout[:, -d_size:]
    d_out_flat = np.argmax(d_enc, axis=1)
    d_shape = [2 * max_stamp + 1, 2 * max_stamp + 1]  # assumed 2D
    d_out = flat_to_2D(d_out_flat, d_shape)
    d_out[to_plot, ]
    # patches
    vc = dataset.view_converter
    p_enc = Xout[:, :len(Xout.T) - d_size]
    p_size = p_enc.shape[1] / 2
    p1_enc = p_enc[:, :p_size]
    p2_enc = p_enc[:, p_size:]
    p1_enc = vc.design_mat_to_topo_view(p1_enc)
    p2_enc = vc.design_mat_to_topo_view(p2_enc)
    pp = dataset.preprocessor
    gcn = pp.items[1]
    means = gcn.means
    normalizers = gcn.normalizers
    toshape = (num_examples, )
    for i in range(input_dim):
        toshape += (1, )
    if num_channels != 1:
        toshape += (1, )
    # When the number of patches and patch-pairs differs, this breaks.
    # I need to match up normalizers/means with their corresponding patches
    # undoing the PCA might be breaking too, but without errors...
    normalizers1 = expand_p1(normalizers)
    normalizers2 = expand_p2(normalizers)
    means1 = expand_p1(means)
    means2 = expand_p2(means)

    p1_enc *= normalizers1.reshape(toshape)
    p1_enc += means1.reshape(toshape)
    p2_enc *= normalizers2.reshape(toshape)
    p2_enc += means2.reshape(toshape)
    # Now, we pull off the same examples from the data to compare to dAE inputs in plots
    outputs = copy.deepcopy(images_data)
    insertpatches(outputs, p1_enc[to_plot], p1x, p1y, patch_width)
    insertpatches(outputs, p2_enc[to_plot], p2x, p2y, patch_width)

    plt.figure()

    for i in range(num2plot):
        # Inputs
        plt.subplot(num2plot, 2, 2 * i + 1)
        plt.imshow(images_data[i], cmap=cm.Greys_r)
        print stamps_data[i]
        a = (stamps_data[i, 2] + patch_width / 2,
             stamps_data[i, 1] + patch_width / 2, stamps_data[i, 6],
             stamps_data[i, 5])
        plt.arrow(a[0], a[1], a[2], a[3], head_width=1.0, head_length=0.6)
        # Outputs
        plt.subplot(num2plot, 2, 2 * i + 2)
        plt.imshow(outputs[i], cmap=cm.Greys_r)
        plt.arrow(a[0],
                  a[1],
                  d_out[to_plot[i], 1],
                  d_out[to_plot[i], 0],
                  head_width=1.0,
                  head_length=0.6)

    plt.show()

    savestr = 'cifar_ppd.png'
    plt.savefig(savestr)