Skip to content

Commit

Permalink
添加模型结构图
Browse files Browse the repository at this point in the history
  • Loading branch information
moon-hotel committed Jul 24, 2022
1 parent 0a70862 commit 921c500
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
98 changes: 96 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
- [x] [5. 基于BERT预训练模型的英文多选项(SWAG)任务](https://mp.weixin.qq.com/s/GqsbMBNt9XcFIjmumR04Pg)    [代码](Tasks/TaskForMultipleChoice.py)
- [x] [6. 基于BERT预训练模型的英文问答(SQuAD)任务](https://www.ylkz.life/deeplearning/p10265968/)    [代码](Tasks/TaskForSQuADQuestionAnswering.py)
- [x] [7. 基于NSL和MLM任务从头训练BERT任务](https://mp.weixin.qq.com/s/2Vtxv1Wj9knEFKUyUeQ_6w)    [代码](Tasks/TaskForPretraining.py)
- [ ] [8. 基于BERT预训练模型的中文关系抽取任务](https://www.ylkz.life)
- [ ] [9. 基于BERT预训练模型的命名体识别任务](https://www.ylkz.life)
- [ ] [8. 基于BERT预训练模型的命名体识别任务](https://www.ylkz.life)
- [ ] [9. 基于BERT预训练模型的中文关系抽取任务](https://www.ylkz.life)

## 工程结构
- `bert_base_chinese`目录中是BERT base中文预训练模型以及配置文件

Expand Down Expand Up @@ -43,12 +44,14 @@
- `BertForMultipleChoice.py`是问题选择模型的实现部分;
- `BertForQuestionAnswering.py`是问题回答(text span)模型的实现部分;
- `BertForNSPAndMLM.py`是BERT模型预训练的两个任务实现部分;
- `BertForTokenClassification.py`是字符分类(如:命名体识别)模型的实现部分;
- `Task`目录中是各个具体下游任务的训练和推理实现
- `TaskForSingleSentenceClassification.py`是单标签单文本分类任务的训练和推理实现,可用于普通的文本分类任务;
- `TaskForPairSentence.py`是文本对分类任务的训练和推理实现,可用于蕴含任务(例如MNLI数据集);
- `TaskForMultipleChoice.py`是问答选择任务的训练和推理实现,可用于问答选择任务(例如SWAG数据集);
- `TaskForSQuADQuestionAnswering.py`是问题回答任务的训练和推理实现,可用于问题问答任务(例如SQuAD数据集);
- `TaskForPretraining.py`是BERT模型中MLM和NSP两个预训练任务的实现部分,可用于BERT模型预训练;
- `TaskForChineseNER.py`是基于BERT模型的命名体任务训练和推理部分的实现;
- `test`目录中是各个模块的测试案例
- `utils`是各个工具类的实现
- `data_helpers.py`是各个下游任务的数据预处理及数据集构建模块;
Expand All @@ -73,6 +76,11 @@ tqdm==4.61.0
### Step 2. 运行模型
进入`Tasks`目录,运行相关模型.
### 2.1 中文文本分类任务

模型结构与数据处理:

<img src="imgs/21102512313.jpg" width="45%">

```python
python TaskForSingleSentenceClassification.py
```
Expand All @@ -96,6 +104,10 @@ python TaskForSingleSentenceClassification.py

### 2.2 英文文本蕴含任务

模型结构与数据处理:

<img src="imgs/21103032538.jpg" width="45%">

```python
python TaskForPairSentenceClassification.py
```
Expand All @@ -118,6 +130,15 @@ python TaskForPairSentenceClassification.py
```

### 2.3 SWAG多项选择任务

模型结构与数据处理:

<img src="imgs/21110834330.jpg" width="50%">

<img src="imgs/21110819453.jpg" width="50%">

<img src="imgs/21110839843.jpg" width="50%">

```python
python TaskForMultipleChoice.py
```
Expand All @@ -140,6 +161,16 @@ python TaskForMultipleChoice.py

### 2.4 SQuAD问题回答任务

模型结构与数据处理:

<img src="imgs/22010353470.jpg" width="50%">

<img src="imgs/22010226560.jpg" width="50%">

<img src="imgs/22010215402.jpg" width="50%">

<img src="imgs/22010251228.jpg" width="50%">

```python
python TaskForSQuADQuestionAnswering.py
```
Expand Down Expand Up @@ -181,6 +212,12 @@ python evaluate-v1.1.py dev-v1.1.json best_result.json

### 2.5 NSP与MLM任务训练及推理

模型结构与数据处理:

<img src="imgs/22011653159.jpg" width="50%">

<img src="imgs/22011826706.jpg" width="50%">

```python
if __name__ == '__main__':
config = ModelConfig()
Expand All @@ -206,4 +243,61 @@ if __name__ == '__main__':
- INFO: ## 掩盖:日日思君不[MASK]君,共[MASK]长江水。
- INFO: ## 预测:日日思君不见君,共饮长江水。
# ......
```

### 2.6 命名体识别任务训练及推理

模型结构与数据处理:

<img src="imgs/22071731070.jpg" width="50%">

<img src="imgs/22071728641.jpg" width="50%">

```python
if __name__ == '__main__':
config = ModelConfig()
train(config)
sentences = ['智光拿出石壁拓文为乔峰详述事情始末,乔峰方知自己原本姓萧,乃契丹后族。',
'当乔峰问及带头大哥时,却发现智光大师已圆寂。',
'乔峰、阿朱相约找最后知情人康敏问完此事后,就到塞外骑马牧羊,再不回来。']
inference(config, sentences)
```

上述代码运行结束后将会看到类似如下所示的输出结果:

```python
- INFO: Epoch: [1/10], Batch[620/1739], Train Loss: 0.115, Train acc: 0.96386
- INFO: Epoch: [1/10], Batch[240/1739], Train Loss: 0.098, Train acc: 0.96466
- INFO: Epoch: [1/10], Batch[660/1739], Train Loss: 0.087, Train acc: 0.96435
......
- INFO:句子:在澳大利亚等西方国家改变反倾销政策中对中国的划分后,不少欧盟人士也认识到,此种划分已背离中国经济迅速发展的现实。
- INFO: 澳大利亚: LOC
- INFO: 中国: LOC
- INFO: 欧盟: LOC
- INFO: 中国: LOC
......
precision recall f1-score support

O 1.00 0.99 1.00 97640
B-ORG 0.86 0.93 0.89 984
B-LOC 0.94 0.93 0.94 1934
B-PER 0.97 0.97 0.97 884
I-ORG 0.90 0.96 0.93 3945
I-LOC 0.91 0.95 0.93 2556
I-PER 0.99 0.98 0.98 1714

accuracy 0.99 109657
macro avg 0.94 0.96 0.95 109657
weighted avg 0.99 0.99 0.99 109657
```

模型推理结果:
```python
- INFO: 句子:智光拿出石壁拓文为乔峰详述事情始末,乔峰方知自己原本姓萧,乃契丹后族。
- INFO: 智光: PER
- INFO: 乔峰: PER
- INFO: 乔峰: PER
- INFO: 萧: PER
- INFO: 丹: PER
......
```
Binary file added imgs/22071728641.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/22071731070.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 921c500

Please sign in to comment.