示例#1
0
def test_weights_build_from_config():
    ly = layers.Weights((num_vis, num_hid))
    ly.add_constraint({'matrix': constraints.non_negative})
    p = penalties.l2_penalty(0.37)
    ly.add_penalty({'matrix': p})
    ly_new = layers.Layer.from_config(ly.get_config())
    assert ly_new.get_config() == ly.get_config()
示例#2
0
def test_weights_derivative():
    ly = layers.Weights((num_vis, num_hid))
    p = penalties.l2_penalty(0.37)
    ly.add_penalty({'matrix': p})
    vis = be.randn((num_samples, num_vis))
    hid = be.randn((num_samples, num_hid))
    derivs = ly.derivatives(vis, hid)
示例#3
0
def test_exponential_build_from_config():
    ly = layers.ExponentialLayer(num_vis)
    ly.add_constraint({'loc': constraints.non_negative})
    p = penalties.l2_penalty(0.37)
    ly.add_penalty({'log_var': p})
    ly_new = layers.Layer.from_config(ly.get_config())
    assert ly_new.get_config() == ly.get_config()
示例#4
0
def test_onehot_build_from_config():
    ly = layers.OneHotLayer(num_vis)
    ly.add_constraint({'loc': constraints.non_negative})
    p = penalties.l2_penalty(0.37)
    ly.add_penalty({'loc': p})
    ly_new = layers.layer_from_config(ly.get_config())
    assert ly_new.get_config() == ly.get_config()
示例#5
0
def test_gaussian_build_from_config():
    ly = layers.GaussianLayer(num_vis)
    ly.add_constraint({'loc': constraints.non_negative})
    p = penalties.l2_penalty(0.37)
    ly.add_penalty({'log_var': p})
    ly_new = layers.layer_from_config(ly.get_config())
    assert ly_new.get_config() == ly.get_config()
示例#6
0
def test_get_base_config():
    ly = layers.BernoulliLayer(num_vis)
    ly.add_constraint({'loc': constraints.non_negative})
    p = penalties.l2_penalty(0.37)
    ly.add_penalty({'loc': p})
    ly.set_fixed_params(['loc'])
    ly.get_base_config()
示例#7
0
def run(num_epochs=10, show_plot=False):
    num_hidden_units = 100
    batch_size = 100
    mc_steps = 10
    beta_std = 0.6

    # set up the reader to get minibatches
    with util.create_batch(batch_size,
                           train_fraction=0.95,
                           transform=transform) as data:

        # set up the model and initialize the parameters
        vis_layer = layers.BernoulliLayer(data.ncols)
        hid_layer = layers.BernoulliLayer(num_hidden_units, center=False)

        rbm = BoltzmannMachine([vis_layer, hid_layer])
        rbm.connections[0].weights.add_penalty(
            {'matrix': pen.l2_penalty(0.001)})
        rbm.initialize(data, method='pca')

        print('training with persistent contrastive divergence')
        cd = fit.SGD(rbm, data)

        learning_rate = schedules.PowerLawDecay(initial=0.01, coefficient=0.1)
        opt = optimizers.ADAM(stepsize=learning_rate)

        cd.train(opt, num_epochs, mcsteps=mc_steps, method=fit.pcd)
        util.show_metrics(rbm, cd.monitor)

        # evaluate the model
        valid = data.get('validate')
        util.show_reconstructions(rbm,
                                  valid,
                                  show_plot,
                                  n_recon=10,
                                  vertical=False,
                                  num_to_avg=10)
        util.show_fantasy_particles(rbm,
                                    valid,
                                    show_plot,
                                    n_fantasy=5,
                                    beta_std=beta_std,
                                    fantasy_steps=100)

        util.show_weights(rbm, show_plot, n_weights=100)
        print("Done")

    return rbm
示例#8
0
    def run(num_epochs=1, show_plot=False):
        num_hidden_units = 1
        batch_size = 100
        mc_steps = 10
        beta_std = 0.6

        # set up the reader to get minibatches
        with batch.in_memory_batch(samples, batch_size,
                                   train_fraction=0.95) as data:

            # set up the model and initialize the parameters
            vis_layer = layers.BernoulliLayer(data.ncols)
            hid_layer = layers.BernoulliLayer(num_hidden_units, center=False)
            rbm = BoltzmannMachine([vis_layer, hid_layer])

            rbm.connections[0].weights.add_penalty(
                {'matrix': pen.l2_penalty(0.001)})  # Add regularization term

            rbm.initialize(data, method='hinton')  # Initialize weights

            cd = fit.SGD(rbm, data)
            learning_rate = schedules.PowerLawDecay(initial=0.01,
                                                    coefficient=0.1)
            opt = optimizers.ADAM(stepsize=learning_rate)

            print("Train the model...")
            cd.train(opt,
                     num_epochs,
                     mcsteps=mc_steps,
                     method=fit.pcd,
                     verbose=False)
            '''
            # write on file KL divergences
            reverse_KL_div = [ cd.monitor.memory[i]['ReverseKLDivergence'] for i in range(0,len(cd.monitor.memory)) ]
            KL_div = [ cd.monitor.memory[i]['KLDivergence'] for i in range(0,len(cd.monitor.memory)) ]
            for i in range(0,len(cd.monitor.memory)):
            	out_file1.write(str(KL_div[i])+" "+str(reverse_KL_div[i])+"\n")
            out_file1.close()

            # save weights on file
            filename = "results/weights/weights-"+temperature[:-4]+".jpg"
            Gprotein_util.show_weights(rbm, show_plot=False, n_weights=8, Filename=filename, random=False)
            '''
        return rbm
