Table of Contents

Class ProgressiveGAN<T>

Namespace
AiDotNet.NeuralNetworks
Assembly
AiDotNet.dll

Production-ready Progressive GAN (ProGAN) implementation that generates high-resolution images by progressively growing the generator and discriminator during training.

For Beginners: Progressive GAN is a technique for training GANs that can generate very high-resolution images (e.g., 1024x1024 pixels). Instead of trying to generate high-resolution images from the start, it begins by generating small images (e.g., 4x4) and progressively adds new layers to both the generator and discriminator to increase the resolution (4x4 → 8x8 → 16x16 → 32x32 → 64x64 → 128x128 → 256x256 → 1024x1024).

Key innovations:

  1. Progressive Growing: Start with low resolution and gradually add layers
  2. Smooth Fade-in: New layers are faded in smoothly using a blending parameter (alpha)
  3. Minibatch Standard Deviation: Helps prevent mode collapse by adding diversity
  4. Equalized Learning Rate: Normalizes weights at runtime for better training dynamics
  5. Pixel Normalization: Normalizes feature vectors in generator to prevent escalation

Based on "Progressive Growing of GANs for Improved Quality, Stability, and Variation" by Karras et al. (2018)

public class ProgressiveGAN<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, INeuralNetwork<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable

Type Parameters

T

The numeric type for computations (e.g., double, float)

Inheritance
ProgressiveGAN<T>
Implements
IFullModel<T, Tensor<T>, Tensor<T>>
IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>
IParameterizable<T, Tensor<T>, Tensor<T>>
ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>
IGradientComputable<T, Tensor<T>, Tensor<T>>
Inherited Members
Extension Methods

Constructors

ProgressiveGAN(NeuralNetworkArchitecture<T>, NeuralNetworkArchitecture<T>, int, int, int, int, InputType, ILossFunction<T>?, double, double)

Initializes a new instance of Progressive GAN.

public ProgressiveGAN(NeuralNetworkArchitecture<T> generatorArchitecture, NeuralNetworkArchitecture<T> discriminatorArchitecture, int latentSize = 512, int imageChannels = 3, int maxResolutionLevel = 6, int baseFeatureMaps = 512, InputType inputType = InputType.TwoDimensional, ILossFunction<T>? lossFunction = null, double initialLearningRate = 0.001, double learningRateDecay = 0.9999)

Parameters

generatorArchitecture NeuralNetworkArchitecture<T>

Architecture for the generator network.

discriminatorArchitecture NeuralNetworkArchitecture<T>

Architecture for the discriminator network.

latentSize int

Size of the latent vector (typically 512)

imageChannels int

Number of image channels (1 for grayscale, 3 for RGB)

maxResolutionLevel int

Maximum resolution level (0=4x4, 1=8x8, ..., 8=1024x1024)

baseFeatureMaps int

Base number of feature maps (doubled at lower resolutions)

inputType InputType

The type of input.

lossFunction ILossFunction<T>

Loss function for training (defaults to mean squared error for WGAN-GP style)

initialLearningRate double

Initial learning rate (default 0.001)

learningRateDecay double

Learning rate decay factor per update (default 0.9999)

Properties

Alpha

Gets or sets the alpha value for smooth fade-in of new layers. 0 = old layers only, 1 = new layers fully active.

public double Alpha { get; set; }

Property Value

double

CurrentResolutionLevel

Gets the current resolution level (e.g., 0=4x4, 1=8x8, 2=16x16, etc.).

public int CurrentResolutionLevel { get; }

Property Value

int

Discriminator

Gets the discriminator (critic) network that evaluates image quality. Progressively grows to handle higher resolution images.

public ConvolutionalNeuralNetwork<T> Discriminator { get; }

Property Value

ConvolutionalNeuralNetwork<T>

DiscriminatorLosses

Gets the discriminator loss history.

public IReadOnlyList<T> DiscriminatorLosses { get; }

Property Value

IReadOnlyList<T>

Generator

Gets the generator network that produces images from latent codes. Progressively grows to generate higher resolution images.

public ConvolutionalNeuralNetwork<T> Generator { get; }

Property Value

ConvolutionalNeuralNetwork<T>

GeneratorLosses

