美文网首页
把transformers训练完成的模型推送到huggingfa

把transformers训练完成的模型推送到huggingfa

作者: 又双叒叕苟了一天 | 来源:发表于2023-05-12 19:38 被阅读0次

    1. 注册

    注册地址:https://huggingface.co/
    注册完成进去看到推送上去的模型和数据集,现在暂时还没有。

    模型和数据集

    2. 生成token用于和huggingface hub传输

    网址: https://huggingface.co/settings/tokens
    点击New token,有两种模式:只读和读写。一般选读写,并且一台机器一个token。

    token

    3. 把ssh key添加到huggingface

    ssh key:cat ~/.ssh/id_rsa.pub
    网址:https://huggingface.co/settings/keys

    添加ssh key

    4. 安装git-lfs用于大文件传输

    网址:https://git-lfs.com/
    macos安装git-lfs:

    brew install git-lfs
    git lfs install
    

    ubuntu安装git-lfs:

    sudo apt install git-lfs
    git lfs install
    

    5. 登陆huggingface客户端

    shell输入:

    huggingface-cli login
    

    输入第2步生成的token,之后这台机器跑代码就不再需要token了。


    登陆成功

    6. 创建一个本地hub的目录

    mkdir ~/Documents/huggingface_local_hub
    cd ~/Documents/huggingface_local_hub
    git init
    

    7. 把本地hub推到远程hub

    1. output_dir是本地存放的目录
    2. hub_model_id是远程存放的用户空间和模型名称
    training_args = TrainingArguments(
        output_dir="~/Documents/huggingface_local_hub/llm/task_qa_distilbert",
        hub_model_id="smile367/task_qa_distilbert",
        evaluation_strategy="epoch",
        learning_rate=2e-5,
        per_device_train_batch_size=16,
        per_device_eval_batch_size=16,
        num_train_epochs=1,
        weight_decay=0.01,
        push_to_hub=True, 
    )
    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=train_set,
        eval_dataset=test_set,
        tokenizer=tokenizer,
        data_collator=data_collator,
    )
    trainer.push_to_hub()
    

    8. 查看是否推送成功

    可以看到,远程已经有了这个模型。


    推送成功

    9. 使用远程的模型

    pipeline("question-answering", model="smile367/task_qa_distilbert"),其中model参数指定远程的模型。

    test_data = load_dataset("squad", split="validation[:1]")
    print("question: {}".format(test_data["question"]))
    print("context: {}".format(test_data["context"]))
    print("answers: {}".format(test_data["answers"]))
    question_answerer = pipeline("question-answering", model="smile367/task_qa_distilbert")
    result = question_answerer(question=test_data["question"], context=test_data["context"])
    print("result: {}".format(result))
    

    10. 完整代码demo

    BERT实战(下)-问答任务

    相关文章

      网友评论

          本文标题:把transformers训练完成的模型推送到huggingfa

          本文链接:https://www.haomeiwen.com/subject/sizesdtx.html