示例#9
0
def run(num_epochs=10, show_plot=False):
    num_hidden_units = 256
    batch_size = 100
    learning_rate = schedules.PowerLawDecay(initial=0.01, coefficient=0.1)
    mc_steps = 1

    # set up the reader to get minibatches
    data = util.create_batch(batch_size,
                             train_fraction=0.95,
                             transform=transform)

    # set up the model and initialize the parameters
    vis_layer = layers.BernoulliLayer(data.ncols)
    hid_layer = layers.OneHotLayer(num_hidden_units)

    rbm = BoltzmannMachine([vis_layer, hid_layer])
    rbm.connections[0].weights.add_penalty({'matrix': pen.l2_penalty(0.001)})
    rbm.initialize(data, method='glorot_normal')

    # set up the optimizer and the fit method
    opt = optimizers.ADAM(stepsize=learning_rate)
    cd = fit.SGD(rbm, data)

    # fit the model
    print('training with persistent contrastive divergence')
    cd.train(opt, num_epochs, method=fit.pcd, mcsteps=mc_steps)

    # evaluate the model
    util.show_metrics(rbm, cd.monitor)
    valid = data.get('validate')
    util.show_reconstructions(rbm,
                              valid,
                              show_plot,
                              n_recon=10,
                              vertical=False,
                              num_to_avg=10)
    util.show_fantasy_particles(rbm, valid, show_plot, n_fantasy=5)
    util.show_weights(rbm, show_plot, n_weights=25)

    # close the HDF5 store
    data.close()
    print("Done")
示例#10
0
def run(paysage_path=None, num_epochs=10, show_plot=False):
    num_hidden_units = 256
    batch_size = 100
    learning_rate = schedules.PowerLawDecay(initial=0.01, coefficient=0.1)
    mc_steps = 1

    (_, _, shuffled_filepath) = \
            util.default_paths(paysage_path)

    # set up the reader to get minibatches
    data = batch.HDFBatch(shuffled_filepath,
                          'train/images',
                          batch_size,
                          transform=pre.binarize_color,
                          train_fraction=0.95)

    # set up the model and initialize the parameters
    vis_layer = layers.BernoulliLayer(data.ncols)
    hid_layer = layers.OneHotLayer(num_hidden_units)

    rbm = model.Model([vis_layer, hid_layer])
    rbm.weights[0].add_penalty({'matrix': pen.l2_penalty(0.001)})
    rbm.initialize(data, method='glorot_normal')

    metrics = [
        'ReconstructionError', 'EnergyDistance', 'EnergyGap', 'EnergyZscore',
        'HeatCapacity', 'WeightSparsity', 'WeightSquare'
    ]
    perf = fit.ProgressMonitor(data, metrics=metrics)

    # set up the optimizer and the fit method
    opt = optimizers.ADAM(stepsize=learning_rate)

    sampler = fit.DrivenSequentialMC.from_batch(rbm, data)

    cd = fit.SGD(rbm,
                 data,
                 opt,
                 num_epochs,
                 method=fit.pcd,
                 sampler=sampler,
                 mcsteps=mc_steps,
                 monitor=perf)

    # fit the model
    print('training with persistent contrastive divergence')
    cd.train()

    # evaluate the model
    util.show_metrics(rbm, perf)
    valid = data.get('validate')
    util.show_reconstructions(rbm,
                              valid,
                              fit,
                              show_plot,
                              n_recon=10,
                              vertical=False,
                              num_to_avg=10)
    util.show_fantasy_particles(rbm, valid, fit, show_plot, n_fantasy=25)
    util.show_weights(rbm, show_plot, n_weights=9)

    # close the HDF5 store
    data.close()
    print("Done")
示例#11
0
def test_get_base_config():
    ly = layers.Weights((num_vis, num_hid))
    ly.add_constraint({'matrix': constraints.non_negative})
    p = penalties.l2_penalty(0.37)
    ly.add_penalty({'matrix': p})
    ly.get_base_config()
示例#12
0
def test_get_penalty_grad():
    ly = layers.Weights((num_vis, num_hid))
    p = penalties.l2_penalty(0.37)
    ly.add_penalty({'matrix': p})
    ly.get_penalty_grad(ly.W(), 'matrix')
示例#13
0
def test_add_penalty():
    ly = layers.Weights((num_vis, num_hid))
    p = penalties.l2_penalty(0.37)
    ly.add_penalty({'matrix': p})