README.md 4.42 KB
Newer Older
Rayyyyy's avatar
Rayyyyy committed
1
2
3
4
5
6
# Sentence-BERT
## 论文
`Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks`
- https://arxiv.org/pdf/1908.10084.pdf

## 模型结构
Rayyyyy's avatar
Rayyyyy committed
7
一种对预训练BERT网络的改进,它使用连体和三重网络结构来获得语义上有意义的句子嵌入,可以使用余弦相似度进行比较。
Rayyyyy's avatar
Rayyyyy committed
8
<div align=center>
Rayyyyy's avatar
Rayyyyy committed
9
    <img src="./doc/model.png" width=300 height=400/>
Rayyyyy's avatar
Rayyyyy committed
10
11
12
13
14
15
</div>

## 算法原理
对于每个句子对,通过网络传递句子A和句子B,从而得到embeddings u 和 v。使用余弦相似度计算embedding的相似度,并将结果与 gold similarity score进行比较。这允许网络进行微调,并识别句子的相似性.

<div align=center>
Rayyyyy's avatar
Rayyyyy committed
16
    <img src="./doc/infer.png" width=500 height=520/>
Rayyyyy's avatar
Rayyyyy committed
17
18
19
</div>

## 环境配置
Rayyyyy's avatar
Rayyyyy committed
20
-v 路径、docker_name和imageID根据实际情况修改
Rayyyyy's avatar
Rayyyyy committed
21
22
23
24
25
26
27
28

### Docker(方法一)
```bash
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-centos7.6-dtk24.04-py310

docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=32G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash

cd /your_code_path/sentence-bert_pytorch
Rayyyyy's avatar
Rayyyyy committed
29
pip install -r requirements.txt
Rayyyyy's avatar
Rayyyyy committed
30
31
pip install -U huggingface_hub hf_transfer
export HF_ENDPOINT=https://hf-mirror.com
Rayyyyy's avatar
Rayyyyy committed
32
33
34
35
36
37
38
39
40
```

### Dockerfile(方法二)
```bash
cd ./docker
docker build --no-cache -t sbert:latest .
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=32G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash

cd /your_code_path/sentence-bert_pytorch
Rayyyyy's avatar
Rayyyyy committed
41
pip install -r requirements.txt
Rayyyyy's avatar
Rayyyyy committed
42
43
pip install -U huggingface_hub hf_transfer
export HF_ENDPOINT=https://hf-mirror.com
Rayyyyy's avatar
Rayyyyy committed
44
45
46
```

### Anaconda(方法三)
chenzk's avatar
chenzk committed
47
1. 关于本项目DCU显卡所需的特殊深度学习库可从光合开发者社区下载安装: https://developer.sourcefind.cn/tool/
Rayyyyy's avatar
Rayyyyy committed
48
49
50
51
52

```bash
DTK软件栈:dtk24.04
python:python3.10
torch:2.1.0
Rayyyyy's avatar
Rayyyyy committed
53
torchvision: 0.16.0
Rayyyyy's avatar
Rayyyyy committed
54
55
56
57
58
59
60
61
```

Tips:以上dtk软件栈、python、torch等DCU相关工具版本需要严格一一对应

2. 其他非特殊库直接按照requirements.txt安装

```bash
cd /your_code_path/sentence-bert_pytorch
Rayyyyy's avatar
Rayyyyy committed
62
pip install -r requirements.txt
Rayyyyy's avatar
Rayyyyy committed
63
64
pip install -U huggingface_hub hf_transfer
export HF_ENDPOINT=https://hf-mirror.com
Rayyyyy's avatar
Rayyyyy committed
65
66
67
```

