Skip to main content
QUICK REVIEW

[Paper Review] TreeCaps: Tree-Based Capsule Networks for Source Code Processing

Nghi D. Q. Bui, Yijun Yu|arXiv (Cornell University)|Sep 5, 2020
Software Engineering ResearchComputer Science46 references7 citations
TL;DR

TreeCaps proposes a novel capsule network architecture that combines tree-based convolutional neural networks (TBCNN) with capsule routing to learn source code representations directly from abstract syntax trees (ASTs), avoiding noisy graph-based semantic analysis. It achieves state-of-the-art accuracy and robustness in code functionality classification and function name prediction across Java and C/C++ programs, outperforming existing models like Code2vec, GGNN, and GREAT.

ABSTRACT

Recently program learning techniques have been proposed to process source code based on syntactical structures (e.g., Abstract Syntax Trees) and/or semantic information (e.g., Dependency Graphs). Although graphs may be better at capturing various viewpoints of code semantics than trees, constructing graph inputs from code needs static code semantic analysis that may not be accurate and introduces noise during learning. Although syntax trees are precisely defined according to the language grammar and easier to construct and process than graphs, previous tree-based learning techniques have not been able to learn semantic information from trees to achieve better accuracy than graph-based techniques. We propose a new learning technique, named TreeCaps, by fusing together capsule networks with tree-based convolutional neural networks, to achieve learning accuracy higher than existing graph-based techniques while it is based only on trees. TreeCaps introduces novel variable-to-static routing algorithms into the capsule networks to compensate for the loss of previous routing algorithms. Aside from accuracy, we also find that TreeCaps is the most robust to withstand those semantic-preserving program transformations that change code syntax without modifying the semantics. Evaluated on a large number of Java and C/C++ programs, TreeCaps models outperform prior deep learning models of program source code, in terms of both accuracy and robustness for program comprehension tasks such as code functionality classification and function name prediction

Motivation & Objective

  • To develop a deep learning model for source code comprehension that avoids the noise and complexity of graph-based semantic analysis.
  • To improve learning accuracy and robustness in program understanding tasks by leveraging the precise structure of abstract syntax trees (ASTs).
  • To address the challenge of dynamic capsule counts in capsule networks when applied to variable-sized ASTs by introducing novel routing mechanisms.
  • To evaluate the model's performance and robustness against semantic-preserving code transformations, which simulate adversarial attacks.

Proposed method

  • Adapts Tree-Based Convolutional Neural Networks (TBCNN) to extract local node features from ASTs, producing variable-sized capsule outputs.
  • Introduces a Primary Variable Capsule (PVC) layer that generates a dynamic number of capsules based on the input AST structure.
  • Proposes two routing algorithms: (1) Dynamic Routing with Shared Weights (DRSW), and (2) a novel Variable-to-Static (VTS) routing that selects top-activating capsules to map to a fixed number of secondary capsules.
  • Applies dynamic routing from the Secondary Capsule (SC) layer to a final Code Capsule (CC) layer with a fixed number of capsules, tailored to the classification task.
  • Uses a shared transformation matrix in DRSW and a selective attention mechanism in VTS to improve routing efficiency and representation learning.
  • Employs a margin-based loss function to train the model end-to-end for code classification and function name prediction tasks.

Experimental results

Research questions

  • RQ1Can a capsule network architecture effectively learn from ASTs without relying on graph-based semantic dependencies?
  • RQ2How does the proposed VTS routing mechanism compare to standard dynamic routing in terms of convergence speed and accuracy?
  • RQ3To what extent does TreeCaps maintain prediction consistency under semantic-preserving code transformations compared to existing models?
  • RQ4Does TreeCaps achieve superior performance in code functionality classification and function name prediction compared to state-of-the-art models like Code2vec, GGNN, and GREAT?
  • RQ5How does the model’s robustness correlate with its ability to generalize across syntactically altered but semantically equivalent code?

Key findings

  • TreeCaps achieves higher classification accuracy and F1 scores than Code2vec, ASTNN, TBCNN, GGNN, GREAT, and GNN-FiLM on code functionality classification and function name prediction tasks.
  • The VTS routing algorithm converges faster and achieves higher validation accuracy than DRSW, likely due to fewer parameters and a more focused routing process.
  • TreeCaps-VTS exhibits the lowest percentage of prediction changes (PPC) under semantic-preserving transformations—only 12.3% for the OJ dataset—making it the most robust model tested.
  • The model maintains high performance across diverse codebases in Java and C/C++, demonstrating strong generalization across programming languages.
  • TreeCaps outperforms GREAT in both F1 score and training time, indicating better efficiency and representation learning despite using only ASTs.
  • The model's robustness suggests strong potential for use in adversarial settings, such as detecting or resisting code obfuscation and adversarial examples.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.