, ,



information image

This repository incorporates the Tensorflow implementation of the Bayesian GAN by Yunus Saatchi and Andrew Gordon Wilson. This paper will doubtless be showing at NIPS 2017.

The bibliographic knowledge for the paper is

  title={Bayesian {GAN}},
  author={Saatchi, Yunus and Wilson, Andrew Gordon},
  booktitle={Advances in Neural Recordsdata Processing Techniques},


  1. Introduction
  2. Dependencies
  3. Coaching choices
  4. Utilization

    1. Installation
    2. Synthetic Recordsdata
    3. Examples: MNIST, CIFAR10, CelebA, SVHN
    4. Personalized knowledge


Within the Bayesian GAN we propose conditional posteriors for the generator and discriminator weights, and marginalize these posteriors thru stochastic gradient Hamiltonian Monte Carlo. Key properties of the Bayesian solution to GANs encompass (1) appropriate predictions on semi-supervised studying issues; (2) minimal intervention for right efficiency; (three) a probabilistic formulation for inference per adversarial ideas; (Four) avoidance of mode give scheme; and (5) a illustration of extra than one complementary generative and discriminative units for knowledge, forming a probabilistic ensemble.

We illustrate a multimodal posterior over the parameters of the generator. Each and each atmosphere of these parameters corresponds to a pleasurable generative hypothesis for the knowledge. We tag here samples generated for two varied settings of this weight vector, much like varied writing kinds. The Bayesian GAN retains this total distribution over parameters. In distinction, a ancient GAN represents this total distribution with a degree estimate (analogous to a single maximum likelihood solution), lacking doubtlessly compelling explanations for the knowledge.


This code has the following dependencies (version amount needed):

  • python 2.7
  • tensorflow==1.0.0

To set up tensorflow 1.0.0 on linux please notice directions at https://www.tensorflow.org/variations/r1.0/set up/.

You presumably can set up scikit-learn 0.17.1 with the following characterize

pip set up scikit-learn==0.17.1

Alternatively, you’re going to be in a local to develop a conda ambiance and space it up the usage of the provided ambiance.yml file, as such:

conda env develop -f ambiance.yml -n bgan

then load the ambiance the usage of

provide instructed bgan

Coaching choices

bayesian_gan_hmc.py has the following coaching choices.

  • --out_dir: path to the folder, the put the outputs will doubtless be saved
  • --n_save: samples and weights are saved every n_save iterations; default a hundred
  • --z_dim: dimensionalit of z vector for generator; default a hundred
  • --data_path: path to the knowledge; notice knowledge preparation for a detailed
    dialogue; this parameter is required
  • --dataset: can even be mnist, cifar, svhn or celeb; default mnist
  • --gen_observed: amount of information “noticed” by the generator; this affects the
    scaling of the noise variance and prior; default a thousand
  • --batch_size: batch measurement for coaching; default sixty Four
  • --prior_std: std of the prior distribution over the weights; default 1
  • --numz: same as J within the paper; amount of samples of z to integrate it out; default 1
  • --num_mcmc: same as M within the paper; amount of MCMC NN weight samples per z; default 1
  • --lr: studying rate feeble by the Adam optimizer; default 0.0002
  • --optimizer: optimization methodology to be feeble: adam (tf.prepare.AdamOptimizer) or sgd (tf.prepare.MomentumOptimizer); default adam
  • --semi_supervised: discontinue semi-supervised studying
  • --N: amount of labeled samples for semi-supervised studying
  • --train_iter: amount of coaching iterations; default 50000
  • --save_samples: build generated samples staunch thru coaching
  • --save_weights: build weights staunch thru coaching
  • --random_seed: random seed; display masks that atmosphere this seed doesn’t lead to a hundred% reproducible
    outcomes if GPU is feeble

You presumably can additionally sail WGANs with --wasserstein or prepare an ensemble of DCGANs with --ml_ensemble . In particular you’re going to be in a local to prepare a DCGAN with --ml_ensemble 1.



  1. Set up the main dependencies
  2. Clone this repository

Synthetic Recordsdata

To sail the bogus experiment from the paper you’re going to be in a local to employ bgan_synth script. Shall we embrace,
the following comand will prepare the Bayesian GAN (with D=a hundred and d=10) for 5000 iterations and retailer the outcomes in .

./bgan_synth.py --x_dim a hundred --z_dim 10 --numz 10 --out 

To sail the ML GAN for the same knowledge sail

./bgan_synth.py --x_dim a hundred --z_dim 10 --numz 1 --out 

bgan_synth has --save_weights, --out_dir, --z_dim, --numz, --wasserstein, --train_iter and --x_dim parameters.
x_dim contolls the dimensionality of the noticed knowledge (x within the paper). For description of
other parameters please notice Coaching choices.

While you sail the above two commands you’re going to note the output of every and each a centesimal iteration in .
So, let’s sing, the Bayesian GAN’s output on the 900th iteration will request devour:

In distinction, the output of the long-established GAN (much like numz=1, which forces ML estimation) will request devour:

indicating clearly the tendency of mode give scheme within the long-established GAN which, for this synthetic example, is entirely refrained from by the Bayesian GAN.

