SAGAN<T>
Models & Types · AiDotNet.NeuralNetworks
Self-Attention GAN (SAGAN) — a deep convolutional GAN whose generator maps a 1D latent vector to an image and whose discriminator scores images as real or fake. SAGAN (Zhang et al. 2019) augments the DCGAN backbone with self-attention and spectral normalization for long-range coherence and training stability.
For Beginners: A GAN has two networks competing: a generator (the “artist”) that turns random noise into images, and a discriminator (the “critic”) that tells real images from generated ones. As in every GAN since Goodfellow et al. 2014, the generator’s INPUT is a 1D latent noise vector z (e.g. 128 numbers). Its first layer is a fully-connected projection that reshapes z into a small spatial feature map, which transposed convolutions then upsample into a full image. SAGAN’s distinctive ingredient is self-attention, letting each location attend to the whole feature map so global structure stays consistent.
This implementation derives from GenerativeAdversarialNetwork,
which provides the proven adversarial training loop (tape-based generator and
discriminator updates with BCE-with-logits, per Goodfellow 2014 §3 / Radford
2015), and builds paper-faithful generator/discriminator architectures:
• Generator: latent[B, latentSize] → Dense → Reshape[C₀, s, s] → ⟨Deconv 4×4
stride 2 + BatchNorm + ReLU⟩×log₂(target/s) → Deconv → image[C, H, W] (Tanh).
• Discriminator: image[C, H, W] → ⟨Conv 4×4 stride 2 (+ BatchNorm) + LeakyReLU⟩
→ Conv → logit (no final sigmoid; BCE-with-logits is the criterion).
Based on “Self-Attention Generative Adversarial Networks” by Zhang et al. (2019).
Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → ImageGeneratorModelLayoutBase<T> → GenerativeAdversarialNetwork<T> → SAGAN<T>
Example
var noise = Tensor<float>.CreateRandom(new[] { 1, 128 });
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 SAGAN<float>(latentSize: 128, imageChannels: 3, imageHeight: 64, imageWidth: 64))
.Build(trainX, trainY);
var generated = result.Predict(noise);
Constructors
| Constructor | Summary |
|---|---|
SAGAN(NeuralNetworkArchitecture<T> architecture, SAGANOptions options) |
Convenience constructor deriving image dimensions from a single architecture. |
SAGAN(NeuralNetworkArchitecture<T> generatorArchitecture, NeuralNetworkArchitecture<T> discriminatorArchitecture, SAGANOptions options, int[] attentionLayers, ILossFunction<T> lossFunction) |
Backward-compatible constructor that accepts explicit generator/discriminator architectures. |
SAGAN(int latentSize, int imageChannels, int imageHeight, int imageWidth, SAGANOptions options, int[] attentionLayers, ILossFunction<T> lossFunction) |
Initializes a SAGAN with paper-faithful architectures built from the image dimensions and latent size. |
Properties
| Property | Summary |
|---|---|
bool UseSpectralNormalization { get; set; } |
Gets or sets whether spectral normalization is used. |
int LatentSize { get; } |
Gets the size of the latent (noise) vector input to the generator. |
int NumClasses { get; } |
Gets the number of classes for conditional generation (0 for unconditional). |
int[] AttentionLayers { get; } |
Gets the indices of layers where self-attention is applied (Zhang et al. |
Methods
| Method | Summary |
|---|---|
ModelMetadata<T> GetModelMetadata() |