Hugging Face 的 Transformers 库快速入门 (一)开箱即用的 pipelines

2023-10-31

注:本系列教程仅供学习使用, 由原作者授权, 均转载自小昇的博客



前言

Transformers 是由 Hugging Face 开发的一个 NLP 包,支持加载目前绝大部分的预训练模型。随着 BERT、GPT 等大规模语言模型的兴起,越来越多的公司和研究者采用 Transformers 库来构建 NLP 应用,因此熟悉 Transformers 库的使用方法很有必要。

注:本系列教程只专注于处理文本,多模态方法请查阅相关文档


开箱即用的 pipelines

Transformers 库将目前的 NLP 任务归纳为几下几类:

  • 文本分类: 例如情感分析、句子对关系判断等;
  • 对文本中的词语进行分类: 例如词性标注 (POS)、命名实体识别 (NER) 等;
  • 文本生成: 例如填充预设的模板 (prompt)、预测文本中被遮掩掉 (masked) 的词语;
  • 从文本中抽取答案: 例如根据给定的问题从一段文本中抽取出对应的答案;
  • 根据输入文本生成新的句子: 例如文本翻译、自动摘要等。

Transformers 库最基础的对象就是 pipeline() 函数,它封装了预训练模型和对应的前处理和后处理环节。我们只需输入文本,就能得到预期的答案。目前常用的 pipelines 有:

  • feature-extraction (获得文本的向量化表示)
  • fill-mask (填充被遮盖的词、片段)
  • ner (命名实体识别)
  • question-answering (自动问答)
  • sentiment-analysis (情感分析)
  • summarization (自动摘要)
  • text-generation (文本生成)
  • translation (机器翻译)
  • zero-shot-classification (零训练样本分类)

下面我们以常见的几个 NLP 任务为例,展示如何调用这些 pipeline 模型。


情感分析

借助情感分析 pipeline,我们只需要输入文本,就可以得到其情感标签(积极/消极)以及对应的概率:

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
result = classifier("I've been waiting for a HuggingFace course my whole life.")
print(result)
results = classifier(
  ["I've been waiting for a HuggingFace course my whole life.", "I hate this so much!"]
)
print(results)
No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english)

[{'label': 'POSITIVE', 'score': 0.9598048329353333}]
[{'label': 'POSITIVE', 'score': 0.9598048329353333}, {'label': 'NEGATIVE', 'score': 0.9994558691978455}]

pipeline 模型会自动完成以下三个步骤:

  1. 将文本预处理为模型可以理解的格式;
  2. 将预处理好的文本送入模型;
  3. 对模型的预测值进行后处理,输出人类可以理解的格式。

pipeline 会自动选择合适的预训练模型来完成任务。例如对于情感分析,默认就会选择微调好的英文情感模型 distilbert-base-uncased-finetuned-sst-2-english

Transformers 库会在创建对象时下载并且缓存模型,只有在首次加载模型时才会下载,后续会直接调用缓存好的模型。


零训练样本分类

零训练样本分类 pipeline 允许我们在不提供任何标注数据的情况下自定义分类标签。

from transformers import pipeline

