WGANGP<T>

Models & Types · AiDotNet.NeuralNetworks

Represents a Wasserstein GAN with Gradient Penalty (WGAN-GP), an improved version of WGAN that uses gradient penalty instead of weight clipping to enforce the Lipschitz constraint.

Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → ImageGeneratorModelLayoutBase<T> → WGANGP<T>

For Beginners

WGAN-GP is an enhanced version of WGAN with better training stability.

Key improvements over WGAN:

  • Uses a “gradient penalty” instead of hard weight limits
  • This penalty gently guides the critic to behave correctly
  • More stable and reliable training
  • Produces higher quality results
  • Easier to use (fewer hyperparameters to tune)

The gradient penalty ensures the critic learns smoothly without the problems that weight clipping can cause.

Reference: Gulrajani et al., “Improved Training of Wasserstein GANs” (2017)

How It Works

WGAN-GP improves upon WGAN by:

  • Replacing weight clipping with a gradient penalty term
  • Providing smoother and more stable training
  • Avoiding pathological behavior caused by weight clipping
  • Achieving better performance and convergence
  • Eliminating the need to tune the clipping threshold

Example

var architecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 4);
var noise = Tensor<float>.CreateRandom(new[] { 1, 100 });
var trainX = Tensor<float>.CreateRandom(4, 8);
var trainY = Tensor<float>.CreateRandom(4, 2);
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
    .ConfigureModel(new WGANGP<float>(architecture))
    .Build(trainX, trainY);
var generated = result.Predict(noise);

Constructors

Constructor Summary
WGANGP(NeuralNetworkArchitecture<T> architecture, WGANGPOptions options) Creates a WGAN-GP with default generator and critic architectures derived from a single architecture.
WGANGP(NeuralNetworkArchitecture<T> generatorArchitecture, NeuralNetworkArchitecture<T> criticArchitecture, InputType inputType, WGANGPOptions options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> generatorOptimizer, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> criticOptimizer, ILossFunction<T> lossFunction) Initializes a new instance of the WGANGP class.

Properties

Property Summary
NeuralNetworkBase<T> Critic { get; set; } Gets the critic network that evaluates data quality.
NeuralNetworkBase<T> Generator { get; set; } Gets the generator network that creates synthetic data.

Methods

Method Summary
Dictionary<string, Tensor<T>> GetNamedLayerActivations(Tensor<T> input) Collects activations by running each subnetwork in turn, not by walking the composite list.
Dictionary<string, double> EvaluateModel(int sampleSize) Evaluates the WGAN-GP by generating images and calculating metrics.
ModelMetadata<T> GetModelMetadata()
Tensor<T> ForwardForTraining(Tensor<T> input) The generator’s training forward, on the same noise-to-image path as PredictCore.
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-GP using tensor batches.
void ResetOptimizerState() Resets both optimizer states for a fresh training run.