mlsanityData to 7 Oct 2026

An Exploratory Study on Code Attention in BERT

arXiv 2204.10200 ↗First posted 5 Apr 2022cs.SE cs.AI cs.CLcited by 12

Not on the map. Why?

Abstract (arXiv)

Many recent models in software engineering introduced deep neural models based on the Transformer architecture or use transformer-based Pre-trained Language Models (PLM) trained on code. Although these models achieve the state of the arts results in many downstream tasks such as code summarization and bug detection, they are based on Transformer and PLM, which are mainly studied in the Natural Language Processing (NLP) field. The current studies rely on the reasoning and practices from NLP for these models in code, despite the differences between natural languages and programming languages. There is also limited literature on explaining how code is modeled. Here, we investigate the attention behavior of PLM on code and compare it with natural language. We pre-trained BERT, a Transformer based PLM, on code and explored what kind of information it learns, both semantic and syntactic. We run several experiments to analyze the attention values of code constructs on each other and what BERT learns in each layer. Our analyses show that BERT pays more attention to syntactic entities, specifically identifiers and separators, in contrast to the most attended token [CLS] in NLP. This observation motivated us to leverage identifiers to represent the code sequence instead of the [CLS] token when used for code clone detection. Our results show that employing embeddings from identifiers increases the performance of BERT by 605% and 4% F1-score in its lower layers and the upper layers, respectively. When identifiers' embeddings are used in CodeBERT, a code-based PLM, the performance is improved by 21-24% in the F1-score of clone detection. The findings can benefit the research community by using code-specific representations instead of applying the common embeddings used in NLP, and open new directions for developing smaller models with similar performance.

Key facts. Cited by 12 papers in the map (data through 7 Oct 2026). Builds on 33 of its 48 references that are in the map. First posted on arXiv 5 Apr 2022.

Builds on

33 of 48 references.

References · 33

  1. 2017Attention Is All You Needcited by 59,932
  2. 2018BERT: Pre-training of Deep Bidirectional Transformers for Language Understandingcited by 40,204
  3. 2019RoBERTa: A Robustly Optimized BERT Pretraining Approachcited by 13,266
  4. 2019Language Models as Knowledge Bases?cited by 1,922
  5. 2020CodeBERT: A Pre-Trained Model for Programming and Natural Languagescited by 1,286
  6. 2019BERT Rediscovers the Classical NLP Pipelinecited by 1,046
  7. 2019What Does BERT Look At? An Analysis of BERT's Attentioncited by 994
  8. 2021CodeXGLUE: A Machine Learning Benchmark Dataset for Code Understanding and Generationcited by 732
  9. 2019CodeSearchNet Challenge: Evaluating the State of Semantic Code Searchcited by 638
  10. 2020GraphCodeBERT: Pre-training Code Representations with Data Flowcited by 600
  11. 2019What do you learn from context? Probing for sentence structure in contextualized word representationscited by 436
  12. 2019A Multiscale Visualization of Attention in the Transformer Modelcited by 292
21 more
  1. 2018code2seq: Generating Sequences from Structured Representations of Codecited by 177
  2. 2021Masked Language Modeling and the Distributional Hypothesis: Order Word Matters Pre-training for Littlecited by 176
  3. 2020A Transformer-based Approach for Source Code Summarizationcited by 129
  4. 2018Improving Automatic Source Code Summarization via Deep Reinforcement Learningcited by 99
  5. 2019A Neural Model for Generating Natural Language Summaries of Program Subroutinescited by 76
  6. 2022Natural Attack for Pre-trained Models of Codecited by 72
  7. 2018DeepBugs: A Learning Approach to Name-based Bug Detectioncited by 65
  8. 2020Multi-task Learning based Pre-trained Language Model for Code Completioncited by 58
  9. 2020On the Generalizability of Neural Program Models with respect to Semantic-Preserving Program Transformationscited by 41
  10. 2021What do pre-trained code models know about code?cited by 36
  11. 2018Automated software vulnerability detection with machine learningcited by 34
  12. 2020Improved Automatic Summarization of Subroutines via Attention to File Contextcited by 34
  13. 2021Code Structure Guided Transformer for Source Code Summarizationcited by 29
  14. 2017Sorting and Transforming Program Repair Ingredients via Deep Learning Code Similaritiescited by 26
  15. 2020SCELMo: Source Code Embeddings from Language Modelscited by 16
  16. 2018Studying the Difference Between Natural and Programming Language Corporacited by 13
  17. 2020Embedding Java Classes with code2vec: Improvements from Variable Obfuscationcited by 12
  18. 2020TranS^3: A Transformer-based Framework for Unifying Code Summarization and Code Searchcited by 11
  19. 2020On the Generation, Structure, and Semantics of Grammar Patterns in Source Code Identifierscited by 5
  20. 2021On the Naming of Methods: A Survey of Professional Developerscited by 4
  21. 2021How Developers Choose Namescited by 3

Built on by

Cited by year

Citing papers per year: 2022: 1; 2023: 6; 2024: 1; 2025: 4.

12 papers.