{"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/you-can-t-pick-your-neighbors-or-can-you-when","title":"You can't pick your neighbors, or can you? When and how to rely on retrieval in the $k$NN-LM","arxiv_id":"2210.15859","date":"2022-10-28","proceeding":null,"authors":["Andrew Drozdov","Shufan Wang","Razieh Rahimi","Andrew McCallum","Hamed Zamani","Mohit Iyyer"],"abstract":"Retrieval-enhanced language models (LMs), which condition their predictions on text retrieved from large external datastores, have recently shown significant perplexity improvements compared to standard LMs. One such approach, the $k$NN-LM, interpolates any existing LM's predictions with the output of a $k$-nearest neighbors model and requires no additional training. In this paper, we explore the importance of lexical and semantic matching in the context of items retrieved by $k$NN-LM. We find two trends: (1) the presence of large overlapping $n$-grams between the datastore and evaluation set plays an important factor in strong performance, even when the datastore is derived from the training data; and (2) the $k$NN-LM is most beneficial when retrieved items have high semantic similarity with the query. Based on our analysis, we define a new formulation of the $k$NN-LM that uses retrieval quality to assign the interpolation coefficient. We empirically measure the effectiveness of our approach on two English language modeling datasets, Wikitext-103 and PG-19. Our re-formulation of the $k$NN-LM is beneficial in both cases, and leads to nearly 4% improvement in perplexity on the Wikitext-103 test set.","url_abs":"https://arxiv.org/abs/2210.15859v1","url_pdf":"https://arxiv.org/pdf/2210.15859v1.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":"you-can-t-pick-your-neighbors-or-can-you-when","repo_url":"https://github.com/iesl/knnlm-retrieval-quality","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}}],"tasks":[{"task_slug":"language-modeling","task_name":"Language Modeling"},{"task_slug":"language-modelling","task_name":"Language Modelling"},{"task_slug":"retrieval","task_name":"Retrieval"},{"task_slug":"semantic-similarity","task_name":"Semantic Similarity"},{"task_slug":"semantic-textual-similarity","task_name":"Semantic Textual Similarity"}],"methods":[{"method_slug":"test","method_name":"Test"}],"datasets_introduced":[],"methods_introduced":[],"results":[{"leaderboard":"/sota/language-modelling-on-wikitext-103","task":"Language Modelling","dataset":"WikiText-103","model":"kNN-LM w/ Adaptive Coefficient","rank_in_archive_order":9,"of":89,"metrics":{"Number of params":"247M","Test perplexity":"15.5","Validation perplexity":"15.72"},"uses_additional_data":false}],"syntology":{"syntology_url":"https://syntology.ai/paper/2210.15859","atlas_url":"https://app.syntology.ai/?focus=2210.15859","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2210.15859"}},"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/iesl/knnlm-retrieval-quality","reach":{"status":"ok","spdx":"MIT"}}],"summary":{"ran":1,"unverified":2},"by_repo_kind":{"official":{"samples":3,"ran":1,"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":3,"samples":[{"code_sha256_prefix":"cf04f6570c3c52e0","entry":"eval_ppl","repo":"iesl/knnlm-retrieval-quality","repo_kind":"official","path":"rq/knnlm_func.py","file_url":"https://github.com/iesl/knnlm-retrieval-quality/blob/HEAD/rq/knnlm_func.py","link_basis":"harvester_set","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":false,"mcp_get_code":{"code_sha256":"cf04f6570c3c52e0"}},{"code_sha256_prefix":"ce631a6c2360ef70","entry":"get_knn_prob","repo":"iesl/knnlm-retrieval-quality","repo_kind":"official","path":"rq/knnlm_func.py","file_url":"https://github.com/iesl/knnlm-retrieval-quality/blob/HEAD/rq/knnlm_func.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":false,"mcp_get_code":{"code_sha256":"ce631a6c2360ef70"}},{"code_sha256_prefix":"9f81909ace8f8f15","entry":"run_eval_ppl","repo":"iesl/knnlm-retrieval-quality","repo_kind":"official","path":"rq/knnlm_func.py","file_url":"https://github.com/iesl/knnlm-retrieval-quality/blob/HEAD/rq/knnlm_func.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":false,"mcp_get_code":{"code_sha256":"9f81909ace8f8f15"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}