---
title: Conditional Recurrent GAN (cR-GAN)
url: https://www.emergentmind.com/topics/conditional-recurrent-gan-cr-gan
type: topic
---

# Conditional Recurrent GAN (cR-GAN)

A conditional recurrent GAN (cR-GAN) is a generative adversarial framework that incorporates recurrent structures and explicit conditional mechanisms, enabling the generation of sequential data conditioned on specified attributes. In the clinical time-series domain, cR-GANs are realized by Multi-label Time-series GANs (MTGAN), which are designed for generating realistic and controllable electronic health record (EHR) sequences, particularly addressing the challenge of rare disease modeling. These architectures employ gated recurrent units (GRUs) for sequential modeling, smooth conditioning matrices to enforce explicit label control, and critic (discriminator) networks that score entire sequences along with temporal features using Wasserstein distance and gradient penalty for stable training [2204.04797].

## 1. Generator Architecture and Recurrence

The generator $G$ accepts as input a noise vector $z\in\mathbb{R}^s$ (where $s$ is the GRU hidden-state dimension) and a target disease index $i\in\{1,\dots,d\}$. The output is a patient-level time-series matrix of disease probabilities:
\[
P = [P_1,\;P_2,\;\dots,\;P_T]\in[0,1]^{d\times T}
\]
and the corresponding GRU hidden states
\[
\widetilde H = [\tilde h_1,\;\tilde h_2,\;\dots,\;\tilde h_T]\in\mathbb{R}^{s\times T}.
\]

Generation proceeds by:

- Initializing the hidden state: $\tilde h_0 = 0$.
- First-visit probability: $P_1 = \sigma(W_z\,z + b_z)\in(0,1)^d$.
- For each $t=1,\ldots,T-1$:
  \[
  \tilde h_t = g_{\mathrm{gru}}(P_t,\,\tilde h_{t-1})
  \]
  \[
  P_{t+1} = \sigma(W_h\,\tilde h_t + b_h)\in(0,1)^d
  \]

The GRU cell is specified by:
\[
\begin{aligned}
r_t &= \sigma(W_r\,P_t + U_r\,\tilde h_{t-1} + b_r)  \\
u_t &= \sigma(W_u\,P_t + U_u\,\tilde h_{t-1} + b_u)  \\
\hat h_t &= \tanh(W_c\,P_t + U_c\,(r_t\odot \tilde h_{t-1}) + b_c ) \\
\tilde h_t &= u_t \odot \tilde h_{t-1} + (1-u_t)\odot \hat h_t 
\end{aligned}
\]
where $\odot$ denotes the element-wise product and all $W$, $U$, $b$ are appropriately dimensioned parameters.

## 2. Conditional and Attention Mechanisms

Disease-conditioning employs a "smooth conditional matrix" $C\in\mathbb{R}^{d\times T}$. Steps are:

- For each time $t$:
  \[
  v_t = w_v^\top\,P_t
  \]
  \[
  \mathrm{score}_t = \frac{\exp(v_t)}{\sum_{\tau=1}^T \exp(v_\tau)}
  \]
  where $w_v\in\mathbb{R}^d$ is a learnable vector.
- Set $C_{i,t} = \mathrm{score}_t$ for the target disease $i$.
- Final output:
  \[
  \widetilde P = \min\{1,\;P+C\} \in [0,1]^{d\times T}
  \]

This smooths the presence of rare diseases across the synthetic trajectory, raising the likelihood of their consistent appearance throughout the sequence and drastically reducing the "rare-needle" (RN) metric from $>10^7$ down to $\sim10^4$ for rare codes [2204.04797].

## 3. Critic Network and Wasserstein Loss

The critic $D$ evaluates the realism of a full time-series sequence in conjunction with its temporal features:

- For real data $X=(x_1,\ldots,x_T)\in\{0,1\}^{d\times T}$, compute temporal features $H=(h_1,\ldots,h_T)$ via a frozen GRU, $g'_{\mathrm{gru}}$.
- Concatenate at each time: $m_t = x_t\,\Vert\,h_t \in\mathbb{R}^{d+s}$.
- Pass $m_t$ through a small MLP and average over $t$:
  \[
  r = D(X,H) = \frac{1}{T}\sum_{t=1}^T \mathrm{MLP}(m_t)
  \]
- Use Wasserstein-GP loss:
  \[
  L_D = \mathbb{E}_{\tilde X}[D(\tilde X, \widetilde H)] - \mathbb{E}_X[D(X,H)] + \lambda\,\mathbb{E}_{\hat X, \hat H}\left[(\|\nabla_{(\hat X, \hat H)} D(\hat X, \hat H)\|_2 - 1)^2\right]
  \]
  where $(\hat X, \hat H)$ are linear interpolations between real and generated samples, and $\lambda$ is the penalty coefficient.

