HuggingFace应用——自然语言处理(1):什么是NLP?什么是Transformer?

ops/2024/10/24 19:51:17/
  • 本篇文章属于HuggingFace应用——自然语言处理系列
  • 本篇文章对自然语言处理任务进行基本介绍
  • 参考资料:HuggingFace

    文章目录

    • 1 什么是NLP?
    • 2 主流算法与模型梳理——时间顺序
    • 3 Transformer
      • 3.1 Transformer模型能做什么?
        • Sentiment Analysis 语义分析
        • Zero-shot classification 零样本分类
        • Text generation 文本生成
        • Mask filling 完形填空
        • Named entity recognition 主体识别
        • Question answering 回答问题
        • Summarization 总结
        • Translation 翻译
      • 3.2 Transformer如何工作?——High-level understanding
        • Transformer是语言模型(language model)
        • 迁移学习:Transfer Learning
        • Transformer的结构
          • Attention Layer
          • Transformer模型的原始结构
        • 什么是Architecture?什么是checkpoints?

1 什么是NLP?

  • NLP(自然语言处理)是语言学(linguistic)与机器学习(ML)的交叉学科/应用,核心关注点是:赋予机器对人类语言的理解能力并发展出相应的应用能力
  • 常见的NLP任务与具体应用:
    • 句子分类(Sentence Classification):语义理解、恶意邮件侦查、语句正确性判断、逻辑正确性判断等
    • 词语分类(Word Classification):识别语句中的词语(主语、形容词、动词等)
    • 文本生成(Text Generation):语句补全(ChatGPT),完形填空
    • 总结与提取(Answer Extraction):基于给定的上下文以及问题,获取答案(ChatGPT是通过文本生成解决这个问题的,思路不同)
    • 文本转化(Generating sentence from input sentence):翻译任务、文本总结等
  • 注意:随着技术的快速发展,NLP任务不单单局限于对文本的处理,现在已扩展出多模态NLP任务,例如基于音频输入或视频输入提取出(生成)文本等
  • NLP任务的难点:计算机与人类处理信息的方式不同
    • 语义理解:"I’m happy, thank you"在不同的情况下有很大的差别
    • 语义近似:“I’m happy”, "I feel great"的关系是什么?

2 主流算法与模型梳理——时间顺序

  • 这里对NLP领域的主流算法与模型按照时间线进行简单梳理 —— 感兴趣的可以自行进一步查看与了解
  • Word2Vec (2013)
    • 目标:提出了现在最流行的词嵌入方法,Word2Vector,将单词转换为向量进行表达
    • Paper: “Efficient Estimation of Word Representations in Vector Space”
  • GloVe (2014)
    • 目标:通过从语料库中捕捉全局静态信息来进行词嵌入
    • Paper: “GloVe: Global Vectors for Word Representation”
  • Seq2Seq with Attention (2014)
    • 目标:针对翻译问题,提出了一种带注意力机制的序列到序列建模方法
    • Paper: “Neural Machine Translation by Jointly Learning to Align and Translate”
  • Transformer (2017) ⭐️
    • 目标:提出了一种新的基于注意力机制的序列转化模型(重磅炸弹)
    • Paper: “Attention is All You Need”
  • BERT (Bidirectional Encoder Representations from Transformers) (2018) ⭐️
    • 目标:提出了一种能够捕捉双向上下文信息的语言表示方法
    • Paper: “BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding”
  • GPT (Generative Pre-trained Transformer) (2018)⭐️
    • 目标:提出了一种基于文本生成的适用于各种类型NLP任务的语言生成模型
    • Paper: “Improving Language Understanding by Generative Pre-Training”
  • GPT-2 (2019)
    • 目标:GPT模型的改进版本,能够进行连贯、通顺的文本生成
    • Paper: “Language Models are Unsupervised Multitask Learners”
  • XLNet (2019)
    • 目标:通过将Transformer-XL与自回归预训练相结合来克服BERT的局限性,从而提高语言理解能力
    • Paper: “XLNet: Generalized Autoregressive Pretraining for Language Understanding”
  • RoBERTa (2019)
    • 目标:鲁棒优化版本的BERT,以获得更好的语言建模
    • Paper: “RoBERTa: A Robustly Optimized BERT Pretraining Approach”
  • T5 (Text-to-Text Transfer Transformer) (2019) ⭐️
    • 目标:将NLP任务视为统一的文本到文本问题
    • Paper: “Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer”
  • BART (2019)
    • 目标:提出了一种用于文本生成和摘要的序列到序列模型
    • Paper: “BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension”
  • GPT-3 (2020)⭐️
    • 目标:提出了通过few-shot学习来生成连贯和上下文准确的文本的方法
    • Paper: “Language Models are Few-Shot Learners”
    • 发现:GPT模型规模增大后获得了示例学习的能力(重磅炸弹)
  • 可以看到,其实在GPT-3出来之前,BERT系列的工作还是比较主流的,现在的情况大家也都有目共睹了

