Transformer-based Conditional Variational Autoencoder for Controllable Story Generation

Le Fang, Tao Zeng, Chaochun Liu, Liefeng Bo, Wen Dong, Changyou Chen

Abstract

We investigate large-scale latent variable models (LVMs) for neural story generation—an under-explored application for open-domain long text—with objectives in two threads: generation effectiveness and controllability. LVMs, especially the variational autoencoder (VAE), have achieved both effective and controllable generation through exploiting flexible distributional latent representations. Recently, Transformers and its variants have achieved remarkable effectiveness without explicit latent representation learning, thus lack satisfying controllability in generation. In this paper, we advocate to revive latent variable modeling, essentially the power of representation learning, in the era of Transformers to enhance controllability without hurting state-of-the-art generation effectiveness. Specifically, we integrate latent representation vectors with a Transformer-based pre-trained architecture to build conditional variational autoencoder (CVAE). Model components such as encoder, decoder and the variational posterior are all built on top of pre-trained language models—GPT2 specifically in this paper. Experiments demonstrate state-of-the-art conditional generation ability of our model, as well as its excellent representation learning capability and controllability.

Introduction

In the current landscape of Natural Language Processing, Transformers like GPT-2 are the undisputed leaders in text generation, capable of producing remarkably human-like stories. However, these models often lack controllability. Because they do not maintain an explicit internal representation of a story's holistic features, it is difficult for users to steer the narrative's development or semantic transitions.

Historically, Recurrent Neural Network (RNN)-based Variational Autoencoders (VAEs) excelled at controllability by mapping text into a "latent space"—a mathematical field where different points represent distinct styles or topics. Unfortunately, RNNs cannot match the generation quality of modern Transformers. This research addresses this gap by "reviving" latent variable modeling within the Transformer framework to enable controllable, high-quality, long-form story generation.

Methodology: Transformer-based CVAE

The proposed architecture is built on top of pre-trained GPT-2 models, augmented with components to handle latent variables.

The Transformer-based CVAE architecture, illustrating the encoder, attention-average block, and latent injection points into the decoder.

Architectural Components

  1. The Encoder: The model utilizes the first 6 layers of GPT-2 with unmasked (bi-directional) self-attention. This configuration allows the model to process the entire input sequence simultaneously, creating a more comprehensive summary.
  2. Attention-Average Block: To transform the variable-length sequence of vectors from the encoder into a single, fixed-size latent vector ($z$), a learnable "query" attends to the encoder's output.
  3. Latent Code Injection: Three methods for injecting the latent code $z$ into the GPT-2 decoder were evaluated:
  4. INPUT: Adding $z$ to every input token embedding.
  5. PSA (Pseudo Self-Attention): Injecting $z$ into the self-attention layers as an additional "key" and "value." This proved highly effective.
  6. SOFTMAX: Adding a projected version of $z$ directly to the final word-prediction probabilities.

Training Strategies

To mitigate "posterior collapse"—a common issue where the decoder ignores the latent code—the authors employed a cyclic annealing schedule for the KL-divergence weight ($\beta$). Additionally, they initially froze the pre-trained GPT-2 parameters to prevent "learning deviation" during the early stages of training.

Experiments and Results

The model was evaluated across four datasets: Arxiv and Yelp for initial validation of topic and sentiment clustering, and WritingPrompts (WP) and WikiPlots (WI) for large-scale story generation.

Quantitative Performance

The CVAE models consistently outperformed baselines in terms of Perplexity (PPL), indicating superior next-word prediction. On ROUGE metrics (ROUGE-1, ROUGE-2, and ROUGE-L), the model achieved state-of-the-art or comparable results, demonstrating strong overlap with human-written ground truths.

Latent Space Analysis

A key finding is the model's ability to organize stories meaningfully in its latent space. Visualizations using t-SNE show that the model automatically clusters stories by semantic features.

t-SNE visualizations of the latent space for Arxiv (left) and Yelp (right) datasets, showing clear clustering of topics and sentiments.

In the WikiPlots dataset, the model successfully clustered stories by genre (e.g., "TV series" vs. "Manga") without explicit supervision. This latent space allows for "steering" the narrative: by injecting a latent code from one story into the prompt of another, the model generates a narrative that follows the new prompt while adopting the "vibe" or style of the source latent code.

Conclusion and Future Work

This work demonstrates that integrating latent variables into Transformer architectures can provide the necessary steerability for complex generation tasks like storytelling without sacrificing fluency. While challenges like posterior collapse remain, the success of the CVAE in organizing and utilizing latent representations opens new doors for controllable AI. Future research may focus on more granular control, such as steering specific plot points or character arcs throughout a long-form narrative.

Summary updated

BibTeX
@misc{fang2021transformer,
  author = {Le Fang and Tao Zeng and Chaochun Liu and Liefeng Bo and Wen Dong and Changyou Chen},
  title = {{Transformer-based Conditional Variational Autoencoder for Controllable Story Generation}},
  year = {2021},
  eprint = {2101.00828},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2101.00828},
}
Figure