ConditionalGAN<T>

Models & Types · AiDotNet.NeuralNetworks

Represents a Conditional Generative Adversarial Network (cGAN), which generates data conditioned on additional information such as class labels, attributes, or other contextual data.

Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → ImageGeneratorModelLayoutBase<T> → GenerativeAdversarialNetwork<T> → ConditionalGAN<T>

For Beginners

cGAN lets you control what kind of image is generated.

Key features:

  • You can specify what you want to generate (e.g., “cat” vs. “dog”)
  • Both the generator and discriminator see the conditioning information
  • Generator: “Given this label, create a matching image”
  • Discriminator: “Is this image both real AND matching the label?”

Example use cases:

  • Generate a specific digit (0-9) in MNIST
  • Create images of specific object classes
  • Generate faces with specific attributes (smiling, glasses, etc.)

Reference: Mirza and Osindero, “Conditional Generative Adversarial Nets” (2014)

How It Works

Conditional GANs extend the basic GAN framework by:

  • Conditioning both the generator and discriminator on additional information
  • Allowing controlled generation (e.g., “generate a digit 7”)
  • Enabling class-conditional image synthesis
  • Providing explicit control over the generated output characteristics

Example

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 ConditionalGAN<float>())
    .Build(trainX, trainY);
var generated = result.Predict(noise);

Constructors

Constructor Summary
ConditionalGAN() Initializes a new instance of the ConditionalGAN class with default configuration.
ConditionalGAN(NeuralNetworkArchitecture<T> generatorArchitecture, NeuralNetworkArchitecture<T> discriminatorArchitecture, int numConditionClasses, InputType inputType, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> generatorOptimizer, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> discriminatorOptimizer, ILossFunction<T> lossFunction, ConditionalGANOptions options) Initializes a new instance of the ConditionalGAN class.

Methods

Method Summary
ModelMetadata<T> GetModelMetadata()
Tensor<T> CreateOneHotCondition(int batchSize, int classIndex) Creates a one-hot encoded condition tensor.
Tensor<T> GenerateConditional(Tensor<T> noise, Tensor<T> conditions) Generates images conditioned on specific labels.
Tensor<T> GenerateRandomNoiseTensor(int batchSize, int noiseSize) Generates random noise tensor using vectorized Gaussian noise generation.
ValueTuple<T, T> TrainStep(Tensor<T> realImages, Tensor<T> conditions, Tensor<T> noise) Performs one training step for the conditional GAN.