NLP-Huggingface基本使用方法

NLP的网络结构大同小异,只不过训练策略可能会不同。因为与图像cv不同,文本训练数据非常的多,cv可以使用10几张就可以获得特征向量,而文本做不到学几句话就能让计算机听得懂话。因此,我们都需要使用预训练模型,所以模型的结构都差不多,不会有太大的改变。

配置环境

下载Huggingface包,Huggingface可以说是一个社区,集成了很多nlp的模型、数据集、预训练权重文件等等,并且免费,只需要pip install下载即可。

pip install transformers

测试

cpp 复制代码
import warnings
warnings.filterwarnings("ignore")
from transformers import pipeline
classifier = pipeline("sentiment-analysis")
print(classifier(
    [
        "I've been waiting for a HuggingFace course my whole life.",
        "I hate this so much!"
    ]
))


但是登这个网页需要梯子!

1.分词器tokenizer

from transformers import AutoTokenizer #自动判断模型选用哪种分词器进行分词操作

cpp 复制代码
from transformers import AutoTokenizer
checkpoint = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
raws_inputs = [
        "I've been waiting for a HuggingFace course my whole life.",
        "I hate this so much!"
]
inputs = tokenizer(raws_inputs,padding=True,truncation=True,return_tensors="pt")
print(inputs)

代码中使用了自动分词器选择,它是由checkpoint指定的预训练模型关联的,不用我们自己选择tokenizer,它自动帮我们选择好了。

tokenizer(raws_inputs,padding=True,truncation=True,return_tensors="pt")*中padding代表是否需要自动补零;truncation代表是否需要隔断,默认是true,指不超过计算机的上限512,它也可以被你指定你设定的文本长度然后进行隔断;return_tensors="pt"指的是使用pytorch.

模型的输入经过tokenizer得到两部分: 1)input_id:它由id、[CLS][SEP]两个特殊字符、补零占位符组成;2)attention_mask:为1的表示可以跟谁算,为0的不会参与到self-attention的进一步计算。
注意: attention_mask和padding是配套使用的,当人为去修改padding的时候,也要把attention_mask里面的参数进行修改。不修改的话计算机会认为人为padding的0占位符是有效位将会参与self-attention计算,那么这时候输出结果会有区别。

2.模型Model

from transformers import AutoModel #自动在预训练模型中选择模型

在上述代码加入模型部分:

cpp 复制代码
from transformers import AutoTokenizer
from transformers import AutoModel
checkpoint = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)自动加载分词器
model = AutoModel.from_pretrained(checkpoint)#自动加载模型
raws_inputs = [
        "I've been waiting for a HuggingFace course my whole life.",
        "I hate this so much!"
]
inputs = tokenizer(raws_inputs,padding=True,truncation=True,return_tensors="pt")#输入经过tokenizer得到两部分进行网络输入
outputs = model(**inputs)
print(outputs.last_hidden_state.shape)

这里面2指的是两个输入,16指的是最长id尺寸,768指定是维度。也就是说输出把每个token都编码成768维度的向量。

3.输出头

NLP任务其实就是做分类,比如情感分析是对一个序列做2分类等等。解决任务不同实际上就是对哪部分做分类。那么用什么做分类就选择什么输出头,就import什么东西。

cpp 复制代码
from transformers import AutoTokenizer
from transformers import AutoModelForSequenceClassification
checkpoint = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForSequenceClassification.from_pretrained(checkpoint)
raws_inputs = [
        "I ve been waiting for a HuggingFace course my whole life.",
        "I hate this so much!"
]
inputs = tokenizer(raws_inputs,padding=True,truncation=True,return_tensors="pt")#可以自己写最大长度max_length=8,truncation=True时超过8会被截断
outputs = model(**inputs)
print(outputs.logits.shape)

输出结果的含义:[两个文本,两种可能性所得概率]

4.预测

cpp 复制代码
import torch
from transformers import AutoTokenizer
from transformers import AutoModelForSequenceClassification

checkpoint = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForSequenceClassification.from_pretrained(checkpoint)

raws_inputs = [
        "I ve been waiting for a HuggingFace course my whole life.",
        "I hate this so much!"
]
inputs = tokenizer(raws_inputs,padding=True,truncation=True,return_tensors="pt")

outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits,dim=-1)#对最后一个维度进行softmax
print(predictions)
print(model.config.id2label)
相关推荐
hudawei9968 小时前
词嵌入中语料库矩阵和句子矩阵是怎样的?
人工智能·ai·自然语言处理·词嵌入·word embedding·词向量·语义理解
Mxsoft6198 小时前
某次数据分析偏差,发现时区设置错,修正时间戳救场!
人工智能
努力也学不会java8 小时前
【docker】Docker Register(镜像仓库)
运维·人工智能·机器学习·docker·容器
m0_650108248 小时前
OLMo 2:全开放语言模型的技术突破与实践
论文阅读·人工智能·olmo 2·全开源多模态大模型·全链路开放·训练稳定性
Mintopia8 小时前
🧠 AIGC技术标准制定:Web行业协同的必要性与难点
人工智能·前端框架·trae
轻竹办公PPT8 小时前
AI一键生成年终总结PPT
人工智能·python·powerpoint
是Dream呀8 小时前
昇腾平台 PyTorch 迁移实操:从环境搭建到精度达标的完整步骤
人工智能·pytorch·python·昇腾
Mintopia8 小时前
🧩 Codex 配置自定义指令指南
人工智能·llm·claude
一个处女座的程序猿8 小时前
AGI:《从规模扩张到研究驱动:Ilya Sutskever畅谈AI泛化瓶颈、人类学习启事与超级智能未来之路》
人工智能·llms·ilya sutskever
工藤学编程8 小时前
零基础学AI大模型之Milvus实战:Attu可视化安装+Python整合全案例
人工智能·python·milvus