## 数据集
Rayyyyy's avatar
Rayyyyy committed
68
**训练数据**: [sentence-transformers/stsb](https://hf-mirror.com/datasets/sentence-transformers/stsb),训练代码自动下载。
Rayyyyy's avatar
Update  
Rayyyyy committed
69
**推理数据**: 需要转换成txt格式,参考[gen_simple_wikipedia_v1.py](./gen_simple_wikipedia_v1.py)文件,生成`simple_wiki_pair.txt`
Rayyyyy's avatar
Rayyyyy committed
70
71
72

数据集的目录结构如下:
```
Rayyyyy's avatar
Update  
Rayyyyy committed
73
├── dataset
Rayyyyy's avatar
Rayyyyy committed
74
75
76
77
78
79
80
│   ├──simple_wikipedia_v1
│       ├──simple_wiki_pair.txt # 生成的
│       ├──wiki.simple
│       └──wiki.unsimplified
```

## 训练
chenzk's avatar
chenzk committed
81
82
- **训练**默认模型[bert-base-uncased]
- **微调**默认模型[all-MiniLM-L6-v2]
Rayyyyy's avatar
Rayyyyy committed
83
84

### 单机多卡
Rayyyyy's avatar
Rayyyyy committed
85
86
87
88
89
- 训练
```bash
bash train.sh
```
- 微调
Rayyyyy's avatar
Rayyyyy committed
90
91
92
93
94
```bash
bash finetune.sh
```

### 单机单卡
Rayyyyy's avatar
Rayyyyy committed
95
- 训练
Rayyyyy's avatar
Rayyyyy committed
96
```bash
Rayyyyy's avatar
Rayyyyy committed
97
python training_stsbenchmark.py --train_batch_size 64 --num_epochs 5
Rayyyyy's avatar
Rayyyyy committed
98
99
100
```
- 微调
```bash
Rayyyyy's avatar
Rayyyyy committed
101
python training_stsbenchmark_continue_training.py --train_batch_size 64 --num_epochs 5
Rayyyyy's avatar
Rayyyyy committed
102
103
104
```

## 推理
Rayyyyy's avatar
Rayyyyy committed
105
106
107
1. 预训练模型下载[pretrained models](https://www.sbert.net/docs/pretrained_models.html), 当前默认为[all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)模型;
2. 执行以下命令,测试数据默认为`./datasets/simple_wikipedia_v1/simple_wiki_pair.txt`,可修改`--data_path`参数为其他待测文件地址,文件内容格式请参考[simple_wiki_pair.txt](./datasets/simple_wikipedia_v1/simple_wiki_pair.txt)

Rayyyyy's avatar
Rayyyyy committed
108
```bash
Rayyyyy's avatar
Rayyyyy committed
109
python infer.py --data_path ./dataset/simple_wikipedia_v1/simple_wiki_pair.txt --model_name_or_path all-MiniLM-L6-v2
Rayyyyy's avatar
Rayyyyy committed
110
111
112
113
114
115
116
```

## result
<div align=center>
    <img src="./doc/results.png"/>
</div>

Rayyyyy's avatar
Rayyyyy committed
117
## 精度
Rayyyyy's avatar
Rayyyyy committed
118
119
120
121
122
在sts-test数据集上评估模型,Cosine-Similarity得分对比
| device | backbone | epoch | Pearson | Spearman  |
| :------: | :------: | :------: | :------: | :------: |
| K100 | bert-base-uncased | 5 | 0.8500 | 0.8460 |
| A800 | bert-base-uncased | 5 | 0.8449 | 0.8385 |
Rayyyyy's avatar
Rayyyyy committed
123
124
125

## 应用场景
### 算法类别
Rayyyyy's avatar
Rayyyyy committed
126
语义文本相似度
Rayyyyy's avatar
Rayyyyy committed
127
128
129
130
131

### 热点应用行业
教育,网安,政府

## 源码仓库及问题反馈
chenzk's avatar
chenzk committed
132
- https://developer.sourcefind.cn/codes/modelzoo/sentence-bert_pytorch
Rayyyyy's avatar
Rayyyyy committed
133
134
135

## 参考资料
- https://github.com/UKPLab/sentence-transformers