语言:
- 中文
标签:
- chatglm
- pytorch
- 文本生成
许可证: apache-2.0
小部件:
- 文本: |-
对下面中文拼写纠错:
少先队员因该为老人让坐。
答:
基础模型: THUDM/chatglm3-6b
管道标签: 文本生成
库名称: peft
推理: false
中文拼写纠错LoRA模型
ChatGLM3-6B中文纠错LoRA模型
shibing624/chatglm3-6b-csc-chinese-lora
评估测试数据:
shibing624/chatglm3-6b-csc-chinese-lora在CSC测试集上的整体表现:
输入文本 |
预测结果 |
对下面文本纠错:少先队员因该为老人让坐。 |
少先队员应该为老人让座。 |
在CSC测试集上生成结果的纠错准确率高,由于基于THUDM/chatglm3-6b模型,结果常能带来惊喜,不仅能纠错,还具备句子润色和改写功能。
使用方法
本项目开源在pycorrector项目:pycorrector,支持ChatGLM原生模型及LoRA微调模型,调用方式如下:
安装包:
pip install -U pycorrector
from pycorrector import GptCorrector
model = GptCorrector("THUDM/chatglm3-6b", "chatglm", peft_name="shibing624/chatglm3-6b-csc-chinese-lora")
r = model.correct_batch(["少先队员因该为老人让坐。"])
print(r)
HuggingFace Transformers使用方式
若不依赖pycorrector,可直接通过以下方式调用模型:
首先将输入传入transformer模型,随后获取生成结果。
安装包:
pip install transformers
import os
import torch
from peft import PeftModel
from transformers import AutoTokenizer, AutoModel
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm3-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm3-6b", trust_remote_code=True).half().cuda()
model = PeftModel.from_pretrained(model, "shibing624/chatglm3-6b-csc-chinese-lora")
sents = ['对下面文本纠错\n\n少先队员因该为老人让坐。',
'对下面文本纠错\n\n下个星期,我跟我朋唷打算去法国玩儿。']
def get_prompt(user_query):
vicuna_prompt = "A chat between a curious user and an artificial intelligence assistant. " \
"The assistant gives helpful, detailed, and polite answers to the user's questions. " \
"USER: {query} ASSISTANT:"
return vicuna_prompt.format(query=user_query)
for s in sents:
q = get_prompt(s)
input_ids = tokenizer(q).input_ids
generation_kwargs = dict(max_new_tokens=128, do_sample=True, temperature=0.8)
outputs = model.generate(input_ids=torch.as_tensor([input_ids]).to('cuda:0'), **generation_kwargs)
output_tensor = outputs[0][len(input_ids):]
response = tokenizer.decode(output_tensor, skip_special_tokens=True)
print(response)
输出:
少先队员应该为老人让座。
下个星期,我跟我朋友打算去法国玩儿。
模型文件结构:
chatglm3-6b-csc-chinese-lora
├── adapter_config.json
└── adapter_model.bin
训练参数:

- 训练轮数: 5
- 单设备训练批大小: 6
- 学习率: 2e-05
- 最优步数: 25100
- 训练损失: 0.0834
- 学习率调度器类型: linear
- 基础模型: THUDM/chatglm3-6b
- 预热步数: 50
- "保存策略": "steps"
- "保存步长": 500
- "最大保存数": 10
- "bf16": false
- "fp16": true
- "优化器": "adamw_torch"
- "ddp_find_unused_parameters": false
- "梯度检查点": true
- 最大序列长度: 512
- 最大生成长度: 512
- 提示模板名称: vicuna
- 6 * V100 32GB显卡,训练48小时
训练数据集
训练集包含以下数据:
- 中文拼写纠错数据集:https://huggingface.co/datasets/shibing624/CSC
- 中文语法纠错数据集:https://github.com/shibing624/pycorrector/tree/llm/examples/data/grammar
- 通用GPT4问答数据集:https://huggingface.co/datasets/shibing624/sharegpt_gpt4
如需训练文本纠错模型,请参考https://github.com/shibing624/pycorrector