{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/towards-understanding-the-word-sensitivity-of","title":"Towards Understanding the Word Sensitivity of Attention Layers: A Study via Random Features","arxiv_id":"2402.02969","date":"2024-02-05","proceeding":null,"authors":["Simone Bombari","Marco Mondelli"],"abstract":"Understanding the reasons behind the exceptional success of transformers requires a better analysis of why attention layers are suitable for NLP tasks. In particular, such tasks require predictive models to capture contextual meaning which often depends on one or few words, even if the sentence is long. Our work studies this key property, dubbed word sensitivity (WS), in the prototypical setting of random features. We show that attention layers enjoy high WS, namely, there exists a vector in the space of embeddings that largely perturbs the random attention features map. The argument critically exploits the role of the softmax in the attention layer, highlighting its benefit compared to other activations (e.g., ReLU). In contrast, the WS of standard random features is of order $1/\\sqrt{n}$, $n$ being the number of words in the textual sample, and thus it decays with the length of the context. We then translate these results on the word sensitivity into generalization bounds: due to their low WS, random features provably cannot learn to distinguish between two sentences that differ only in a single word; in contrast, due to their high WS, random attention features have higher generalization capabilities. We validate our theoretical results with experimental evidence over the BERT-Base word embeddings of the imdb review dataset.","url_abs":"https://arxiv.org/abs/2402.02969v2","url_pdf":"https://arxiv.org/pdf/2402.02969v2.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"towards-understanding-the-word-sensitivity-of","repo_url":"https://github.com/simone-bombari/attention-sensitivity","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":0,"framework":"pytorch","reach":{"status":"ok"}}],"tasks":[{"task_slug":"generalization-bounds","task_name":"Generalization Bounds"},{"task_slug":"sensitivity","task_name":"Sensitivity"},{"task_slug":"sentence","task_name":"Sentence"},{"task_slug":"word-embeddings","task_name":"Word Embeddings"}],"methods":[{"method_slug":"softmax","method_name":"Softmax"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"syntology_url":"https://syntology.ai/paper/2402.02969","atlas_url":"https://app.syntology.ai/?focus=2402.02969","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2402.02969"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-25T09:33:49+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/simone-bombari/attention-sensitivity","reach":{"status":"ok"}}],"summary":{"ran":7},"by_repo_kind":{"official":{"samples":7,"ran":7,"repositories":1}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":7,"samples":[{"code_sha256_prefix":"a41e0cab211ab570","entry":"attention_numpy_softmax","repo":"simone-bombari/attention-sensitivity","repo_kind":"official","path":"utils.py","file_url":"https://github.com/simone-bombari/attention-sensitivity/blob/HEAD/utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"a41e0cab211ab570"}},{"code_sha256_prefix":"6fe6d09e0eb26318","entry":"attention_softmax","repo":"simone-bombari/attention-sensitivity","repo_kind":"official","path":"utils.py","file_url":"https://github.com/simone-bombari/attention-sensitivity/blob/HEAD/utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"6fe6d09e0eb26318"}},{"code_sha256_prefix":"cb5ef018c4158aba","entry":"create_imdb_dataset","repo":"simone-bombari/attention-sensitivity","repo_kind":"official","path":"utils_data.py","file_url":"https://github.com/simone-bombari/attention-sensitivity/blob/HEAD/utils_data.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"cb5ef018c4158aba"}},{"code_sha256_prefix":"b21faea335185419","entry":"generate_embeddings","repo":"simone-bombari/attention-sensitivity","repo_kind":"official","path":"utils_data.py","file_url":"https://github.com/simone-bombari/attention-sensitivity/blob/HEAD/utils_data.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"b21faea335185419"}},{"code_sha256_prefix":"87f4cc1f23b67d74","entry":"import_in_df_adversarial","repo":"simone-bombari/attention-sensitivity","repo_kind":"official","path":"utils_plot.py","file_url":"https://github.com/simone-bombari/attention-sensitivity/blob/HEAD/utils_plot.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"87f4cc1f23b67d74"}},{"code_sha256_prefix":"22cbe841b6aefe93","entry":"import_in_df_sensitivity_rf","repo":"simone-bombari/attention-sensitivity","repo_kind":"official","path":"utils_plot.py","file_url":"https://github.com/simone-bombari/attention-sensitivity/blob/HEAD/utils_plot.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"22cbe841b6aefe93"}},{"code_sha256_prefix":"9d27f9aa50a86a4b","entry":"relu","repo":"simone-bombari/attention-sensitivity","repo_kind":"official","path":"utils.py","file_url":"https://github.com/simone-bombari/attention-sensitivity/blob/HEAD/utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"9d27f9aa50a86a4b"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}