zjunlp / EasyEdit

[ACL 2024] An Easy-to-use Knowledge Editing Framework for LLMs.
https://zjunlp.github.io/project/KnowEdit
MIT License
1.74k stars 210 forks source link

用了IKE方法,怎么保存编辑后的模型 #242

Closed threeFeetCat123 closed 4 months ago

threeFeetCat123 commented 4 months ago

你好,

我使用IKE方法,编辑了qwen-7b-chat的模型,结果保存在这里。

EQ$6GL}J3VNE8`QJWB{AJ(4

但当我单独运行编辑后的模型,它没有任何变动,没有编辑成功。但evaluation的结果告诉我,编辑得十分成功。

11U8H$5 M@42 ~JQ5X26_JA

当我看 EasyEdit/tutorial-notebooks 的其他示例时,发现他们都没有保存模型以及demonstration,而是直接将demonstration嵌入到prompt进行测试。

10W}}GLOA9B}3FPTKQ7KFHV

最后,我发现在results下有经过embedding处理的demonstration,demonstration和model并没有在连接在一起发挥作用,导致编辑没有效果。

DG%%ERQLY5~66 7$RV(YAV1

不知道是我的代码缺少一些东西,还是目前EasyEdit还不支持保存经过IKE编辑的模型。

下面是我使用IKE编辑qwen的代码。

def test_IKE():

    parser = argparse.ArgumentParser()
    parser.add_argument('--ds_size', type=int)
    args = parser.parse_args()

    train_ds = json.load(open('/mnt/disk1/hyd/EasyEdit/database/IKE-meaning.json', 'r', encoding='utf-8'))
    if args.ds_size is not None:
        train_ds = random.sample(train_ds, args.ds_size)

    prompts = [test_data_['prompt'] for test_data_ in train_ds]  # 目标问题
    rephrase_prompts = [edit_data_['rephrase_prompt'] for edit_data_ in train_ds]  # 目标问题的其他形式
    target_new = [edit_data_['target_new'] for edit_data_ in train_ds]  # 期望的答案
    locality_prompt = [edit_data_['locality_prompt'] for edit_data_ in train_ds]  # 本地性
    locality_ground_truth = [edit_data_['locality_ground_truth'] for edit_data_ in train_ds]  # 本地答案

    with open('/mnt/disk1/hyd/EasyEdit/logs/IKE_meaning-testdata.json', 'w') as f:
        json.dump(train_ds, f, ensure_ascii=False, indent=4)

    hparams = IKEHyperParams.from_hparams('/mnt/disk1/hyd/EasyEdit/hparams/IKE/qwen-7b-meaing.yaml')
    editor = BaseEditor.from_hparams(hparams)

    # Initialize SentenceTransformer model
    sentence_model = SentenceTransformer(hparams.sentence_model_name)
    # Generate and save sentence embeddings
    encode_ike_facts(sentence_model, train_ds, hparams)

    metrics, edited_model, _ = editor.edit(
        prompts=prompts,
        rephrase_prompts=rephrase_prompts, # new para
        target_new=target_new,
        train_ds=train_ds,
        locality_prompt=locality_prompt,
        locality_ground_truth=locality_ground_truth,
        copy=True,
        return_orig_weights=False,
        keep_original_weight=True,
    )

    print('*' * 80)
    print(metrics)
    edited_model.save_pretrained('/mnt/disk1/hyd/EasyEdit/logs/Qwen-7B-Chat-edited-IKE-meaing', safe_serialization=True)

十分感谢,你能看到这里! 祝你身体健康。

zxlzr commented 4 months ago

您好,IKE方法属于保留模型参数的编辑方法,没有修改模型参数,模型还是原来的模型,详情参见论文https://arxiv.org/abs/2305.12740

zxlzr commented 4 months ago

Do you have any further questions?

threeFeetCat123 commented 4 months ago

那请问我如何将模型保存下来,自己评测编辑效果,而不是通过EasyEdit的evaluation来评测编辑效果。

我想自动化地自行测试,而不是像下面这种自己手动嵌入demonstration,毕竟论文也不是这样评测编辑效果的

10W}}GLOA9B}3FPTKQ7KFHV

感谢。

XeeKee commented 4 months ago

EasyEdit only provides specific evaluation methods. If you want to customize your own evaluation method, I recommend you modify this function:https://github.com/zjunlp/EasyEdit/blob/2cff1a72c88900eb3522325d4121161502118e4a/easyeditor/evaluate/evaluate.py#L164

threeFeetCat123 commented 4 months ago

ok, thanks.