Views
No views yet
"。。。。。")等等。\t、\n等,可以尝试编一句包含特殊字符的文本encode、decode看看能不能还原。如果不包含这些特殊字符,通过add_tokens函数添加。使用len(tokenizer)获取词表大小,tokenizer.vocab_size不统计自己通过add_tokens函数添加的字符。byte level训练1亿个字符至少需要32G内存(其实32G还是不太够,会频繁触发swap),13600k训练时长大概1个小时。char level训练6.5亿个字符(刚好是中文wiki百科的数据量)至少需要32G内存,因为多次触发了swap,实际使用量远不止32G,13600K训练时长约半个小时。tokenizer时从数据集中进行采样。bell open source的数据集BELLE。shift)。EOS、BOS等特殊标记。bell open source的数据集。感谢大佬BELLE。text = f"##提问:\n{example['instruction']}\n##回答:\n{example['output'][EOS]""##回答:"之前的部分("##回答:"也会被忽略),从"##回答:"后面开始。EOS句子结束特殊标记,否则模型decode的时候不知道要什么时候停下来。BOS句子开始标记可填可不填。prompt、chosen和 rejected,rejected这一列有部分数据我是从sft阶段初级模型(比如sft训练4个epoch,取0.5个epoch检查点的模型)生成,如果生成的rejected和chosen相似度在0.9以上,则不要这条数据。huggingface仓库:Phi2-Chinese-0.2B1from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
2import torch
3
4device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
5
6tokenizer = AutoTokenizer.from_pretrained('charent/Phi2-Chinese-0.2B')
7model = AutoModelForCausalLM.from_pretrained('charent/Phi2-Chinese-0.2B').to(device)
8
9
10txt = '感冒了要怎么办?'
11prompt = f"##提问:\n{txt}\n##回答:\n"
12
13# greedy search
14gen_conf = GenerationConfig(
15 num_beams=1,
16 do_sample=False,
17 max_length=320,
18 max_new_tokens=256,
19 no_repeat_ngram_size=4,
20 eos_token_id=tokenizer.eos_token_id,
21 pad_token_id=tokenizer.pad_token_id,
22)
23
24tokend = tokenizer.encode_plus(text=prompt)
25input_ids, attention_mask = torch.LongTensor([tokend.input_ids]).to(device), \
26 torch.LongTensor([tokend.attention_mask]).to(device)
27
28outputs = model.generate(
29 inputs=input_ids,
30 attention_mask=attention_mask,
31 generation_config=gen_conf,
32)
33
34outs = tokenizer.decode(outputs[0].cpu().numpy(), clean_up_tokenization_spaces=True, skip_special_tokens=True,)
35print(outs)
361##提问:
2感冒了要怎么办?
3##回答:
4感冒是由病毒引起的,感冒一般由病毒引起,以下是一些常见感冒的方法:
5- 洗手,特别是在接触其他人或物品后。
6- 咳嗽或打喷嚏时用纸巾或手肘遮住口鼻。
7- 用手触摸口鼻,特别是喉咙和鼻子。
8- 如果咳嗽或打喷嚏,可以用纸巾或手绢来遮住口鼻,但要远离其他人。
9- 如果你感冒了,最好不要触摸自己的眼睛、鼻子和嘴巴。
10- 在感冒期间,最好保持充足的水分和休息,以缓解身体的疲劳。
11- 如果您已经感冒了,可以喝一些温水或盐水来补充体液。
12- 另外,如果感冒了,建议及时就医。1@misc{Charent2023,
2 author={Charent Chen},
3 title={A small Chinese causal language model with 0.2B parameters base on Phi2},
4 year={2023},
5 publisher = {GitHub},
6 journal = {GitHub repository},
7 howpublished = {\url{https://github.com/charent/Phi2-mini-Chinese}},
8}