classifier = pipeline("zero-shot-classification")
result = classifier(
"This is a course about the Transformers library",
candidate_labels=["education", "politics", "business"],
)
print(result)
No model was supplied, defaulted to facebook/bart-large-mnli (https://huggingface.co/facebook/bart-large-mnli)

{'sequence': 'This is a course about the Transformers library', 
 'labels': ['education', 'business', 'politics'], 
 'scores': [0.8445973992347717, 0.11197526752948761, 0.043427325785160065]}

可以看到,pipeline 自动选择了预训练好的 facebook/bart-large-mnli 模型来完成任务。

文本生成

我们首先根据任务需要构建一个模板 (prompt),然后将其送入到模型中来生成后续文本。注意,由于文本生成具有随机性,因此每次运行都会得到不同的结果。

这种模板被称为前缀模板 (Prefix Prompt),了解更多详细信息可以查看《Prompt 方法简介》

from transformers import pipeline

generator = pipeline("text-generation")
results = generator("In this course, we will teach you how to")
print(results)
results = generator(
    "In this course, we will teach you how to",
    num_return_sequences=2,
    max_length=50
) 
print(results)
No model was supplied, defaulted to gpt2 (https://huggingface.co/gpt2)

[{'generated_text': "In this course, we will teach you how to use data and models that can be applied in any real-world, everyday situation. In most cases, the following will work better than other courses I've offered for an undergrad or student. In order"}]
[{'generated_text': 'In this course, we will teach you how to make your own unique game called "Mono" from scratch by doing a game engine, a framework and the entire process starting with your initial project. We are planning to make some basic gameplay scenarios and'}, {'generated_text': 'In this course, we will teach you how to build a modular computer, how to run it on a modern Windows machine, how to install packages, and how to debug and debug systems. We will cover virtualization and virtualization without a programmer,'}]

可以看到,pipeline 自动选择了预训练好的 gpt2 模型来完成任务。我们也可以指定要使用的模型。对于文本生成任务,我们可以在 Model Hub 页面左边选择 Text Generation tag 查询支持的模型。例如,我们在相同的 pipeline 中加载 distilgpt2 模型:

from transformers import pipeline

generator = pipeline("text-generation", model="distilgpt2")
results = generator(
    "In this course, we will teach you how to",
    max_length=30,
    num_return_sequences=2,
)
print(results)
[{'generated_text': 'In this course, we will teach you how to use React in any form, and how to use React without having to worry about your React dependencies because'}, 
 {'generated_text': 'In this course, we will teach you how to use a computer system in order to create a working computer. It will tell you how you can use'}]

还可以通过左边的语言 tag 选择其他语言的模型。例如加载专门用于生成中文古诗的 gpt2-chinese-poem 模型:

from transformers import pipeline

generator = pipeline("text-generation", model="uer/gpt2-chinese-poem")
results = generator(
    "[CLS] 万 叠 春 山 积 雨 晴 ,",
    max_length=40,
    num_return_sequences=2,
)
print(results)
[{'generated_text': '[CLS] 万 叠 春 山 积 雨 晴 , 孤 舟 遥 送 子 陵 行 。 别 情 共 叹 孤 帆 远 , 交 谊 深 怜 一 座 倾 。 白 日 风 波 身 外 幻'}, 
 {'generated_text': '[CLS] 万 叠 春 山 积 雨 晴 , 满 川 烟 草 踏 青 行 。 何 人 唤 起 伤 春 思 , 江 畔 画 船 双 橹 声 。 桃 花 带 雨 弄 晴 光'}]
模型输入与输出中的 CLS 并没有什么意义, 主要是为了与预训练模型中的输入输出格式保持一致 。

遮盖词填充

给定一段部分词语被遮盖掉 (masked) 的文本,使用预训练模型来预测能够填充这些位置的词语。

与前面介绍的文本生成类似,这个任务其实也是先构建模板然后运用模型来完善模板,称为填充模板 (Cloze Prompt)。了解更多详细信息可以查看《Prompt 方法简介》

from transformers import pipeline

unmasker = pipeline("fill-mask")
results = unmasker("This course will teach you all about <mask> models.", top_k=2)
print(results)
No model was supplied, defaulted to distilroberta-base (https://huggingface.co/distilroberta-base)

[{'sequence': 'This course will teach you all about mathematical models.', 
  'score': 0.19619858264923096, 
  'token': 30412, 
  'token_str': ' mathematical'}, 
 {'sequence': 'This course will teach you all about computational models.', 
  'score': 0.04052719101309776, 
  'token': 38163, 
  'token_str': ' computational'}]

可以看到,pipeline 自动选择了预训练好的 distilroberta-base 模型来完成任务。


命名实体识别

命名实体识别 (NER) pipeline 负责从文本中抽取出指定类型的实体,例如人物、地点、组织等等。

from transformers import pipeline

ner = pipeline("ner", grouped_entities=True)
results = ner("My name is Sylvain and I work at Hugging Face in Brooklyn.")
print(results)
No model was supplied, defaulted to dbmdz/bert-large-cased-finetuned-conll03-english (https://huggingface.co/dbmdz/bert-large-cased-finetuned-conll03-english)

[{'entity_group': 'PER', 'score': 0.9981694, 'word': 'Sylvain', 'start': 11, 'end': 18}, 
 {'entity_group': 'ORG', 'score': 0.97960186, 'word': 'Hugging Face', 'start': 33, 'end': 45}, 
 {'entity_group': 'LOC', 'score': 0.99321055, 'word': 'Brooklyn', 'start': 49, 'end': 57}]

可以看到,模型正确地识别出了 Sylvain 是一个人物,Hugging Face 是一个组织,Brooklyn 是一个地名。

这里通过设置参数 grouped_entities=True ,使得 pipeline 自动合并属于同一个实体的多个子词 (token),例如这里将“Hugging”和“Face”合并为一个组织实体,实际上 Sylvain 也进行了子词合并,因为分词器会将 Sylvain 切分为 S ##yl ##va ##in 四个 token。


自动问答

自动问答 pipeline 可以根据给定的上下文回答问题,例如:

from transformers import pipeline

question_answerer = pipeline("question-answering")
answer = question_answerer(
    question="Where do I work?",
    context="My name is Sylvain and I work at Hugging Face in Brooklyn",
)
print(answer)
No model was supplied, defaulted to distilbert-base-cased-distilled-squad (https://huggingface.co/distilbert-base-cased-distilled-squad)

{'score': 0.6949771046638489, 'start': 33, 'end': 45, 'answer': 'Hugging Face'}

可以看到,pipeline 自动选择了在 SQuAD 数据集上训练好的 distilbert-base 模型来完成任务。这里的自动问答 pipeline 实际上是一个抽取式问答模型,即从给定的上下文中抽取答案,而不是生成答案。

根据形式的不同,自动问答 (QA) 系统可以分为三种:

  • 抽取式 QA (extractive QA): 假设答案就包含在文档中,因此直接从文档中抽取答案;
  • 多选 QA (multiple-choice QA): 从多个给定的选项中选择答案,相当于做阅读理解题;
  • 无约束 QA (free-form QA): 直接生成答案文本,并且对答案文本格式没有任何限制。

自动摘要

自动摘要 pipeline 旨在将长文本压缩成短文本,并且还要尽可能保留原文的主要信息,例如:

from transformers import pipeline

summarizer = pipeline("summarization")
results = summarizer(
    """
    America has changed dramatically during recent years. Not only has the number of 
    graduates in traditional engineering disciplines such as mechanical, civil, 
    electrical, chemical, and aeronautical engineering declined, but in most of 
    the premier American universities engineering curricula now concentrate on 
    and encourage largely the study of engineering science. As a result, there 
    are declining offerings in engineering subjects dealing with infrastructure, 
    the environment, and related issues, and greater concentration on high 
    technology subjects, largely supporting increasingly complex scientific 
    developments. While the latter is important, it should not be at the expense 
    of more traditional engineering.

    Rapidly developing economies such as China and India, as well as other 
    industrial countries in Europe and Asia, continue to encourage and advance 
    the teaching of engineering. Both China and India, respectively, graduate 
    six and eight times as many traditional engineers as does the United States. 
    Other industrial countries at minimum maintain their output, while America 
    suffers an increasingly serious decline in the number of engineering graduates 
    and a lack of well-educated engineers.
    """
)
print(results)
No model was supplied, defaulted to sshleifer/distilbart-cnn-12-6 (https://huggingface.co/sshleifer/distilbart-cnn-12-6)

[{'summary_text': ' America has changed dramatically during recent years . The number of engineering graduates in the U.S. has declined in traditional engineering disciplines such as mechanical, civil, electrical, chemical, and aeronautical engineering . Rapidly developing economies such as China and India, as well as other industrial countries in Europe and Asia, continue to encourage and advance engineering .'}]

可以看到,pipeline 自动选择了预训练好的 distilbart-cnn-12-6 模型来完成任务。与文本生成类似,我们也可以通过 max_lengthmin_length 参数来控制返回摘要的长度。


这些 pipeline 背后做了什么?

这些简单易用的 pipeline 模型实际上封装了许多操作,下面我们就来了解一下它们背后究竟做了啥。以第一个情感分析 pipeline 为例,我们运行下面的代码:

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
result = classifier("I've been waiting for a HuggingFace course my whole life.")
print(result)

就会得到结果:

[{'label': 'POSITIVE', 'score': 0.9598048329353333}]

实际上它的背后经过了三个步骤:

  1. 预处理 (preprocessing),将原始文本转换为模型可以接受的输入格式;
  2. 将处理好的输入送入模型;
  3. 对模型的输出进行后处理 (postprocessing),将其转换为人类方便阅读的格式。

在这里插入图片描述

使用分词器进行预处理

因为神经网络模型无法直接处理文本,因此首先需要通过预处理环节将文本转换为模型可以理解的数字。具体地,我们会使用每个模型对应的分词器 (tokenizer) 来进行:

  1. 将输入切分为词语、子词或者符号(例如标点符号),统称为 tokens
  2. 根据模型的词表将每个 token 映射到对应的 token 编号(就是一个数字);
  3. 根据模型的需要,添加一些额外的输入。

我们对输入文本的预处理需要与模型自身预训练时的操作完全一致,只有这样模型才可以正常地工作。注意,每个模型都有特定的预处理操作,如果对要使用的模型不熟悉,可以通过 Model Hub 查询。这里我们使用 AutoTokenizer 类和它的 from_pretrained() 函数,它可以自动根据模型 checkpoint 名称来获取对应的分词器。

情感分析 pipeline 的默认 checkpoint 是 distilbert-base-uncased-finetuned-sst-2-english,下面我们手工下载并调用其分词器:

from transformers import AutoTokenizer

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

raw_inputs = [
    "I've been waiting for a HuggingFace course my whole life.",
    "I hate this so much!",
]
inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors="pt")
print(inputs)
{
    'input_ids': tensor([
        [  101,  1045,  1005,  2310,  2042,  3403,  2005,  1037, 17662, 12172, 2607,  2026,  2878,  2166,  1012,   102],
        [  101,  1045,  5223,  2023,  2061,  2172,   999,   102,     0,     0,
             0,     0,     0,     0,     0,     0]
    ]), 
    'attention_mask': tensor([
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
    ])
}

可以看到,输出中包含两个键 input_idsattention_mask,其中 input_ids 对应分词之后的 tokens 映射到的数字编号列表,而 attention_mask 则是用来标记哪些 tokens 是被填充的(这里“1”表示是原文,“0”表示是填充字符)。

先不要关注 paddingtruncation 这些参数,以及 attention_mask 项,后面我们会详细介绍:)。


将预处理好的输入送入模型

预训练模型的下载方式和分词器 (tokenizer) 类似,Transformers 包提供了一个 AutoModel 类和对应的 from_pretrained() 函数。下面我们手工下载这个 distilbert-base 模型:

from transformers import AutoModel

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

预训练模型的本体只包含基础的 Transformer 模块,对于给定的输入,它会输出一些神经元的值,称为 hidden states 或者特征 (features)。对于 NLP 模型来说,可以理解为是文本的高维语义表示。这些 hidden states 通常会被输入到其他的模型部分(称为 head),以完成特定的任务,例如送入到分类头中完成文本分类任务。

其实前面我们举例的所有 pipelines 都具有类似的模型结构,只是模型的最后一部分会使用不同的 head 以完成对应的任务。
在这里插入图片描述

Transformers 库封装了很多不同的结构,常见的有:

  1. *Model (返回 hidden states)
  2. *ForCausalLM (用于条件语言模型)
  3. *ForMaskedLM (用于遮盖语言模型)
  4. *ForMultipleChoice (用于多选任务)
  5. *ForQuestionAnswering (用于自动问答任务)
  6. *ForSequenceClassification (用于文本分类任务)
  7. *ForTokenClassification (用于 token 分类任务,例如 NER)

Transformer 模块的输出是一个维度为 (Batch size, Sequence length, Hidden size) 的三维张量,其中 Batch size 表示每次输入的样本(文本序列)数量,即每次输入多少个句子,上例中为 2;Sequence length 表示文本序列的长度,即每个句子被分为多少个 token,上例中为 16;Hidden size 表示每一个 token 经过模型编码后的输出向量(语义表示)的维度。

预训练模型编码后的输出向量的维度通常都很大,例如 Bert 模型 base 版本的输出为 768 维,一些大模型的输出维度为 3072 甚至更高。

我们可以打印出这里使用的 distilbert-base 模型的输出维度:

from transformers import AutoTokenizer, AutoModel

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

raw_inputs = [
    "I've been waiting for a HuggingFace course my whole life.",
    "I hate this so much!",
]
inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors="pt")
outputs = model(**inputs)
print(outputs.last_hidden_state.shape)
torch.Size([2, 16, 768])

Transformers 模型的输出格式类似 namedtuple 或字典,可以像上面那样通过属性访问,也可以通过键(outputs["last_hidden_state"]),甚至索引访问(outputs[0])。

对于情感分析任务,很明显我们最后需要使用的是一个文本分类 head。因此,实际上我们不会使用 AutoModel 类,而是使用 AutoModelForSequenceClassification

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)

raw_inputs = [
    "I've been waiting for a HuggingFace course my whole life.",
    "I hate this so much!",
]
inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors="pt")
outputs = model(**inputs)
print(outputs.logits.shape)
torch.Size([2, 2])

可以看到,对于 batch 中的每一个样本,模型都会输出一个两维的向量(每一维对应一个标签,positive 或 negative)。

对模型输出进行后处理

由于模型的输出只是一些数值,因此并不适合人类阅读。例如我们打印出上面例子的输出:

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)

