Skip to content

Commit 7fb53ae

Browse files
committed
Update application forlder name
1 parent 74f9fae commit 7fb53ae

Some content is hidden

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

80 files changed

+323
-283
lines changed

application/neural_search/recall/domain_adaptive_pretraining/data_tools/process_data.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

application/neural_search/README.md renamed to applications/neural_search/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# 手把手搭建一个语义检索系统
1+
# 手把手搭建一个语义检索系统
22

33
## 1. 场景概述
44

55
检索系统存在于我们日常使用的很多产品中,比如商品搜索系统、学术文献检索系等等,本方案提供了检索系统完整实现。限定场景是用户通过输入检索词 Query,快速在海量数据中查找相似文档。
66

77
所谓语义检索(也称基于向量的检索),是指检索系统不再拘泥于用户 Query 字面本身,而是能精准捕捉到用户 Query 后面的真正意图并以此来搜索,从而更准确地向用户返回最符合的结果。通过使用最先进的语义索引模型找到文本的向量表示,在高维向量空间中对它们进行索引,并度量查询向量与索引文档的相似程度,从而解决了关键词索引带来的缺陷。
88

9-
例如下面两组文本 Pair,如果基于关键词去计算相似度,两组的相似度是相同的。而从实际语义上看,第一组相似度高于第二组。
9+
例如下面两组文本 Pair,如果基于关键词去计算相似度,两组的相似度是相同的。而从实际语义上看,第一组相似度高于第二组。
1010

1111
```
1212
车头如何放置车牌 前牌照怎么装
@@ -20,7 +20,7 @@
2020
通常检索业务的数据都比较庞大,都会分为召回(索引)、排序两个环节。召回阶段主要是从至少千万级别的候选集合里面,筛选出相关的文档,这样候选集合的数目就会大大降低,在之后的排序阶段就可以使用一些复杂的模型做精细化或者个性化的排序。一般采用多路召回策略(例如关键词召回、热点召回、语义召回结合等),多路召回结果聚合后,经过统一的打分以后选出最优的 TopK 的结果。
2121

2222
### 2.1 系统特色
23-
23+
2424
+ 低门槛
2525
+ 手把手搭建起检索系统
2626
+ 无需标注数据也能构建检索系统
@@ -31,7 +31,7 @@
3131
+ 仅有无监督数据: SimCSE
3232
+ 仅有有监督数据: InBatchNegative
3333
+ 兼具无监督数据 和 有监督数据:融合模型
34-
+ 进一步优化方案: 面向领域的预训练 Domain-adaptive Pretraining
34+
+ 进一步优化方案: 面向领域的预训练 Domain-adaptive Pretraining
3535
+ 性能快
3636
+ 基于 Paddle Inference 快速抽取向量
3737
+ 基于 Milvus 快速查询和高性能建库
@@ -55,8 +55,8 @@
5555
我们针对不同的数据情况推出三种语义索引方案,如下图所示,您可以参照此方案,快速建立语义索引:
5656

5757
| ⭐️ 无监督数据 | ⭐️ 有监督数据 | **召回方案** |
58-
| ------------ | ------------ | ------------ |
59-
||| SimCSE |
58+
| ------------ | ------------ | ------------ |
59+
||| SimCSE |
6060
||| In-batch Negatives|
6161
||| SimCSE+ In-batch Negatives |
6262

@@ -84,7 +84,7 @@
8484

8585
#### 3.1.2 评估指标
8686

87-
**模型效果指标**
87+
**模型效果指标**
8888
* 在语义索引召回阶段使用的指标是 Recall@K,表示的是预测的前topK(从最后的按得分排序的召回列表中返回前K个结果)结果和语料库中真实的前 K 个相关结果的重叠率,衡量的是检索系统的查全率。
8989

9090
* 在排序阶段使用的指标为AUC,AUC反映的是分类器对样本的排序能力,如果完全随机得对样本分类,那么AUC应该接近0.5。分类器越可能把真正的正样本排在前面,AUC越大,分类性能越好。
@@ -121,7 +121,7 @@
121121
├── milvus # milvus建库数据集
122122
├── milvus_data.csv. # 构建召回库的数据(模拟实际业务线上的语料库,实际语料库远大于这里的规模),用于直观演示相关文献召回效果
123123
├── recall # 召回阶段数据集
124-
├── train_unsupervised.csv # 无监督训练集,用于训练 SimCSE
124+
├── train_unsupervised.csv # 无监督训练集,用于训练 SimCSE
125125
├── train.csv # 有监督训练集,用于训练 In-batch Negative
126126
├── dev.csv # 召回阶段验证集,用于评估召回模型的效果,SimCSE 和 In-batch Negative 共用
127127
├── corpus.csv # 构建召回库的数据(模拟实际业务线上的语料库,实际语料库远大于这里的规模),用于评估召回阶段模型效果,SimCSE 和 In-batch Negative 共用
@@ -143,10 +143,10 @@ a. 软件环境:
143143

144144

145145
- python >= 3.6
146-
- paddlenlp >= 2.2.1
146+
- paddlenlp >= 2.2.1
147147
- paddlepaddle-gpu >=2.2
148148
- CUDA Version: 10.2
149-
- NVIDIA Driver Version: 440.64.00
149+
- NVIDIA Driver Version: 440.64.00
150150
- Ubuntu 16.04.6 LTS (Docker)
151151

152152

@@ -179,7 +179,7 @@ pip install -r requirements.txt
179179

180180
第二步:无监督训练 SimCSE
181181

182-
训练用时 16hour53min,可参考:[SimCSE](./recall/simcse/)
182+
训练用时 16hour53min,可参考:[SimCSE](./recall/simcse/)
183183

184184
第三步:有监督训练
185185

@@ -201,7 +201,7 @@ pip install -r requirements.txt
201201

202202
**召回系统搭建**
203203

204-
召回系统使用索引引擎 Milvus,可参考 [milvus_system](./recall/milvus/)
204+
召回系统使用索引引擎 Milvus,可参考 [milvus_system](./recall/milvus/)
205205
我们展示一下系统的效果,输入的文本如下:
206206

207207
```
@@ -243,8 +243,8 @@ pip install -r requirements.txt
243243

