Introduction
In automated software development, two tasks are paramount: Code Summarization (CS), which generates a natural language description from source code, and Code Generation (CG), which generates source code from a natural language intent.
Historically, researchers have treated these as independent problems. However, they are intuitively "dual" tasks: the input of one is the output of the other. Previous neural approaches failed to exploit this relationship, missing out on the underlying structural and semantic correlations between code and its description.
This paper proposes a dual training framework that treats Code Summarization and Code Generation as inverse tasks of one another. By training both models simultaneously and enforcing constraints on their joint probability and attention weights, the researchers demonstrate that each task can act as a regularizer for the other, significantly boosting performance across both domains.
Methods: The Dual Training Framework
The authors introduce a novel framework designed to bridge the gap between these two tasks by leveraging their inherent symmetry.
The framework consists of three main components:
- Base Models: Both the CS and CG models use a standard Sequence-to-Sequence (Seq2Seq) architecture equipped with an attention mechanism. They utilize bidirectional LSTMs for encoders and standard LSTMs for decoders.
- Probabilistic Duality: This constraint is based on the principle that the joint probability of a code-comment pair $(x, y)$ should be the same regardless of direction—$P(x)P(y|x) = P(y)P(x|y)$. The authors add a regularization term to the loss function to force the two models to remain mathematically consistent with each other.
- Attention Duality: A novel contribution of this paper is the enforcement of symmetric attention. The authors argue that if a specific code token (e.g.,
.find()) aligns with a specific word in a comment (e.g., "search"), that alignment should be bidirectional. They use Jensen-Shannon divergence to minimize the distance between the attention weight matrices of the CS and CG models, effectively "smoothing" the attention and improving alignment.
The training process involves a "warm-start" where models are pretrained independently before being fine-tuned jointly using these dual constraints.
Key Insights
- Symmetry as a Teacher: The most striking idea is that the attention mechanism in a code generator can "teach" the code summarizer where to look, and vice versa. By enforcing symmetry, the models learn more meaningful alignments than they would in isolation.
- Probabilistic Constraints: By linking the two models through a shared joint probability, the framework ensures that the models don't just learn to map strings, but learn a more cohesive underlying distribution of how language and code relate.
- Dual Training vs. Multi-task Learning: While multi-task learning often shares parameters, this dual framework keeps parameters separate but constrains the outputs and internal states (attention) to be consistent. This allows each model to specialize while still benefiting from the other.
Datasets
The models were evaluated on two primary datasets collected from GitHub projects:
- Java Dataset: Approximately 70,000 training pairs of Java methods and Javadoc comments.
- Python Dataset: Approximately 55,000 training pairs of Python functions and documentation strings.
To calculate marginal probabilities ($P(x)$ and $P(y)$) for the probabilistic duality constraint, the authors also used larger sets of code-only and comment-only data from GitHub to train monolingual language models.
Key Results
The dual framework outperformed several state-of-the-art baselines (including CODE-NN and DeepCom) on both tasks.
- Code Summarization (CS): On the Java dataset, the Dual Model achieved a BLEU score of 42.39, compared to 41.01 for the basic model. Similar improvements were seen in METEOR and ROUGE-L metrics across both Java and Python.
- Code Generation (CG): The impact on CG was even more pronounced. On the Java dataset, the BLEU score jumped from 10.86 (basic) to 17.17 (dual).
- Code Validity: The "Percentage of Valid Code" (code that can be successfully parsed into an Abstract Syntax Tree) increased significantly, rising from 19.6% to 27.4% for Java and from 41.8% to 51.9% for Python.
Limitations and Future Work
Despite the clear improvements, absolute BLEU scores for Code Generation remain relatively low (in the 12–17 range). This highlights that generating complex, functional code from short text descriptions is still an extremely difficult task.
Furthermore, the current dual model treats code primarily as a sequence of tokens. Integrating structural information (like Abstract Syntax Trees) or explicit grammar rules into the dual framework is a promising next step. Finally, incorporating external API knowledge could further boost performance, representing an exciting avenue for future research.
Summary updated