ToB企服应用市场:ToB评测及商务社交产业平台

标题: 增量预练习网络安全大模型的一次实验 [打印本页]

作者: 愛在花開的季節    时间: 2025-1-22 04:01
标题: 增量预练习网络安全大模型的一次实验
一、配景

探索利用网络安全知识,对开源基模型进行增强,评估是否能使基模型在网络安全范畴表现出更好地专业度。
项目基于云起无垠SecGPT开源项目,在hugeface开源数据集的底子上,增加了自有预练习数据,进行增量预练习。
参考链接:
  1. https://github.com/Clouditera/secgpt
复制代码

回到顶部(go to top)
二、数据集预备


0x1:预练习数据

预练习利用“安全册本,安全知识库,安全论文,安全社区文章,漏洞库”等等安全内容,


https://huggingface.co/datasets/w8ay/security-paper-datasets?row=0 


笔者增加了一份自己近10年以内的博客文章,




将上述格式转换为hugeface兼容格式,huggingface支持以下4种数据格式的数据集,只需要在load的时间设定格式就好了。
Data formatLoading scriptExample
CSV & TSVcsvload_dataset("csv", data_files="my_file.csv")
Text filestextload_dataset("text", data_files="my_file.txt")
JSON & JSON Linesjsonload_dataset("json", data_files="my_file.jsonl")
Pickled DataFramespandasload_dataset("pandas", data_files="my_dataframe.pkl")


  1. # -- coding: utf-8 --**
  2. import json
  3. import pandas as pd
  4. from datasets import load_dataset
  5. from datasets import Dataset, concatenate_datasets
  6. def prepare_cnblogs_data():
  7.     with open("./posts.json", 'r', encoding='utf-8') as file:
  8.         data = json.load(file)
  9.     original_json = list()
  10.     for item in data:
  11.         original_json.append(
  12.             {
  13.                 "text": item['Body'],
  14.                 "category": "cnblogs"
  15.             }
  16.         )
  17.     print(original_json[0])
  18.     print(original_json[1])
  19.     with open("./posts_hugeface_dataset.json", 'w', encoding='utf-8') as file:
  20.         # 将列表保存到文件,注意使用ensure_ascii=False参数
  21.         json.dump(original_json, file, ensure_ascii=False, indent=2)
  22.     df = pd.DataFrame(original_json)
  23.     df.to_pickle('./posts_hugeface_dataset.pkl')
  24. def prepare_security_paper_dataset():
  25.     dataset = load_dataset("w8ay/security-paper-datasets")
  26.     df_train = dataset['train'].to_pandas()
  27.     df_train.to_pickle('security_paper_dataset.pkl')
  28. def merge_dataset():
  29.     security_paper_dataset = load_dataset("pandas", data_files="security_paper_dataset.pkl")['train'].to_pandas()
  30.     posts_hugeface_dataset = load_dataset("pandas", data_files="posts_hugeface_dataset.pkl")['train'].to_pandas()
  31.     dataset_part1 = Dataset.from_pandas(security_paper_dataset)
  32.     dataset_part2 = Dataset.from_pandas(posts_hugeface_dataset)
  33.     combined_dataset = concatenate_datasets([dataset_part1, dataset_part2])
  34.     print(combined_dataset[0])
  35.     print(len(combined_dataset))
  36.     combined_dataset_df = pd.DataFrame(combined_dataset)
  37.     combined_dataset_df.to_pickle('security_paper_and_posts_dataset.pickle')
  38. if __name__ == "__main__":
  39.     prepare_security_paper_dataset()
  40.     prepare_cnblogs_data()
  41.     merge_dataset()
复制代码


这里将多份本地pickle语料数据合并,




0x2:有监视数据

通过构造各类有监视安万能力数据集,让模型能了解各类安全指令。 


有监视SFT数据集构建有几个注意点:

参考链接:


  1. https://huggingface.co/w8ay/secgpt
  2. https://huggingface.co/datasets/TigerResearch/tigerbot-zhihu-zh-10k
  3. https://zhuanlan.zhihu.com/p/548355568
  4. https://zhuanlan.zhihu.com/p/564816807
  5. https://zhuanlan.zhihu.com/p/554678463
  6. https://huggingface.co/datasets/w8ay/security-paper-datasets
  7. https://huggingface.co/w8ay/secgpt
复制代码



回到顶部(go to top)
三、模型练习


0x1:基模型选择

基于Baichuan-13B (无道德限定,较好中文支持,显存资源占用小)
Baichuan-13B 是由百川智能继 Baichuan-7B 之后开发的包含 130 亿参数的开源可商用的大规模语言模型,在权威的中文和英文 benchmark 上均取得同尺寸最好的结果。本次发布包含有预练习 (Baichuan-13B-Base) 和对齐 (Baichuan-13B-Chat) 两个版本。Baichuan-13B 有如下几个特点:


0x2:运行环境



V100 * 8 

0x3:增量预练习 

启动练习,
  1. python train.py
复制代码




参考链接:
  1. https://huggingface.co/baichuan-inc/Baichuan-13B-Base
  2. https://huggingface.co/w8ay/secgpt
  3. https://zhuanlan.zhihu.com/p/554678463
复制代码

回到顶部(go to top)
四、结果展示

模型结果的输出存在随机性,模型可能知道答案,但是随机后改变输出,这时间可以增加提示词让模型注意力集中。
也可以做RLHF强化学习,让模型输出多个结果,选择正确的那个,提升模型结果。

0x1:用百川原生基模型

  1. python3 webdemo.py --base_model baichuan-inc/Baichuan-13B-Base
复制代码



0x2:用增量预练习后的模型


  1. python3 webdemo.py --base_model baichuan-inc/Baichuan-13B-Base --lora /data_vdb1/secgpt/output/epoch-0-step-2400
复制代码



  1. http://47.236.142.150:7860/
复制代码




参考链接:
  1. https://github.com/Clouditera/secgpt
复制代码

回到顶部(go to top)
五、后续思考




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4