• 在hugging face上发布自己的模型 (ubuntu 19.0)


    以ubuntu为例子

    前言

    hugging face 主页:https://huggingface.co/
    用邮箱注册一个账号

    Install git lfs

    huggingface 的使用和github类似,但是github单个文件不大于50MB,而一个模型动辄几百MB,需要用到lfd

    $curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
    $sudo apt-get install git-lfs
    $git lfs install
    
    • 1
    • 2
    • 3

    在终端登录

    (base) workspace:~$ huggingface-cli login
    
            _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
            _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
            _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
            _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
            _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|
    
            To login, `huggingface_hub` now requires a token generated from https://huggingface.co/settings/tokens .
    
    Token:
    Login successful
    Your token has been saved to /home/t-enshengshi/.huggingface/token
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    为了后续create repo方便,这里使用开通write权限而并非仅仅是read权限。

    创建与上传repo

    huggingface-cli repo create model_name
    git clone https://huggingface.co/username/model_name
    
    • 1
    • 2

    上传类似于github的操作

    git add .
    git commit -m "first commit"
    git push
    
    • 1
    • 2
    • 3

    上传文件说明

    上传文件包括了model和tokenizer两部分。

    model.save_pretrained("pytorch_model.bin")
    
    • 1

    可以得到pytorch_model.bin

    tokenizer.save_pretrained("./saved_pre_model")
    
    • 1

    可以得到added_tokens.json merges.txt special_tokens_map.json tokenizer_config.json vocab.json

    使用

    我是用的Roberta,以roberta为例子

    import torch
    from transformers import RobertaTokenizer, RobertaConfig, RobertaModel
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    tokenizer = RobertaTokenizer.from_pretrained("Ensheng/coco")
    model = RobertaModel.from_pretrained("Ensheng/coco")
    
    • 1
    • 2
    • 3
    • 4
    • 5

    output

    Downloading: 100%
    916k/916k [00:00<00:00, 1.60MB/s]
    Downloading: 100%
    434k/434k [00:00<00:00, 411kB/s]
    Downloading: 100%
    941/941 [00:00<00:00, 25.7kB/s]
    Downloading: 100%
    1.63k/1.63k [00:00<00:00, 45.5kB/s]
    Downloading: 100%
    1.10k/1.10k [00:00<00:00, 29.4kB/s]
    Downloading: 100%
    738/738 [00:00<00:00, 16.7kB/s]
    Downloading: 100%
    481M/481M [00:08<00:00, 58.3MB/s]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    测试tokenizer 和model

    nl_tokens=tokenizer.tokenize("return maximum value")
    code_tokens=tokenizer.tokenize("def max(a,b): if a>b: return a else return b")
    tokens=[tokenizer.cls_token]+nl_tokens+[tokenizer.sep_token]+code_tokens+[tokenizer.sep_token]
    tokens_ids=tokenizer.convert_tokens_to_ids(tokens)
    context_embeddings=model(torch.tensor(tokens_ids)[None,:])[0]
    print(context_embeddings)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    output

    tensor([[[-0.6205,  0.2075, -0.6909,  ...,  0.4914,  1.5620,  0.5642],
             [-0.6205,  0.2075, -0.6909,  ...,  0.4914,  1.5620,  0.5642],
             [-0.6205,  0.2075, -0.6909,  ...,  0.4914,  1.5620,  0.5642],
             ...,
             [-0.3708,  0.5695, -1.5493,  ..., -0.0023,  1.2854,  0.3780],
             [ 1.3056, -0.1004, -0.6191,  ..., -0.4956,  1.5792,  1.5347],
             [ 0.1874,  1.3228, -0.9529,  ..., -1.0119,  1.7750,  1.3678]]],
           grad_fn=)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    Win10微信 PC 端打字卡顿解决方法
    单例模式(常用)
    【推荐系统->论文阅读】Wide&Deep模型
    6.typescript类
    JAVA毕业设计河南省农村多元化养老服务管理系统设计与实现计算机源码+lw文档+系统+调试部署+数据库
    Java EE 用户删除和修改功能
    uni-app 微信小程序支付/公众号支付/h5支付宝/h5微信/支付宝app支付/微信app支付
    拼多多商品详情API接口接入流程如下:
    零拷贝原理与实现
    C# ORM框架,freesql,mysql数据库
  • 原文地址:https://blog.csdn.net/qq_36097393/article/details/126333377