Gets the generator loss history.

public IReadOnlyList<T> GeneratorLosses { get; }

Property Value

IReadOnlyList<T>

IsFadingIn

Gets whether the network is currently in the fade-in phase after a growth step.

public bool IsFadingIn { get; }

Property Value

bool

LastGradientPenalty

Gets the last computed gradient penalty value (for monitoring purposes only). NOTE: The gradient penalty is NOT included in training because proper WGAN-GP requires second-order gradients (d/dθ ||∇_x D(x)||²) which this engine does not support. This property allows users to monitor the GP value during training for diagnostic purposes.

public T LastGradientPenalty { get; }

Property Value

T

LatentSize

Gets the size of the latent vector (noise input). Typically 512 for high-quality image generation.

public int LatentSize { get; }

Property Value

int

MaxResolutionLevel

Gets the maximum resolution level the network can achieve.

public int MaxResolutionLevel { get; }

Property Value

int

ParameterCount

Gets the total number of trainable parameters in the ProgressiveGAN.

public override int ParameterCount { get; }

Property Value

int

Remarks

This includes all parameters from both the Generator and Discriminator networks.

UseMinibatchStdDev

Gets or sets whether to use minibatch standard deviation. Helps improve diversity and prevent mode collapse.

public bool UseMinibatchStdDev { get; set; }

Property Value

bool

UsePixelNormalization

Gets or sets whether to use pixel normalization in the generator. Helps prevent unhealthy competition between feature magnitudes.

public bool UsePixelNormalization { get; set; }

Property Value

bool

Methods

ClearLossHistory()

Clears the loss history.

public void ClearLossHistory()

CreateNewInstance()

Creates a new instance of the same type as this neural network.

protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()

Returns

IFullModel<T, Tensor<T>, Tensor<T>>

A new instance of the same neural network type.

Remarks

For Beginners: This creates a blank version of the same type of neural network.

It's used internally by methods like DeepCopy and Clone to create the right type of network before copying the data into it.

DeserializeNetworkSpecificData(BinaryReader)

Deserializes network-specific data that was not covered by the general deserialization process.

protected override void DeserializeNetworkSpecificData(BinaryReader reader)

Parameters

reader BinaryReader

The BinaryReader to read the data from.

Remarks

This method is called at the end of the general deserialization process to allow derived classes to read any additional data specific to their implementation.

For Beginners: Continuing the suitcase analogy, this is like unpacking that special compartment. After the main deserialization method has unpacked the common items (layers, parameters), this method allows each specific type of neural network to unpack its own unique items that were stored during serialization.

Generate(Tensor<T>)

Generates images from specific latent codes.

public Tensor<T> Generate(Tensor<T> latentCodes)

Parameters

latentCodes Tensor<T>

Latent codes to use

Returns

Tensor<T>

Generated images tensor

Generate(int)

Generates images from random latent codes.

public Tensor<T> Generate(int numImages)

Parameters

numImages int

Number of images to generate

Returns

Tensor<T>

Generated images tensor

GetCurrentResolution()

Gets the current image resolution based on the resolution level.

public int GetCurrentResolution()

Returns

int

GetModelMetadata()

Gets the metadata for this neural network model.

public override ModelMetadata<T> GetModelMetadata()

Returns

ModelMetadata<T>

A ModelMetaData object containing information about the model.

GetParameters()

Gets all trainable parameters of the network as a single vector.

public override Vector<T> GetParameters()

Returns

Vector<T>

A vector containing all parameters of the network.

Remarks

For Beginners: Neural networks learn by adjusting their "parameters" (also called weights and biases). This method collects all those adjustable values into a single list so they can be updated during training.

GrowNetworks()

Grows the networks to the next resolution level. Call this periodically during training to progressively increase resolution.

public bool GrowNetworks()

Returns

bool

True if growth was successful, false if already at maximum resolution

Remarks

Current implementation: Updates metadata (resolution level, alpha) for blending output resolutions. The Generator/Discriminator architectures must be pre-configured for the maximum target resolution, and this method controls which resolution path is active via the alpha blending factor.

Note: This is a simplified progressive growing implementation. True progressive growing (dynamically adding layers at runtime) would require significant architectural changes to support on-the-fly network mutation while preserving learned weights.

