Implementation:Microsoft DeepSpeedExamples GAN Generator Discriminator
| Knowledge Sources | |
|---|---|
| Domains | Generative Adversarial Networks, Deep Learning |
| Last Updated | 2026-02-07 12:00 GMT |
Overview
Defines the Generator and Discriminator model classes for a DCGAN architecture, along with a weight initialization function.
Description
This module implements a standard Deep Convolutional GAN (DCGAN) architecture with two core classes. The Generator class takes a latent noise vector of dimension nz and progressively upsamples it through a series of transposed convolution layers (ConvTranspose2d), batch normalization, and ReLU activations, producing a 64x64 image with nc output channels and a final Tanh activation. The architecture uses a feature map multiplier ngf to control the width of each layer.
The Discriminator class performs the inverse operation, taking an nc-channel 64x64 image and progressively downsampling it through Conv2d layers with batch normalization and LeakyReLU(0.2) activations, producing a single scalar output with a Sigmoid activation for real/fake classification. The feature map multiplier ndf controls the width of each layer.
Both models support multi-GPU execution via nn.parallel.data_parallel when the input is on CUDA and ngpu > 1. The weights_init function initializes convolutional layers with normal distribution (mean=0, std=0.02) and batch normalization layers with normal weights (mean=1, std=0.02) and zero bias, following standard DCGAN initialization practice.
Usage
Use these model classes when training a DCGAN with DeepSpeed. The Generator and Discriminator are instantiated with the desired architecture parameters and initialized with weights_init before being passed to the DeepSpeed training engine.
Code Reference
Source Location
- Repository: Microsoft_DeepSpeedExamples
- File: training/gan/gan_model.py
- Lines: 1-76
Signature
def weights_init(m):
...
class Generator(nn.Module):
def __init__(self, ngpu, ngf, nc, nz):
...
def forward(self, input):
...
class Discriminator(nn.Module):
def __init__(self, ngpu, ndf, nc):
...
def forward(self, input):
...
Import
from gan_model import Generator, Discriminator, weights_init
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| ngpu | int | Yes | Number of GPUs to use for data parallelism |
| ngf | int | Yes | Size of feature maps in the Generator (base channel count) |
| ndf | int | Yes | Size of feature maps in the Discriminator (base channel count) |
| nc | int | Yes | Number of image channels (e.g., 3 for RGB) |
| nz | int | Yes | Size of the latent noise vector (Generator only) |
| input | torch.Tensor | Yes | Input tensor: (batch, nz, 1, 1) for Generator; (batch, nc, 64, 64) for Discriminator |
Outputs
| Name | Type | Description |
|---|---|---|
| Generator output | torch.Tensor | Generated image tensor of shape (batch, nc, 64, 64) with values in [-1, 1] |
| Discriminator output | torch.Tensor | Probability scores of shape (batch,) indicating real/fake classification |
Usage Examples
from gan_model import Generator, Discriminator, weights_init
import torch
ngpu = 1
nz = 100
ngf = 64
ndf = 64
nc = 3
# Create models
netG = Generator(ngpu=ngpu, ngf=ngf, nc=nc, nz=nz)
netG.apply(weights_init)
netD = Discriminator(ngpu=ngpu, ndf=ndf, nc=nc)
netD.apply(weights_init)
# Generate fake images
noise = torch.randn(16, nz, 1, 1)
fake_images = netG(noise) # Shape: (16, 3, 64, 64)
# Discriminate
scores = netD(fake_images) # Shape: (16,)