GenerativeAdversarialNetwork<T>
Models & Types · AiDotNet.NeuralNetworks
Represents a Generative Adversarial Network (GAN), a deep learning architecture that consists of two neural networks (a generator and a discriminator) competing against each other in a zero-sum game.
Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → ImageGeneratorModelLayoutBase<T> → GenerativeAdversarialNetwork<T>
Implements: IAuxiliaryLossLayer<T>
Known derived types: BigGAN<T>, ConditionalGAN<T>, DCGAN<T>, ProgressiveGAN<T>, SAGAN<T>
For Beginners
A GAN is like an art forger and an art detective competing against each other.
Think of it this way:
- The generator is like an art forger trying to create fake paintings that look real
- The discriminator is like an art detective trying to tell which paintings are real and which are fake
- As the forger gets better, the detective has to improve too
- As the detective gets better, the forger is forced to create more convincing fakes
- Eventually, the forger becomes so good that their fake paintings are nearly indistinguishable from real ones
This continuous competition drives both networks to improve, resulting in a generator that can create remarkably realistic synthetic data like images, music, or text.
How It Works
A Generative Adversarial Network (GAN) is a powerful machine learning architecture that uses two neural networks - a generator and a discriminator - that are trained simultaneously through adversarial training. The generator network learns to create realistic synthetic data samples (like images), while the discriminator network learns to distinguish between real data and the generator’s synthetic outputs. As training progresses, the generator becomes better at creating realistic data, and the discriminator becomes better at distinguishing real from fake, pushing each other to improve in a competitive process.
Example
var options = new GenerativeAdversarialNetworkOptions { };
var noise = Tensor<float>.CreateRandom(new[] { 1, 100 });
var trainX = Tensor<float>.CreateRandom(4, 8);
var trainY = Tensor<float>.CreateRandom(4, 2);
var generatorArchitecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 8);
var discriminatorArchitecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 1);
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
.ConfigureModel(new GenerativeAdversarialNetwork<float>(
generatorArchitecture, discriminatorArchitecture,
inputType: InputType.OneDimensional))
.Build(trainX, trainY);
var generated = result.Predict(noise);
Constructors
| Constructor | Summary |
|---|---|
GenerativeAdversarialNetwork(NeuralNetworkArchitecture<T> generatorArchitecture, NeuralNetworkArchitecture<T> discriminatorArchitecture, InputType inputType, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> generatorOptimizer, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> discriminatorOptimizer, ILossFunction<T> lossFunction, GenerativeAdversarialNetworkOptions options) |
Initializes a new instance of the GenerativeAdversarialNetwork class. |
Properties
| Property | Summary |
|---|---|
NeuralNetworkBase<T> Discriminator { get; set; } |
Gets the discriminator network that distinguishes between real and synthetic data. |
NeuralNetworkBase<T> Generator { get; set; } |
Gets the generator network that creates synthetic data. |
T AuxiliaryLossWeight { get; set; } |
Gets or sets the weight for auxiliary losses (gradient penalty, feature matching). |
bool UseAuxiliaryLoss { get; set; } |
Gets or sets whether to use auxiliary losses (gradient penalty, feature matching) during training. |
bool UseFeatureMatching { get; set; } |
Gets or sets whether feature matching is enabled for generator training. |
double FeatureMatchingWeight { get; set; } |
Gets or sets the weight applied to the feature matching loss. |
int[] FeatureMatchingLayers { get; set; } |
Gets or sets the indices of discriminator layers to use for feature matching. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, Tensor<T>> GetNamedLayerActivations(Tensor<T> input) |
Gets named layer activations from the generator network. |
Dictionary<string, double> EvaluateModel(int sampleSize) |
Evaluates the GAN by generating a batch of images and calculating metrics for their quality. |
Dictionary<string, double> EvaluateModelWithTensors(int sampleSize) |
Evaluates the GAN using tensor operations. |
Dictionary<string, string> GetAuxiliaryLossDiagnostics() |
Gets diagnostic information about the auxiliary losses. |
Dictionary<string, string> GetDiagnostics() |
Gets diagnostic information about this component’s state and behavior. |
ModelMetadata<T> GetModelMetadata() |
Gets metadata about the GAN model, including information about both generator and discriminator components. |
T ComputeAuxiliaryLoss() |
Computes the auxiliary loss for the GAN, which includes gradient penalty and feature matching losses. |
T ComputeFeatureMatchingLoss() |
Computes the feature matching loss between real and generated data. |
T ComputeGradientPenalty(Tensor<T> realSamples, Tensor<T> fakeSamples, double lambda) |
|
Tensor<T> DiscriminateImages(Tensor<T> images) |
Evaluates how real a batch of images appears to the discriminator. |
Tensor<T> GenerateImages(Tensor<T> noise) |
Generates synthetic images using tensor operations. |
Tensor<T> GenerateQualityImages(int count, double minDiscriminatorScore) |
Generates high-quality images by filtering based on discriminator scores. |
Tensor<T> GenerateRandomNoiseTensor(int batchSize, int noiseSize) |
Generates a tensor of random noise for the generator. |
ValueTuple<T, T> TrainStep(Tensor<T> realImages, Tensor<T> noise) |
Performs one step of training for both the generator and discriminator using tensor batches. |
void EnableFeatureMatching(bool enable) |
Enables feature matching loss to encourage the generator to match statistics of real data. |
void EnableGradientPenalty(bool enable) |
Enables gradient penalty (WGAN-GP) for improved training stability. |
void EnableGradientPenalty(bool enable, double lambda) |
Enables WGAN-GP gradient penalty and overrides the penalty coefficient. |
void SetTrainingMode(bool isTraining) |
Propagates training/eval mode to the Generator and Discriminator sub-networks. |