大家好,我是java1234_小锋老师,最近更新《AI大模型应用开发入门-拥抱Hugging Face与Transformers生态》专辑,感谢大家支持。

本课程主要介绍和讲解Hugging Face和Transformers,包括加载预训练模型,自定义数据集,模型推理,模型微调,模型性能评估等。是AI大模型应用开发的入门必备知识。

使用AutoModel自动模型方式调用Bert预训练模

AutoModel 是一个用来自动加载各种预训练 Transformer 模型的类。你可以使用它直接从 Hugging Face 模型库中加载模型,而无须关心具体的模型实现。主要用于构建和微调模型,以便在特定的任务上进行更深入的开发。使用 AutoModel 通常需要更细粒度的操作,比如输入数据的处理、模型的前向传播、输出的处理等。更适合需要模型架构和输入输出控制的用户,如研究人员或需要定制模型的开发者。

AutoModel 还是 pipeline 对比

特性 AutoModel pipeline
使用复杂性 较复杂,需要手动处理各个步骤 简单,自动处理所有步骤
适用场景 适合需要灵活性和微调能力的开发 适合快速应用于特定任务
示例代码 model = AutoModel.from_pretrained("xxx") classifier = pipeline("task")
输出处理 需要手动处理模型输出 自动处理输出
适用人群 研究人员、开发者等 快速原型或希望简化流程的用户

具体示例:

import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification


def test_classfication():
    # 加载分词器
    tokenizer = AutoTokenizer.from_pretrained('./Bert-base-chinese')

    # 加载模型
    model = AutoModelForSequenceClassification.from_pretrained('./Bert-base-chinese')
    print(model)

    # 准备输入数据
    input_ids = tokenizer.encode(
        text='今天天气不错',  # 输入文本
        return_tensors='pt',  # 返回PyTorch张量
        padding="max_length",  # 填充到最大长度
        truncation=True,  # 截断超出长度的输入
        max_length=10  # 输入最大长度
    )
    print(input_ids, input_ids.shape)

    # 评估模式
    model.eval()

    # 模型预测
    output = model(input_ids)

    # 5. 获取预测结果
    logits = output.logits
    prediction = torch.argmax(logits, dim=-1)
    print(logits, prediction)
    sentiment = prediction.item()  # 转换为 Python 整数
    sentiment_label = "积极" if sentiment == 1 else "消极" if sentiment == 0 else "中性"
    print(sentiment_label)


if __name__ == '__main__':
    test_classfication()

运行结果:

BertForSequenceClassification(
  (bert): BertModel(
    (embeddings): BertEmbeddings(
      (word_embeddings): Embedding(21128, 768, padding_idx=0)
      (position_embeddings): Embedding(512, 768)
      (token_type_embeddings): Embedding(2, 768)
      (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
      (dropout): Dropout(p=0.1, inplace=False)
    )
    (encoder): BertEncoder(
      (layer): ModuleList(
        (0-11): 12 x BertLayer(
          (attention): BertAttention(
            (self): BertSdpaSelfAttention(
              (query): Linear(in_features=768, out_features=768, bias=True)
              (key): Linear(in_features=768, out_features=768, bias=True)
              (value): Linear(in_features=768, out_features=768, bias=True)
              (dropout): Dropout(p=0.1, inplace=False)
            )
            (output): BertSelfOutput(
              (dense): Linear(in_features=768, out_features=768, bias=True)
              (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
              (dropout): Dropout(p=0.1, inplace=False)
            )
          )
          (intermediate): BertIntermediate(
            (dense): Linear(in_features=768, out_features=3072, bias=True)
            (intermediate_act_fn): GELUActivation()
          )
          (output): BertOutput(
            (dense): Linear(in_features=3072, out_features=768, bias=True)
            (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
            (dropout): Dropout(p=0.1, inplace=False)
          )
        )
      )
    )
    (pooler): BertPooler(
      (dense): Linear(in_features=768, out_features=768, bias=True)
      (activation): Tanh()
    )
  )
  (dropout): Dropout(p=0.1, inplace=False)
  (classifier): Linear(in_features=768, out_features=2, bias=True)
)
tensor([[ 101,  791, 1921, 1921, 3698,  679, 7231,  102,    0,    0]]) torch.Size([1, 10])
tensor([[-0.2556,  0.3660]], grad_fn=<AddmmBackward0>) tensor([1])
积极

AutoTokenizer 负责将生文本转换为模型可以理解的输入格式。这一过程通常涉及分词、编码等步骤。

以下介绍 AutoTokenizer.encode 方法的重要参数:

AutoTokenizer.encode 方法参数

  1. text (str or List[str])

    • 要编码的输入文本,可以是单个字符串或字符串列表。如果是列表,AutoTokenizer 将对每个字符串进行编码。

  2. text_pair (str or List[str], optional)

    • 如果您想要处理文本对(例如,问答任务),可以使用此参数。它的值也可以是单个字符串或字符串列表,表示要与 text 一同编码的第二个文本。

  3. max_length (int, optional)

    • 限制返回的序列最大长度。如果输入文本的编码长度超过此参数,将会被截断。如果没有设置,默认长度由模型的最大输入长度决定。

  4. padding (bool or str, optional)

    • 控制返回的序列是否进行填充。可以设置为 True,让所有返回的序列填充到同一最大长度,或者为 'max_length',使所有序列填充到 max_length 指定的长度。默认值通常是 False

  5. truncation (bool or str, optional)

    • 控制是否截断输入序列。通常可以设置为 True,以截断到最大长度,也可以设置为 'longest_first''do_not_truncate' 等选项。默认值通常是 False

  6. return_tensors (str, optional)

    • 如果设置,返回的张量将被转换为指定框架的张量格式,例如 'pt'(PyTorch)或者 'tf'(TensorFlow)。如果不设置,返回的是普通的 Python 列表。

  7. return_attention_mask (bool, optional)

    • 是否返回注意力掩码。注意力掩码通常用于指示模型在处理输入序列时,应该关注哪些位置。

  8. return_token_type_ids (bool, optional)

    • 是否返回 token 类型 ID 值,在一些任务(如问答)中可能会用到,它帮助模型区分不同的文本对。

  9. add_special_tokens (bool, optional)

    • 控制是否在输入文本前后添加特殊的标记(如 [CLS][SEP]),这些标记在 BERT 和其他一些模型中是必需的,帮助模型理解输入的结构。

  10. encoding (str, optional)

    • 指定字符编码类型,例如 "utf-8"。这通常在处理文本输入时很有用。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