To in discovering the sythetic experiment extra, and to generate the Jensen-Shannon divergence plots, you’re going to be in a local to take a look at out the notebook synth.ipynb.


bayesian_gan_hmc script enables to prepare the mannequin on long-established and custom datasets. Below
we record the usage of this script.

Recordsdata preparation

To reproduce the experiments on MNIST, CIFAR10, CelebA and SVHN datasets you glean gotten to manage the
knowledge and employ a correct --data_path.

  • for MNIST you develop not deserve to manage the knowledge and can present any --data_path;
  • for CIFAR10 please ranking and extract the python version of the knowledge from
    https://www.cs.toronto.edu/~kriz/cifar.html; then employ the inch to the itemizing containing cifar-10-batches-py as --data_path;
  • for SVHN please ranking train_32x32.mat and test_32x32.mat recordsdata from http://ufldl.stanford.edu/housenumbers/ and employ the itemizing containing these recordsdata as your --data_path;
  • for CelebA you’re going to deserve to glean openCV installed.
    You presumably can ranking the ranking links for the knowledge at http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html.
    You’re going to deserve to develop celebA folder with Anno and img_align_celeba subfolders. Anno should always devour the
    list_attr_celeba.txt and img_align_celeba should always devour the .jpg recordsdata. You’re going to additionally deserve to gash
    the footage by operating datasets/crop_faces.py script with --data_path the put is the inch
    to the folder containing celebA. When coaching the mannequin, you’re going to deserve to employ the same for --data_path;

Unsupervised coaching

You presumably can prepare the mannequin in unsupervised scheme by operating the bayesian_gan_hmc script with out --semi parameter.
Shall we embrace, employ

./bayesian_gan_hmc.py --data_path  --dataset svhn --numz 1 --num_mcmc 10 --out_dir 
 --train_iter 75000 --save_samples --n_save a hundred

to prepare the mannequin on the SVHN dataset. This characterize will sail the methodology for 75000 iterations and build samples every a hundred iterations. Right here should always lead to the itemizing the put the outcomes will doubtless be saved. Glimpse knowledge preparation piece for an clarification of guidelines on how to space . Glimpse coaching choices piece for a top level conception of different coaching choices.


Semi-supervised coaching

You presumably can prepare the mannequin in semi-supervised atmosphere by operating bayesian_gan_hmc with --semi option. Expend -N parameter to space the amount of labeled examples to prepare on. Shall we embrace, employ

./bayesian_gan_hmc.py --data_path  --dataset cifar --numz 1 --num_mcmc 10
--out_dir  --train_iter 75000 --N 4000 --semi --lr 0.00005

to prepare the mannequin on CIFAR10 dataset with 4000 labeled examples. This characterize will prepare the mannequin for 75000 iterations and retailer the outputs in folder.

To prepare the mannequin on MNIST with 200 labeled examples you’re going to be in a local to employ the following characterize.

./bayesian_gan_hmc.py --data_path / --dataset mnist --numz 5 --num_mcmc 5
--out_dir  --train_iter 30000 -N 200 --semi --lr 0.001

Personalized knowledge

To prepare the mannequin on a custom dataset you glean gotten to clarify a category with a particular interface.
Explain we’re making an are attempting to prepare the mannequin on the digits dataset.
This datasets includes 8×8 pictures of digits. Let’s grunt that the knowledge is saved in
x_tr.npy, y_tr.npy, x_te.npy and y_te.npy recordsdata. We are succesful of expend that x_tr.npy and x_te.npy
glean shapes of the strategy (?, eight, eight, 1). We can then present an explanation for the category much like this dataset in
bgan_util.py as follows.

class Digits:

    def __init__(self):
        self.imgs = np.load('x_tr.npy') 
        self.test_imgs = np.load('x_te.npy')
        self.labels = np.load('y_tr.npy')
        self.test_labels = np.load('y_te.npy')
        self.labels = one_hot_encoded(self.labels, 10)
        self.test_labels = one_hot_encoded(self.test_labels, 10) 
        self.x_dim = [8, 8, 1]
        self.num_classes = 10

    def get_batch(batch_size, x, y): 
        """Returns a batch from the given arrays.
        idx = np.random.different(range(x.shape[0]), measurement=(batch_size,), change=Unsuitable)
        return x[idx], y[idx]

    def next_batch(self, batch_size, class_id=None):
        return self.get_batch(batch_size, self.imgs, self.labels)

    def test_batch(self, batch_size):
        return self.get_batch(batch_size, self.test_imgs, self.test_labels)

The class must glean next_batch and test_batch, and must glean the imgs, labels, test_imgs,
test_labels, x_dim and num_classes fields.

Now we can import the Digits class in bayesian_gan_hmc.py

from bgan_util import Digits

and add the following lines to to the processing of --dataset parameter.

if args.dataset == "digits":
    dataset = Digits()

After this preparation is completed, we can prepare the mannequin with, let’s sing,

./bayesian_gan_hmc.py --data_path  --dataset digits --numz 1 --num_mcmc 10 
--out_dir  --train_iter 5000 --save_samples


We thank Pavel Izmailov for relief with stress discovering out this code and establishing the educational.

Read Extra

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%