InitializeLayers()

Initializes the layers of the neural network based on the architecture.

protected override void InitializeLayers()

Remarks

For Beginners: This method sets up all the layers in your neural network according to the architecture you've defined. It's like assembling the parts of your network before you can use it.

Predict(Tensor<T>)

Makes a prediction using the neural network.

public override Tensor<T> Predict(Tensor<T> input)

Parameters

input Tensor<T>

The input data to process.

Returns

Tensor<T>

The network's prediction.

Remarks

For Beginners: This is the main method you'll use to get results from your trained neural network. You provide some input data (like an image or text), and the network processes it through all its layers to produce an output (like a classification or prediction).

SerializeNetworkSpecificData(BinaryWriter)

Serializes network-specific data that is not covered by the general serialization process.

protected override void SerializeNetworkSpecificData(BinaryWriter writer)

Parameters

writer BinaryWriter

The BinaryWriter to write the data to.

Remarks

This method is called at the end of the general serialization process to allow derived classes to write any additional data specific to their implementation.

For Beginners: Think of this as packing a special compartment in your suitcase. While the main serialization method packs the common items (layers, parameters), this method allows each specific type of neural network to pack its own unique items that other networks might not have.

SetTrainingMode(bool)

Sets the neural network to either training or inference mode.

public override void SetTrainingMode(bool isTraining)

Parameters

isTraining bool

True to enable training mode; false to enable inference mode.

Remarks

For Beginners: Neural networks behave differently during training versus when making predictions.

When in training mode (isTraining = true): - The network keeps track of intermediate calculations needed for learning - Certain layers like Dropout and BatchNormalization behave differently - The network uses more memory but can learn from its mistakes

When in inference/prediction mode (isTraining = false): - The network only performs forward calculations - It uses less memory and runs faster - It cannot learn or update its parameters

Think of it like the difference between taking a practice test (training mode) where you can check your answers and learn from mistakes, versus taking the actual exam (inference mode) where you just give your best answers based on what you've already learned.

Train(Tensor<T>, Tensor<T>)

Trains the neural network on a single input-output pair.

public override void Train(Tensor<T> input, Tensor<T> expectedOutput)

Parameters

input Tensor<T>

The input data.

expectedOutput Tensor<T>

The expected output for the given input.

Remarks

This method performs one training step on the neural network using the provided input and expected output. It updates the network's parameters to reduce the error between the network's prediction and the expected output.

For Beginners: This is how your neural network learns. You provide: - An input (what the network should process) - The expected output (what the correct answer should be)

The network then:

  1. Makes a prediction based on the input
  2. Compares its prediction to the expected output
  3. Calculates how wrong it was (the loss)
  4. Adjusts its internal values to do better next time

After training, you can get the loss value using the GetLastLoss() method to see how well the network is learning.

TrainStep(Tensor<T>, int)

Performs a single training step on a batch of real images. Uses vectorized operations throughout for optimal performance.

public (T discriminatorLoss, T generatorLoss) TrainStep(Tensor<T> realImages, int batchSize)

Parameters

realImages Tensor<T>

Batch of real images

batchSize int

Number of images in the batch

Returns

(T Accuracy, T Loss)

Tuple of (discriminator loss, generator loss)

Exceptions

ArgumentNullException

Thrown when realImages is null

ArgumentOutOfRangeException

Thrown when batchSize is not positive

UpdateAlpha(double)

Updates the alpha value for progressive fade-in during training. Should be called each training step during fade-in phase to smoothly blend new layers.

public bool UpdateAlpha(double alphaIncrement)

Parameters

alphaIncrement double

Amount to increase alpha per step (typical: 1.0 / fadeInSteps)

Returns

bool

True if still fading in, false if fade-in is complete (Alpha >= 1.0)

UpdateParameters(Vector<T>)

Updates the network's parameters with new values.

public override void UpdateParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

The new parameter values to set.

Remarks

For Beginners: During training, a neural network's internal values (parameters) get adjusted to improve its performance. This method allows you to update all those values at once by providing a complete set of new parameters.

This is typically used by optimization algorithms that calculate better parameter values based on training data.