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

## 模型结构

<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
29
30

### 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
pip install -e .
Rayyyyy's avatar
Rayyyyy committed
31
32
pip install -U huggingface_hub hf_transfer
export HF_ENDPOINT=https://hf-mirror.com
Rayyyyy's avatar
Rayyyyy committed
33
34
35
36
37
38
39
40
41
42
43
44
45
```

### Dockerfile(方法二)

```bash
cd ./docker
cp ../requirements.txt requirements.txt

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
pip install -e .
Rayyyyy's avatar
Rayyyyy committed
46
47
pip install -U huggingface_hub hf_transfer
export HF_ENDPOINT=https://hf-mirror.com
Rayyyyy's avatar
Rayyyyy committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
```

### Anaconda(方法三)
1. 关于本项目DCU显卡所需的特殊深度学习库可从光合开发者社区下载安装: https://developer.hpccube.com/tool/

```bash
DTK软件栈:dtk24.04
python:python3.10
torch:2.1.0
```

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

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

```bash
cd /your_code_path/sentence-bert_pytorch
pip install -e .
Rayyyyy's avatar
Rayyyyy committed
66
67
pip install -U huggingface_hub hf_transfer
export HF_ENDPOINT=https://hf-mirror.com
Rayyyyy's avatar
Rayyyyy committed
68
69
70
```

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

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

## 训练
Rayyyyy's avatar
Rayyyyy committed
84
85
- **训练**默认模型[bert-base-uncased](https://huggingface.co/google-bert/bert-base-uncased)
- **微调**默认模型[all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)
Rayyyyy's avatar
Rayyyyy committed
86
87

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

### 单机单卡
Rayyyyy's avatar
Rayyyyy committed
98
- 训练
Rayyyyy's avatar
Rayyyyy committed
99
```bash
Rayyyyy's avatar
Rayyyyy committed
100
101
102
103
104
python training_stsbenchmark.py
```
- 微调
```bash
python training_stsbenchmark_continue_training.py
Rayyyyy's avatar
Rayyyyy committed
105
106
107
```

## 推理
Rayyyyy's avatar
Rayyyyy committed
108
109
110
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
111
```bash
Rayyyyy's avatar
Rayyyyy committed
112
python infer.py --data_path ./datasets/simple_wikipedia_v1/simple_wiki_pair.txt --model_name_or_path all-MiniLM-L6-v2
Rayyyyy's avatar
Rayyyyy committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
```

## result

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

### 精度
暂无

## 应用场景
### 算法类别
NLP

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

## 源码仓库及问题反馈
- https://developer.hpccube.com/codes/modelzoo/sentence-bert_pytorch

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