Exemple #1
0
def main():
    try:
        os.mkdir(args.plot_dir)
    except:
        pass

    # settings
    max_epoch = 1000
    n_trains_per_epoch = 2
    batchsize_positive = 100
    batchsize_negative = 100
    plotsize = 400

    # config
    config_energy_model = to_object(params_energy_model["config"])
    config_generative_model = to_object(params_generative_model["config"])

    # seed
    np.random.seed(args.seed)
    if args.gpu_enabled:
        cuda.cupy.random.seed(args.seed)

    # init weightnorm layers
    if config_energy_model.use_weightnorm:
        print "initializing weight normalization layers of the energy model ..."
        x_positive = sampler.sample_from_swiss_roll(batchsize_positive * 10, 2,
                                                    10)
        ddgm.compute_energy(x_positive)

    if config_generative_model.use_weightnorm:
        print "initializing weight normalization layers of the generative model ..."
        x_negative = ddgm.generate_x(batchsize_negative * 10)

    fixed_z = ddgm.sample_z(plotsize)
    fixed_target = sampler.sample_from_swiss_roll(600, 2, 10)

    progress = Progress()
    for epoch in xrange(1, max_epoch):
        progress.start_epoch(epoch, max_epoch)
        sum_energy_positive = 0
        sum_energy_negative = 0
        sum_kld = 0

        for t in xrange(n_trains_per_epoch):
            # sample from data distribution
            x_positive = sampler.sample_from_swiss_roll(
                batchsize_positive, 2, 10)

            # sample from generator
            x_negative = ddgm.generate_x(batchsize_negative)

            # train energy model
            energy_positive = ddgm.compute_energy_sum(x_positive)
            energy_negative = ddgm.compute_energy_sum(x_negative)
            loss = energy_positive - energy_negative
            ddgm.backprop_energy_model(loss)

            # train generative model
            x_negative = ddgm.generate_x(batchsize_negative)
            kld = ddgm.compute_kld_between_generator_and_energy_model(
                x_negative)
            ddgm.backprop_generative_model(kld)

            sum_energy_positive += float(energy_positive.data)
            sum_energy_negative += float(energy_negative.data)
            sum_kld += float(kld.data)
            if t % 10 == 0:
                progress.show(t, n_trains_per_epoch, {})

        progress.show(
            n_trains_per_epoch, n_trains_per_epoch, {
                "x+": sum_energy_positive / n_trains_per_epoch,
                "x-": sum_energy_negative / n_trains_per_epoch,
                "KLD": int(sum_kld / n_trains_per_epoch)
            })
        ddgm.save(args.model_dir)

        # init
        fig = pylab.gcf()
        fig.set_size_inches(8.0, 8.0)
        pylab.clf()

        plot(fixed_target, color="#bec3c7", s=20)
        plot(ddgm.generate_x_from_z(fixed_z, as_numpy=True, test=True),
             color="#e84c3d",
             s=20)

        # save
        pylab.savefig("{}/{}.png".format(args.plot_dir, 100000 + epoch))
