Abstract
Program comprehension is a fundamental task in software development and maintenance processes. Software developers often need to understand a large amount of existing code before they can develop new features or fix bugs in existing programs. Being able to process programming language code automatically and provide summaries of code functionality accurately can significantly help developers to reduce time spent in code navigation and understanding, and thus increase productivity. Different from natural language articles, source code in programming languages often follows rigid syntactical structures and there can exist dependencies among code elements that are located far away from each other through complex control flows and data flows. Existing studies on tree-based convolutional neural networks (TBCNN) and gated graph neural networks (GGNN) are not able to capture essential semantic dependencies among code elements accurately. In this paper, we propose novel tree-based capsule networks (TreeCaps) and relevant techniques for processing program code in an automated way that encodes code syntactical structures and captures code dependencies more accurately. Based on evaluation on programs written in different programming languages, we show that our TreeCaps-based approach can outperform other approaches in classifying the functionalities of many programs.
Introduction
Program comprehension—the ability of a machine to summarize or classify what a piece of code does—is notoriously difficult. Unlike natural language, code follows rigid syntactical rules and contains critical dependencies between elements that might be physically far apart, such as a variable defined at the top of a file and used at the very bottom.
Existing approaches have significant drawbacks. Tree-based CNNs (TBCNNs) are effective at capturing local patterns but struggle with long-distance relationships. Gated Graph Neural Networks (GGNNs) can model complex dependencies but often require expensive manual program analysis to define edges, which can introduce noise and slow down training. TreeCaps addresses these challenges by automatically discovering "part-whole" relationships within the code's hierarchical structure without needing manual feature engineering.
The TreeCaps Architecture
The core innovation of this work is the Tree-Structured Capsule Network. Capsule Networks are designed to preserve spatial hierarchies, making them a perfect metaphor for code structure (e.g., a statement is part of a loop, which is part of a function).
1. AST Vectorization
Programs are first parsed into Abstract Syntax Trees (ASTs). Node types, such as ForStatement or BinaryOperator, are converted into vectors using an embedding technique similar to Word2Vec to capture initial semantic meaning.
2. Primary Variable Capsule (PVC) Layer
Because programs vary in size, the number of nodes in an AST is inconsistent. This layer utilizes a "continuous binary tree" convolution to generate a variable number of capsules based on the specific tree's size.
3. Variable-to-Static Routing
Standard capsule routing requires a fixed number of inputs. TreeCaps solves this with a novel "variable-to-static" routing algorithm. It sorts capsules by their $L_2$ norm—representing the "probability of existence" of a feature—and routes the most important nodes to a fixed set of higher-level capsules.
4. Code Capsule (CC) Layer
The final layer employs dynamic routing to determine the final classification of the program based on the aggregated features from the previous layers.
Experimental Evaluation
The authors evaluated TreeCaps on three datasets across different programming languages:
- Dataset A (Python): 6 classes of sorting algorithms.
- Dataset B (Java): 10 classes of sorting algorithms sourced from GitHub.
- Dataset C (C): 104 classes of programs from a pedagogical programming contest.
For Java and C, the researchers used srcML to create a unified set of over 400 AST node types, ensuring the model could handle cross-language structures effectively.
Key Results and Insights
TreeCaps demonstrated superior performance across all tested languages:
- Java (Dataset B): TreeCaps achieved 92.11% accuracy, a significant improvement over TBCNN (75.00%) and GGNN (85.00%).
- C (Dataset C): It reached 87.95% accuracy, outperforming previous state-of-the-art models.
- Python (Dataset A): It achieved a perfect 100% accuracy.
An ablation study revealed that replacing the novel routing algorithm with standard "Dynamic Max Pooling" caused accuracy to drop by nearly 9%, proving that the capsule routing mechanism is the primary driver of performance gains. By sorting capsules by their norm before routing, the model effectively learns to ignore "boilerplate" code and focus on the logic defining the program's purpose.
Conclusion
TreeCaps provides a robust framework for capturing semantic dependencies in source code without requiring manual data-flow graph computation. While capsule networks are more computationally expensive than standard CNNs (with a noted 16% increase in inference time), the gains in accuracy and the ability to model complex code structures are substantial. Future work will explore applying TreeCaps to tasks like bug detection, vulnerability localization, and code clone detection.
Summary updated