@@ -42,7 +42,7 @@ In-batch Negatives 策略的训练数据为语义相似的 Pair 对,策略核
42
42
43
43
### 技术方案
44
44
45
- 双塔模型,采用ERNIE1.0热启, 在召回训练阶段引入In-batch Negatives 策略,使用hnswlib建立索引库,进行召回测试。
45
+ 双塔模型,在召回训练阶段引入In-batch Negatives 策略,使用hnswlib建立索引库,进行召回测试。
46
46
47
47
48
48
### 评估指标
@@ -53,10 +53,10 @@ Recall@K召回率是指预测的前topK(top-k是指从最后的按得分排序
53
53
54
54
** 效果评估**
55
55
56
- | 模型 | Recall@1 | Recall@5 | Recall@10 | Recall@20 | Recall@50 | 策略简要说明 |
56
+ | 策略 | 模型 | Recall@1 | Recall@5 | Recall@10 | Recall@20 | Recall@50 |
57
57
| ------------ | ------------ | ------------ | --------- | --------- | --------- | --------- |
58
- | In-batch Negatives | 51.301 | 65.309| 69.878| 73.996| 78.881| Inbatch-negative有监督训练 |
59
-
58
+ | In-batch Negatives | ernie 1.0 | 51.301 | 65.309| 69.878| 73.996| 78.881|
59
+ | In-batch Negatives | rocketqa-zh-base-query-encoder | ** 59.622 ** | ** 75.089 ** | ** 79.668 ** | ** 83.404 ** | ** 87.773 ** |
60
60
61
61
62
62
<a name =" 环境依赖 " ></a >
@@ -166,10 +166,10 @@ Recall@K召回率是指预测的前topK(top-k是指从最后的按得分排序
166
166
167
167
| Model| 训练参数配置| 硬件| MD5|
168
168
| ------------ | ------------ | ------------ | -----------|
169
- | [ batch_neg] ( https://bj.bcebos.com/v1/paddlenlp/models/inbatch_model.zip ) | <div style =" width : 150pt " >margin:0.2 scale:30 epoch:3 lr:5E-5 bs:64 max_len:64 </div >| <div style =" width : 100pt " >4卡 v100-16g</div >| f3e5c7d7b0b718c2530c5e1b136b2d74|
169
+ | [ batch_neg] ( https://bj.bcebos.com/v1/paddlenlp/models/inbatch_model.zip ) | <div style =" width : 150pt " >ernie 1.0 margin:0.2 scale:30 epoch:3 lr:5E-5 bs:64 max_len:64 </div >| <div style =" width : 100pt " >4卡 v100-16g</div >| f3e5c7d7b0b718c2530c5e1b136b2d74|
170
170
171
- ### 训练环境说明
172
171
172
+ ### 训练环境说明
173
173
174
174
- NVIDIA Driver Version: 440.64.00
175
175
- Ubuntu 16.04.6 LTS (Docker)
@@ -185,7 +185,7 @@ Recall@K召回率是指预测的前topK(top-k是指从最后的按得分排序
185
185
然后运行下面的命令使用GPU训练,得到语义索引模型:
186
186
187
187
```
188
- root_path=recall
188
+ root_path=inbatch
189
189
python -u -m paddle.distributed.launch --gpus "0,1,2,3" \
190
190
train_batch_neg.py \
191
191
--device gpu \
@@ -194,11 +194,11 @@ python -u -m paddle.distributed.launch --gpus "0,1,2,3" \
194
194
--learning_rate 5E-5 \
195
195
--epochs 3 \
196
196
--output_emb_size 256 \
197
+ --model_name_or_path rocketqa-zh-base-query-encoder \
197
198
--save_steps 10 \
198
199
--max_seq_length 64 \
199
200
--margin 0.2 \
200
201
--train_set_file recall/train.csv \
201
- --evaluate \
202
202
--recall_result_dir "recall_result_dir" \
203
203
--recall_result_file "recall_result.txt" \
204
204
--hnsw_m 100 \
@@ -217,6 +217,7 @@ python -u -m paddle.distributed.launch --gpus "0,1,2,3" \
217
217
* ` learning_rate ` : 训练的学习率的大小
218
218
* ` epochs ` : 训练的epoch数
219
219
* ` output_emb_size ` : Transformer 顶层输出的文本向量维度
220
+ * ` model_name_or_path ` : 预训练模型,用于模型和` Tokenizer ` 的参数初始化
220
221
* ` save_steps ` : 模型存储 checkpoint 的间隔 steps 个数
221
222
* ` max_seq_length ` : 输入序列的最大长度
222
223
* ` margin ` : 正样本相似度与负样本之间的目标 Gap
@@ -234,7 +235,7 @@ python -u -m paddle.distributed.launch --gpus "0,1,2,3" \
234
235
也可以使用bash脚本:
235
236
236
237
```
237
- sh scripts/train_batch_neg .sh
238
+ sh scripts/train .sh
238
239
```
239
240
240
241
@@ -270,6 +271,7 @@ python -u -m paddle.distributed.launch --gpus "3" --log_dir "recall_log/" \
270
271
--recall_result_dir "recall_result_dir" \
271
272
--recall_result_file "recall_result.txt" \
272
273
--params_path "${root_dir}/model_40/model_state.pdparams" \
274
+ --model_name_or_path rocketqa-zh-base-query-encoder \
273
275
--hnsw_m 100 \
274
276
--hnsw_ef 100 \
275
277
--batch_size 64 \
@@ -280,16 +282,17 @@ python -u -m paddle.distributed.launch --gpus "3" --log_dir "recall_log/" \
280
282
--corpus_file "recall/corpus.csv"
281
283
```
282
284
参数含义说明
283
- * ` device ` : 使用 cpu/gpu 进行训练
284
- * ` recall_result_dir ` : 召回结果存储目录
285
- * ` recall_result_file ` : 召回结果的文件名
285
+ * ` device ` : 使用 cpu/gpu 进行训练
286
+ * ` recall_result_dir ` : 召回结果存储目录
287
+ * ` recall_result_file ` : 召回结果的文件名
286
288
* ` params_path ` : 待评估模型的参数文件名
287
- * ` hnsw_m ` : hnsw 算法相关参数,保持默认即可
288
- * ` hnsw_ef ` : hnsw 算法相关参数,保持默认即可
289
- * ` output_emb_size ` : Transformer 顶层输出的文本向量维度
290
- * ` recall_num ` : 对 1 个文本召回的相似文本数量
291
- * ` similar_text_pair ` : 由相似文本对构成的评估集
292
- * ` corpus_file ` : 召回库数据 corpus_file
289
+ * ` model_name_or_path ` : 预训练模型,用于模型和` Tokenizer ` 的参数初始化
290
+ * ` hnsw_m ` : hnsw 算法相关参数,保持默认即可
291
+ * ` hnsw_ef ` : hnsw 算法相关参数,保持默认即可
292
+ * ` output_emb_size ` : Transformer 顶层输出的文本向量维度
293
+ * ` recall_num ` : 对 1 个文本召回的相似文本数量
294
+ * ` similar_text_pair ` : 由相似文本对构成的评估集
295
+ * ` corpus_file ` : 召回库数据 corpus_file
293
296
294
297
也可以使用下面的bash脚本:
295
298
@@ -383,10 +386,11 @@ python inference.py
383
386
```
384
387
root_dir="checkpoints/inbatch"
385
388
386
- python -u -m paddle.distributed.launch --gpus "3 " \
389
+ python -u -m paddle.distributed.launch --gpus "0 " \
387
390
predict.py \
388
391
--device gpu \
389
392
--params_path "${root_dir}/model_40/model_state.pdparams" \
393
+ --model_name_or_path rocketqa-zh-base-query-encoder \
390
394
--output_emb_size 256 \
391
395
--batch_size 128 \
392
396
--max_seq_length 64 \
@@ -396,6 +400,7 @@ python -u -m paddle.distributed.launch --gpus "3" \
396
400
参数含义说明
397
401
* ` device ` : 使用 cpu/gpu 进行训练
398
402
* ` params_path ` : 预训练模型的参数文件名
403
+ * ` model_name_or_path ` : 预训练模型,用于模型和` Tokenizer ` 的参数初始化
399
404
* ` output_emb_size ` : Transformer 顶层输出的文本向量维度
400
405
* ` text_pair_file ` : 由文本 Pair 构成的待预测数据集
401
406
@@ -423,7 +428,9 @@ predict.sh文件包含了cpu和gpu运行的脚本,默认是gpu运行的脚本
423
428
首先把动态图模型转换为静态图:
424
429
425
430
```
426
- python export_model.py --params_path checkpoints/inbatch/model_40/model_state.pdparams --output_path=./output
431
+ python export_model.py --params_path checkpoints/inbatch/model_40/model_state.pdparams \
432
+ --model_name_or_path rocketqa-zh-base-query-encoder \
433
+ --output_path=./output
427
434
```
428
435
也可以运行下面的bash脚本:
429
436
@@ -449,7 +456,9 @@ corpus_list=[['中西方语言与文化的差异','中西方文化差异以及
449
456
然后使用PaddleInference
450
457
451
458
```
452
- python deploy/python/predict.py --model_dir=./output
459
+ python deploy/python/predict.py \
460
+ --model_dir=./output \
461
+ --model_name_or_path rocketqa-zh-base-query-encoder
453
462
```
454
463
也可以运行下面的bash脚本:
455
464
@@ -501,9 +510,16 @@ Paddle Serving的部署有两种方式,第一种方式是Pipeline的方式,
501
510
502
511
#### Pipeline方式
503
512
504
- 启动 Pipeline Server:
513
+ 修改模型需要用到的` Tokenizer `
514
+
515
+ ```
516
+ self.tokenizer = AutoTokenizer.from_pretrained("rocketqa-zh-base-query-encoder")
517
+ ```
518
+
519
+ 然后启动 Pipeline Server:
505
520
506
521
```
522
+ cd deploy/python
507
523
python web_service.py
508
524
```
509
525
@@ -520,7 +536,7 @@ list_data = [
520
536
然后运行:
521
537
522
538
```
523
- python rpc_client.py
539
+ python deploy/python/ rpc_client.py
524
540
```
525
541
模型的输出为:
526
542
@@ -547,12 +563,12 @@ python -m paddle_serving_server.serve --model serving_server --port 9393 --gpu_i
547
563
也可以使用脚本:
548
564
549
565
```
550
- sh deploy/C++ /start_server.sh
566
+ sh deploy/cpp /start_server.sh
551
567
```
552
568
Client 可以使用 http 或者 rpc 两种方式,rpc 的方式为:
553
569
554
570
```
555
- python deploy/C++ /rpc_client.py
571
+ python deploy/cpp /rpc_client.py
556
572
```
557
573
运行的输出为:
558
574
```
@@ -571,7 +587,7 @@ time to cost :0.3960278034210205 seconds
571
587
或者使用 http 的客户端访问模式:
572
588
573
589
```
574
- python deploy/C++ /http_client.py
590
+ python deploy/cpp /http_client.py
575
591
```
576
592
运行的输出为:
577
593
@@ -599,6 +615,7 @@ python -u -m paddle.distributed.launch --gpus "0,1,2,3" \
599
615
train_batch_neg.py \
600
616
--device gpu \
601
617
--save_dir ./checkpoints/simcse_inbatch_negative \
618
+ --model_name_or_path rocketqa-zh-base-query-encoder \
602
619
--batch_size 64 \
603
620
--learning_rate 5E-5 \
604
621
--epochs 3 \
0 commit comments