3 Transformer

3.1 Transformer模型能做什么?

  • 本章节是对Transformer功能的介绍,会用到Transformers库中的pipeline工具,这里大家不用纠结这个工具具体是如何实现的
    • pipeline定义了端到端的功能路径
    • 选择需要使用的pipeline名称(是HF预定义的)
    • 不同的pipeline支持不同的参数,例如model(用于选择模型)

Sentiment Analysis 语义分析

from transformers import pipelineclassifier = pipeline("sentiment-analysis")
classifier("I've been waiting for a HuggingFace course my whole life.")
  • 输出:[{'label': 'POSITIVE', 'score': 0.9598047137260437}]

Zero-shot classification 零样本分类

from transformers import pipelineclassifier = pipeline("zero-shot-classification")
classifier("This is a course about the Transformers library",candidate_labels=["education", "politics", "business"],
)
  • 输出:
{'sequence': 'This is a course about the Transformers library','labels': ['education', 'business', 'politics'],'scores': [0.8445963859558105, 0.111976258456707, 0.043427448719739914]}

Text generation 文本生成

from transformers import pipelinegenerator = pipeline("text-generation")
generator("In this course, we will teach you how to")
  • 输出:
[{'generated_text': 'In this course, we will teach you how to understand and use ''data flow and data interchange when handling user data. We ''will be working with one or more of the most commonly used ''data flows — data flows of various types, as seen by the ''HTTP'}]

Mask filling 完形填空

from transformers import pipelineunmasker = pipeline("fill-mask")
unmasker("This course will teach you all about <mask> models.", top_k=2)
  • 输出:
[{'sequence': 'This course will teach you all about mathematical models.','score': 0.19619831442832947,'token': 30412,'token_str': ' mathematical'},{'sequence': 'This course will teach you all about computational models.','score': 0.04052725434303284,'token': 38163,'token_str': ' computational'}]

Named entity recognition 主体识别

from transformers import pipelinener = pipeline("ner", grouped_entities=True)
ner("My name is Sylvain and I work at Hugging Face in Brooklyn.")
  • 输出:PER(person), ORG(organization), LOC(location)
[{'entity_group': 'PER', 'score': 0.99816, 'word': 'Sylvain', 'start': 11, 'end': 18}, {'entity_group': 'ORG', 'score': 0.97960, 'word': 'Hugging Face', 'start': 33, 'end': 45}, {'entity_group': 'LOC', 'score': 0.99321, 'word': 'Brooklyn', 'start': 49, 'end': 57}
]

Question answering 回答问题

from transformers import pipelinequestion_answerer = pipeline("question-answering")
question_answerer(question="Where do I work?",context="My name is Sylvain and I work at Hugging Face in Brooklyn",
)
  • 输出:
{'score': 0.6385916471481323, 'start': 33, 'end': 45, 'answer': 'Hugging Face'}

Summarization 总结

