当前位置:   article > 正文

Huggingface的介绍,使用(CSDN最强Huggingface入门手册)

huggingface

Huggingface的介绍,使用(CSDN最强Huggingface入门手册)

返回论文目录
返回资料目录

1.Huggingface的简介

Huggingface即是网站名也是其公司名,随着transformer浪潮,Huggingface逐步收纳了众多最前沿的模型和数据集等有趣的工作,与transformers库结合,可以快速使用学习这些模型。目前提到NLP必然绕不开Huggingface。

2.Huggingface的具体介绍

进入Huggingface网站,如下图所示。
在这里插入图片描述
其主要包含:

  • Models(模型),包括各种处理CV和NLP等任务的模型,上面模型都是可以免费获得
  • Datasets(数据集),包括很多数据集
  • Spaces(分享空间),包括社区空间下最新的一些有意思的分享,可以理解为huggingface朋友圈
  • Docs(文档,各种模型算法文档),包括各种模型算法等说明使用文档
  • Solutions(解决方案,体验等),包括others
  • Pricing(dddd) ,懂的都懂

3.Huggingface的Models

点开Models。可以看到下图的任务,再点开+23 Tasks。
在这里插入图片描述

可以看到下图所有的任务。
其中,主要包括计算机视觉、自然语言处理、语音处理、多模态、表格处理、强化学习。
在这里插入图片描述
在这里插入图片描述
展开介绍:
Computer Vision(计算机视觉任务):包括lmage Classification(图像分类),lmage Segmentation(图像分割)、zero-Shot lmage Classification(零样本图像分类)、lmage-to-Image(图像到图像的任务)、Unconditional lmage Generation(无条件图像生成)、Object Detection(目标检测)、Video Classification(视频分类)、Depth Estimation(深度估计,估计拍摄者距离图像各处的距离)

Natural Language Processing(自然语言处理):包括Translation(机器翻译)、Fill-Mask(填充掩码,预测句子中被遮掩的词)、Token Classification(词分类)、Sentence Similarity(句子相似度)、Question Answering(问答系统),Summarization(总结,缩句)、Zero-Shot Classification (零样本分类)、Text Classification(文本分类)、Text2Text(文本到文本的生成)、Text Generation(文本生成)、Conversational(聊天)、Table Question Answer(表问答,1.预测表格中被遮掩单词2.数字推理,判断句子是否被表格数据支持)

Audio(语音):Automatic Speech Recognition(语音识别)、Audio Classification(语音分类)、Text-to-Speech(文本到语音的生成)、Audio-to-Audio(语音到语音的生成)、Voice Activity Detection(声音检测、检测识别出需要的声音部分)

Multimodal(多模态):Feature Extraction(特征提取)、Text-to-Image(文本到图像)、Visual Question Answering(视觉问答)、Image2Text(图像到文本)、Document Question Answering(文档问答)

Tabular(表格):Tabular Classification(表分类)、Tabular Regression(表回归)

Reinforcement Learning(强化学习):Reinforcement Learning(强化学习)、Robotics(机器人)

API地址(可以自己对着学,博客这里就只是给出些简单介绍)

模型的使用

一般来说,页面上会给出模型的介绍。例如,我们打开其中一个fill-mask任务下下载最多的模型bert-base-uncased。
在这里插入图片描述
可以看到模型描述:
在这里插入图片描述
使用方法-1
需要提前安装transformers库,可以直接pip install transformers安装。还有PytorchTensorFlow库,读者自行下载。
下载完后可以使用pipeline直接简单的使用这些模型。第一次执行时pipeline会加载模型,模型会自动下载到本地,可以直接用。第一个参数是任务类型,第二个是具体模型名字。

from transformers import pipeline
unmasker = pipeline('fill-mask', model='bert-base-uncased')
unmasker("Hello I'm a [MASK] model.")
  • 1
  • 2
  • 3

模型下载在本地的地址:

C:\Users\【自己的用户名】\.cache\huggingface\hub
  • 1

当然,不同模型使用方法略有区别,直接通过页面学习或文档学习最好。

在这里插入图片描述

可以自定义加载输入分词器:使用AutoTokenizer

from transformers import AutoTokenizer
#下面这种方式可以自动加载bert-base-uncased中使用的分词器
tokenizer=AutoTokenizer.from_pretrained("bert-base-uncased")
  • 1
  • 2
  • 3

可以自定义加载模型结构:使用AutoModel

不包括输入分词器和输出部分!!!

from transformers import AutoModel
#下面这种方式可以自动加载bert-base-uncased中使用的模型,没有最后的全连接输出层和softmax
model=AutoModel.from_pretrained("bert-base-uncased")
  • 1
  • 2
  • 3

可以自定义加载模型和输出部分:使用AutoModelForSequenceClassification等

from transformers import AutoModelForSequenceClassification
#下面这种方式可以自动加载bert-base-uncased中使用的模型(包括了输出部分),有最后的全连接输出层
model=AutoModel.AutoModelForSequenceClassification("bert-base-uncased")
  • 1
  • 2
  • 3

模型保存

model.save_pretrained("./")#保持到当前目录
  • 1

1个简单流程例子

input=['The first sentence!','The second sentence!']
from transformers import AutoTokenizer
tokenizer=AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")#https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english
input=tokenizer(input, padding=True, truncation=True, return_tensors='pt')#padding='max_length'
from transformers import AutoModelForSequenceClassification
model=AutoModel.AutoModelForSequenceClassification("distilbert-base-uncased-finetuned-sst-2-english")
print(model)
output=model(**input)
print(output.logits.shape)
import torch
predictions=torch.nn.functional.softmax(output.logits, dim=1)
print(predictions)
print(model.config.id2label)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

使用方法-2

在这里插入图片描述

下面以ChatGLM2-6B为例(见上图),先在github上git下ChatGLM2-6B除模型外的相关文件

git clone https://github.com/THUDM/ChatGLM2-6B.git
cd ChatGLM2-6B
  • 1
  • 2

再安装好相关依赖

pip install -r requirements.txt
  • 1

接下来可以类似刚才的方法一直接执行下面代码,会在网上自动下载模型文件

>>> from transformers import AutoTokenizer, AutoModel
>>> tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
>>> model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True, device='cuda')
>>> model = model.eval()
>>> response, history = model.chat(tokenizer, "你好", history=[])
>>> print(response)
你好声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签