Transformers预训练模型使用:序列分类 Sequence Classification
transformers
huggingface/transformers: 是一个基于 Python 的自然语言处理库,它使用了 PostgreSQL 数据库存储数据。适合用于自然语言处理任务的开发和实现,特别是对于需要使用 Python 和 PostgreSQL 数据库的场景。特点是自然语言处理库、Python、PostgreSQL 数据库。
项目地址:https://gitcode.com/gh_mirrors/tra/transformers

·
序列分类任务的工作是将文本序列归类到预设类型中,如新闻分类、情感分类等,就属于这类任务。
情感分类
以下是使用pipelines
来进行情感分类的例子,具体任务是判断输入文本是消极的还是积极的。
示例:
from transformers import pipeline
classifier = pipeline("sentiment-analysis")
result = classifier("I hate you")[0]
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
result = classifier("I love you")[0]
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
输出结果:
label: NEGATIVE, with score: 0.9991
label: POSITIVE, with score: 0.9999
- 该模型会返回文本的标签,即
NEGATIVE
或POSITIVE
,和它在这个标签上的评价分数score
。
语句释义
以下是语句释义的例子,主要任务是使用一个模型来判断两个序列是否可以相互解释。过程如下:
- 实例化一个文本标记器(tokenizer)和一个BERT模型(model),并将加载预训练的权重。
- 用两个句子建立一个序列,其中包含特定模型的分隔符、标记类型ID和注意力掩码。这一步可以使用文本标记器自动生成。
- 将创建好的序列送入模型,并获得分类结果。结果由两个类组成:0(表示不能释义)和1(表示可以释义)。
- 使用softmax将其转化为分类的概率。
- 打印结果。
示例:
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased-finetuned-mrpc")
model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased-finetuned-mrpc")
classes = ["不能释义", "可以释义"]
sentence_0 = "I am a student coming from China."
sentence_1 = "I am a big pig."
sentence_2 = "I am a person who studies in the school"
paraphrase = tokenizer(sentence_0, sentence_2, return_tensors="pt")
not_paraphrase = tokenizer(sentence_0, sentence_1, return_tensors="pt")
paraphrase_classification = model(**paraphrase)
not_paraphrase_classification = model(**not_paraphrase)
p_result = torch.softmax(paraphrase_classification[0], dim=1)[0]
np_result = torch.softmax(not_paraphrase_classification[0], dim=1)[0]
print(sentence_0, "\n", sentence_2)
for i in range(len(classes)):
print("{}:{:.2f}%".format(classes[i], p_result[i].item() * 100))
print(sentence_0, "\n", sentence_1)
for i in range(len(classes)):
print("{}:{:.2f}%".format(classes[i], np_result[i].item() * 100))
输出结果:
I am a student coming from China.
I am a person who studies in the school
不能释义:23.34%
可以释义:76.66%
I am a student coming from China.
I am a big pig.
不能释义:92.51%
可以释义:7.49%




huggingface/transformers: 是一个基于 Python 的自然语言处理库,它使用了 PostgreSQL 数据库存储数据。适合用于自然语言处理任务的开发和实现,特别是对于需要使用 Python 和 PostgreSQL 数据库的场景。特点是自然语言处理库、Python、PostgreSQL 数据库。
最近提交(Master分支:7 个月前 )
c9d1e523
* Update installation.md
* Update README.md 6 小时前
d253de6d
* initial
* fix
* fix
* update
* fix
* fixes
* quantization
* attention mask visualizer
* multimodal
* small changes
* fix code samples 7 小时前
更多推荐
所有评论(0)