The generator loss is
\[
L_G = -\mathbb{E}_{z,i}[D(G(z,i))]
\]

## 4. Training Procedure and Stabilization

Training alternates updating the critic and the generator. The procedure includes:

- Pre-training the GRU $g'_{\mathrm{gru}}$ on next-visit binary prediction (binary-cross-entropy), then freezing it to extract real temporal features for the critic.
- Discrete sampling: instead of feeding $[0,1]$-valued probabilities directly to $D$, each $\widetilde P_{t,i}$ is Bernoulli-sampled to yield $\tilde x_{t,i}\in\{0,1\}$.
- Pseudocode:

  ```
  Given: real dataset 𝓓, batch size B,
         pre-trained GRU g′ for real-feature H,
         critic-updates n_critic, λ for GP.
  repeat until convergence:
    sample target disease i ∼ Uniform{1…d}
    #— Train critic n_critic times:
    for j=1…n_critic:
      sample B real sequences X∼p(x|i) that contain disease i
      compute H = g′(X)
      sample B noise vectors z∼p(z)
      (P̃, H̃) = G(z,i)  # generator forward
      sample X̃∼Bernoulli(P̃) # discrete visits
      sample ε∼U[0,1], set
        X̂ = ε X + (1−ε) X̃,
        Ĥ = ε H + (1−ε) H̃
      compute ∇D(X̂,Ĥ) and the GP term
      update D by ∇_{D} L_D
    #— Train generator once:
    sample B noise vectors z∼p(z)
    (P̃, H̃) = G(z,i)
    update G by ∇_{G} L_G
  ```

Standard settings: GRU hidden size $s=256$, $\dim(z)=256$, batch size $=256$, $n_{\mathrm{critic}}=1$, $\lambda=10$, learning rates $\ell_r(G)=10^{-4}$, $\ell_r(D)=10^{-5}$ with 0.1 decay every $10^5$ steps, Adam with $\beta_1=0.5$, $\beta_2=0.9$, pre-training epochs $=200$, $\ell_r=10^{-3}$ [2204.04797].

## 5. Evaluation Metrics and Downstream Tasks

Assessment comprises statistical and predictive metrics:

- **Statistical:**
  - GT: Number of distinct disease codes appearing in the synthetic set.
  - JSD$_v$, JSD$_p$: Jensen–Shannon divergence, visit and patient-level disease frequency distributions.
  - ND$_v$, ND$_p$: Normalized distance (for rare diseases):
    \[
    \mathrm{ND} = \frac{1}{d}\sum_{i=1}^d \frac{2\,|p_{\mathrm{real}}(i)-p_{\mathrm{syn}}(i)|}{p_{\mathrm{real}}(i)+p_{\mathrm{syn}}(i)}
    \]
  - RN: Number of synthetic samples needed before all $d$ real disease codes appear at least once.

- **Downstream prediction:** Models trained on synthetic + real data are evaluated on:
  - Multi-label diagnosis prediction (weighted $F_1$ for $T+1$ visit).
  - Heart-failure onset at $T+1$ (AUC).
  - Parkinson’s disease onset at $T+1$ (AUC).

RNN-based predictors evaluated include Dipole and GRAM, with and without synthetic pre-training. MTGAN yields lowest JSD/ND, much improved GT/RN, and the greatest improvement on rare-disease prediction (e.g., Parkinson’s onset) in MIMIC-III/IV datasets [2204.04797].

## 6. Limitations and Future Directions

MTGAN (cR-GAN) is limited to the generation of discrete diagnosis code sequences. Continuous variables such as labs, vitals, or medication dosages, as well as missing-data patterns, are not modeled in the current framework. Prospective research aims to extend cR-GANs to handle mixed-type EHR (discrete + continuous), incorporate privacy preservation (e.g., differential privacy), and multi-modal data (e.g., notes combined with codes) [2204.04797].

## 7. Context within Sequential Conditional GANs

The conditional recurrent GAN paradigm demonstrates utility beyond clinical EHR, such as in perceptual video compression, where recurrent conditional discriminators enforce both spatial and temporal consistency in generated sequences, as exemplified in PLVC [2109.03082]. Both domains leverage conditioning (on target labels, or latent/motion/temporal state) and recurrence (via GRUs or ConvLSTMs) to enable high-fidelity, temporally coherent data generation. This suggests a broader applicability of cR-GANs to diverse sequential generative modeling tasks where explicit control and memory are operational requisites.

Source: https://www.emergentmind.com/topics/conditional-recurrent-gan-cr-gan