raw_inputs = [
    "I've been waiting for a HuggingFace course my whole life.",
    "I hate this so much!",
]
inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors="pt")
outputs = model(**inputs)
print(outputs.logits)
tensor([[-1.5607,  1.6123],
        [ 4.1692, -3.3464]], grad_fn=<AddmmBackward0>)

模型对第一个句子输出 [−1.5607,1.6123],对第二个句子输出 [4.1692,−3.3464],它们并不是概率值,而是模型最后一层输出的 logits 值。要将他们转换为概率值,还需要让它们经过一个 SoftMax 层,例如:

import torch
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
print(predictions)
tensor([[4.0195e-02, 9.5980e-01],
        [9.9946e-01, 5.4418e-04]], grad_fn=<SoftmaxBackward0>)

所有 Transformers 模型都会输出 logits 值,因为训练时的损失函数通常会自动结合激活函数(例如 SoftMax)与实际的损失函数(例如交叉熵 cross entropy)。

这样模型的预测结果就是容易理解的概率值:第一个句子 [0.0402,0.9598],第二个句子 [0.9995,0.0005]。最后,为了得到对应的标签,可以读取模型 config 中提供的 id2label 属性:

print(model.config.id2label)
{0: 'NEGATIVE', 1: 'POSITIVE'}

