Skip to content

Commit 374d31b

Browse files
吴高升mls1999725tianxinchenxiaozeng
authored
Add neural search application codes (#1463)
* add semantic indexing files * update indexing code * update milvus config * update readme * update readme * update readme * update readme * add code * add export model * add base model * add inference code * fix the inference bug * rename dir * add ernie matching code * update ernie matching code * update readme * update readme * update readme * rename dir * update ernie 1.0 * update ernie readme * add simcse * update simcse readme * add inbach negative code * update readme * add batch neg train code * update readme * add experimental results * update readme * update readme * update milvus files * update readme * add missing codes * add requirements * add inbatch negative * add inbatch negative code * update batch negative * update code * update * add simcse codes * update the readme * update simcse readme * add simcse infer code * add simcse inference * update model * update readme * update ernie matching readme * update readme * remove redundant code * update readme * update inbatch readme * update readme * update recall set * update readme * update readme * updatea bash * update inbatch negative readme * update readme * update readme * delete anne files * update baseline * update cpu support * update readme * update readme * update readme * update readme * rename readme * update * update readme * update files * update files * update readme * update readme * add simcse codes * delete files * update simcse * update readme * add updates * update readme * update readme * update cpu setting * add text matching * update bash * update export module * update * update readme * update files ernie 1.0 * update files * add updates * update readme * update readme * delete reduntant code * update readme * update readme * update readme * move dir * update readme * update readme * update readme * update readme * update readme * update * update readme * update readme * update readme * update files * update readme * update readme * update readme * update img * update readme * update readme * update readme * update names * update requirements * adjust the directory * update readme * update readme * update readme * update readme * update readme * update * Tiny Fix Updata * update readme * update readme * update readme * update readme * update * adjust the readme format * update readme * update readme * add RocketQA * update readme * Update * update readme * update data sample * Update README.md Co-authored-by: mls1999725 <[email protected]> Co-authored-by: tianxin <[email protected]> Co-authored-by: chenxiaozeng <[email protected]>
1 parent 7336d52 commit 374d31b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+11486
-5
lines changed

application/neural_search/README.md

Lines changed: 287 additions & 0 deletions
Large diffs are not rendered by default.
643 KB
Loading
92.3 KB
Loading
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
2+
**目录**
3+
4+
* [背景介绍](#背景介绍)
5+
* [ERNIE-Gram](#ERNIE-Gram)
6+
* [1. 技术方案和评估指标](#技术方案)
7+
* [2. 环境依赖](#环境依赖)
8+
* [3. 代码结构](#代码结构)
9+
* [4. 数据准备](#数据准备)
10+
* [5. 模型训练](#模型训练)
11+
* [6. 评估](#开始评估)
12+
* [7. 预测](#预测)
13+
* [8. 部署](#部署)
14+
15+
<a name="背景介绍"></a>
16+
17+
# 背景介绍
18+
19+
基于ERNIE-Gram训练Pair-wise模型。Pair-wise 匹配模型适合将文本对相似度作为特征之一输入到上层排序模块进行排序的应用场景。
20+
21+
22+
<a name="ERNIE-Gram"></a>
23+
24+
# ERNIE-Gram
25+
26+
<a name="技术方案"></a>
27+
28+
## 1. 技术方案和评估指标
29+
30+
### 技术方案
31+
32+
双塔模型,使用ERNIE-Gram预训练模型,使用margin_ranking_loss训练模型。
33+
34+
35+
### 评估指标
36+
37+
(1)采用 AUC 指标来评估排序模型的排序效果。
38+
39+
**效果评估**
40+
41+
| 模型 | AUC |
42+
| ------------ | ------------ |
43+
| ERNIE-Gram | 0.801 |
44+
45+
<a name="环境依赖"></a>
46+
47+
## 2. 环境依赖和安装说明
48+
49+
**环境依赖**
50+
51+
* python >= 3.x
52+
* paddlepaddle >= 2.1.3
53+
* paddlenlp >= 2.2
54+
* pandas >= 0.25.1
55+
* scipy >= 1.3.1
56+
57+
<a name="代码结构"></a>
58+
59+
## 3. 代码结构
60+
61+
以下是本项目主要代码结构及说明:
62+
63+
```
64+
ernie_matching/
65+
├── deply # 部署
66+
└── python
67+
├── deploy.sh # 预测部署bash脚本
68+
└── predict.py # python 预测部署示例
69+
|—— scripts
70+
├── export_model.sh # 动态图参数导出静态图参数的bash文件
71+
├── train_pairwise.sh # Pair-wise 单塔匹配模型训练的bash文件
72+
├── evaluate.sh # 评估验证文件bash脚本
73+
├── predict_pairwise.sh # Pair-wise 单塔匹配模型预测脚本的bash文件
74+
├── export_model.py # 动态图参数导出静态图参数脚本
75+
├── model.py # Pair-wise 匹配模型组网
76+
├── data.py # Pair-wise 训练样本的转换逻辑 、Pair-wise 生成随机负例的逻辑
77+
├── train_pairwise.py # Pair-wise 单塔匹配模型训练脚本
78+
├── evaluate.py # 评估验证文件
79+
├── predict_pairwise.py # Pair-wise 单塔匹配模型预测脚本,输出文本对是相似度
80+
81+
```
82+
83+
<a name="数据准备"></a>
84+
85+
## 4. 数据准备
86+
87+
### 数据集说明
88+
89+
样例数据如下:
90+
```
91+
个人所得税税务筹划 基于新个税视角下的个人所得税纳税筹划分析新个税;个人所得税;纳税筹划 个人所得税工资薪金税务筹划研究个人所得税,工资薪金,税务筹划
92+
液压支架底座受力分析 ZY4000/09/19D型液压支架的有限元分析液压支架,有限元分析,两端加载,偏载,扭转 基于ANSYS的液压支架多工况受力分析液压支架,四种工况,仿真分析,ANSYS,应力集中,优化
93+
迟发性血管痉挛 西洛他唑治疗动脉瘤性蛛网膜下腔出血后脑血管痉挛的Meta分析西洛他唑,蛛网膜下腔出血,脑血管痉挛,Meta分析 西洛他唑治疗动脉瘤性蛛网膜下腔出血后脑血管痉挛的Meta分析西洛他唑,蛛网膜下腔出血,脑血管痉挛,Meta分析
94+
氧化亚硅 复合溶胶-凝胶一锅法制备锂离子电池氧化亚硅/碳复合负极材料氧化亚硅,溶胶-凝胶法,纳米颗粒,负极,锂离子电池 负载型聚酰亚胺-二氧化硅-银杂化膜的制备和表征聚酰亚胺,二氧化硅,银,杂化膜,促进传输
95+
```
96+
97+
98+
### 数据集下载
99+
100+
101+
- [literature_search_data](https://bj.bcebos.com/v1/paddlenlp/data/literature_search_data.zip)
102+
103+
```
104+
├── milvus # milvus建库数据集
105+
├── milvus_data.csv. # 构建召回库的数据
106+
├── recall # 召回(语义索引)数据集
107+
├── corpus.csv # 用于测试的召回库
108+
├── dev.csv # 召回验证集
109+
├── test.csv # 召回测试集
110+
├── train.csv # 召回训练集
111+
├── train_unsupervised.csv # 无监督训练集
112+
├── sort # 排序数据集
113+
├── test_pairwise.csv # 排序测试集
114+
├── dev_pairwise.csv # 排序验证集
115+
└── train_pairwise.csv # 排序训练集
116+
117+
```
118+
119+
<a name="模型训练"></a>
120+
121+
## 5. 模型训练
122+
123+
**排序模型下载链接:**
124+
125+
126+
|Model|训练参数配置|硬件|MD5|
127+
| ------------ | ------------ | ------------ |-----------|
128+
|[ERNIE-Gram-Sort](https://bj.bcebos.com/v1/paddlenlp/models/ernie_gram_sort.zip)|<div style="width: 150pt">epoch:3 lr:5E-5 bs:64 max_len:64 </div>|<div style="width: 100pt">4卡 v100-16g</div>|d24ece68b7c3626ce6a24baa58dd297d|
129+
130+
131+
### 训练环境说明
132+
133+
134+
- NVIDIA Driver Version: 440.64.00
135+
- Ubuntu 16.04.6 LTS (Docker)
136+
- Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz
137+
138+
139+
### 单机单卡训练/单机多卡训练
140+
141+
这里采用单机多卡方式进行训练,通过如下命令,指定 GPU 0,1,2,3 卡, 基于ERNIE-Gram训练模型,数据量比较大,需要20小时10分钟左右。如果采用单机单卡训练,只需要把`--gpu`参数设置成单卡的卡号即可
142+
143+
训练的命令如下:
144+
145+
```
146+
python -u -m paddle.distributed.launch --gpus "0,2,3,4" train_pairwise.py \
147+
--device gpu \
148+
--save_dir ./checkpoints \
149+
--batch_size 32 \
150+
--learning_rate 2E-5 \
151+
--margin 0.1 \
152+
--eval_step 100 \
153+
--train_file data/train_pairwise.csv \
154+
--test_file data/dev_pairwise.csv
155+
```
156+
也可以运行bash脚本:
157+
158+
```
159+
sh scripts/train_pairwise.sh
160+
```
161+
162+
<a name="评估"></a>
163+
164+
## 6. 评估
165+
166+
167+
```
168+
unset CUDA_VISIBLE_DEVICES
169+
python -u -m paddle.distributed.launch --gpus "0" evaluate.py \
170+
--device gpu \
171+
--batch_size 32 \
172+
--learning_rate 2E-5 \
173+
--init_from_ckpt "./checkpoints/model_30000/model_state.pdparams" \
174+
--test_file data/dev_pairwise.csv
175+
```
176+
也可以运行bash脚本:
177+
178+
```
179+
sh scripts/evaluate.sh
180+
```
181+
182+
183+
成功运行后会输出下面的指标:
184+
185+
```
186+
eval_dev auc:0.796
187+
```
188+
189+
<a name="预测"></a>
190+
191+
## 7. 预测
192+
193+
### 准备预测数据
194+
195+
待预测数据为 tab 分隔的 tsv 文件,每一行为 1 个文本 Pair,和文本pair的语义索引相似度,部分示例如下:
196+
197+
```
198+
中西方语言与文化的差异 第二语言习得的一大障碍就是文化差异。 0.5160342454910278
199+
中西方语言与文化的差异 跨文化视角下中国文化对外传播路径琐谈跨文化,中国文化,传播,翻译 0.5145505666732788
200+
中西方语言与文化的差异 从中西方民族文化心理的差异看英汉翻译语言,文化,民族文化心理,思维方式,翻译 0.5141439437866211
201+
中西方语言与文化的差异 中英文化差异对翻译的影响中英文化,差异,翻译的影响 0.5138794183731079
202+
中西方语言与文化的差异 浅谈文化与语言习得文化,语言,文化与语言的关系,文化与语言习得意识,跨文化交际 0.5131710171699524
203+
```
204+
205+
206+
207+
### 开始预测
208+
209+
以上述 demo 数据为例,运行如下命令基于我们开源的 ERNIE-Gram模型开始计算文本 Pair 的语义相似度:
210+
211+
```shell
212+
python -u -m paddle.distributed.launch --gpus "0" \
213+
predict_pairwise.py \
214+
--device gpu \
215+
--params_path "./checkpoints/model_30000/model_state.pdparams"\
216+
--batch_size 128 \
217+
--max_seq_length 64 \
218+
--input_file 'sort/test_pairwise.csv'
219+
```
220+
也可以直接执行下面的命令:
221+
222+
```
223+
sh scripts/predict_pairwise.sh
224+
```
225+
得到下面的输出,分别是query,title和对应的预测概率:
226+
227+
```
228+
{'query': '中西方语言与文化的差异', 'title': '第二语言习得的一大障碍就是文化差异。', 'pred_prob': 0.85112214}
229+
{'query': '中西方语言与文化的差异', 'title': '跨文化视角下中国文化对外传播路径琐谈跨文化,中国文化,传播,翻译', 'pred_prob': 0.78629625}
230+
{'query': '中西方语言与文化的差异', 'title': '从中西方民族文化心理的差异看英汉翻译语言,文化,民族文化心理,思维方式,翻译', 'pred_prob': 0.91767526}
231+
{'query': '中西方语言与文化的差异', 'title': '中英文化差异对翻译的影响中英文化,差异,翻译的影响', 'pred_prob': 0.8601749}
232+
{'query': '中西方语言与文化的差异', 'title': '浅谈文化与语言习得文化,语言,文化与语言的关系,文化与语言习得意识,跨文化交际', 'pred_prob': 0.8944413}
233+
```
234+
235+
<a name="部署"></a>
236+
237+
## 8. 部署
238+
239+
### 动转静导出
240+
241+
首先把动态图模型转换为静态图:
242+
243+
```
244+
python export_model.py --params_path checkpoints/model_30000/model_state.pdparams --output_path=./output
245+
```
246+
也可以运行下面的bash脚本:
247+
248+
```
249+
sh scripts/export_model.sh
250+
```
251+
252+
### Paddle Inference
253+
254+
修改预测文件路径:
255+
256+
```
257+
input_file='../../sort/test_pairwise.csv'
258+
```
259+
260+
然后使用PaddleInference
261+
262+
```
263+
python predict.py --model_dir=../../output
264+
```
265+
也可以运行下面的bash脚本:
266+
267+
```
268+
sh deploy.sh
269+
```
270+
得到下面的输出,输出的是样本的query,title以及对应的概率:
271+
272+
```
273+
Data: {'query': '中西方语言与文化的差异', 'title': '第二语言习得的一大障碍就是文化差异。'} prob: [0.8511221]
274+
Data: {'query': '中西方语言与文化的差异', 'title': '跨文化视角下中国文化对外传播路径琐谈跨文化,中国文化,传播,翻译'} prob: [0.7862964]
275+
Data: {'query': '中西方语言与文化的差异', 'title': '从中西方民族文化心理的差异看英汉翻译语言,文化,民族文化心理,思维方式,翻译'} prob: [0.91767514]
276+
Data: {'query': '中西方语言与文化的差异', 'title': '中英文化差异对翻译的影响中英文化,差异,翻译的影响'} prob: [0.8601747]
277+
Data: {'query': '中西方语言与文化的差异', 'title': '浅谈文化与语言习得文化,语言,文化与语言的关系,文化与语言习得意识,跨文化交际'} prob: [0.8944413]
278+
```
279+
280+
## Reference
281+
282+
[1] Xiao, Dongling, Yu-Kun Li, Han Zhang, Yu Sun, Hao Tian, Hua Wu, and Haifeng Wang. “ERNIE-Gram: Pre-Training with Explicitly N-Gram Masked Language Modeling for Natural Language Understanding.” ArXiv:2010.12148 [Cs].

0 commit comments

Comments
 (0)