Exemple #2
0
def main():
    # load MNIST images
    images = load_rgb_images(args.image_dir)

    # config
    config_energy_model = to_object(params_energy_model["config"])
    config_generative_model = to_object(params_generative_model["config"])

    # settings
    max_epoch = 1000
    n_trains_per_epoch = 500
    batchsize_positive = 128
    batchsize_negative = 128
    plot_interval = 5

    # seed
    np.random.seed(args.seed)
    if args.gpu_device != -1:
        cuda.cupy.random.seed(args.seed)

    # init weightnorm layers
    if config_energy_model.use_weightnorm:
        print "initializing weight normalization layers of the energy model ..."
        x_positive = sample_from_data(images, batchsize_positive * 5)
        ddgm.compute_energy(x_positive)

    if config_generative_model.use_weightnorm:
        print "initializing weight normalization layers of the generative model ..."
        x_negative = ddgm.generate_x(batchsize_negative * 5)

    progress = Progress()
    for epoch in xrange(1, max_epoch):
        progress.start_epoch(epoch, max_epoch)
        sum_energy_positive = 0
        sum_energy_negative = 0
        sum_loss = 0
        sum_kld = 0

        for t in xrange(n_trains_per_epoch):
            # sample from data distribution
            x_positive = sample_from_data(images, batchsize_positive)

            # sample from generator
            x_negative = ddgm.generate_x(batchsize_negative)

            # train energy model
            energy_positive = ddgm.compute_energy_sum(x_positive)
            energy_negative = ddgm.compute_energy_sum(x_negative)
            loss = energy_positive - energy_negative
            ddgm.backprop_energy_model(loss)

            # train generative model
            # TODO: KLD must be greater than or equal to 0
            x_negative = ddgm.generate_x(batchsize_negative)
            kld = ddgm.compute_kld_between_generator_and_energy_model(
                x_negative)
            ddgm.backprop_generative_model(kld)

            sum_energy_positive += float(energy_positive.data)
            sum_energy_negative += float(energy_negative.data)
            sum_loss += float(loss.data)
            sum_kld += float(kld.data)
            progress.show(t, n_trains_per_epoch, {})

        progress.show(
            n_trains_per_epoch, n_trains_per_epoch, {
                "x+": int(sum_energy_positive / n_trains_per_epoch),
                "x-": int(sum_energy_negative / n_trains_per_epoch),
                "loss": sum_loss / n_trains_per_epoch,
                "kld": sum_kld / n_trains_per_epoch
            })
        ddgm.save(args.model_dir)

        if epoch % plot_interval == 0 or epoch == 1:
            plot(filename="epoch_{}_time_{}min".format(
                epoch, progress.get_total_time()))
Exemple #3
0
def main():
    # settings
    max_epoch = 1000
    n_trains_per_epoch = 500
    batchsize_positive = 100
    batchsize_negative = 100

    # config
    config_energy_model = to_object(params_energy_model["config"])
    config_generative_model = to_object(params_generative_model["config"])

    # seed
    np.random.seed(args.seed)
    if args.gpu_enabled:
        cuda.cupy.random.seed(args.seed)

    # init weightnorm layers
    if config_energy_model.use_weightnorm:
        print "initializing weight normalization layers of the energy model ..."
        x_positive = sampler.sample_from_gaussian_mixture(
            batchsize_positive * 10, 2, 10)
        ddgm.compute_energy(x_positive)

    if config_generative_model.use_weightnorm:
        print "initializing weight normalization layers of the generative model ..."
        x_negative = ddgm.generate_x(batchsize_negative * 10)

    progress = Progress()
    for epoch in xrange(1, max_epoch):
        progress.start_epoch(epoch, max_epoch)
        sum_energy_positive = 0
        sum_energy_negative = 0
        sum_kld = 0

        for t in xrange(n_trains_per_epoch):
            # sample from data distribution
            x_positive = sampler.sample_from_gaussian_mixture(
                batchsize_positive, 2, 10)

            # sample from generator
            x_negative = ddgm.generate_x(batchsize_negative)

            # train energy model
            energy_positive = ddgm.compute_energy_sum(x_positive)
            energy_negative = ddgm.compute_energy_sum(x_negative)
            loss = energy_positive - energy_negative
            ddgm.backprop_energy_model(loss)

            # train generative model
            x_negative = ddgm.generate_x(batchsize_negative)
            kld = ddgm.compute_kld_between_generator_and_energy_model(
                x_negative)
            ddgm.backprop_generative_model(kld)

            sum_energy_positive += float(energy_positive.data)
            sum_energy_negative += float(energy_negative.data)
            sum_kld += float(kld.data)
            if t % 10 == 0:
                progress.show(t, n_trains_per_epoch, {})

        progress.show(
            n_trains_per_epoch, n_trains_per_epoch, {
                "x+": sum_energy_positive / n_trains_per_epoch,
                "x-": sum_energy_negative / n_trains_per_epoch,
                "KLD": int(sum_kld / n_trains_per_epoch)
            })
        ddgm.save(args.model_dir)