from transformers import pipelinesummarizer = pipeline("summarization")
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.
"""
)
  • 输出:
[{'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 .'}]

Translation 翻译

from transformers import pipelinetranslator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en")
translator("Ce cours est produit par Hugging Face.")
  • 输出:
[{'translation_text': 'This course is produced by Hugging Face.'}]

3.2 Transformer如何工作?——High-level understanding

  • 本章节从模型设计的角度对Transformer的工作原理进行解析,更加细粒度的工作与计算流程请查阅其他资料
  • 这里推荐一个好用的Transformer可视化网站——Transformer Visulization
  • 基于Transformer架构衍生出的模型可以分为三类:
    • GPT系列模型:自回归Transformer(auto-regressive - token by token
    • BERT系列模型:自编码Transformer(auto-encoding)
      • 什么是auto-encoding?
        • 与GPT系列模型不同,BERT在进行“生成”时需要同时考虑上下文(context),这被称作基于上下文的表示(Contextual Rrepresentation);此外,BERT使用MLM进行训练(Masked Large Language Model:即对部分token添加mask,模型训练时实际上是在做完形填空),这与autoencoder的行为相似,即重建input(predict)。
        • 自编码器(autoencoder)是一种主要用于无监督学习的人工神经网络。它最初被设计为学习有效的数据编码,通常用于降维、特征学习或数据重建.
    • BART/T5系列:序列-序列Transformer(Seq2Seq,保持了Transformer模型的初始结构)

Transformer是语言模型(language model)

  • Transformer在初始情况下通常基于大语料库进行无监督训练——学习并理解特定的language(当你的训练数据足够丰富时,可以认为Transformer是一个世界语言模型)
  • 这样训练出来的模型随后需要进行Transfer Learning——以将通用语言模型迁移到特定任务中
    • 对于next work prediction/generation任务,常用的训练方法是因故语言模型(causal language modeling)——即模型看到前面的 N N N个words来预测下一个words
    • 对于完形填空任务,常用的训练方法为MLM

迁移学习:Transfer Learning

  • (Pre-training)基础模型的训练是非常耗时耗材耗力的,因此,对于普通组织或个人而言,基于大企业开源的基础模型进行模型微调与迁移是非常重要的(统称为Post-training),这也是HuggingFace存在的意义之一
    • 至于Pre-training和Post-training的技术方案,这本身就是一个非常热点的研究问题,以Post-training为例:
      • Fine-tuning(模型微调):SFT(supervised fine-tuning)、Domain-Specific Fine-Tuning
      • Instruction Tuning(指令微调):训练数据集主要由两部分组成(instruction,response)
      • 人类指导的强化学习(RLHF:Reinforcement Learning with Human Feedback):人类对LLM的输出进行评价(排序)、训练一个RM(reward model)模拟人类的评价标准来对输出进行评价;使用以PPO(Proximal Policy Optimization)等强化学习算法来调整LLM的输出,LLM的目标是最大化RM的奖励
      • Adapter Tuning:adapter本身是NN(小型),通过将adapters添加到pre-trained的模型中,同时在模型训练时,冻结pre-trained模型的参数(Frozen),只训练adapter的参数以实现对LLM的调整
      • LoRA(Low-Rank Adaptation)⭐️:通过在pre-trained模型中添加低秩矩阵实现对LLM的调整
        • Cheap and fast
      • Prompt层面⭐️:在不训练模型的情况下改变模型的输出(现在很多这样的产品)
      • 蒸馏(Distillation):训练一个小模型——目标是模拟大模型的输出
      • Active Learning:先让大模型判断哪些token是他不好理解的,然后人工为这些token打上标签;随后基于这些token对LLM进行训练 —— Deep Active Learning for Named Entity Recognition
      • 基于RL的自对齐(Self-Alignment with RL):一种基于RM的LLM自对齐技术,较新,需要进一步调研 —— Constitutional AI: Harmlessness from AI Feedback
      • 结合上述多种方法对模型进行微调

Transformer的结构

  • 总体而言,Transformer模型包含两个部分:Encoder、Decoder
    • Transformer
  • Encoder与Decoder可以被分开使用:正如上面提到的各种类型的Transformer模型
Attention Layer
  • Attention是Transformer的核心,可以参考这篇论文——“Attention Is All You Need”
  • 核心:Attention层将告诉模型在处理每个单词的表示时,特别注意你传递给它的句子中的某些单词(或多或少地忽略其他单词)
    • 例如:“李四上午上了英语课,他说他很喜欢这门课”,当处理“这”时,它能够关联到“英语”;当处理“他”时,它需要能够关联到“李四”
    • 这在几乎任何自然语言处理任务中都是非常重要的
Transformer模型的原始结构
  • Original Transformer

  • 最初是被设计用于翻译任务

    • Encoder用于编码整段原始输入,Decoder用于生成对应的结果
    • Encoder中的Attention需要关注全部的上下文信息;而Decoder只需要关注前面已经生成的单次的信息

什么是Architecture?什么是checkpoints?

  • Architecture:指模型的骨架——层次的定义和操作
  • Checkpoint:即针对特定Architecture的模型可以载入的模型参数权重值
  • Model:没有确定的含义,有时指Architecture,有时指Checkpoint,有时是两者的结合

本文章到此结束,后续章节将持续更新,如果您觉得本篇文章对你有所帮助,请关注博主,谢谢


http://www.ppmy.cn/ops/128139.html

相关文章

计算广告第三版pdf

需要该书pdf版本的同学点赞&#xff0c;私信我&#xff1a;

Hallo 2:通过单张图像与音频生成1小时4K分辨率人像视频的人工智能技术解析

Hallo 2&#xff1a;通过单张图像与音频生成1小时4K分辨率人像视频的人工智能技术解析 近年来&#xff0c;随着人工智能技术的飞速发展&#xff0c;视频生成领域也在不断突破。特别是在虚拟主播、影视制作等行业中&#xff0c;如何高效地生成高质量人像视频已成为当下的热门话…

Linux命令(ubuntu)安装deb文件

Linux命令&#xff08;ubuntu&#xff09;安装deb文件 在Linux中&#xff0c;.deb 文件是 Debian 和基于 Debian 的系统&#xff08;如 Ubuntu&#xff09;使用的软件包格式。要安装 .deb 文件&#xff0c;您可以使用 dpkg 命令。以下是安装 .deb 文件的步骤&#xff1a; 打开…

Linux:sh脚本

文章目录 1 标头2 执行脚本文件3 数组4 传递参数5 运算符 && 分支语句6 循环语句6.1 for循环6.2 while循环6.3 until循环6.4 case语句 && read输入6.5 循环控制 7 函数8 文件、文件夹、字符串判断 && 示例 1 标头 #!/bin/sh指明了脚本中命令的解释器 …

构建后端为etcd的CoreDNS的容器集群(一)、生成自签名证书

笔者拟使用官方的etcd和CoreDNS容器镜像生成带自签名的分布式DNS容器集群。按计划需做生成自签名证书、部署etcd集群、配置CoreDNS以使用etcd作为后端共三步&#xff0c;本文为第一步。 一、生成自签名证书 1、准备CFSSL工具 官网下载&#xff1a; [rootlocalhost ~]# cd /o…

深入解析 JavaScript 构造函数:特性、用法与原型链

在 JavaScript 中&#xff0c;构造函数是实现面向对象编程的关键工具之一。它与 this 关键字、箭头函数的作用域链以及原型和原型链紧密相关。本文将全面深入地探讨 JavaScript 构造函数的各个方面。 一、构造函数的定义与用法 构造函数是一种特殊的函数&#xff0c;用于创建…

c++ pdf文件提取txt文本示例

最近抽空采用之前封装的接口将pdf文件提取出txt文本&#xff0c;顺利完成&#xff0c;界面如下所示&#xff1a; 提起的效果如下所示&#xff1a; 输出的txt文本内容如下&#xff1a; 下载链接&#xff1a;https://download.csdn.net/download/u011269801/89905548

线性可分支持向量机的原理推导 9-23拉格朗日乘子α的最大化问题 公式解析

本文是将文章《线性可分支持向量机的原理推导》中的公式单独拿出来做一个详细的解析&#xff0c;便于初学者更好的理解。 公式 9-23 是支持向量机&#xff08;SVM&#xff09;优化过程中从最大化问题到对偶问题的关键步骤之一。它将目标函数简化为关于拉格朗日乘子 α \alpha …