244244
| 模型 | AUC |
245245
| ------------ | ------------ |
246-
| Baseline: In-batch Negatives | 0.582 |
247-
| ERNIE-Gram | 0.801 |
246+
| Baseline: In-batch Negatives | 0.582 |
247+
| ERNIE-Gram | 0.801 |
248248

249249
同样输入文本:
250250

File renamed without changes.
File renamed without changes.

application/neural_search/ranking/ernie_matching/README.md renamed to applications/neural_search/ranking/ernie_matching/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<a name="ERNIE-Gram"></a>
2323

24-
# ERNIE-Gram
24+
# ERNIE-Gram
2525

2626
<a name="技术方案"></a>
2727

@@ -40,7 +40,7 @@
4040

4141
| 模型 | AUC |
4242
| ------------ | ------------ |
43-
| ERNIE-Gram | 0.801 |
43+
| ERNIE-Gram | 0.801 |
4444

4545
<a name="环境依赖"></a>
4646

@@ -113,7 +113,7 @@ ernie_matching/
113113
├── test_pairwise.csv # 排序测试集
114114
├── dev_pairwise.csv # 排序验证集
115115
└── train_pairwise.csv # 排序训练集
116-
116+
117117
```
118118

119119
<a name="模型训练"></a>
@@ -131,7 +131,7 @@ ernie_matching/
131131
### 训练环境说明
132132

133133

134-
- NVIDIA Driver Version: 440.64.00
134+
- NVIDIA Driver Version: 440.64.00
135135
- Ubuntu 16.04.6 LTS (Docker)
136136
- Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz
137137

File renamed without changes.
File renamed without changes.

application/neural_search/ranking/ernie_matching/deploy/python/predict.py renamed to applications/neural_search/ranking/ernie_matching/deploy/python/predict.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
args = parser.parse_args()
6161
# yapf: enable
6262

63+
6364
def read_text_pair(data_path):
6465
"""Reads data."""
6566
with open(data_path, 'r', encoding='utf-8') as f:
@@ -212,6 +213,7 @@ def predict(self, data, tokenizer):
212213

213214
return sim_score
214215

216+
215217
if __name__ == "__main__":
216218
# Define predictor to do prediction.
217219
predictor = Predictor(args.model_dir, args.device, args.max_seq_length,
@@ -222,9 +224,9 @@ def predict(self, data, tokenizer):
222224
'ernie-gram-zh')
223225

224226
# test_ds = load_dataset("lcqmc", splits=["test"])
225-
input_file='sort/test_pairwise.csv'
227+
input_file = 'sort/test_pairwise.csv'
226228

227-
test_ds = load_dataset(read_text_pair,data_path=input_file, lazy=False)
229+
test_ds = load_dataset(read_text_pair, data_path=input_file, lazy=False)
228230

229231
data = [{'query': d['query'], 'title': d['title']} for d in test_ds]
230232

application/neural_search/ranking/ernie_matching/evaluate.py renamed to applications/neural_search/ranking/ernie_matching/evaluate.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from data import convert_pairwise_example as convert_example
3232
from model import PairwiseMatching
3333
import pandas as pd
34-
from tqdm import tqdm
34+
from tqdm import tqdm
3535

3636
# yapf: disable
3737
parser = argparse.ArgumentParser()
@@ -89,22 +89,24 @@ def evaluate(model, metric, data_loader, phase="dev"):
8989
metric.reset()
9090
model.train()
9191

92+
9293
# 构建读取函数,读取原始数据
9394
def read(src_path, is_predict=False):
94-
data=pd.read_csv(src_path,sep='\t')
95+
data = pd.read_csv(src_path, sep='\t')
9596
for index, row in tqdm(data.iterrows()):
96-
query=row['query']
97-
title=row['title']
98-
neg_title=row['neg_title']
99-
yield {'query':query, 'title':title,'neg_title':neg_title}
97+
query = row['query']
98+
title = row['title']
99+
neg_title = row['neg_title']
100+
yield {'query': query, 'title': title, 'neg_title': neg_title}
101+
100102

101103
def read_test(src_path, is_predict=False):
102-
data=pd.read_csv(src_path,sep='\t')
104+
data = pd.read_csv(src_path, sep='\t')
103105
for index, row in tqdm(data.iterrows()):
104-
query=row['query']
105-
title=row['title']
106-
label=row['label']
107-
yield {'query':query, 'title':title,'label':label}
106+
query = row['query']
107+
title = row['title']
108+
label = row['label']
109+
yield {'query': query, 'title': title, 'label': label}
108110

109111

110112
def do_train():
@@ -115,30 +117,26 @@ def do_train():
115117

116118
set_seed(args.seed)
117119

118-
119-
dev_ds=load_dataset(read_test,src_path=args.test_file,lazy=False)
120+
dev_ds = load_dataset(read_test, src_path=args.test_file, lazy=False)
120121
print(dev_ds[0])
121122

122123
pretrained_model = ppnlp.transformers.ErnieGramModel.from_pretrained(
123124
'ernie-gram-zh')
124125
tokenizer = ppnlp.transformers.ErnieGramTokenizer.from_pretrained(
125126
'ernie-gram-zh')
126127

127-
128128
trans_func_eval = partial(
129129
convert_example,
130130
tokenizer=tokenizer,
131131
max_seq_length=args.max_seq_length,
132132
phase="eval")
133133

134-
135134
batchify_fn_eval = lambda samples, fn=Tuple(
136135
Pad(axis=0, pad_val=tokenizer.pad_token_id), # pair_input
137136
Pad(axis=0, pad_val=tokenizer.pad_token_type_id), # pair_segment
138137
Stack(dtype="int64") # label
139138
): [data for data in fn(samples)]
140139

141-
142140
dev_data_loader = create_dataloader(
143141
dev_ds,
144142
mode='dev',
@@ -155,7 +153,6 @@ def do_train():
155153
metric = paddle.metric.Auc()
156154
evaluate(model, metric, dev_data_loader, "dev")
157155

158-
159156

160157
if __name__ == "__main__":
161158
do_train()
File renamed without changes.

0 commit comments

Comments
 (0)