mymusise / ChatGLM-Tuning

基于ChatGLM-6B + LoRA的Fintune方案
MIT License
3.71k stars 444 forks source link

完全学习不到数据集的内容 #206

Open starhui70520 opened 1 year ago

starhui70520 commented 1 year ago

RT,我设置了lora_rank=32 max_steps=80000 一共40条QA数据 最后输出如下:

输入问题:XX大学XX校区的篮球场在哪? 训练回答:XX大学XX校区的篮球场位于足球场的南侧,第二饭堂正门的正前方的斜下侧 输出回答:XX大学XX校区的篮球场位于校园的东南部,具体地址是南海大道38号,可乘坐公交车或步行前往。

输入问题:XX大学在哪 训练回答:XX大学在广东省有三个校区分别是佛山,南海,清远 输出回答:XX大学位于广东省广州市天河区五山路

这种情况是什么原因导致的,求指教

starhui70520 commented 1 year ago

补充输入参数如下
--per_device_train_batch_size 6 \ --gradient_accumulation_steps 1 \ --max_steps 80000 \ --save_steps 1000 \ --save_total_limit 2 \ --learning_rate 1e-4 \ --fp16 \ --remove_unused_columns false \ --logging_steps 50 \ --output_dir /tmp/output'

starhui70520 commented 1 year ago

推理代码如下: import json import io import torch from transformers import AutoModel from transformers import AutoTokenizer from peft import PeftModel from cover_alpaca2jsonl import format_example import sys

torch.set_default_tensor_type(torch.cuda.HalfTensor) model = AutoModel.from_pretrained("/tmp/pretrainmodel", trust_remote_code=True, load_in_8bit=False, device_map='auto')

model = PeftModel.from_pretrained(model, "/tmp/dataset/checkpoint-79000/checkpoint-79000") torch.set_default_tensor_type(torch.cuda.HalfTensor)

tokenizer = AutoTokenizer.from_pretrained("/tmp/pretrainmodel", trust_remote_code=True)

alpaca数据集

instructions = [ {'instruction': 'Give three tips for staying healthy.', 'input': '', 'output': '1.Eat a balanced diet and make sure to include plenty of fruits and vegetables. \n2. Exercise regularly to keep your body active and strong. \n3. Get enough sleep and maintain a consistent sleep schedule.', }, {'instruction': 'What are the three primary colors?', 'input': '', 'output': 'The three primary colors are red, blue, and yellow.', }]

alpaca数据集,问题翻译为中文,output为chatgpt的输出

instructions += [ {'instruction': '请给出三个健康生活的建议', 'input': '', 'output': '1.保持均衡的饮食:饮食是影响身体健康的一个重要因素。\n2.定期运动:运动对身体健康有很多好处。\n3.睡眠充足:良好的睡眠质量对身体健康至关重要。', }, {'instruction': '三原色是什么?(请用中文)', 'input': '', 'output': '三原色是指色彩三基色,即红色、绿色和蓝色。在色彩学中,通过不同比例的三原色的叠加可以形成各种色彩,这也是彩色显示技术和印刷技术的基础', }, {'instruction': '以下哪家公司类型与其它的不同', 'input': '微博、京东、淘宝', 'output': '微博', } ]

读取JSON文件

with io.open('/tmp/dataset/QA-Only.json', 'r', encoding='utf-8') as f: data_str = f.read()

按行分割数据

data_list = data_str.strip().split('\n')

解析数据并添加到instructions列表中

for item_str in data_list: item = json.loads(item_str) instructions.append({ 'instruction': item['input'].strip(), 'input': '', 'output': item['target'].strip() })

with torch.no_grad(): for idx, item in enumerate(instructions): feature = format_example(item) input_text = feature['context'] ids = tokenizer.encode(input_text) input_ids = torch.LongTensor([ids]) out = model.generate( input_ids=input_ids, max_length=150, do_sample=False, temperature=0 ) out_text = tokenizer.decode(out[0]) answer = out_text.replace(input_text, "").replace("\nEND", "").strip() item['infer_answer'] = answer

重定向stdout输出到文件

    sys.stdout = open('/tmp/output/OP.log', 'a', encoding='utf-8')
    print(out_text)
    print(f"### {idx+1}.Answer:\n", item.get('output'), '\n\n')
    sys.stdout.close()
    sys.stdout = sys.__stdout__
XianglongTan commented 1 year ago

猜测 lora 的 rank 不能太大,不然不容易学到特定数据集的知识

binhoul commented 4 months ago

mark