NetGANOperator

NetGANOperator[{generator,discriminator}]

represents a network to perform Generative Adversarial Network (GAN) training with a generative net generator and a classification net discriminator.

NetGANOperator[{generator,discriminator},loss]

specifies the loss type to be used.

Details and Options

  • NetGANOperator can be used to train generative models on various types of data.
  • During training of a NetGANOperator[], the generator and the discriminator play a zero-sum game against each other. The generator attempts to fool the discriminator, and the discriminator attempts to distinguish real data from generated data.
  • In NetGANOperator[{generator,discriminator},], generator and discriminator should take exactly one input and produce exactly one output. Additionally, generator should produce an output compatible with the input of discriminator, which should output a scalar number.
  • Input ports of NetGANOperator[] are:
  • "Sample"a real sample, which the discriminator should score high
    "Latent"seed input to the generator to produce fake samples
  • Output ports are:
  • "LossReal"loss of the discriminator on the real sample
    "LossFake"loss of the discriminator on the fake sample
    "GeneratedFake"generated fake sample
  • NetGANOperator implements the following architecture:
  • During training of a NetGANOperator[], NetTrain automatically minimizes the sum of losses "LossReal" and "LossFake" for the discriminator. Gradients coming from the discriminator to the generator are inverted, so that NetTrain maximizes "LossFake" for the generator.
  • Typically, TrainingUpdateSchedule is used in NetTrain to avoid updating the discriminator and the generator at the same time. TrainingUpdateSchedule can also be used to update the discriminator more often than the generator.
  • In NetGANOperator[,loss], possible values for loss include:
  • Automaticautomatic loss depending on the discriminator output layer
    "JensenShannon"Jensen-Shannon divergence
    "Wasserstein"approximation of the Earth Mover distance used in Wasserstein-GAN
  • With "JensenShannon", losses are given by:
  • "LossReal"-Log[discriminator[sample]]
    "LossFake"-Log[1-discriminator[generator[latent]]
  • With "Wasserstein", losses are given by:
  • "LossReal"-discriminator[sample]
    "LossFake"discriminator[generator[latent]]
  • The following training parameter can be included:
  • LearningRateMultipliersAutomaticlearning rate multipliers for trainable arrays in the net
  • Options[NetGANOperator] gives the list of default options to construct the operator. Options[NetGANOperator[]] gives the list of default options to evaluate the operator on some data.
  • Information[NetGANOperator[]] gives a report about the operator.
  • Information[NetGANOperator[],prop] gives the value of the property prop of NetGANOperator[]. Possible properties are the same as for NetGraph.

Examples

open allclose all

Basic Examples  (1)

Define a NetGANOperator in order to generate numbers:

Train the operator, alternating updates of the generator and the discriminator:

Extract the generator:

Generate numbers and visualize their distribution:

Applications  (4)

Sample from a One-Dimensional Distribution  (1)

Take a target distribution:

Define how to sample from that distribution:

Plot the distribution and a histogram of random samples:

Define a generator of scalar values:

Define a discriminator that outputs a score for a given scalar:

Create a NetGANOperator that embeds the generator and the discriminator to train them in an adversarial manner:

Create a data generator that yields latent random variables and sampled real data:

Sample from this data generator:

In order to monitor the training of NetGANOperator, it will be useful to make a function to watch generated samples:

Check the monitoring on a random initialization of NetGANOperator:

In this figure, the green curve is the target distribution, the red curve is the discriminator function and the orange histogram represents the distribution of generated fake numbers.

Train a generative adversarial network and watch live how it performs:

Watch the generation of the trained model:

Generate numbers and plot the histogram:

Sample from a Two-Dimensional Distribution  (1)

Take a target two-dimensional distribution:

Plot the distribution:

Define a generator of pairs of numbers:

Define a discriminator that outputs a score for a given pair of numbers:

Create a NetGANOperator that embeds the generator and the discriminator to train them in an adversarial manner:

Compute the losses and the generated pair of points on some data:

Write a function to visualize how the generator and the discriminator of NetGANOperator are performing:

With a random initialization, generated samples are random:

Train NetGANOperator:

After training, the generated samples fit the target distribution better:

Generate Images of Digits  (1)

Take images of the MNIST dataset:

Define a binary discriminator for images:

Define a generator for images of the same size:

Define a data generator that yields random seeds and sampled images of digits:

Define a function to monitor how good the generated images are:

Try the function on a randomly initialized NetGANOperator:

Train a NetGANOperator to generate images, watching generated images through the training process:

Extract the generator from the trained NetGANOperator and use it to generate new images of digits:

Transform a Digit into Another with Cycle-GAN  (1)

Get the images of two different digits:

See some random images:

Take a pretrained image classifier:

Define a binary discriminator from this pretrained classifier:

Define a generator that takes an image and generates another image of the same shape:

Define a Cycle-GAN architecture, using two instances of NetGANOperator and shared generators, adding cycle consistency losses to adversarial losses:

Define a function to visualize the generated images:

Watch the generated images on a random initialization:

Train the Cycle-GAN:

Properties & Relations  (1)

Consider a couple of generators and discriminators, and the corresponding NetGANOperator:

The generator inside NetGANOperator receives inverted gradients from the discriminator.

If NetGANOperator is trained alone, with no layer before or after the generator, this is equivalent to using a negative learning rate multiplier:

Training the NetGANOperator is equivalent to training the following NetGraph:

It is also equivalent to the following NetGraph, where the discriminator is shared at two places:

Introduced in 2020
 (12.1)