Package: keras-adversarial
Combine multiple models into a single Keras model. GANs made easy!AdversarialModelsimulates multi-player games. A single call tomodel.fittakes targets for each player and updates all of the
players. UseAdversarialOptimizerfor complete control of whether
updates are simultaneous, alternating, or something else entirely. No
more fooling withTrainableeither!Installationgitclonehttps://github.com/bstriner/keras_adversarial.gitcdkeras_adversarialpythonsetup.pyinstallUsagePlease check the examples folder for exemplary usage.Instantiating an adversarial modelBuild separate models for each component / player such as generator
and discriminator.Build a combined model. For a GAN, this might have an input for
images and an input for noise and an output for D(fake) and an output
for D(real)Pass the combined model and the separate models to theAdversarialModelconstructoradversarial_model=AdversarialModel(base_model=gan,player_params=[generator.trainable_weights,discriminator.trainable_weights],player_names=["generator","discriminator"])The resulting model will have the same inputs asganbut separate
targets and metrics for each player. This is accomplished by copying the
model for each player. If each player has a different model, useplayer_models(see below regarding dropout).adversarial_model=AdversarialModel(player_models=[gan_g,gan_d],player_params=[generator.trainable_weights,discriminator.trainable_weights],player_names=["generator","discriminator"])Compiling an adversarial modelUseadversarial_compileto compile the model. The parameters are anAdversarialOptimizerand a list ofOptimizerobjects for each
player. The loss is passed tomodel.compilefor each model, so may
be a dictionary or other object. Use the same order forplayer_optimizersas you did forplayer_paramsandplayer_names.model.adversarial_compile(adversarial_optimizer=adversarial_optimizer,player_optimizers=[Adam(1e-4,decay=1e-4),Adam(1e-3,decay=1e-4)],loss='binary_crossentropy')Training a simple adversarial modelAdversarial models can be trained usingfitand callbacks just like
any other Keras model. Just make sure to provide the correct targets in
the correct order.For example, given simple GAN namedgan: * Inputs:[x]*
Targets:[y_fake, y_real]* Metrics:[loss, loss_y_fake, loss_y_real]AdversarialModel(base_model=gan,player_names=['g','d']...)will
have: * Inputs:[x]* Targets:[g_y_fake, g_y_real, d_y_fake, d_y_real]* Metrics:[loss, g_loss, g_loss_y_fake, g_loss_y_real, d_loss, d_loss_y_fake, d_loss_y_real]Adversarial OptimizersThere are many possible strategies for optimizing multiplayer games.AdversarialOptimizeris a base class that abstracts those strategies
and is responsible for creating the training function. *AdversarialOptimizerSimultaneousupdates each player simultaneously
*AdversarialOptimizerAlternatingupdates each player in a
round-robin *UnrolledAdversarialOptimizerunrolls updates to
stabilize training (only tested in Theano; slow to build graph but runs
reasonably fast)ExamplesMNIST Generative Adversarial Network (GAN)example_gan.pyshows how to create a GAN in Keras for the MNIST dataset.Example GANCIFAR10 Generative Adversarial Network (GAN)example_gan_cifar10.pyshows how to create a GAN in Keras for the CIFAR10 dataset.Example GANMNIST Bi-Directional Generative Adversarial Network (BiGAN)example_bigan.pyshows how to create a BiGAN in Keras.Example BiGANMNIST Adversarial Autoencoder (AAE)An AAE is like a cross between a GAN and a Variational Autoencoder
(VAE).example_aae.pyshows how to create an AAE in Keras.Example AAEUnrolled Generative Adversarial Networkexample_gan_unrolled.pyshows how to use the unrolled optimizer.WARNING: Unrolling the discriminator 8 times takes about 6 hours to
build the function on my computer, but only a few minutes for epoch of
training. Be prepared to let it run a long time or turn the depth down
to around 4.NotesDropoutWhen training adversarial models using dropout, you may want to create
separate models for each player.If you want to train a discriminator with dropout, but train the
generator against the discriminator without dropout, create two models.
* GAN to train generator:D(G(z, dropout=0.5), dropout=0)* GAN to
train discriminator:D(G(z, dropout=0), dropout=0.5)If you create separate models, useplayer_modelsparameter ofAdversarialModelconstructor.If you aren’t using dropout, one model is sufficient, and usebase_modelparameter ofAdversarialModelconstructor, which will
duplicate thebase_modelfor each player.Theano and TensorflowI do most of my development in theano but try to test tensorflow when I
have extra time. The goal is to support both. Please let me know any
issues you have with either backend.Questions?Feel free to start an issue or a PR here or in Keras if you are having
any issues or think of something that might be useful.