Home

Awesome

ConSERT

Code for our ACL 2021 paper - ConSERT: A Contrastive Framework for Self-Supervised Sentence Representation Transfer

Requirements

torch==1.6.0
cudatoolkit==10.0.103
cudnn==7.6.5
sentence-transformers==0.3.9
transformers==3.4.0
tensorboardX==2.1
pandas==1.1.5
sentencepiece==0.1.85
matplotlib==3.4.1
apex==0.1.0

To install apex, run:

git clone https://github.com/NVIDIA/apex
cd apex
pip install -v --disable-pip-version-check --no-cache-dir ./

Get Started

English STS Tasks

  1. Download pre-trained language model (e.g. bert-base-uncased) to folder ./bert-base-uncased from HuggingFace's Library
  2. Download STS datasets to ./data folder by running cd data && bash get_transfer_data.bash. The script is modified from SentEval toolkit
  3. Run the scripts in the folder ./scripts to reproduce our experiments. For example, run the following script to train unsupervised consert-base:
    bash scripts/unsup-consert-base.sh
    

Update: Recently we update the support for RoBERTa model. Download the RoBERTa model to folder e.g. ./roberta-base and use scripts in ./scripts/roberta instead.

Chinese STS Tasks

  1. Download the pre-trained language model (e.g. chinese-roberta-wwm-ext) to folder ./chinese-roberta-wwm-ext from HuggingFace's Library
  2. Download the chinese STS datasets to ./data folder by running cd data && bash get_chinese_sts_data.bash
  3. Run the scripts in the folder ./scripts/chinese to train models for chinese STS tasks. For example, run the following script to train base model for atec_ccks task:
    bash scripts/chinese/unsup-consert-base-atec_ccks.sh
    
    The --chinese_dataset option in main.py is used to select which chinese STS dataset to use.

Pre-trained Models & Results

English STS Tasks

IDModelSTS12STS13STS14STS15STS16STSbSICK-RAvg.
-bert-base-uncased (baseline)35.2059.5349.3763.3962.7348.1858.6053.86
-bert-large-uncased (baseline)33.0657.6447.9555.8362.4249.6653.8751.49
1unsup-consert-base [Google Drive] [百度云q571]64.6478.4969.0779.7275.9573.9767.3172.74
2unsup-consert-large [Google Drive] [百度云9fm1]70.2883.2373.8082.7377.1477.7470.1976.45
3sup-sbert-base (re-impl.) [Google Drive] [百度云msqy]69.9376.0072.1578.5973.5376.1073.0174.19
4sup-sbert-large (re-impl.) [Google Drive] [百度云0oir]73.0677.7775.2181.6377.3079.7474.7577.07
5sup-consert-joint-base [Google Drive] [百度云jks5]70.9279.9874.8881.7676.4678.9978.1577.31
6sup-consert-joint-large [Google Drive] [百度云xua4]73.1581.4577.0483.3277.2881.1578.3478.82
7sup-consert-sup-unsup-base [Google Drive] [百度云5mc8]73.0284.8677.3282.7078.2081.3475.0078.92
8sup-consert-sup-unsup-large [Google Drive] [百度云tta1]74.9985.5879.1784.2580.1983.1777.4380.68
9sup-consert-joint-unsup-base [Google Drive] [百度云cf07]74.4684.1977.0883.7778.5581.3777.0179.49
10sup-consert-joint-unsup-large [Google Drive] [百度云v5x5]76.9385.2078.6985.4479.3482.9376.7180.75

Note:

  1. All the base models are trained from bert-base-uncased and the large models are trained from bert-large-uncased.
  2. For the unsupervised transfer, we merge all unlabeled texts from 7 STS datasets (STS12-16, STSbenchmark and SICK-Relatedness) as the training data (total 89192 sentences), and use the STSbenchmark dev split (including 1500 human-annotated sentence pairs) to select the best checkpoint.
  3. The sentence representations are obtained by averaging the token embeddings at the last two layers of BERT.
  4. For model 2 to 10, we re-trained them on a single GeForce RTX 3090 with pytorch 1.8.1 and cuda 11.1 (rather than V100, pytorch 1.6.0 and cuda 10.0 in our initial experiments) and changed the max_seq_length from 64 to 40 to reduce the required GPU memory (only for large models). Consequently, the results shown here may be slightly different from those reported in our paper.

