def example_mnist_tap_machine(paysage_path=None,
                              num_epochs=10,
                              show_plot=False):

    num_hidden_units = 256
    batch_size = 100
    learning_rate = schedules.power_law_decay(initial=0.1, coefficient=0.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=batch.binarize_color,
                          train_fraction=0.95)

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

    rbm = model.Model([vis_layer, hid_layer])
    rbm.initialize(data, 'glorot_normal')

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

    opt = optimizers.Gradient(stepsize=learning_rate,
                              tolerance=1e-4,
                              ascent=True)

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

    sgd = fit.SGD(rbm,
                  data,
                  opt,
                  num_epochs,
                  sampler=sampler,
                  method=fit.tap,
                  monitor=perf)

    # fit the model
    print('Training with stochastic gradient ascent using TAP expansion')
    sgd.train()

    util.show_metrics(rbm, perf)
    valid = data.get('validate')
    util.show_reconstructions(rbm,
                              valid,
                              fit,
                              show_plot,
                              n_recon=10,
                              vertical=False)
    util.show_fantasy_particles(rbm, valid, fit, show_plot, n_fantasy=25)
    util.show_weights(rbm, show_plot, n_weights=25)
    # close the HDF5 store
    data.close()
    print("Done")
Beispiel #2
0
def example_mnist_deep_rbm(paysage_path=None, num_epochs=10, show_plot=False):
    num_hidden_units = 500
    batch_size = 100
    learning_rate = schedules.power_law_decay(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=batch.binarize_color,
                          train_fraction=0.99)

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

    rbm = model.Model([vis_layer, hid_1_layer, hid_2_layer])
    rbm.initialize(data)

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

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

    sampler = fit.SequentialMC.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 contrastive divergence')
    cd.train_layerwise()

    # evaluate the model
    util.show_metrics(rbm, perf)
    valid = data.get('validate')
    util.show_reconstructions(rbm, valid, fit, show_plot)
    util.show_fantasy_particles(rbm, valid, fit, show_plot)
    util.show_weights(rbm, show_plot)

    # close the HDF5 store
    data.close()
    print("Done")
Beispiel #3
0
def compute_fantasy_particles(rbm, v_data, fit, n_fantasy=25):
    from math import sqrt
    grid_size = int(sqrt(n_fantasy))
    assert grid_size == sqrt(
        n_fantasy), "n_fantasy must be the square of an integer"

    random_samples = rbm.random(v_data)
    model_state = State.from_visible(random_samples, rbm)

    schedule = schedules.power_law_decay(initial=1.0, coefficient=0.5)
    sampler = fit.DrivenSequentialMC(rbm, schedule=schedule)

    sampler.set_negative_state(model_state)
    sampler.update_negative_state(1000)

    v_model = rbm.deterministic_iteration(1, sampler.neg_state).units[0]
    idx = numpy.random.choice(range(len(v_model)), n_fantasy, replace=False)

    grid = numpy.array([be.to_numpy_array(v_model[i]) for i in idx])
    return grid.reshape(grid_size, grid_size, -1)
Beispiel #4
0
def test_tap_machine(paysage_path=None):
    num_hidden_units = 10
    batch_size = 50
    num_epochs = 1
    learning_rate = schedules.power_law_decay(initial=0.1, coefficient=0.1)

    if not paysage_path:
        paysage_path = os.path.dirname(
            os.path.dirname(os.path.abspath(__file__)))
    filepath = os.path.join(paysage_path, 'mnist', 'mnist.h5')

    if not os.path.exists(filepath):
        raise IOError(
            "{} does not exist. run mnist/download_mnist.py to fetch from the web"
            .format(filepath))

    shuffled_filepath = os.path.join(paysage_path, 'mnist',
                                     'shuffled_mnist.h5')

    # shuffle the data
    if not os.path.exists(shuffled_filepath):
        shuffler = batch.DataShuffler(filepath, shuffled_filepath, complevel=0)
        shuffler.shuffle()

    # set a seed for the random number generator
    be.set_seed()

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

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

    rbm = model.Model([vis_layer, hid_layer])
    rbm.initialize(data)

    # obtain initial estimate of the reconstruction error
    perf = fit.ProgressMonitor(data, metrics=['ReconstructionError'])
    untrained_performance = perf.check_progress(rbm)

    # set up the optimizer and the fit method
    opt = optimizers.Gradient(stepsize=learning_rate,
                              tolerance=1e-3,
                              ascent=True)

    sampler = fit.SequentialMC(rbm)

    solver = fit.SGD(rbm,
                     data,
                     opt,
                     num_epochs,
                     sampler=sampler,
                     method=fit.tap,
                     monitor=perf)

    # fit the model
    print('training with stochastic gradient ascent')
    solver.train()

    # obtain an estimate of the reconstruction error after 1 epoch
    trained_performance = perf.check_progress(rbm)

    assert (trained_performance['ReconstructionError'] <
            untrained_performance['ReconstructionError']), \
    "Reconstruction error did not decrease"

    # close the HDF5 store
    data.close()