WGAN<T>
Models & Types · AiDotNet.NeuralNetworks
Represents a Wasserstein Generative Adversarial Network (WGAN), which uses the Wasserstein distance (Earth Mover’s distance) to measure the difference between the generated and real data distributions.
Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → ImageGeneratorModelLayoutBase<T> → WGAN<T>
For Beginners
WGAN is an improved GAN that solves many training problems.
Key improvements over vanilla GAN:
- More stable training (less likely to fail)
- The loss value actually tells you how well training is going
- No mode collapse issues (generating only a few types of outputs)
- Can train the discriminator (critic) many times without problems
The main change is using a different mathematical way to measure the difference between real and fake images, which turns out to be much more stable.
Reference: Arjovsky et al., “Wasserstein GAN” (2017)
How It Works
WGAN addresses several training instabilities in vanilla GANs by:
- Using Wasserstein distance instead of Jensen-Shannon divergence
- Replacing the discriminator with a “critic” that doesn’t output probabilities
- Enforcing a Lipschitz constraint through weight clipping
- Providing a loss that correlates with image quality
- Enabling more stable training and better convergence
Example
var options = new WGANOptions { };
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 criticArchitecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 1);
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
.ConfigureModel(new WGAN<float>(
generatorArchitecture, criticArchitecture,
inputType: InputType.OneDimensional))
.Build(trainX, trainY);
var generated = result.Predict(noise);
Constructors
| Constructor | Summary |
|---|---|
WGAN(NeuralNetworkArchitecture<T> generatorArchitecture, NeuralNetworkArchitecture<T> criticArchitecture, InputType inputType, WGANOptions options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> generatorOptimizer, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> criticOptimizer, ILossFunction<T> lossFunction) |
Initializes a new instance of the WGAN class. |
Properties
| Property | Summary |
|---|---|
NeuralNetworkBase<T> Critic { get; set; } |
Gets the critic network (called discriminator in vanilla GAN) that evaluates data. |
NeuralNetworkBase<T> Generator { get; set; } |
Gets the generator network that creates synthetic data. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, Tensor<T>> GetNamedLayerActivations(Tensor<T> input) |
Named activations of both subnetworks: the generator’s layers, then the critic’s over the image the generator produced. |
Dictionary<string, double> EvaluateModel(int sampleSize) |
Evaluates the WGAN by generating images and calculating metrics. |
ModelMetadata<T> GetModelMetadata() |
|
Tensor<T> GenerateImages(Tensor<T> noise) |
Generates synthetic images using the generator. |
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 training step for the WGAN using tensor batches. |
void ResetOptimizerState() |
Resets both optimizer states for a fresh training run. |