Chinese STS Tasks

IDModelatec_ccksbqlcqmcpawsxstsb
-chinese-roberta-wwm-ext (baseline)11.2840.2159.8909.3560.86
-chinese-roberta-wwm-ext-large (baseline)13.7536.7760.3609.9458.72
1unsup-consert-base-atec_ccks [百度云v593]27.3947.6160.7008.1764.74
2unsup-consert-base-bq [百度云cwnu]11.5547.2061.4708.4764.44
3unsup-consert-base-lcqmc [百度云pdax]04.5738.1567.3408.8067.78
4unsup-consert-base-pawsx [百度云iko8]08.6638.3561.9709.3663.89
5unsup-consert-base-stsb [百度云7r7n]07.2940.8167.3906.2472.82
6unsup-consert-large-atec_ccks [百度云4zlz]29.9247.5059.8309.7266.37
7unsup-consert-large-bq [百度云gnte]15.4747.0860.1010.0466.94
8unsup-consert-large-lcqmc [百度云wetp]11.4037.7966.4510.8167.68
9unsup-consert-large-pawsx [百度云u6t9]11.2536.2565.1511.3867.45
10unsup-consert-large-stsb [百度云8tyz]08.0040.8567.7210.1374.50

Note:

  1. All the base models are trained from hfl/chinese-roberta-wwm-ext and the large models are trained from hfl/chinese-roberta-wwm-ext-large.
  2. For each model, we train it on single chinese STS dataset but evaluate it on all 5 datasets. The bold numbers indicate that the model is evaluated on the same dataset it trained on.
  3. The sentence representations are also obtained by averaging the token embeddings at the last two layers of BERT.
  4. For base models, we set batch_size to 96 and max_seq_length to 64, while for large models, we set batch_size to 32 and max_seq_length to 40 to reduce the required GPU memory.

Citation

@inproceedings{yan-etal-2021-consert,
    title = "{C}on{SERT}: A Contrastive Framework for Self-Supervised Sentence Representation Transfer",
    author = "Yan, Yuanmeng  and
      Li, Rumei  and
      Wang, Sirui  and
      Zhang, Fuzheng  and
      Wu, Wei  and
      Xu, Weiran",
    booktitle = "Proceedings of the 59th Annual Meeting of the Association for Computational Linguistics and the 11th International Joint Conference on Natural Language Processing (Volume 1: Long Papers)",
    month = aug,
    year = "2021",
    address = "Online",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2021.acl-long.393",
    doi = "10.18653/v1/2021.acl-long.393",
    pages = "5065--5075",
    abstract = "Learning high-quality sentence representations benefits a wide range of natural language processing tasks. Though BERT-based pre-trained language models achieve high performance on many downstream tasks, the native derived sentence representations are proved to be collapsed and thus produce a poor performance on the semantic textual similarity (STS) tasks. In this paper, we present ConSERT, a Contrastive Framework for Self-Supervised SEntence Representation Transfer, that adopts contrastive learning to fine-tune BERT in an unsupervised and effective way. By making use of unlabeled texts, ConSERT solves the collapse issue of BERT-derived sentence representations and make them more applicable for downstream tasks. Experiments on STS datasets demonstrate that ConSERT achieves an 8{\%} relative improvement over the previous state-of-the-art, even comparable to the supervised SBERT-NLI. And when further incorporating NLI supervision, we achieve new state-of-the-art performance on STS tasks. Moreover, ConSERT obtains comparable results with only 1000 samples available, showing its robustness in data scarcity scenarios.",
}