Update README.md
Browse files
README.md
CHANGED
|
@@ -7,13 +7,14 @@ language:
|
|
| 7 |
library_name: transformers
|
| 8 |
tags:
|
| 9 |
- text-generation-inference
|
|
|
|
| 10 |
---
|
| 11 |
# Phi2-Chinese-0.2B 从0开始训练自己的Phi2中文小模型
|
| 12 |
|
| 13 |
**本项目为实验项目,开源代码及模型权重,预训练数据较少,如果需要效果更好的中文小模型,可以参考项目[ChatLM-mini-Chinese](https://github.com/charent/ChatLM-mini-Chinese)**
|
| 14 |
|
| 15 |
# 1. ⚗️数据清洗
|
| 16 |
-
代码:[dataset.ipynb](
|
| 17 |
比如句末添加句号、繁体转简体、全角转半角、删除重复的标点符号(比如有些对话语料非常多`"。。。。。"`)等等。
|
| 18 |
具体的数据清洗过程请参考项目[ChatLM-mini-Chinese](https://github.com/charent/ChatLM-mini-Chinese)。
|
| 19 |
|
|
@@ -33,7 +34,7 @@ tokenizer训练非常吃内存:
|
|
| 33 |
|
| 34 |
|
| 35 |
# 3. ⛏️CLM因果模型预训练
|
| 36 |
-
代码:[pretrain.ipynb](
|
| 37 |
|
| 38 |
用大量文本进行无监督预训练,我这里除了基本的数据集外,还加入`wiki`百科的数据。
|
| 39 |
|
|
@@ -45,7 +46,7 @@ CLM预训练过程中,模型输入和输出是一样的,计算交叉熵损
|
|
| 45 |
|
| 46 |
|
| 47 |
# 4. ⚒️SFT指令微调
|
| 48 |
-
代码:[sft.ipynb](
|
| 49 |
|
| 50 |
主要使用`bell open source`的数据集。感谢大佬[BELLE](https://github.com/LianjiaTech/BELLE)。
|
| 51 |
|
|
@@ -60,7 +61,7 @@ text = f"##提问:\n{example['instruction']}\n##回答:\n{example['output'][EOS]
|
|
| 60 |
|
| 61 |
|
| 62 |
# 5. 📝dpo偏好优化
|
| 63 |
-
代码:[dpo.ipynb](
|
| 64 |
|
| 65 |
根据个人喜好对SFT模型微调,数据集要构造三列`prompt`、`chosen`和 `rejected`,`rejected`这一列有部分数据我是从sft阶段初级模型(比如sft训练4个`epoch`,取0.5个`epoch`检查点的模型)生成,如果生成的`rejected`和`chosen`相似度在0.9以上,则不要这条数据。
|
| 66 |
|
|
@@ -68,14 +69,17 @@ DPO过程中要有两个模型,一个是要训练的模型,一个是参考
|
|
| 68 |
|
| 69 |
# 6. 📑本项目模型使用方法
|
| 70 |
模型权重`huggingface`仓库:[Phi2-Chinese-0.2B](https://huggingface.co/charent/Phi2-Chinese-0.2B)
|
|
|
|
| 71 |
```python
|
| 72 |
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 73 |
import torch
|
| 74 |
|
| 75 |
-
tokenizer = AutoTokenizer.from_pretrained('charent/Phi2-Chinese-0.2B')
|
| 76 |
-
model = AutoModelForCausalLM.from_pretrained('charent/Phi2-Chinese-0.2B')
|
| 77 |
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
txt = '感冒了要怎么办?'
|
| 80 |
prompt = f"##提问:\n{txt}\n##回答:\n"
|
| 81 |
|
|
|
|
| 7 |
library_name: transformers
|
| 8 |
tags:
|
| 9 |
- text-generation-inference
|
| 10 |
+
pipeline_tag: text-generation
|
| 11 |
---
|
| 12 |
# Phi2-Chinese-0.2B 从0开始训练自己的Phi2中文小模型
|
| 13 |
|
| 14 |
**本项目为实验项目,开源代码及模型权重,预训练数据较少,如果需要效果更好的中文小模型,可以参考项目[ChatLM-mini-Chinese](https://github.com/charent/ChatLM-mini-Chinese)**
|
| 15 |
|
| 16 |
# 1. ⚗️数据清洗
|
| 17 |
+
代码:[dataset.ipynb](https://github.com/charent/Phi2-mini-Chinese/blob/main/0.dataset.ipynb)。
|
| 18 |
比如句末添加句号、繁体转简体、全角转半角、删除重复的标点符号(比如有些对话语料非常多`"。。。。。"`)等等。
|
| 19 |
具体的数据清洗过程请参考项目[ChatLM-mini-Chinese](https://github.com/charent/ChatLM-mini-Chinese)。
|
| 20 |
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
# 3. ⛏️CLM因果模型预训练
|
| 37 |
+
代码:[pretrain.ipynb](https://github.com/charent/Phi2-mini-Chinese/blob/main/2.pretrain.ipynb)
|
| 38 |
|
| 39 |
用大量文本进行无监督预训练,我这里除了基本的数据集外,还加入`wiki`百科的数据。
|
| 40 |
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
# 4. ⚒️SFT指令微调
|
| 49 |
+
代码:[sft.ipynb](https://github.com/charent/Phi2-mini-Chinese/blob/main/3.sft.ipynb)
|
| 50 |
|
| 51 |
主要使用`bell open source`的数据集。感谢大佬[BELLE](https://github.com/LianjiaTech/BELLE)。
|
| 52 |
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
# 5. 📝dpo偏好优化
|
| 64 |
+
代码:[dpo.ipynb](https://github.com/charent/Phi2-mini-Chinese/blob/main/4.dpo.ipynb)
|
| 65 |
|
| 66 |
根据个人喜好对SFT模型微调,数据集要构造三列`prompt`、`chosen`和 `rejected`,`rejected`这一列有部分数据我是从sft阶段初级模型(比如sft训练4个`epoch`,取0.5个`epoch`检查点的模型)生成,如果生成的`rejected`和`chosen`相似度在0.9以上,则不要这条数据。
|
| 67 |
|
|
|
|
| 69 |
|
| 70 |
# 6. 📑本项目模型使用方法
|
| 71 |
模型权重`huggingface`仓库:[Phi2-Chinese-0.2B](https://huggingface.co/charent/Phi2-Chinese-0.2B)
|
| 72 |
+
|
| 73 |
```python
|
| 74 |
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 75 |
import torch
|
| 76 |
|
|
|
|
|
|
|
| 77 |
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
| 78 |
|
| 79 |
+
tokenizer = AutoTokenizer.from_pretrained('charent/Phi2-Chinese-0.2B')
|
| 80 |
+
model = AutoModelForCausalLM.from_pretrained('charent/Phi2-Chinese-0.2B').to(device)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
txt = '感冒了要怎么办?'
|
| 84 |
prompt = f"##提问:\n{txt}\n##回答:\n"
|
| 85 |
|