Feature Extraction
Transformers
PyTorch
bert
Inference Endpoints
voidism commited on
Commit
57b2bcc
1 Parent(s): 095056b

update README

Browse files
Files changed (1) hide show
  1. README.md +212 -0
README.md CHANGED
@@ -1,3 +1,215 @@
1
  ---
2
  license: apache-2.0
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
  ---
4
+ # DiffCSE: Difference-based Contrastive Learning for Sentence Embeddings
5
+
6
+ [![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)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-orange.svg)](https://opensource.org/licenses/MIT)
8
+ [![Arxiv](https://img.shields.io/badge/arXiv-2204.10298-B21A1B)](https://arxiv.org/abs/2204.10298)
9
+ [![Hugging Face Models](https://img.shields.io/badge/%F0%9F%A4%97-Models-yellow)](https://huggingface.co/voidism)
10
+ [![Hugging Face Transformers](https://img.shields.io/badge/%F0%9F%A4%97-Transformers-pink?color=FF33CC)](https://github.com/huggingface/transformers)
11
+ [![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/YungSungChuang/status/1517518077902000129)
12
+ [![GitHub Stars](https://img.shields.io/github/stars/voidism/DiffCSE?style=social)](https://github.com/voidism/DiffCSE/stargazers)
13
+
14
+ [![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)
15
+
16
+ arXiv link: https://arxiv.org/abs/2204.10298
17
+ To be published in [**NAACL 2022**](https://2022.naacl.org/)
18
+
19
+ Authors:
20
+ [Yung-Sung Chuang](https://people.csail.mit.edu/yungsung/),
21
+ [Rumen Dangovski](http://super-ms.mit.edu/rumen.html),
22
+ [Hongyin Luo](http://people.csail.mit.edu/hyluo/),
23
+ [Yang Zhang](https://mitibmwatsonailab.mit.edu/people/yang-zhang/),
24
+ [Shiyu Chang](https://code-terminator.github.io/),
25
+ [Marin Soljačić](http://www.mit.edu/~soljacic/marin.html),
26
+ [Shang-Wen Li](https://swdanielli.github.io/),
27
+ [Scott Wen-tau Yih](https://scottyih.org/),
28
+ [Yoon Kim](https://people.csail.mit.edu/yoonkim/),
29
+ [James Glass](http://groups.csail.mit.edu/sls/people/glass.shtml)
30
+
31
+
32
+ Our 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.
33
+
34
+ ## Overview
35
+ ![DiffCSE](diffcse.png)
36
+
37
+ We 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.
38
+
39
+ ## Setups
40
+
41
+ [![Python](https://img.shields.io/badge/python-3.9.5-blue?logo=python&logoColor=FED643)](https://www.python.org/downloads/release/python-395/)
42
+ [![Pytorch](https://img.shields.io/badge/pytorch-1.7.1-red?logo=pytorch)](https://pytorch.org/get-started/previous-versions/)
43
+
44
+ ### Requirements
45
+ * Python 3.9.5
46
+
47
+ ### Install our customized Transformers package
48
+ ```
49
+ cd transformers-4.2.1
50
+ pip install .
51
+ ```
52
+ > If you have already installed `transformers==4.2.1` through pip, you need to put `modeling_bert.py` into `<your_python_env>/site-packages/transformers/models/bert/modeling_bert.py` and `modeling_roberta.py` into `<your_python_env>/site-packages/transformers/models/bert/modeling_roberta.py`.
53
+ > We modify these two files in the package so that we can perform _conditional_ pretraining tasks using BERT/RoBERTa. If possible, please directly pip install our customized Transformers package.
54
+
55
+ ### Install other packages
56
+ ```
57
+ pip install -r requirements.txt
58
+ ```
59
+
60
+ ### Download the pretraining dataset
61
+ ```
62
+ cd data
63
+ bash download_wiki.sh
64
+ ```
65
+
66
+ ### Download the downstream dataset
67
+ ```
68
+ cd SentEval/data/downstream/
69
+ bash download_dataset.sh
70
+ ```
71
+
72
+ ## Training
73
+ (The same as `run_diffcse.sh`.)
74
+ ```bash
75
+ python train.py \
76
+ --model_name_or_path bert-base-uncased \
77
+ --generator_name distilbert-base-uncased \
78
+ --train_file data/wiki1m_for_simcse.txt \
79
+ --output_dir <your_output_model_dir> \
80
+ --num_train_epochs 2 \
81
+ --per_device_train_batch_size 64 \
82
+ --learning_rate 7e-6 \
83
+ --max_seq_length 32 \
84
+ --evaluation_strategy steps \
85
+ --metric_for_best_model stsb_spearman \
86
+ --load_best_model_at_end \
87
+ --eval_steps 125 \
88
+ --pooler_type cls \
89
+ --mlp_only_train \
90
+ --overwrite_output_dir \
91
+ --logging_first_step \
92
+ --logging_dir <your_logging_dir> \
93
+ --temp 0.05 \
94
+ --do_train \
95
+ --do_eval \
96
+ --batchnorm \
97
+ --lambda_weight 0.005 \
98
+ --fp16 --masking_ratio 0.30
99
+ ```
100
+
101
+ Our new arguments:
102
+ * `--lambda_weight`: the lambda coefficient mentioned in Section 3 of our paper.
103
+ * `--masking_ratio`: the masking ratio for MLM generator to randomly replace tokens.
104
+ * `--generator_name`: the model name of generator. For `bert-base-uncased`, we use `distilbert-base-uncased`. For `roberta-base`, we use `distilroberta-base`.
105
+
106
+
107
+ Arguments from [SimCSE](https://github.com/princeton-nlp/SimCSE):
108
+ * `--train_file`: Training file path (`data/wiki1m_for_simcse.txt`).
109
+ * `--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`).
110
+ * `--temp`: Temperature for the contrastive loss. We always use `0.05`.
111
+ * `--pooler_type`: Pooling method.
112
+ * `--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.
113
+
114
+ For 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.
115
+
116
+ ## Evaluation
117
+
118
+
119
+ [![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)
120
+ We provide a simple colab notebook to reproduce our results easily. We can also run the commands below for evaluation:
121
+
122
+ ```bash
123
+ python evaluation.py \
124
+ --model_name_or_path <your_output_model_dir> \
125
+ --pooler cls_before_pooler \
126
+ --task_set <sts|transfer|full> \
127
+ --mode test
128
+ ```
129
+
130
+ To evaluate our pretrained DiffCSE checkpoints, we can use the following scripts:
131
+
132
+ ### BERT
133
+ #### STS
134
+
135
+ ```bash
136
+ python evaluation.py \
137
+ --model_name_or_path voidism/diffcse-bert-base-uncased-sts \
138
+ --pooler cls_before_pooler \
139
+ --task_set sts \
140
+ --mode test
141
+ ```
142
+ #### Transfer Tasks
143
+
144
+ ```bash
145
+ python evaluation.py \
146
+ --model_name_or_path voidism/diffcse-bert-base-uncased-trans \
147
+ --pooler cls_before_pooler \
148
+ --task_set transfer \
149
+ --mode test
150
+ ```
151
+
152
+ ### RoBERTa
153
+ #### STS
154
+
155
+ ```bash
156
+ python evaluation.py \
157
+ --model_name_or_path voidism/diffcse-roberta-base-sts \
158
+ --pooler cls_before_pooler \
159
+ --task_set sts \
160
+ --mode test
161
+ ```
162
+ #### Transfer Tasks
163
+
164
+ ```bash
165
+ python evaluation.py \
166
+ --model_name_or_path voidism/diffcse-roberta-base-trans \
167
+ --pooler cls_before_pooler \
168
+ --task_set transfer \
169
+ --mode test
170
+ ```
171
+
172
+ For more detailed information, please check [SimCSE's GitHub repo](https://github.com/princeton-nlp/SimCSE).
173
+
174
+
175
+ ## Pretrained models
176
+
177
+ [![Hugging Face Models](https://img.shields.io/badge/%F0%9F%A4%97-Models-yellow)](https://huggingface.co/voidism)
178
+
179
+ * DiffCSE-BERT-base (STS): https://huggingface.co/voidism/diffcse-bert-base-uncased-sts
180
+ * DiffCSE-BERT-base (transfer tasks): https://huggingface.co/voidism/diffcse-bert-base-uncased-trans
181
+ * DiffCSE-RoBERTa-base (STS): https://huggingface.co/voidism/diffcse-roberta-base-sts
182
+ * DiffCSE-RoBERTa-base (transfer tasks): https://huggingface.co/voidism/diffcse-roberta-base-trans
183
+
184
+ We can load the models using the API provided by [SimCSE](https://github.com/princeton-nlp/SimCSE).
185
+ See [Getting Started](https://github.com/princeton-nlp/SimCSE#getting-started) for more information.
186
+
187
+ ```python
188
+ from diffcse import DiffCSE
189
+ model_bert_sts = DiffCSE("voidism/diffcse-bert-base-uncased-sts")
190
+ model_bert_trans = DiffCSE("voidism/diffcse-bert-base-uncased-trans")
191
+ model_roberta_sts = DiffCSE("voidism/diffcse-roberta-base-sts")
192
+ model_roberta_trans = DiffCSE("voidism/diffcse-roberta-base-trans")
193
+ ```
194
+
195
+ ## Citations
196
+
197
+ [![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)
198
+
199
+ Please cite our paper and the SimCSE paper if they are helpful to your work!
200
+
201
+ ```bibtex
202
+ @inproceedings{chuang2022diffcse,
203
+ title={{DiffCSE}: Difference-based Contrastive Learning for Sentence Embeddings},
204
+ 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},
205
+ booktitle={Annual Conference of the North American Chapter of the Association for Computational Linguistics (NAACL)},
206
+ year={2022}
207
+ }
208
+
209
+ @inproceedings{gao2021simcse,
210
+ title={{SimCSE}: Simple Contrastive Learning of Sentence Embeddings},
211
+ author={Gao, Tianyu and Yao, Xingcheng and Chen, Danqi},
212
+ booktitle={Empirical Methods in Natural Language Processing (EMNLP)},
213
+ year={2021}
214
+ }
215
+ ```