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:
-
Automatic automatic 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:
-
LearningRateMultipliers Automatic learning 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 allBasic Examples (1)Summary of the most common use cases
Define a NetGANOperator in order to generate numbers:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-c5wu2x

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-2so6vs


https://wolfram.com/xid/01lydvc5yzpc6hjtq-bgrkqj

Generate numbers and visualize their distribution:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-1uiocy

Applications (4)Sample problems that can be solved with this function
Sample from a One-Dimensional Distribution (1)

https://wolfram.com/xid/01lydvc5yzpc6hjtq-0tqk1n
Define how to sample from that distribution:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-0jxcqb
Plot the distribution and a histogram of random samples:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-cdmz7x

https://wolfram.com/xid/01lydvc5yzpc6hjtq-xvymdp

Define a generator of scalar values:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-dsco4h

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-eibm1l

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-31rpzx

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-y0m2hx
Sample from this data generator:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-jinlud

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-x3gl2o
Check the monitoring on a random initialization of NetGANOperator:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-26uaf6

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:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-l8gogz

Watch the generation of the trained model:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-hpd0wz

Generate numbers and plot the histogram:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-pnuqqg


https://wolfram.com/xid/01lydvc5yzpc6hjtq-uxb1u9

Sample from a Two-Dimensional Distribution (1)
Take a target two-dimensional distribution:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-8riwt4

https://wolfram.com/xid/01lydvc5yzpc6hjtq-o3epid

Define a generator of pairs of numbers:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-lavzem

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-o1cnd8

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-cdlw8p

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-xc5mji

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-1aac7a
With a random initialization, generated samples are random:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-upz26u

Train NetGANOperator:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-ifp8jv

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-73o8gn

Generate Images of Digits (1)
Take images of the MNIST dataset:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-xf166n

Define a binary discriminator for images:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-q7h5bb

Define a generator for images of the same size:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-onagey

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-82s5kh

https://wolfram.com/xid/01lydvc5yzpc6hjtq-zrltpw

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-11zj5h
Try the function on a randomly initialized NetGANOperator:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-yxw37m

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-vbltro


https://wolfram.com/xid/01lydvc5yzpc6hjtq-tgwpne


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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-1o840y


https://wolfram.com/xid/01lydvc5yzpc6hjtq-2lmyim

https://wolfram.com/xid/01lydvc5yzpc6hjtq-t3c2go

Transform a Digit into Another with Cycle-GAN (1)
Get the images of two different digits:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-ueqxtd

https://wolfram.com/xid/01lydvc5yzpc6hjtq-jq8t17


Take a pretrained image classifier:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-d0w8ph

Define a binary discriminator from this pretrained classifier:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-kivncl


https://wolfram.com/xid/01lydvc5yzpc6hjtq-zeb67k

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-sib3c9

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-3x4vj9

Define a function to visualize the generated images:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-bd4pup
Watch the generated images on a random initialization:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-eqtj11


https://wolfram.com/xid/01lydvc5yzpc6hjtq-4ji7wx


https://wolfram.com/xid/01lydvc5yzpc6hjtq-dgqdcg


https://wolfram.com/xid/01lydvc5yzpc6hjtq-5d0k9s

Properties & Relations (1)Properties of the function, and connections to other functions
Consider a couple of generators and discriminators, and the corresponding NetGANOperator:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-7e3dwv


https://wolfram.com/xid/01lydvc5yzpc6hjtq-vm7hc9


https://wolfram.com/xid/01lydvc5yzpc6hjtq-on85mt

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:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-ibcvzk

Training the NetGANOperator is equivalent to training the following NetGraph:

https://wolfram.com/xid/01lydvc5yzpc6hjtq-ix0q13

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

https://wolfram.com/xid/01lydvc5yzpc6hjtq-kh5jmh

Wolfram Research (2020), NetGANOperator, Wolfram Language function, https://reference.wolfram.com/language/ref/NetGANOperator.html.
Text
Wolfram Research (2020), NetGANOperator, Wolfram Language function, https://reference.wolfram.com/language/ref/NetGANOperator.html.
Wolfram Research (2020), NetGANOperator, Wolfram Language function, https://reference.wolfram.com/language/ref/NetGANOperator.html.
CMS
Wolfram Language. 2020. "NetGANOperator." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NetGANOperator.html.
Wolfram Language. 2020. "NetGANOperator." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NetGANOperator.html.
APA
Wolfram Language. (2020). NetGANOperator. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetGANOperator.html
Wolfram Language. (2020). NetGANOperator. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetGANOperator.html
BibTeX
@misc{reference.wolfram_2025_netganoperator, author="Wolfram Research", title="{NetGANOperator}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/NetGANOperator.html}", note=[Accessed: 26-March-2025
]}
BibLaTeX
@online{reference.wolfram_2025_netganoperator, organization={Wolfram Research}, title={NetGANOperator}, year={2020}, url={https://reference.wolfram.com/language/ref/NetGANOperator.html}, note=[Accessed: 26-March-2025
]}