{"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/k-nn-prompting-beyond-context-learning-with","title":"$k$NN Prompting: Beyond-Context Learning with Calibration-Free Nearest Neighbor Inference","arxiv_id":"2303.13824","date":"2023-03-24","proceeding":null,"authors":["Benfeng Xu","Quan Wang","Zhendong Mao","Yajuan Lyu","Qiaoqiao She","Yongdong Zhang"],"abstract":"In-Context Learning (ICL), which formulates target tasks as prompt completion conditioned on in-context demonstrations, has become the prevailing utilization of LLMs. In this paper, we first disclose an actual predicament for this typical usage that it can not scale up with training data due to context length restriction. Besides, existing works have shown that ICL also suffers from various biases and requires delicate calibration treatment. To address both challenges, we advocate a simple and effective solution, $k$NN Prompting, which first queries LLM with training data for distributed representations, then predicts test instances by simply referring to nearest neighbors. We conduct comprehensive experiments to demonstrate its two-fold superiority: 1) Calibration-Free: $k$NN Prompting does not directly align LLM output distribution with task-specific label space, instead leverages such distribution to align test and training instances. It significantly outperforms state-of-the-art calibration-based methods under comparable few-shot scenario. 2) Beyond-Context: $k$NN Prompting can further scale up effectively with as many training data as are available, continually bringing substantial improvements. The scaling trend holds across 10 orders of magnitude ranging from 2 shots to 1024 shots as well as different LLMs scales ranging from 0.8B to 30B. It successfully bridges data scaling into model scaling, and brings new potentials for the gradient-free paradigm of LLM deployment. Code is publicly available.","url_abs":"https://arxiv.org/abs/2303.13824v1","url_pdf":"https://arxiv.org/pdf/2303.13824v1.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":"k-nn-prompting-beyond-context-learning-with","repo_url":"https://github.com/benfengxu/knnprompting","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":0,"framework":"pytorch","reach":null}],"tasks":[{"task_slug":"in-context-learning","task_name":"In-Context Learning"}],"methods":[{"method_slug":"align","method_name":"ALIGN"},{"method_slug":"test","method_name":"Test"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"syntology_url":null,"atlas_url":"https://app.syntology.ai/?focus=2303.13824","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2303.13824"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+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/benfengxu/knnprompting","reach":null}],"summary":{"ran":1,"unverified":3},"by_repo_kind":{"official":{"samples":4,"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":4,"samples":[{"code_sha256_prefix":"a7620efe62081c78","entry":"AnchorStore","repo":"benfengxu/knnprompting","repo_kind":"official","path":"knn_prompting.py","file_url":"https://github.com/benfengxu/knnprompting/blob/HEAD/knn_prompting.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":"a7620efe62081c78"}},{"code_sha256_prefix":"917154d75800f542","entry":"llm_gen","repo":"BenfengXu/KNNPrompting","repo_kind":"official","path":"knn_prompting.py","file_url":"https://github.com/BenfengXu/KNNPrompting/blob/HEAD/knn_prompting.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"917154d75800f542"}},{"code_sha256_prefix":"14353319f8ba9318","entry":"main","repo":"benfengxu/knnprompting","repo_kind":"official","path":"knn_prompting.py","file_url":"https://github.com/benfengxu/knnprompting/blob/HEAD/knn_prompting.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"14353319f8ba9318"}},{"code_sha256_prefix":"e35887df7c0c3f55","entry":"parse_args","repo":"benfengxu/knnprompting","repo_kind":"official","path":"knn_prompting.py","file_url":"https://github.com/benfengxu/knnprompting/blob/HEAD/knn_prompting.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"e35887df7c0c3f55"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}