codellama fine-tune 2 检查点恢复

本文介绍了如何在finetune_lora.sh和finetune_clm_lora.py脚本中实现从检查点恢复训练,包括修改checkpoint路径、加载模型状态和适配器权重,适用于PyTorch模型的训练过程。
部署运行你感兴趣的模型镜像

检查点恢复

看了一下目前搜不到llama2中文社区提供的finetune_lora.sh中修改使得可以从检查点恢复的方法,所以小记一下

1.修改finetune_lora.sh

添加这段代码

--resume_from_checkpoint ${output_model}/checkpoint-1800 \

把具体的checkpoint字段修改为你的checkpoint文件夹目录名称

2.修改finetune_clm_lora.py (全量微调同)


 # Training
    if training_args.do_train:
        checkpoint = None
        if training_args.resume_from_checkpoint is not None:
            resume_from_checkpoint = training_args.resume_from_checkpoint
            checkpoint_name = os.path.join(resume_from_checkpoint, "pytorch_model.bin")
            if not os.path.exists(checkpoint_name):
                checkpoint_name = os.path.join(
                    resume_from_checkpoint, "adapter_model.bin"
                )  # only LoRA model - LoRA config above has to fit
                resume_from_checkpoint = (
                    False  # So the trainer won't try loading its state
                )
            # The two files above have a different name depending on how they were saved, but are actually the same.
            if os.path.exists(checkpoint_name):
                checkpoint = True
                print(f"Restarting from {checkpoint_name}")
                adapters_weights = torch.load(checkpoint_name)
                set_peft_model_state_dict(model, adapters_weights)
            else:
                print(f"Checkpoint {checkpoint_name} not found")
            # checkpoint = Fa
        elif last_checkpoint is not None:
            checkpoint = last_checkpoint
        
        if torch.__version__ >= "2" and sys.platform != "win32":
            model = torch.compile(model)
        
        train_result = trainer.train(resume_from_checkpoint=checkpoint)
        trainer.save_model()  # Saves the tokenizer too for easy upload

然后就ok了

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

Fine - tune数据指的是用于模型微调(fine - tune)阶段的数据集。微调是通过使用在大数据上得到的预训练好的模型来初始化自己的模型权重,进而提升模型在特定场景和任务中的精度的方法,这就要求预训练模型质量要有保证。Fine - tune数据的作用是让模型在预训练的基础上,进一步学习特定场景、特定任务的数据特征,从而使模型适用于自己的场景和任务[^1]。 例如在文本分类任务中,对于在通用语料库中预训练的BERT模型,在进行特定领域(如电影评论)的文本分类任务时,由于该领域数据分布可能与BERT预训练时不同,就需要使用该领域特定的数据作为Fine - tune数据,对BERT模型进行进一步训练,让模型适应电影评论这个特定领域的文本分类任务。可以采用任务内的预训练(在目标任务的训练数据上进一步预训练)、领域内的预训练(预训练数据从目标任务的相同领域获得)、跨领域预训练(预训练数据从目标任务的相同和其他不同领域获得)等方式来利用Fine - tune数据进行微调[^2]。 ```python # 示意使用Fine - tune数据进行微调 import torch from transformers import BertForSequenceClassification, BertTokenizer # 加载预训练模型和分词器 model = BertForSequenceClassification.from_pretrained('bert-base-uncased') tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') # 假设fine_tune_data是处理好的Fine - tune数据 fine_tune_data = [...] # 这里简单示意将数据转为模型可接受的输入 inputs = tokenizer(fine_tune_data, return_tensors='pt', padding=True, truncation=True) # 进行微调训练 optimizer = torch.optim.Adam(model.parameters(), lr=1e-5) model.train() for _ in range(num_epochs): outputs = model(**inputs) loss = outputs.loss loss.backward() optimizer.step() optimizer.zero_grad() ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值