An Exploratory Study on Code Attention in BERT
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
- 2017Attention Is All You Needcited by 59,932
- 2018BERT: Pre-training of Deep Bidirectional Transformers for Language Understandingcited by 40,204
- 2019RoBERTa: A Robustly Optimized BERT Pretraining Approachcited by 13,266
- 2019Language Models as Knowledge Bases?cited by 1,922
- 2020CodeBERT: A Pre-Trained Model for Programming and Natural Languagescited by 1,286
- 2019BERT Rediscovers the Classical NLP Pipelinecited by 1,046
- 2019What Does BERT Look At? An Analysis of BERT's Attentioncited by 994
- 2021CodeXGLUE: A Machine Learning Benchmark Dataset for Code Understanding and Generationcited by 732
- 2019CodeSearchNet Challenge: Evaluating the State of Semantic Code Searchcited by 638
- 2020GraphCodeBERT: Pre-training Code Representations with Data Flowcited by 600
- 2019What do you learn from context? Probing for sentence structure in contextualized word representationscited by 436
- 2019A Multiscale Visualization of Attention in the Transformer Modelcited by 292
21 more
- 2018code2seq: Generating Sequences from Structured Representations of Codecited by 177
- 2021Masked Language Modeling and the Distributional Hypothesis: Order Word Matters Pre-training for Littlecited by 176
- 2020A Transformer-based Approach for Source Code Summarizationcited by 129
- 2018Improving Automatic Source Code Summarization via Deep Reinforcement Learningcited by 99
- 2019A Neural Model for Generating Natural Language Summaries of Program Subroutinescited by 76
- 2022Natural Attack for Pre-trained Models of Codecited by 72
- 2018DeepBugs: A Learning Approach to Name-based Bug Detectioncited by 65
- 2020Multi-task Learning based Pre-trained Language Model for Code Completioncited by 58
- 2020On the Generalizability of Neural Program Models with respect to Semantic-Preserving Program Transformationscited by 41
- 2021What do pre-trained code models know about code?cited by 36
- 2018Automated software vulnerability detection with machine learningcited by 34
- 2020Improved Automatic Summarization of Subroutines via Attention to File Contextcited by 34
- 2021Code Structure Guided Transformer for Source Code Summarizationcited by 29
- 2017Sorting and Transforming Program Repair Ingredients via Deep Learning Code Similaritiescited by 26
- 2020SCELMo: Source Code Embeddings from Language Modelscited by 16
- 2018Studying the Difference Between Natural and Programming Language Corporacited by 13
- 2020Embedding Java Classes with code2vec: Improvements from Variable Obfuscationcited by 12
- 2020TranS^3: A Transformer-based Framework for Unifying Code Summarization and Code Searchcited by 11
- 2020On the Generation, Structure, and Semantics of Grammar Patterns in Source Code Identifierscited by 5
- 2021On the Naming of Methods: A Survey of Professional Developerscited by 4
- 2021How Developers Choose Namescited by 3
Built on by
Citing papers per year: 2022: 1; 2023: 6; 2024: 1; 2025: 4.
12 papers.
- 2023Large Language Models for Software Engineering: A Systematic Literature Reviewcited by 389
- 2023Deep Learning for Code Intelligence: Survey, Benchmark and Toolkitcited by 22
- 2022AST-Probe: Recovering abstract syntax trees from hidden representations of pre-trained language modelscited by 16
- 2023Investigating semantic subspaces of Transformer sentence embeddings through linear structural probingcited by 12
- 2023The EarlyBIRD Catches the Bug: On Exploiting Early Layers of Encoder Models for More Efficient Code Classificationcited by 5
- 2025Neuron-Guided Interpretation of Code LLMs: Where, Why, and How?cited by 3
- 2023APIContext2Com: Code Comment Generation by Incorporating Pre-Defined API Documentationcited by 2
- 2023Naturalness of Attention: Revisiting Attention in Code Language Modelscited by 1
- 2024Beyond Self-learned Attention: Mitigating Attention Bias in Transformer-based Models Using Attention Guidancecited by 1
- 2025On The Impact of Merge Request Deviations on Code Review Practicescited by 0
- 2025Resource-Efficient Automatic Software Vulnerability Assessment via Knowledge Distillation and Particle Swarm Optimizationcited by 0
- 2025Toward Explaining Large Language Models in Software Engineering Taskscited by 0
- 2025Toward Explaining Large Language Models in Software Engineering Tasks23 Dec 2025
- 2025Neuron-Guided Interpretation of Code LLMs: Where, Why, and How?23 Dec 2025
- 2025Resource-Efficient Automatic Software Vulnerability Assessment via Knowledge Distillation and Particle Swarm Optimization30 Jul 2025
- 2025On The Impact of Merge Request Deviations on Code Review Practices10 Jun 2025
- 2024Beyond Self-learned Attention: Mitigating Attention Bias in Transformer-based Models Using Attention Guidance26 Feb 2024
- 2023Deep Learning for Code Intelligence: Survey, Benchmark and Toolkit30 Dec 2023
- 2023Naturalness of Attention: Revisiting Attention in Code Language Models22 Nov 2023
- 2023Investigating semantic subspaces of Transformer sentence embeddings through linear structural probing18 Oct 2023
- 2023Large Language Models for Software Engineering: A Systematic Literature Review21 Aug 2023
- 2023The EarlyBIRD Catches the Bug: On Exploiting Early Layers of Encoder Models for More Efficient Code Classification8 May 2023
- 2023APIContext2Com: Code Comment Generation by Incorporating Pre-Defined API Documentation3 Mar 2023
- 2022AST-Probe: Recovering abstract syntax trees from hidden representations of pre-trained language models23 Jun 2022