---
title: KnowPrompt
canonical_url: "https://www.modelscope.cn/models/jeno11/KnowPrompt"
md_url: "https://www.modelscope.cn/models/jeno11/KnowPrompt.md"
repository: jeno11/KnowPrompt
chinese_name: "基于知识协同优化的调优方法"
last_updated: 2023-02-09
license: "Apache License 2.0"
library_name:
  - pytorch
frameworks:
  - pytorch
language:
  - ch
domain:
  - nlp
downloads: 68
stars: 5
---

# KnowPrompt

> KnowPrompt - jeno11 在 ModelScope 开源的模型。KnowPrompt用于关系抽取任务，通过学习模板词和答案词，将实体和关系的知识注入模型，并在知识约束下协同优化它们的表示

jeno11/KnowPrompt 是 ModelScope 魔搭社区上的机器学习模型，采用 Apache License 2.0 许可。

- **Repository**: jeno11/KnowPrompt
- **License**: Apache License 2.0
- **Downloads**: 68
- **Stars**: 5
- **Last updated**: 2023-02-09

Source: https://www.modelscope.cn/models/jeno11/KnowPrompt

---

# KnowPrompt基于知识协同优化的调优方法
KnowPrompt用于关系抽取任务，通过学习模板词和答案词，将实体和关系的知识注入模型，并在知识约束下协同优化它们的表示

## 模型描述

先将知识注入提示，然后用提示调优模型实现知识抽取。具体实现如下：使用集合实体嵌入初始化的实体周围的类型标记，作为可学习的虚拟模板词来注入实体类型知识；同时使用标签计算平均嵌入作为虚拟答案来注入关系知识。在这个结构中，实体和关系相互约束，虚拟模板词应与上下文相关联，因此我们引入协同优化来校正模板和答案词。
<div align=center><img src="model.png" width="80%" height="80%" /></div>

## 期望模型使用方式以及适用范围

本模型用于关系抽取任务，抽取后的信息可供更多下游NLP任务使用，比如：信息检索、对话生成和问答。具体使用方式请参考代码示例。

### 如何使用

#### 依赖安装

需要安装如下包

```
numpy==1.20.3
tokenizers==0.10.3
pytorch_lightning==1.3.1
regex==2021.4.4
torch
transformers==4.7.0
tqdm==4.49.0
activations==0.1.0
dataclasses==0.8
file_utils==0.0.1
flax==0.3.4
PyYAML==5.4.1
utils==1.0.1
```

#### 代码范例

```python
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

kp_pipeline = pipeline(Tasks.information_extraction, 'jeno11/KnowPrompt')
kp_pipeline(input={"token": ["the", "treatment", "includes", "an", "aromatherapy", "mask", "and", "moisturizer", "."], "h": {"name": "treatment", "pos": [1, 2]}, "t": {"name": "mask", "pos": [5, 6]}})

```

### 模型局限性以及可能的偏差

模型训练数据中的关系标签是有限的，只能在一定程度上概括真实世界中的关系

## 训练数据介绍

采用了SemEval数据集

| **Dataset** | **Train** | **Val** | **Test** | **Rel** |
| ----------- | ------------ | ---------- | ----------- | ---------- |
| SemEval     | 6,507        | 1,493      | 2,717       | 19         |

## 模型训练流程

### 训练

训练分为两个阶段，第一阶段对虚拟模板词和模拟答案词进行协同优化，第二阶段基于被优化的虚拟模板词和模拟答案词为语言模型调参，用较小的学习率优化所有参数。针对不同数据集，超参设置不同，具体可见源码中的脚本文件。以SemEval为例，超参设置如下：

```
max_epochs=10
max_sequence_length=256
batch_size=16
learning_rate=3e-5
batch_size=16
t_lambda=0.001
```

### 数据评估及结果

在基础配置上，同其他模型的对比结果如下表所示：

| **Methods** | **Precision** |
| ----------- | ------------- |
| Fine-tuning | 87.6          |
| KnowBERT    | 89.1          |
| MTB         | 89.5          |
| PTR         | 89.9          |
| KnowPrompt  | 90.2 (+0.3)   |

低资源配置上，使用LM-BFF提出的 8-, 16-, 32- 方法采样，从初始训练和验证集中抽取每个类的k个实例，以形成few-shot的训练和验证集。同其他模型的对比结果如下表所示:

| Split | **Methods** | **Precision** |
| ----- | ----------- | ------------- |
| k=8   | Fine-tuning | 41.3          |
|       | GDPNet      | 42.0          |
|       | PTR         | 70.5          |
|       | KnowPrompt  | 74.3 (+33.0)  |
| k=16  | Fine-tuning | 65.2          |
|       | GDPNet      | 67.5          |
|       | PTR         | 81.3          |
|       | KnowPrompt  | 82.9 (+17.7)  |
| k=32  | Fine-tuning | 80.1          |
|       | GDPNet      | 81.2          |
|       | PTR         | 84.2          |
|       | KnowPrompt  | 84.8 (+4.7)   |

可见，标注样例越少，KnowPrompt相对其它模型的效果越好。

#### 相关论文以及引用信息

```
@inproceedings{DBLP:conf/www/ChenZXDYTHSC22,
author    = {Xiang Chen and
               Ningyu Zhang and
               Xin Xie and
               Shumin Deng and
               Yunzhi Yao and
               Chuanqi Tan and
               Fei Huang and
               Luo Si and
               Huajun Chen},
  editor    = {Fr{\'{e}}d{\'{e}}rique Laforest and
               Rapha{\"{e}}l Troncy and
               Elena Simperl and
               Deepak Agarwal and
               Aristides Gionis and
               Ivan Herman and
               Lionel M{\'{e}}dini},
  title     = {KnowPrompt: Knowledge-aware Prompt-tuning with Synergistic Optimization
               for Relation Extraction},
  booktitle = {{WWW} '22: The {ACM} Web Conference 2022, Virtual Event, Lyon, France,
               April 25 - 29, 2022},
  pages     = {2778--2788},
  publisher = {{ACM}},
  year      = {2022},
  url       = {https://doi.org/10.1145/3485447.3511998},
  doi       = {10.1145/3485447.3511998},
  timestamp = {Tue, 26 Apr 2022 16:02:09 +0200},
  biburl    = {https://dblp.org/rec/conf/www/ChenZXDYTHSC22.bib},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}
```bash
 git clone https://www.modelscope.cn/jeno11/knowprompt_demo.git
```