于是我们可以得到最终的预测结果:

  1. 第一个句子: NEGATIVE: 0.0402, POSITIVE: 0.9598
  2. 第二个句子: NEGATIVE: 0.9995, POSITIVE: 0.0005

总结

在本文中我们初步介绍了如何使用 Transformers 包提供的 pipeline 对象来处理各种 NLP 任务,并且对 pipeline 背后的工作原理进行了简单的说明。在下一篇转载文章中,我们会具体介绍组成 pipeline 的两个重要组件模型Models 类)和分词器Tokenizers 类)的参数以及使用方式。

参考资料:

小昇的博客
Transformers 官方文档
HuggingFace 在线教程
小昇的Github项目

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Hugging Face 的 Transformers 库快速入门 (一)开箱即用的 pipelines 的相关文章

随机推荐

  • 方差、协方差、期望、相关系数等概念集合

    首先说明一下 本文是本人在复习方差等相关知识的过程中 通过网络上的相关讲解 进行个人总结后得到的 并非个人原创 在此发布只是为了作为一个学习记录与大家分享 1 期望 试验中可能出现的值及其概率的乘积 即是数学期望 1 离散型 离散型随机变量
  • git add 命令详解

    1 前言 2 git add 基本操作 3 git add 命令参数 4 git add 背后做了什么 1 前言 众所周知 git 中有工作区 暂存区 版本库三大组成部分 工作区 电脑中能看到的目录 也就是写代码的地方 暂存区 英文叫 st
  • vue3中通过自定义指令,实现点击空白处触发事件,点击非自身dom触发事件

    我们经常在开发过程中 会遇到这些问题 怎么实现点击空白处关闭指定盒子 怎么实现点击空白处收起下拉框 即 怎么触发点击空白处事件 怎么触发 点击非自身dom而触发的事件 在vue3当中 使用自定义指令解决这个问题 在utils directi
  • 开启network-manager.service

    ubuntu20 04 本身系统会默认开机自动连接网络服务 但是我之前自己设置关闭了 所以现在要手动打开使用一下命令 先进入root xxz sudo systemctl start network manager service 回车执行
  • 《一》HI3518E视频处理基础知识----- 系统控制mpp

    目录 一 MPP的概述 1 视频方面 2 音频方面 3 MPP所处层次框架图 二 mpp处理平台架构 三 视频缓存池 1 视频缓冲池 VB 2 要点 3 相关的数据结构和API 1 VB CONF S 2 HI MPI VB SetConf
  • 家谱(特殊的层级人物关系)数据结构与自动排版算法的一种实现

    github源代码 家谱海本地私有版 https github com fengchangfight familytreesea 出处 http www fengchang cc post 24 家谱的数据结构并不复杂 逻辑上可以抽象成一种
  • BES2300x笔记(28) -- 左右耳同时按下的骚操作

    哈喽大家好 这是该系列博文的第二十八篇 篇 lt lt 系列博文索引 快速通道 gt gt 一 前言 市面上的TWS耳机 一般中高端耳机都会有触摸按键和入耳检测功能 使用触摸按键更方便外观和防水处理 但同时也限制了UI交互方式 有限的交互方
  • eclipse快速打开和闭合函数方法代码块的快捷方式

    ctrl shift 小键盘 收起和ctrl shift 小键盘 闭合
  • 基于深度学习的变化检测算法实现

    我是研究生期间研究主要研究SAR影像的变化检测 这是一段简单的基于深度学习的变化检测方法 以CNN实现 首先说下基于深度学习的变化检测任务的思路 制作训练样本 gt 训练模型 gt 使用训练的模型遍历图片中每个像元得出结果 1 筛选训练样本
  • spring.ftl

    lt ftl strip whitespace true gt lt spring ftl This file consists of a collection of FreeMarker macros aimed at easing so
  • css中垂直对齐常用的几种方法

    一 行高 line height 法 如果要垂直居中的只有一行或几个文字 那它的制作最为简单 只要让文字的行高和容器的高度相同即可 比如 p height 30px line height 30px width 100px overflow
  • Angularjs理解二

    1 dom加载完毕 找寻ng app 先从上到下找相关的指令 然后分两阶段执行 先找到所有的指令 完成编译 得到一个个链接函数 最后在链接到一个个controller上 还是边编译边链接 先执行 injector invoke rootSc
  • 光耦PC817

    光耦一共4个引脚 两个输入 两个输出 输入接5v和gnd 5v接时加100欧姆电阻 输出不大于35v电压 这时输出端通路 只是通路 不是短路 转载于 https www cnblogs com judes p 5686414 html
  • PCA主成分分析(入门计算+深入解析)(一)

    PCA主成分分析 入门 深入 最大方差理论 几何意义 Principal components analysis 转载请注明 云南省高校数据化运营管理工程研究中心博客http blog csdn net m0 37788308 articl
  • Python解决相对路径问题

    学习Python中 一直被相对路径困扰 只能使用绝对路径 解决方法 加上下面代码 import os sys os chdir sys path 0 这个问题到现在也没有搞清楚 因为在命令行直接敲命令运行py文件可以直接使用相对路径 而我在
  • 浅谈三目运算符(c++)

    目录 什么是三目运算符 三目运算符怎么用 基本用法 进阶用法 注意事项 相关题目 B2035 判断数正负 B2037 奇偶数判断 B2052 简单计算器 什么是三目运算符 三目运算符是分支结构中的一种运算 根据不同的条件 执行不同的操作并返
  • win10家庭版解决无法进入本地组策略编辑器问题

    win10家庭版无法进入本地组策略编辑器 现象 运行框中输入 gpedit msc 提示 Windows找不到文件gpedit msc 解决办法 一 桌面新建一个文本文档 输入以下代码后保存文档 echo off pushd dp0 dir
  • 【华为OD机试真题 Python】最优高铁城市修建方案(200分)

    前言 本专栏将持续更新互联网大厂机试真题 并进行详细的分析与解答 包含完整的代码实现 希望可以帮助到正在努力的你 关于大厂机试流程 面经 面试指导等 如有任何疑问 欢迎联系我 wechat steven moda email nansun0
  • python读取Java配置文件properties配置文件

    python没有专门处理properties格式的包 只有处理标准的ini格式的包 所以需要自己写一个python程序来处理 class Properties object def init self fileName self fileN
  • Hugging Face 的 Transformers 库快速入门 (一)开箱即用的 pipelines

    注 本系列教程仅供学习使用 由原作者授权 均转载自小昇的博客 文章目录 前言 开箱即用的 pipelines 情感分析 零训练样本分类 文本生成 遮盖词填充 命名实体识别 自动问答 自动摘要 这些 pipeline 背后做了什么 使用分词器