{"id":13754247,"url":"https://github.com/voidism/DiffCSE","last_synced_at":"2025-05-09T22:31:42.926Z","repository":{"id":44666361,"uuid":"481012433","full_name":"voidism/DiffCSE","owner":"voidism","description":"Code for the NAACL 2022 long paper \"DiffCSE: Difference-based Contrastive Learning for Sentence Embeddings\"","archived":false,"fork":false,"pushed_at":"2022-10-27T19:56:54.000Z","size":6601,"stargazers_count":291,"open_issues_count":9,"forks_count":27,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-16T07:33:16.807Z","etag":null,"topics":["contrastive-learning","representation-learning","self-supervised-learning","sentence-embeddings","sentence-similarity","sentence-transformers"],"latest_commit_sha":null,"homepage":"https://arxiv.org/abs/2204.10298","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/voidism.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-13T00:08:59.000Z","updated_at":"2024-11-13T17:10:09.000Z","dependencies_parsed_at":"2022-07-16T01:30:35.413Z","dependency_job_id":null,"html_url":"https://github.com/voidism/DiffCSE","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voidism%2FDiffCSE","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voidism%2FDiffCSE/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voidism%2FDiffCSE/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voidism%2FDiffCSE/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/voidism","download_url":"https://codeload.github.com/voidism/DiffCSE/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253335729,"owners_count":21892720,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["contrastive-learning","representation-learning","self-supervised-learning","sentence-embeddings","sentence-similarity","sentence-transformers"],"created_at":"2024-08-03T09:01:51.891Z","updated_at":"2025-05-09T22:31:37.905Z","avatar_url":"https://github.com/voidism.png","language":"Python","readme":"# DiffCSE: Difference-based Contrastive Learning for Sentence Embeddings\n\n[![Version](https://img.shields.io/badge/Version-v0.1.0-blue?color=FF8000?color=009922)](https://img.shields.io/badge/Version-v0.1.0-blue)\n[![License: MIT](https://img.shields.io/badge/License-MIT-orange.svg)](https://opensource.org/licenses/MIT)\n[![Arxiv](https://img.shields.io/badge/arXiv-2204.10298-B21A1B)](https://arxiv.org/abs/2204.10298)\n[![Hugging Face Models](https://img.shields.io/badge/%F0%9F%A4%97-Models-yellow)](https://huggingface.co/voidism)\n[![Hugging Face Transformers](https://img.shields.io/badge/%F0%9F%A4%97-Transformers-pink?color=FF33CC)](https://github.com/huggingface/transformers)\n[![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/YungSungChuang/status/1517518077902000129)\n[![GitHub Stars](https://img.shields.io/github/stars/voidism/DiffCSE?style=social)](https://github.com/voidism/DiffCSE/stargazers)\n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/voidism/DiffCSE/blob/master/diffcse_evaluation.ipynb)\n\narXiv link: https://arxiv.org/abs/2204.10298  \nTo be published in [**NAACL 2022**](https://2022.naacl.org/)\n\nAuthors:\n[Yung-Sung Chuang](https://people.csail.mit.edu/yungsung/), \n[Rumen Dangovski](http://super-ms.mit.edu/rumen.html),\n[Hongyin Luo](http://people.csail.mit.edu/hyluo/),\n[Yang Zhang](https://mitibmwatsonailab.mit.edu/people/yang-zhang/),\n[Shiyu Chang](https://code-terminator.github.io/),\n[Marin Soljačić](http://www.mit.edu/~soljacic/marin.html),\n[Shang-Wen Li](https://swdanielli.github.io/),\n[Scott Wen-tau Yih](https://scottyih.org/),\n[Yoon Kim](https://people.csail.mit.edu/yoonkim/),\n[James Glass](http://groups.csail.mit.edu/sls/people/glass.shtml)\n\n\nOur code is mainly based on the code of [SimCSE](https://arxiv.org/abs/2104.08821). Please refer to their [repository](https://github.com/princeton-nlp/SimCSE) for more detailed information.\n\n## Overview\n![DiffCSE](diffcse.png)\n\nWe propose DiffCSE, an unsupervised contrastive learning framework for learning sentence embeddings. DiffCSE learns sentence embeddings that are sensitive to the difference between the original sentence and an edited sentence, where the edited sentence is obtained by stochastically masking out the original sentence and then sampling from a masked language model. We show that DiffSCE is an instance of equivariant contrastive learning [(Dangovski et al., 2021)](https://arxiv.org/abs/2111.00899), which generalizes contrastive learning and learns representations that are insensitive to certain types of augmentations and sensitive to other \"harmful\" types of augmentations. Our experiments show that DiffCSE achieves state-of-the-art results among unsupervised sentence representation learning methods, outperforming unsupervised SimCSE by 2.3 absolute points on semantic textual similarity tasks. \n\n## Setups\n\n[![Python](https://img.shields.io/badge/python-3.9.5-blue?logo=python\u0026logoColor=FED643)](https://www.python.org/downloads/release/python-395/)\n[![Pytorch](https://img.shields.io/badge/pytorch-1.7.1-red?logo=pytorch)](https://pytorch.org/get-started/previous-versions/)\n\n### Requirements\n* Python 3.9.5\n\n### Install our customized Transformers package\n```\ncd transformers-4.2.1\npip install .\n```\n\n### Install other packages\n```\npip install -r requirements.txt\n```\n\n### Download the pretraining dataset\n```\ncd data\nbash download_wiki.sh\n```\n\n### Download the downstream dataset\n```\ncd SentEval/data/downstream/\nbash download_dataset.sh\n```\n\n## Training\n(The same as `run_diffcse.sh`.)\n```bash\npython train.py \\\n    --model_name_or_path bert-base-uncased \\\n    --generator_name distilbert-base-uncased \\\n    --train_file data/wiki1m_for_simcse.txt \\\n    --output_dir \u003cyour_output_model_dir\u003e \\\n    --num_train_epochs 2 \\\n    --per_device_train_batch_size 64 \\\n    --learning_rate 7e-6 \\\n    --max_seq_length 32 \\\n    --evaluation_strategy steps \\\n    --metric_for_best_model stsb_spearman \\\n    --load_best_model_at_end \\\n    --eval_steps 125 \\\n    --pooler_type cls \\\n    --mlp_only_train \\\n    --overwrite_output_dir \\\n    --logging_first_step \\\n    --logging_dir \u003cyour_logging_dir\u003e \\\n    --temp 0.05 \\\n    --do_train \\\n    --do_eval \\\n    --batchnorm \\\n    --lambda_weight 0.005 \\\n    --fp16 --masking_ratio 0.30\n```\n\nOur new arguments:\n* `--lambda_weight`: the lambda coefficient mentioned in Section 3 of our paper.\n* `--masking_ratio`: the masking ratio for MLM generator to randomly replace tokens.\n* `--generator_name`: the model name of generator. For `bert-base-uncased`, we use `distilbert-base-uncased`. For `roberta-base`, we use `distilroberta-base`.\n\n\nArguments from [SimCSE](https://github.com/princeton-nlp/SimCSE):\n* `--train_file`: Training file path (`data/wiki1m_for_simcse.txt`). \n* `--model_name_or_path`: Pre-trained checkpoints to start with such as BERT-based models (`bert-base-uncased`, `bert-large-uncased`, etc.) and RoBERTa-based models (`RoBERTa-base`, `RoBERTa-large`).\n* `--temp`: Temperature for the contrastive loss. We always use `0.05`.\n* `--pooler_type`: Pooling method.\n* `--mlp_only_train`: For unsupervised SimCSE or DiffCSE, it works better to train the model with MLP layer but test the model without it. You should use this argument when training unsupervised SimCSE/DiffCSE models.\n\nFor the results in our paper, we use a NVidia 2080Ti GPU with CUDA 11.2. Using different types of devices or different versions of CUDA/Python/PyTorch may lead to slightly different performance.\n\n## Evaluation\n\n \n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/voidism/DiffCSE/blob/master/diffcse_evaluation.ipynb)  \nWe provide a simple colab notebook to reproduce our results easily. We can also run the commands below for evaluation:\n\n```bash\npython evaluation.py \\\n    --model_name_or_path \u003cyour_output_model_dir\u003e \\\n    --pooler cls_before_pooler \\\n    --task_set \u003csts|transfer|full\u003e \\\n    --mode test\n```\n\nTo evaluate our pretrained DiffCSE checkpoints, we can use the following scripts:\n\n### BERT \n#### STS\n\n```bash\npython evaluation.py \\\n    --model_name_or_path voidism/diffcse-bert-base-uncased-sts \\\n    --pooler cls_before_pooler \\\n    --task_set sts \\\n    --mode test\n```\n#### Transfer Tasks\n\n```bash\npython evaluation.py \\\n    --model_name_or_path voidism/diffcse-bert-base-uncased-trans \\\n    --pooler cls_before_pooler \\\n    --task_set transfer \\\n    --mode test\n```\n\n### RoBERTa \n#### STS\n\n```bash\npython evaluation.py \\\n    --model_name_or_path voidism/diffcse-roberta-base-sts \\\n    --pooler cls_before_pooler \\\n    --task_set sts \\\n    --mode test\n```\n#### Transfer Tasks\n\n```bash\npython evaluation.py \\\n    --model_name_or_path voidism/diffcse-roberta-base-trans \\\n    --pooler cls_before_pooler \\\n    --task_set transfer \\\n    --mode test\n```\n\nFor more detailed information, please check [SimCSE's GitHub repo](https://github.com/princeton-nlp/SimCSE).\n\n\n## Pretrained models\n\n[![Hugging Face Models](https://img.shields.io/badge/%F0%9F%A4%97-Models-yellow)](https://huggingface.co/voidism)\n\n* DiffCSE-BERT-base (STS): https://huggingface.co/voidism/diffcse-bert-base-uncased-sts\n* DiffCSE-BERT-base (transfer tasks): https://huggingface.co/voidism/diffcse-bert-base-uncased-trans\n* DiffCSE-RoBERTa-base (STS): https://huggingface.co/voidism/diffcse-roberta-base-sts\n* DiffCSE-RoBERTa-base (transfer tasks): https://huggingface.co/voidism/diffcse-roberta-base-trans\n\nWe can load the models using the API provided by [SimCSE](https://github.com/princeton-nlp/SimCSE). \nSee [Getting Started](https://github.com/princeton-nlp/SimCSE#getting-started) for more information.\n\n```python\nfrom diffcse import DiffCSE\nmodel_bert_sts = DiffCSE(\"voidism/diffcse-bert-base-uncased-sts\")\nmodel_bert_trans = DiffCSE(\"voidism/diffcse-bert-base-uncased-trans\")\nmodel_roberta_sts = DiffCSE(\"voidism/diffcse-roberta-base-sts\")\nmodel_roberta_trans = DiffCSE(\"voidism/diffcse-roberta-base-trans\")\n```\n\n## Citations\n\n[![DOI](https://img.shields.io/badge/DOI-10.48550/arXiv.2204.10298-green?color=FF8000?color=009922)](https://doi.org/10.48550/arXiv.2204.10298)\n\nPlease cite our paper and the SimCSE paper if they are helpful to your work!\n\n```bibtex\n@inproceedings{chuang2022diffcse,\n   title={{DiffCSE}: Difference-based Contrastive Learning for Sentence Embeddings},\n   author={Chuang, Yung-Sung and Dangovski, Rumen and Luo, Hongyin and Zhang, Yang and Chang, Shiyu and Soljacic, Marin and Li, Shang-Wen and Yih, Wen-tau and Kim, Yoon and Glass, James},\n   booktitle={Annual Conference of the North American Chapter of the Association for Computational Linguistics (NAACL)},\n   year={2022}\n}\n\n@inproceedings{gao2021simcse,\n   title={{SimCSE}: Simple Contrastive Learning of Sentence Embeddings},\n   author={Gao, Tianyu and Yao, Xingcheng and Chen, Danqi},\n   booktitle={Empirical Methods in Natural Language Processing (EMNLP)},\n   year={2021}\n}\n```\n","funding_links":[],"categories":["文本匹配 文本检索 文本相似度"],"sub_categories":["其他_文本生成、文本对话"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvoidism%2FDiffCSE","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvoidism%2FDiffCSE","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvoidism%2FDiffCSE/lists"}