{"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/emc-2-efficient-mcmc-negative-sampling-for","title":"EMC$^2$: Efficient MCMC Negative Sampling for Contrastive Learning with Global Convergence","arxiv_id":"2404.10575","date":"2024-04-16","proceeding":null,"authors":["Chung-Yiu Yau","Hoi-To Wai","Parameswaran Raman","Soumajyoti Sarkar","Mingyi Hong"],"abstract":"A key challenge in contrastive learning is to generate negative samples from a large sample set to contrast with positive samples, for learning better encoding of the data. These negative samples often follow a softmax distribution which are dynamically updated during the training process. However, sampling from this distribution is non-trivial due to the high computational costs in computing the partition function. In this paper, we propose an Efficient Markov Chain Monte Carlo negative sampling method for Contrastive learning (EMC$^2$). We follow the global contrastive learning loss as introduced in SogCLR, and propose EMC$^2$ which utilizes an adaptive Metropolis-Hastings subroutine to generate hardness-aware negative samples in an online fashion during the optimization. We prove that EMC$^2$ finds an $\\mathcal{O}(1/\\sqrt{T})$-stationary point of the global contrastive loss in $T$ iterations. Compared to prior works, EMC$^2$ is the first algorithm that exhibits global convergence (to stationarity) regardless of the choice of batch size while exhibiting low computation and memory cost. Numerical experiments validate that EMC$^2$ is effective with small batch training and achieves comparable or better performance than baseline algorithms. We report the results for pre-training image encoders on STL-10 and Imagenet-100.","url_abs":"https://arxiv.org/abs/2404.10575v1","url_pdf":"https://arxiv.org/pdf/2404.10575v1.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":"emc-2-efficient-mcmc-negative-sampling-for","repo_url":"https://github.com/amazon-science/contrastive_emc2","is_official":1,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"Apache-2.0"}}],"tasks":[{"task_slug":"contrastive-learning","task_name":"Contrastive Learning"}],"methods":[{"method_slug":"contrastive-learning","method_name":"Contrastive Learning"},{"method_slug":"set","method_name":"SET"},{"method_slug":"softmax","method_name":"Softmax"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2404.10575","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2404.10575"}},"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/amazon-science/contrastive_emc2","reach":{"status":"ok","spdx":"Apache-2.0"}}],"summary":{"ran":11,"unverified":3},"by_repo_kind":{"official":{"samples":14,"ran":11,"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":"a05a54f3fc3dbfa9","entry":"all_gather_matrices","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"communication.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/communication.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"a05a54f3fc3dbfa9"}},{"code_sha256_prefix":"a4d6e26b346c837e","entry":"all_gather_vectors","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"communication.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/communication.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"a4d6e26b346c837e"}},{"code_sha256_prefix":"fdcddcdef8722c34","entry":"convert_layers","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"model.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/model.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"fdcddcdef8722c34"}},{"code_sha256_prefix":"6e1acdb5ff584cbb","entry":"estimate_inner_prod","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"cache.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/cache.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"6e1acdb5ff584cbb"}},{"code_sha256_prefix":"904c357058bab2d9","entry":"find_classes","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"folder.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/folder.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"904c357058bab2d9"}},{"code_sha256_prefix":"cd3d80672004efc8","entry":"get_diagonal_blocks","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"evaluation.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/evaluation.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"cd3d80672004efc8"}},{"code_sha256_prefix":"aec20ef76445c38c","entry":"has_file_allowed_extension","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"folder.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/folder.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"aec20ef76445c38c"}},{"code_sha256_prefix":"044dd7c8dd4adba5","entry":"info_nce_loss","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"main_simclr.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/main_simclr.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"044dd7c8dd4adba5"}},{"code_sha256_prefix":"d9fa6dde50b3f702","entry":"is_image_file","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"folder.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/folder.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"d9fa6dde50b3f702"}},{"code_sha256_prefix":"d73de0f2b46b2268","entry":"parse_args","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"arguments.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/arguments.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"d73de0f2b46b2268"}},{"code_sha256_prefix":"5ceb2c36a8c88939","entry":"remove_diagonal_blocks","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"evaluation.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/evaluation.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"5ceb2c36a8c88939"}},{"code_sha256_prefix":"0f11d82afcfe7ee7","entry":"all_gather_tensors","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"communication.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/communication.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"0f11d82afcfe7ee7"}},{"code_sha256_prefix":"7e5b6f7d2706cbc7","entry":"build_model","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"model.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/model.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"7e5b6f7d2706cbc7"}},{"code_sha256_prefix":"2553afafba450290","entry":"linear_evaluation","repo":"amazon-science/contrastive_emc2","repo_kind":"official","path":"linear_eval.py","file_url":"https://github.com/amazon-science/contrastive_emc2/blob/HEAD/linear_eval.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"2553afafba450290"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}