{"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/contrastive-learning-for-prompt-based-few","title":"Contrastive Learning for Prompt-Based Few-Shot Language Learners","arxiv_id":"2205.01308","date":"2022-05-03","proceeding":"NAACL 2022 7","authors":["Yiren Jian","Chongyang Gao","Soroush Vosoughi"],"abstract":"The impressive performance of GPT-3 using natural language prompts and in-context learning has inspired work on better fine-tuning of moderately-sized models under this paradigm. Following this line of work, we present a contrastive learning framework that clusters inputs from the same class for better generality of models trained with only limited examples. Specifically, we propose a supervised contrastive framework that clusters inputs from the same class under different augmented \"views\" and repel the ones from different classes. We create different \"views\" of an example by appending it with different language prompts and contextual demonstrations. Combining a contrastive loss with the standard masked language modeling (MLM) loss in prompt-based few-shot learners, the experimental results show that our method can improve over the state-of-the-art methods in a diverse set of 15 language tasks. Our framework makes minimal assumptions on the task or the base model, and can be applied to many recent methods with little modification. The code will be made available at: https://github.com/yiren-jian/LM-SupCon.","url_abs":"https://arxiv.org/abs/2205.01308v1","url_pdf":"https://arxiv.org/pdf/2205.01308v1.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":"contrastive-learning-for-prompt-based-few","repo_url":"https://github.com/yiren-jian/lm-supcon","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}}],"tasks":[{"task_slug":"contrastive-learning","task_name":"Contrastive Learning"},{"task_slug":"in-context-learning","task_name":"In-Context Learning"},{"task_slug":"language-modeling","task_name":"Language Modeling"},{"task_slug":"language-modelling","task_name":"Language Modelling"},{"task_slug":"masked-language-modeling","task_name":"Masked Language Modeling"}],"methods":[{"method_slug":"adam","method_name":"Adam"},{"method_slug":"attention","method_name":"Attention"},{"method_slug":"attention-dropout","method_name":"Attention Dropout"},{"method_slug":"base","method_name":"BASE"},{"method_slug":"bpe","method_name":"BPE"},{"method_slug":"contrastive-learning","method_name":"Contrastive Learning"},{"method_slug":"cosine-annealing","method_name":"Cosine Annealing"},{"method_slug":"dense-connections","method_name":"Dense Connections"},{"method_slug":"dropout","method_name":"Dropout"},{"method_slug":"gpt-3","method_name":"GPT-3"},{"method_slug":"layer-normalization","method_name":"Layer Normalization"},{"method_slug":"linear-layer","method_name":"Linear Layer"},{"method_slug":"linear-warmup-with-cosine-annealing","method_name":"Linear Warmup With Cosine Annealing"},{"method_slug":"multi-head-attention","method_name":"Multi-Head Attention"},{"method_slug":"residual-connection","method_name":"Residual Connection"},{"method_slug":"softmax","method_name":"Softmax"},{"method_slug":"weight-decay","method_name":"Weight Decay"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"syntology_url":null,"atlas_url":"https://app.syntology.ai/?focus=2205.01308","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2205.01308"}},"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/yiren-jian/lm-supcon","reach":{"status":"ok","spdx":"MIT"}},{"provenance":"deterministic:regex_extraction","url":"https://github.com/yiren-jian/LM-SupCon","reach":{"status":"ok","spdx":"MIT"}}],"summary":{"ran_honours":1,"ran_draft_wrong":1,"ran":3,"ran_fixture":1,"unverified":5},"by_repo_kind":{"official":{"samples":11,"ran":6,"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":0,"samples":[{"code_sha256_prefix":"e448786d5ff1875a","entry":"default_dev_objective","repo":"yiren-jian/LM-SupCon","repo_kind":"official","path":"src/trainer.py","file_url":"https://github.com/yiren-jian/LM-SupCon/blob/HEAD/src/trainer.py","link_basis":"harvester_set","language":"python","status":"ran_honours","verification_level":1,"contract_check":"HONOURS","metamorphic_tier":"well_formed","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"e448786d5ff1875a"}},{"code_sha256_prefix":"f15117422b72cf43","entry":"get_only_chars","repo":"yiren-jian/LM-SupCon","repo_kind":"official","path":"src/eda.py","file_url":"https://github.com/yiren-jian/LM-SupCon/blob/HEAD/src/eda.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"f15117422b72cf43"}},{"code_sha256_prefix":"e768e25ca95f70cd","entry":"input_example_to_string","repo":"yiren-jian/LM-SupCon","repo_kind":"official","path":"src/dataset.py","file_url":"https://github.com/yiren-jian/LM-SupCon/blob/HEAD/src/dataset.py","link_basis":"harvester_set","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"e768e25ca95f70cd"}},{"code_sha256_prefix":"fbef70dc7c700501","entry":"input_example_to_tuple","repo":"yiren-jian/LM-SupCon","repo_kind":"official","path":"src/dataset.py","file_url":"https://github.com/yiren-jian/LM-SupCon/blob/HEAD/src/dataset.py","link_basis":"harvester_set","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"fbef70dc7c700501"}},{"code_sha256_prefix":"b69b1ad29fd21db5","entry":"select_neighbors","repo":"yiren-jian/LM-SupCon","repo_kind":"official","path":"src/label_search.py","file_url":"https://github.com/yiren-jian/LM-SupCon/blob/HEAD/src/label_search.py","link_basis":"harvester_set","language":"python","status":"ran_fixture","verification_level":1,"contract_check":"RAISES","metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"b69b1ad29fd21db5"}},{"code_sha256_prefix":"b62076c5483738dc","entry":"text_classification_metrics","repo":"yiren-jian/LM-SupCon","repo_kind":"official","path":"src/processors.py","file_url":"https://github.com/yiren-jian/LM-SupCon/blob/HEAD/src/processors.py","link_basis":"harvester_set","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"b62076c5483738dc"}},{"code_sha256_prefix":"47923f36537f5f79","entry":"eval_pairing_acc","repo":"yiren-jian/LM-SupCon","repo_kind":"official","path":"src/label_search.py","file_url":"https://github.com/yiren-jian/LM-SupCon/blob/HEAD/src/label_search.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":true,"mcp_get_code":{"code_sha256":"47923f36537f5f79"}},{"code_sha256_prefix":"461078e5a822e755","entry":"get_synonyms","repo":"yiren-jian/LM-SupCon","repo_kind":"official","path":"src/eda.py","file_url":"https://github.com/yiren-jian/LM-SupCon/blob/HEAD/src/eda.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":true,"mcp_get_code":{"code_sha256":"461078e5a822e755"}},{"code_sha256_prefix":"be6b57168b0424f3","entry":"select_likely_words","repo":"yiren-jian/LM-SupCon","repo_kind":"official","path":"src/label_search.py","file_url":"https://github.com/yiren-jian/LM-SupCon/blob/HEAD/src/label_search.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":true,"mcp_get_code":{"code_sha256":"be6b57168b0424f3"}},{"code_sha256_prefix":"56366bd6ff30a29a","entry":"synonym_replacement","repo":"yiren-jian/LM-SupCon","repo_kind":"official","path":"src/eda.py","file_url":"https://github.com/yiren-jian/LM-SupCon/blob/HEAD/src/eda.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":true,"mcp_get_code":{"code_sha256":"56366bd6ff30a29a"}},{"code_sha256_prefix":"090deb73a074d4df","entry":"tokenize_multipart_input","repo":"yiren-jian/LM-SupCon","repo_kind":"official","path":"src/dataset.py","file_url":"https://github.com/yiren-jian/LM-SupCon/blob/HEAD/src/dataset.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":true,"mcp_get_code":{"code_sha256":"090deb73a074d4df"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}