Exemple #1
0
        model.update(chain_d, chain_m, lr_W1, lr_b1, lr_o1)

    # Print Norms of the Parameters
    print numx.mean(numxExt.get_norms(wl1.weights)), '\t', numx.mean(
        numxExt.get_norms(wl2.weights)), '\t',
    print numx.mean(numxExt.get_norms(l1.bias)), '\t', numx.mean(
        numxExt.get_norms(l2.bias)), '\t',
    print numx.mean(numxExt.get_norms(l3.bias)), '\t', numx.mean(
        l1.offset), '\t', numx.mean(l2.offset), '\t', numx.mean(l3.offset)

# Show weights
VIS.imshow_matrix(
    VIS.tile_matrix_rows(wl1.weights,
                         v11,
                         v12,
                         v21,
                         v22,
                         border_size=1,
                         normalized=False), 'Weights 1')
VIS.imshow_matrix(
    VIS.tile_matrix_rows(numx.dot(wl1.weights, wl2.weights),
                         v11,
                         v12,
                         v31,
                         v32,
                         border_size=1,
                         normalized=False), 'Weights 2')

# # Samplesome steps
chain_m = [
    numx.float64(numx.random.rand(10 * batch_size, v11 * v12) < 0.5),
            corruptor=None,
            # Rather strong sparsity regularization
            reg_sparseness=2.0,
            desired_sparseness=0.001,
            reg_contractive=0.0,
            reg_slowness=0.0,
            data_next=None,
            # The gradient restriction is important for fast learning, see also GRBMs
            restrict_gradient=0.1,
            restriction_norm='Cols')

# Show filters/features
filters = vis.tile_matrix_rows(ae.w,
                               v1,
                               v2,
                               h1,
                               h2,
                               border_size=1,
                               normalized=True)
vis.imshow_matrix(filters, 'Filter')

# Show samples
samples = vis.tile_matrix_rows(train_data[0:100].T,
                               v1,
                               v2,
                               10,
                               10,
                               border_size=1,
                               normalized=True)
vis.imshow_matrix(samples, 'Data samples')
# Load data (download is not existing)
data = io.load_olivetti_faces(path='olivettifaces.mat')

# Specify image width and height for displaying
width = height = 64

# PCA
pca = PCA(input_dim=width * height)
pca.train(data=data)

# Show the first 100 eigenvectors of the covariance matrix
eigenvectors = vis.tile_matrix_rows(matrix=pca.projection_matrix,
                                    tile_width=width,
                                    tile_height=height,
                                    num_tiles_x=10,
                                    num_tiles_y=10,
                                    border_size=1,
                                    normalized=True)
vis.imshow_matrix(
    matrix=eigenvectors,
    windowtitle='First 100 Eigenvectors of the covariance matrix')

# Show the first 100 images
images = vis.tile_matrix_rows(matrix=data[0:100].T,
                              tile_width=width,
                              tile_height=height,
                              num_tiles_x=10,
                              num_tiles_y=10,
                              border_size=1,
                              normalized=True)
Exemple #4
0
            trainer.model, betas=betas)
        train_LL = numx.mean(
            ESTIMATOR.LL_lower_bound(trainer.model, train_set, logZ))
        print("AIS  LL: ", 2**(v11 + 1) * train_LL)

        logZ = ESTIMATOR.partition_function_exact(trainer.model)
        train_LL = numx.mean(ESTIMATOR.LL_exact(trainer.model, train_set,
                                                logZ))
        print("True LL: ", 2**(v11 + 1) * train_LL)
        print()

# Show weights
VIS.imshow_matrix(
    VIS.tile_matrix_rows(dbm.W1,
                         v11,
                         v12,
                         v21,
                         v22,
                         border_size=1,
                         normalized=False), 'Weights 1')
VIS.imshow_matrix(
    VIS.tile_matrix_rows(numx.dot(dbm.W1, dbm.W2),
                         v11,
                         v12,
                         v31,
                         v32,
                         border_size=1,
                         normalized=False), 'Weights 2')

VIS.show()
Exemple #5
0
# ZCA projects the whitened data back to the original space, thus does not
# perform a dimensionality reduction but a whitening in the original space
whitened_data = zca.project(data)

# Create a ZCA node and train it (you could also use PCA whitened=True)
ica = ICA(input_dim=width * height)
ica.train(data=whitened_data,
          iterations=100,
          convergence=1.0,
          status=True)

# Show whitened images
images = vis.tile_matrix_rows(matrix=data[0:100].T,
                              tile_width=width,
                              tile_height=height,
                              num_tiles_x=10,
                              num_tiles_y=10,
                              border_size=1,
                              normalized=True)
vis.imshow_matrix(matrix=images,
                  windowtitle='First 100 image patches')

# Show some whitened images
images = vis.tile_matrix_rows(matrix=whitened_data[0:100].T,
                              tile_width=width,
                              tile_height=height,
                              num_tiles_x=10,
                              num_tiles_y=10,
                              border_size=1,
                              normalized=True)
vis.imshow_matrix(matrix=images,
Exemple #6
0
def generate_samples(rbm,
                     data,
                     iterations,
                     stepsize,
                     v1,
                     v2, 
                     sample_states = False, 
                     whitening = None):
    ''' Generates samples from the given RBM model.     
    
    :Parameters:
        rbm:           RBM model.
                      -type: RBM model object.
        
        data:          Data to sample from.
                      -type: numpy array [num samples, dimensions]
        
        iterations:    Number of Gibbs sampling steps.
                      -type: int
        
        stepsize:      After how many steps a sample should be plotted.
                      -type: int
        
        v1:            X-Axis of the reorder image patch.
                      -type: int
        
        v2:            Y-Axis of the reorder image patch.
                      -type: int
        
        sample_states: If true returns the sates , probabilities otherwise.
                      -type: bool
        
        whitening:     If the data has been preprocessed it needs to be 
                       undone.
                      -type: preprocessing object or None
        
    :Returns:
        Matrix with image patches order along X-Axis and it's evolution in 
        Y-Axis.
       -type: numpy array
    
    '''
    result = data
    if whitening != None:
        result = whitening.unproject(data)
    vis_states = data
    for i in xrange(1,iterations+1):    
        hid_probs = rbm.probability_h_given_v(vis_states)
        hid_states = rbm.sample_h(hid_probs)
        vis_probs = rbm.probability_v_given_h(hid_states)   
        vis_states = rbm.sample_v(vis_probs)
        if i % stepsize == 0: 
            if whitening != None:
                if sample_states:
                    result = numx.vstack((result,
                                        whitening.unproject(vis_states)))
                else:
                    result = numx.vstack((result,
                                        whitening.unproject(vis_probs)))
            else:
                if sample_states:
                    result = numx.vstack((result,vis_states))
                else:
                    result = numx.vstack((result,vis_probs))
                
    import pydeep.misc.visualization as Vis
    return Vis.tile_matrix_rows(result.T, v1,v2,iterations/stepsize+1
                               ,data.shape[0], border_size = 1,
                               normalized = False)