普通视图

发现新文章,点击刷新页面。
昨天以前首页

大模型 RAG 基础:信息检索、文本向量化及 BGE-M3 embedding 实践(2024)

2024年8月4日 08:00

本文整理一些文本向量化(embedding)和信息检索的知识,它们是如今大模型生成文本时常用的技术 —— “增强检索生成”(RAG)—— 的基础:

Fig. Similarity score based on BERT embedding. Image source

水平及维护精力所限,文中不免存在错误或过时之处,请酌情参考。 传播知识,尊重劳动,年满十八周岁,转载请注明出处



RAG (Retrieval-Augmented Generation,检索增强生成),是一种利用信息检索(Information Retrieval) 技术增强大模型生成效果(generation)的技术。RAG 在步骤上很简单,

  1. 搭建高质量文档数据库
    • 对优质文档进行某种格式的转换(或称编码),例如基于 BERT 将文本段落转换成 数值格式的向量(这个过程称为 embedding),然后
    • 将这些 embeddings 存储到合适的数据库(例如 ES 或向量数据库);
  2. 针对用户输入进行数据库检索
    • 对用户输入的 query 进行相同的转换(embedding),然后
    • 利用最近邻等相似性算法,在文档库中寻找最相似的文本段落(与给定问题最相关的段落);
  3. 大模型生成返回给用户的内容
    • 将找到文本段落送到大模型,辅助生成最终的输出文本,返回给用户。

本文主要关注以上 1 & 2 步骤中的 embedding & retrieval 阶段。

1 信息检索(information retrieval)技术三大发展阶段

信息检索的技术发展大致可分为三个阶段:

  1. 基于统计信息关键字匹配(statistical keyword matching)

    • 是一种 sparse embedding —— embedding 向量的大部分字段都是 0;
  2. 基于深度学习模型的上下文和语义理解

    • 属于 dense embedding —— embedding 向量的大部分字段都非零;
  3. 所谓的“学习型”表示,组合上面两种的优点,称为 learned sparse embedding

    • 既有深度学习模型的上下文和语义理解能力;
    • 又具备稀疏表示的可解释性(interpretability of sparse representations)和低计算复杂度。

下面分别来看。

1.1 基于统计信息和关键词匹配(1970s-2010s

1.1.1 典型算法:TF-IDFBM25

早期信息检索系统主要是基于统计信息 + 匹配关键词,算法包括,

  • TF-IDF (term frequency - inverse document frequency), 1970s
  • BM25 (Best Matching), 1980s

1.1.2 原理

分析语料库的词频和分布(term frequency and distribution), 作为评估文档的相关性(document relevance)的基础。

1.1.3 优缺点

  • 优点:方法简单,效果不错,所以使用很广泛。
  • 缺点:单纯根据词频等统计和关键字检索做判断,不理解语义。

1.2 基于深度学习和上下文语义

1.2.1 Word2Vec (Google, 2013)

2013 年,谷歌提出了 Word2Vec

  • 首次尝试使用高维向量来表示单词,能分辨它们细微的语义差别;
  • 标志着向机器学习驱动的信息检索的转变。

1.2.2 BERT (Google, 2019)

基于 transformer 的预训练(pretrain)语言模型 BERT 的出现,彻底颠覆了传统的信息检索范式。

核心设计和优点

  1. transformer 的核心是 self-attention,
    • self-attention 能量化给定单词与句子中其他单词的关联性程度
    • 换句话说就是:能在上下文中分辨单词的含义;
  2. BERT 是双向(前向+后向)transformer,
    • 可以理解为在预训练时,每个句子正向读一遍,反向再读一遍;
    • 能更好地捕获句子的上下文语义(contextual semantics);
    • 最终输出是一个 dense vector,本质上是对语义的压缩;
  3. 基于 dense vector 描述,用最近邻算法就能对给定的 query 进行检索,强大且语义准确。

局限性:领域外(Out-of-Domain)信息检索效果差

BERT 严重依赖预训练数据集的领域知识(domain-specific knowledge), 预训练过程使 BERT 偏向于预训练数据的特征, 因此在领域外(Out-Of-Domain),例如没有见过的文本片段,表现就不行了。

解决方式之一是fine-tune(精调/微调),但成本相对较高, 因为准备高质量数据集的成本是很高的。

另一方面,尽管传统 sparse embedding 在词汇不匹配问题时虽然也存在挑战, 但在领域外信息检索中,它们的表现却优于 BERT。 这是因为在这类算法中,未识别的术语不是靠“学习”,而是单纯靠“匹配”。

1.3 学习型:组合前两种的优点

1.3.1 原理:传统 sparse vector 与上下文化信息的融合

  1. 先通过 BERT 等深度学习模型生成 dense embedding;
  2. 再引入额外的步骤对以上 dense embedding 进行稀疏化,得到一个 sparse embedding;

代表算法:BGE-M3。

1.3.2 与传统 sparse embedding 的区别

根据以上描述,乍一看,这种 learned sparse embedding 与传统 sparse embedding 好像没太大区别, 但实际上二者有着本质不同,这种 embedding,

  • 引入了 Token Importance Estimation;
  • 既保留了关键词搜索能力,又利用上下文信息,丰富了 embedding 的稀疏表示;
  • 能够辨别相邻或相关的 token 的重要性,即使这些 token 在文本中没有明确出现。

1.3.3 优点

  • 将稀疏表示与学习上下文结合,同时具备精确匹配和语义理解两大能力,在领域外场景有很强的泛化能力;
  • 与 dense embedding 相比更简洁,只保留了最核心的文本信息;
  • 固有的稀疏性使向量相似性搜索所需的计算资源极少;
  • 术语匹配特性还增强了可解释性,能够更精确地洞察底层的检索过程,提高了系统的透明度。

2 信息检索:三种 embedding 的对比

简单来说, vector embedding,或称向量表示,是一个单词或句子在高维向量空间中的数值表示

  • 高维空间:一个维度能代表一个特征或属性,高维意味着分辨率高,能区分细微的语义差异;
  • 数值表示:一个 embedding 一般就是一个浮点数数组,所以方便计算。

对应上一节介绍的三个主要发展阶段,常见的有三种 embedding 类型:

  1. traditional sparse embedding
  2. dense embedding
  3. learned sparse embedding

2.1 Sparse embedding (lexical matching)

  • 映射成一个高维(维度一般就是 vocabulary 空间大小)向量
  • 向量的大部分元素都是 0,非零值表明 token 在特定文档中的相对重要性,只为那些输入文本中出现过的 token 计算权重
  • 典型模型:BM25(对 TF-IDF 的改进)

非常适合关键词匹配任务(keyword-matching tasks)。

2.2 Dense embedding (e.g. BERT-based)

  • 映射到一个(相对低维)向量,所有维度都非零
  • 相比 sparse embedding 维度要低很多,例如基于 BERT 默认 1x768 维度;
  • 典型模型:BGE-v1.5

所有维度都非零,包含语义理解,信息非常丰富,因此适用于 语义搜索任务(semantic search tasks)。

Multi-vector retrieval

  • 用多个向量表示一段文本,可以看做是对 dense retrieval 的一种扩展
  • 模型:ColBERT

2.3 Learned sparse embedding

结合了传统 sparse embedding 的精确度和 dense embedding 的语义丰富性,

  • 可以通过深度学习模型“学习”相关 token 的重要性,即使是一些并未出现过的 token,
  • 生成的“学习型”稀疏表示,能有效捕捉 query 和 doc 中的关键词。

3 Embedding & retrieval 工作原理详解

这里主要介绍 BGE-M3 模型的原理。BGE-M3 建立在 BERT 之上,因此需要先回顾 BERT 的基本原理。

3.1 BERT 是如何工作的

3.1.1 理论基础

3.1.2 BERT dense embedding 工作流

以输入 "Milvus is a vector database built for scalable similarity search" 为例,工作过程 [2]:

Fig. BERT dense embedding.

  1. Tokenization
    1. 将输入文本转成 token 序列
    2. BERT 还会插入两个特殊的 token:[CLS] token 表示开始,[SEP] token 表示一个句子的结束。
  2. Embedding:使用 embedding matrix 将每个 token 转换为一个向量,详见 BERT 论文;
  3. Encoding:这些向量通过多层 encoder,每层由 self-attention 和 feed-forward 神经网络组成
    1. 会根据所有其他 token 提供的上下文细化每个 token 的表示。
  4. Output:输出一系列最终的 embedding vectors

最终生成的 dense embedding 能够捕捉单个单词的含义及其在句子中的相互关系。

理解 BERT 是如何生成 dense embedding 之后,接下来看看基于 BERT dense embedding 的信息检索是如何工作的。

3.2 基于 BERT dense embedding 的文档检索是如何工作的

有了 dense embedding 之后,针对给定文本输入检索文档就很简单了,只需要再加一个最近邻之类的算法就行。

下面是两个句子的相似度判断,原理跟文档检索是一样的:

Fig. Similarity score based on BERT embedding. Image source

下面看个具体的 embedding & retrieval 模型:BGE-M3。

3.3 BGE-M3(BERT-based learned sparse embedding)是如何工作的?

BGE 是一系列 embedding 模型,扩展了 BERT 的能力。BGE-M3 是目前最新的一个,3 个 M 是强调的多个 multi- 能力:

  • Multi-Functionality
  • Multi-Linguisticity
  • Multi-Granularity

3.3.1 设计 & 特点

BGE-M3 通过更精细的方法来捕捉每个 token 的重要性,

  1. Token importance estimation:BERT 在分类/相似性比较时仅关注第一个 token([CLS]), BGE-M3 则扩大到关注序列中的每个 token Hi
  2. 线性变换:在 encoder 的输出层上又增加一个线性层,计算每个 token 的 importance weights Wlex
  3. 激活函数:
    • WlexHi 的乘积经过 Rectified Linear Unit (ReLU) 激活函数,得到每个 token 的术语权重 Wt
    • ReLU 的结果是非负的,有助于 embedding 的稀疏性。
  4. learned sparse embedding:以上输出的是一个 sparse embedding,其中每个 token 都有一个相关的 weights,表明在整个输入文本上下文中的重要性。

下面看个例子。

3.3.2 BGE-M3 生成 learned sparse embedding 的过程

还是前面例子提到的输入,

  1. 先走 BERT dense embedding 的流程,
  2. 最后加一个 linear 层,得到 learned sparse embedding。

Fig. BGE-M3 learned sparse embedding. Image source

In M3-Embedding, the [CLS] embedding is used for dense retrieval, while embeddings from other tokens are used for sparse retrieval and multi-vector retrieval [3].

4 BGE-M3 实战

4.1 相似度判断(检索)

$ pip install FlagEmbedding peft sentencepiece

来自官方的代码,稍作修改:

from FlagEmbedding import BGEM3FlagModel

model = BGEM3FlagModel('/root/bge-m3', use_fp16=True)

queries = ["What is BGE M3?",
           "Defination of BM25"]
docs = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
        "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]

query_embeddings = model.encode(queries, batch_size=12, max_length=8192,)['dense_vecs']
docs_embeddings  = model.encode(docs)['dense_vecs']
similarity = query_embeddings @ docs_embeddings.T
print(similarity)

这个例子是两个问题,分别去匹配两个答案,看彼此之间的相似度(四种组合),运行结果:

[[0.626  0.348 ]
 [0.3499 0.678 ]]
  • 问题 1 和答案 1 相似度是 0.6265
  • 问题 2 和答案 2 相似度是 0.678
  • 问题 1 和答案 2,以及问题 2 和答案 1,相似度只有 0.3x

符合预期。

4.2 精调(fine-tune)

精调的目的是让正样本和负样本的分数差变大。

4.2.1 官方文档

  1. fine-tune the dense embedding
  2. fine-tune all embedding function of m3 (dense, sparse and colbert)

4.2.2 训练数据格式及要求

  1. 文件为 jsonl 格式,每行一个 sample;
  2. 每个 sample 的格式:{"query": str, "pos": List[str], "neg":List[str]}
    • query:用户问题;
    • pos:正样本列表,简单说就是期望给到用户的回答;不能为空,也就是说必需得有正样本;
    • neg:负样本列表,是避免给到用户的回答。
      • 空要写成 "neg": [""],写 "neg": [] 会报错。
      • 另外为空时试过删掉 "neg": [] 也不行,必须得留着这个字段。

注意:

  1. 不是标准 json 格式,所以 python 直接导出一个 json 文件作为训练数据集是不行的。
  2. sample 不能分行,一个 sample 一行。

4.2.3 精调命令及参数配置

从 huggingface 或国内的 modelscope 下载 BGE-M3 模型,

$ git lfs install
$ git clone https://www.modelscope.cn/Xorbits/bge-m3.git

精调命令:

$ cat sft.sh
#!/bin/bash

num_gpus=1
output_dir=/root/bge-sft-output
model_path=/root/bge-m3
train_data=/data/share/bge-dataset
batch_size=2
query_max_len=128    # max 8192
passage_max_len=1024 # max 8192

torchrun --nproc_per_node $num_gpus \
    -m FlagEmbedding.BGE_M3.run \
    --output_dir $output_dir \
    --model_name_or_path $model_path \
    --train_data $train_data \
    --learning_rate 1e-5 \
    --fp16 \
    --num_train_epochs 5 \
    --per_device_train_batch_size $batch_size \
    --dataloader_drop_last True \
    --normlized True \
    --temperature 0.02 \
    --query_max_len $query_max_len \
    --passage_max_len $passage_max_len \
    --train_group_size 2 \
    --negatives_cross_device \
    --logging_steps 10 \
    --same_task_within_batch True \
    --save_steps 10000 \
    --unified_finetuning True \
    --use_self_distill True

几个参数要特别注意下:

  1. query & doc 最大长度

    • query_max_len:支持的最长 query,最大 8192
    • passage_max_len:支持的最长文档(一条 pos 或 neg 记录)长度,最大 8192

    BGE-M3 会分别针对 query 和 doc 初始化两个 tokenizer,以上两个参数其实对应 tokenizer 的 max_length,而 tokenizer 最大支持 8192(见模型目录 tokenizer_config.json)。

  2. batch_size:并行度,直接决定了显存占用大小和精调快慢;
    • BGE-M3 跑起来之后显存占用是恒定的,所以可以多试几个 batch size 配置,把显存用到最大;
  3. save_steps:多少个 step 保存一次 checkpoint,默认值 500 太小,每个 checkpoint ~7GB,多了之后可能会打爆磁盘导致任务失败。

精调快慢取决于 GPU 算力、显存和参数配置,精调开始之后也会打印出预估的完成时间,还是比较准的。

4.2.4 测试精调之后的效果

还是用 4.1 的代码,稍微改一下,不要把 queries 和 docs 作为列表,而是针对每个 query 和 pos/neg 计算相似度得分。 然后针对测试集跑一下,看相似性分数是否有提升。

数据集质量可以的话,精调之后区分度肯定有提升。

4.3 CPU 运行速度优化:将模型转 onnx 格式

如果是在 CPU 上跑模型(不用 GPU), 根据之前实际的 BERT 工程经验,转成 onnx 之后能快几倍,尤其是在 Intel CPU 上 (Intel 公司做了很多优化合并到社区库了)。

但 BGE-M3 官方没有转 onnx 文档,根据第三方的库能成功(稍微改点代码,从本地加载模型),效果待验证。

5 rerank 增强:对 BGE-M3 的检索结果进行重排序

5.1 rerank/reranker 是什么?

rerank 的意思是“重新排序” —— 对 embedding model 检索得到的多个结果(对应多个分数), 重新计算它们的相似性分数,给出一个排名。这是一个可选模块, 用于对检索结果进行增强,把相似度最高的结果返回给用户。

5.1.1 另一种相似度模型

reranker 也是一类计算相似度的模型,例如这个列表 里的都是 rerank/reranker 模型,

  1. bge-reranker-v2-m3:与 bge-m3 配套的 reranker
  2. bge-reranker-v2-gemma:与 google gemma-2b 配套的 reranker

但它们的原理与 BGE-M3 这种 embedding model 有差异。

5.1.2 与 BGE-M3 等模型的差异:cross-encoder vs. bi-encoder

以两个句子的相似度检测为例,

Fig. bi-encoder embedding model vs. cross-encoder model. Image source

  • BGE-M3 属于左边那种,所谓的 bi-encoder embedding model, 简单说就是两个句子分别输入模型,得到各自的 embedding, 然后根据 embedding vector 计算相似度;
  • reranker 属于右边那种,所谓的 cross-encoder model,直接得到结果; 如果对 BERT 的工作原理比较熟悉(见 BERT paper),就会明白这其实就是 BERT 判别两个句子 (next sentense prediction, NSP)任务的延伸。

5.2 embedding 和 reranker 工作流

  1. 用户输入 query 和 doc 列表 doc1/doc2/doc3/...
  2. BGE-M3 计算相似分,返回 topN,例如 [{doc1, score1}, {doc2, score2}, {doc3, score3}],其中 score1 >= score2 >= score3
  3. reranker 接受 query 和 BGE-M3 的结果,用自己的模型重新计算 querydoc1/doc2/doc3 的相似度分数。

5.3 BGE-M3 得到相似分之后,为什么要通过 reranker 再计算一遍?

这里可能有个疑问:step 2 不是已经检索出最相关的 N 个 doc 了吗? 为什么又要进入 step3,用另外一个完全不同的模型(reranker)再计算一种相似分呢?

简单来说,embdding 和 rerank 都是 NLP 中理解给定的两个句子(或文本片段)的关系的编码技术。 再参考刚才的图,

Fig. bi-encoder embedding model vs. cross-encoder model. Image source

  • bi-encoder
    • 分别对两个句子进行编码,得到两个独立的 embedding,再计算相似度。
    • 速度快,准确性相对低。
  • cross-encoder

    • 同时对两个句子编码,输出一个相似度分数;也可以换句话说,把两个句子合成一个句子编码,所以两个句子是彼此依赖的
    • 速度慢,准确性高

总结起来:embedding model 计算的相似度是粗粒度的,只能算粗排; reranker 对 embedding model 得到的若干结果再进行细排; 要体会和理解这种差异,还是要看基础 paper BERT:预训练深度双向 Transformers 做语言理解(Google,2019)

6 总结

本文整理了一些 BGE-M3 相关的 RAG 知识。前两篇参考资料非常好,本文很多内容都来自它们,感谢作者。

参考资料

  1. Enhancing Information Retrieval with Sparse Embeddings, zilliz.com/learn, 2024
  2. Exploring BGE-M3 and Splade: Two Machine Learning Models for Generating Sparse Embeddings, medium.com/@zilliz_learn, 2024
  3. BGE-M3 paper
  4. Cross encoders and bi-encoders, medium.com, 2024

Written by Human, Not by AI Written by Human, Not by AI

[译] 什么是 GPT?Transformer 工作原理的动画展示(2024)

2024年5月12日 08:00

译者序

本文翻译自 2024 年的一个视频(前半部分),这是原作者 Deep Learning 系列的第 5 章,强烈推荐原视频:

Transformer 预测下一个单词四部曲。MLP 也称为 feed-forward。

作者以深厚的技术积累,将一些复杂系统以可视化的方式讲给普通人,这种能力是极其难得的。 本译文希望通过“文字+动图”这种可视化又方便随时停下来思考的方式介绍 Transformer 的内部工作原理。 如果想进一步从技术和实现上了解 Transformer/GPT/LLM,可参考:

水平及维护精力所限,译文不免存在错误或过时之处,如有疑问,请查阅原视频。 传播知识,尊重劳动,年满十八周岁,转载请注明出处

以下是译文。



1 图解 “Generative Pre-trained Transformer”(GPT)

GPT 是 Generative Pre-trained Transformer 的缩写,直译为“生成式预训练 transformer”, 我们先从字面上解释一下它们分别是什么意思。

1.1 Generative:生成式

“Generative”(生成式)意思很直白,就是给定一段输入(例如,最常见的文本输入), 模型就能续写(“编”)下去。

1.1.1 可视化

下面是个例子,给定 “The most effective way to learn computer science is” 作为输入, 模型就开始续写后面的内容了。

“Generative”:生成(续写)文本的能力。

1.1.2 生成式 vs. 判别式(译注)

文本续写这种生成式模型,区别于 BERT 那种判别式模型(用于分类、完形填空等等),

1.2 Pre-trained:预训练

“Pre-trained”(预训练)指的是模型是用大量数据训练出来的

1.2.1 可视化

“Pre-trained”:用大量数据进行训练。
图中的大量旋钮/仪表盘就是所谓的“模型参数”,训练过程就是在不断优化这些参数,后面会详细介绍。

1.2.2 预训练 vs. 增量训练(微调)

“预”这个字也暗示了模型还有在特定任务中进一步训练的可能 —— 也就是我们常说的“微调”(finetuning)。

如何对预训练模型进行微调: InstructGPT:基于人类反馈训练语言模型遵从指令的能力(OpenAI,2022)。 译注。

1.3 Transformer:一类神经网络架构

“GPT” 三个词中最重要的其实是最后一个词 Transformer。 Transformer 是一类神经网络/机器学习模型,作为近期 AI 领域的核心创新, 推动着这个领域近几年的极速发展。

Transformer 直译为“变换器”或“转换器”,通过数学运算不断对输入数据进行变换/转换。另外,变压器、变形金刚也是这个词。 译注。

Transformer:一类神经网络架构的统称。

Transformer 最后的输出层。后面还会详细介绍

1.4 小结

如今已经可以基于 Transformer 构建许多不同类型的模型,不限于文本,例如,

本文希望通过“文字+动图”这种可视化又方便随时停下来思考的方式,解释 Transformer 的内部工作原理。

2 Transformer 起源与应用

2.1 Attention Is All You Need, Google, 2017,机器翻译

Transformer 是 Google 2017 年在 Attention Is All You Need paper 中提出的, 当时主要用于文本翻译

2.2 Generative Transformer

之后,Transformer 的应用场景扩展到了多个领域,例如 ChatGPT 背后也是 Transformer, 这种 Transformer 接受一段文本(或图像/音频)作为输入,然后就能预测接下来的内容。 以预测下一个单词为例,如下图所示,下一个单词有多种可能,各自的概率也不一样:

但有了一个这样的预测下一个单词模型,就能通过如下步骤让它生成更长的文字,非常简单:

  1. 初始文本输入模型;
  2. 模型预测出下一个可能的单词列表及其概率,然后通过某种算法(不一定挑概率最大的) 从中选一个作为下一个单词,这个过程称为采样(sampling);
  3. 将新单词追加到文本结尾,然后将整个文本再次输入模型;转 2;

以上 step 2 & 3 不断重复,得到的句子就越来越长。

2.3 GPT-2/GPT-3 生成效果(文本续写)预览

来看看生成的效果,这里拿 GPT-2 和 GPT-3 作为例子。

下面是在我的笔记本电脑上运行 GPT-2,不断预测与采样,逐渐补全为一个故事。 但结果比较差,生成的故事基本上没什么逻辑可言:

下面是换成 GPT-3(模型不再开源,所以是通过 API), GPT-3 和 GPT-2 基本架构一样,只是规模更大, 但效果突然变得非常好, 生成的故事不仅合乎逻辑,甚至还暗示 “物种 π” 居住在一个数学和计算王国:

2.4 ChatGPT 等交互式大模型

以上这个不断重复“预测+选取”来生成文本的过程,就是 ChatGPT 或其他类似大语言模型(LLM) 的底层工作原理 —— 逐单词(token)生成文本

2.5 小结

以上是对 GPT 及其背后的 Transformer 的一个感性认识。接下来我们就深入到 Transformer 内部, 看看它是如何根据给定输入来预测(计算)出下一个单词的。

3 Transformer 数据处理四部曲

为理解 Transformer 的内部工作原理,本节从端到端(从最初的用户输入,到最终的模型输出)的角度看看数据是如何在 Transformer 中流动的。 从宏观来看,输入数据在 Transformer 中经历如下四个处理阶段:

Transformer 数据处理四部曲

下面分别来看。

3.1 Embedding:分词与向量表示

首先,输入内容会被拆分成许多小片段(这个过程称为 tokenization),这些小片段称为 token

  • 对于文本:token 通常是单词、词根、标点符号,或者其他常见的字符组合;
  • 对于图片:token 可能是一小块像素区域;
  • 对于音频:token 可能是一小段声音。

然后,将每个 token 用一个向量(一维数组)来表示。

3.1.1 token 的向量表示

这实际上是以某种方式在编码该 token;

Embedding:每个 token 对应一个 N*1 维度的数值格式表示的向量。

3.1.2 向量表示的直观解释

如果把这些向量看作是在高维空间中的坐标, 那么含义相似的单词在这个高维空间中是相邻的

词义相近的四个单词 “leap/jump/skip/hop” 在向量空间中是相邻的

将输入进行 tokenization 并转成向量表示之后,输入就从一个句子就变成了一个向量序列。 接下来,这个向量序列会进行一个称为 attention 的运算。

3.2 Attention:embedding 向量间的语义交流

3.2.1 语义交流

attention 使得向量之间能够相互“交流”信息。这个交流是双向的,在这个过程中,每个向量都会更新自身的值。

这种信息“交流”是有上下文和语义理解能力的。

3.2.2 例子:”machine learning model” / “fashion model

例如,“model” 这个词在 “machine learning model”(机器学习模型)和在 “fashion model”(时尚模特)中的意思就完全不一样, 因此虽然是同一个单词(token),但对应的 embedding 向量是不同的

Attention 模块的作用就是确定上下文中哪些词之间有语义关系,以及如何准确地理解这些含义(更新相应的向量)。 这里说的“含义”(meaning),指的是编码在向量中的信息。

3.3 Feed-forward / MLP:向量之间无交流

Attention 模块让输入向量们彼此充分交换了信息(例如,单词 “model” 指的应该是“模特”还是“模型”), 然后,这些向量会进入第三个处理阶段:

第三阶段:多层感知机(multi-layer perceptron),也称为前馈层(feed-forward layer)。

3.3.1 针对所有向量做一次性变换

这个阶段,向量之间没有互相“交流”,而是并行地经历同一处理:

3.3.2 直观解释

后面会看,从直观上来说,这个步骤有点像对每个向量都提出一组同样的问题,然后根据得到的回答来更新对应的向量

以上解释中省略了归一化等一些中间步骤,但已经可以看出: attention 和 feed-forward 本质上都是大量的矩阵乘法

本文的一个目的就是让读者理解这些矩阵乘法的直观意义。

3.3.3 重复 Attention + Feed-forward 模块,组成多层网络

Transformer 基本上是不断复制 Attention 和 Feed-forward 这两个基本结构, 这两个模块的组合成为神经网络的一层。在每一层,

  • 输入向量通过 attention 更新彼此;
  • feed-forward 模块将这些更新之后的向量做统一变换,得到这一层的输出向量;

3.4 Unembedding:概率

3.4.1 最后一层 feed-forward 输出中的最后一个向量

如果一切顺利,最后一层 feed-forward 输出中的最后一个向量(the very last vector in the sequence), 就已经包含了句子的核心意义(essential meaning of the passage)。对这个向量进行 unembedding 操作(也是一次性矩阵运算), 得到的就是下一个单词的备选列表及其概率:

图:原始输入为 "To date, the cleverest thinker of all time was",让模型预测下一个 token。经过多层 attention + feed-forward 之后, 最后一层输出的最后一个向量已经学习到了输入句子表达的意思,(经过简单转换之后)就能作为下一个单词的概率

3.4.2 下一个单词的选择

根据一定的规则选择一个 token,

  • 注意这里不一定选概率最大的,根据工程经验,一直选概率最大的,生成的文本会比较呆板;
  • 实际上由一个称为 temperature 的参数控制;

3.5 小结

以上就是 Transformer 内部的工作原理。

前面已经提到,有了一个这样的预测下一个单词模型,就能通过如下步骤让它生成更长的文字,非常简单:

  1. 初始文本输入模型;
  2. 模型预测出下一个可能的单词列表及其概率,然后通过某种算法(不一定挑概率最大的) 从中选一个作为下一个单词,这个过程称为采样(sampling);
  3. 将新单词追加到文本结尾,然后将整个文本再次输入模型;转 2;

4 GPT -> ChatGPT:从文本补全到交互式聊天助手

GPT-3 的早期演示就是这样的:给 GPT-3 一段起始文本,它就自动补全(续写)故事和文章。 这正式以上介绍的 Transformer 的基本也是核心功能。

ChatGPT 的核心是 GPT 系列(GPT 3/3.5/4),但它怎么实现聊天这种工作方式的呢?

4.1 系统提示词,伪装成聊天

其实很简单,将输入文本稍作整理,弄成聊天内容,然后把这样的文本再送到 GPT/Transformer, 它就会把这个当前是聊天内容,续写下去。最后只需要把它续写的内容再抽出来返回给用户, 对用户来说,就是在聊天。

这段文本设定用户是在与一个 AI 助手交互的场景,这就是所谓的系统提示词(system prompt)。

4.2 如何训练一个企业级 GPT 助手(译注)

OpenAI 官方对 GPT->ChatGPT 有过专门分享:如何训练一个企业级 GPT 助手(OpenAI,2023)

基础模型不是助手,它们不想回答问题,只想补全文档。 因此,如果让它们“写一首关于面包和奶酪的诗”,它们不仅不“听话”,反而会有样学样,列更多的任务出来,像下面左图这样,

这是因为它只是在忠实地补全文档。 但如果你能成功地提示它,例如,开头就说“这是一首关于面包和奶酪的诗”, 那它接下来就会真的补全一首这样的诗出来,如右图。

我们还可以通过 few-shot 来进一步“欺骗”它。把你想问的问题整理成一个“提问+回答”的文档格式, 前面给一点正常的论述,然后突然来个问题,它以为自己还是在补全文档,其实已经把问题回答了:

这就是把基础模型调教成一个 AI 助手的过程。

5 总结

本文整理翻译了原视频的前半部分,通过可视化方式解释 GPT/Transformer 的内部工作原理。 原视频后面的部分是关于 general deep learning, machine learning 等等的基础,想继续学习的,强烈推荐。


Written by Human, Not by AI Written by Human, Not by AI

不花钱的同声传译

作者 Lenciel
2024年5月7日 09:29
语音转文本再进行处理(记录、总结或者翻译)。由于目前的解决方案基本都是跑在服务器端的,所以也有一些问题。首先是不够安全。不管是私人的通话,还是工作的会议,把音频录制和处理交给第三方,特别是国内一些厂商,还是让人感觉有点害怕的。其次是不够灵活。比如,YouTube 自动加字幕的功能,依赖 Google 的服务。本地下载了一部冷门的电影,就还得老老实实花时间去找字幕。这些问题的解决,核心是下面两个方面...

[译] 大模型推理的极限:理论分析、数学建模与 CPU/GPU 实测(2024)

2024年4月6日 08:00

译者序

本文翻译自 2024 年的一篇文章: LLM inference speed of light, 分析了大模型推理的速度瓶颈及量化评估方式,并给出了一些实测数据(我们在国产模型上的实测结果也大体吻合), 对理解大模型推理内部工作机制和推理优化较有帮助。

A100-80GB PICe 推理延迟与吞吐。Image Source

译者水平有限,不免存在遗漏或错误之处。如有疑问,敬请查阅原文。

以下是译文。



摘要

在开发 calm 的过程中,我们考虑的一个核心问题是: 推理的极限在哪儿?因为我们需要以此为准绳,去衡量真实推理系统的速度。

calm 是一个基于 CUDA、完全从头开始编写的轻量级 transformer-based language models 推理实现

本文试图讨论这个极限及其影响。 如果对推导细节感兴趣,可参考这个 python notebook

1 推理机制

1.1 transformer:逐 token 生成,无法并行

当语言模型生成文本时,它是逐个 token 进行的。 可以把语言模型(特别是 decoder-only text transformer,本文统称为 LLM) 看做是一个函数

  • 输入:一个 token
  • 输出:一组概率,每个概率对应词汇表中一个 token。
  • 推理程序使用概率来指导抽样,产生(从词汇表中选择)下一个 token 作为最终输出。

词汇表(vocabulary):通常由单词、单词片段、中文汉字等组成(这些都称为 token)。 vocabulary 长什么样,可以可以看一下 bert-base-chinese 的词典 vocab.txt。 更多基础:

  1. GPT 是如何工作的:200 行 Python 代码实现一个极简 GPT(2023)
  2. Transformer 是如何工作的:600 行 Python 代码实现 self-attention 和两类 Transformer(2019)

译注。

文本生成过程就是不断重复以上过程。可以看出,在生成一个文本序列时,没有并行性的可能性

speculative execution 尝试通过一个 less accurate predictor 来实现某种程度的并行,本文不讨论。

1.2 生成过程建模:矩阵乘法

广义上,当处理一个 token 时,模型执行两种类型的操作:

  1. 矩阵-向量乘法:一个大矩阵(例如 8192x8192)乘以一个向量,得到另一个向量,
  2. attention 计算

    在生成过程中,模型不仅可以看到当前 token 的状态,还可以看到序列中所有之前 token 的内部状态 —— 这些状态被存储在一个称为 KV-cache 的结构中, 它本质上是文本中每个之前位置的 key 向量和 value 向量的集合

    attention 为当前 token 生成一个 query 向量,计算它与所有之前位置的 key 向量之间的点积, 然后归一化得到的一组标量,并通过对所有之前的 value 向量进行加权求和来计算一个 value 向量,使用点积得到最终得分。

This description omits multi-head attention and the details of “normalization” (softmax), but neither are critical for understanding the inference performance.

1.3 瓶颈分析

以上两步计算有一个重要的共同特征:从矩阵或 KV-cache 读取的每个元素,只需要进行非常少量的浮点运算

  • 矩阵-向量乘法对每个矩阵元素执行一次乘加运算(2 FLOPs);
  • attention 对每个 key 执行一次乘加,对每个 value 执行一次乘加

1.3.1 典型“算力-带宽”比

现代 CPU/GPU 的 ALU 操作(乘法、加法)内存 IO 速度要快得多。例如:

  • AMD Ryzen 7950X:67 GB/s 内存带宽和 2735 GFLOPS,Flop:byte = 40:1
  • NVIDIA GeForce RTX 4090:1008 GB/s 显存带宽和 83 TFLOPS,Flop:byte = 82:1
  • NVIDIA H100 SXM:3350 GB/s 内存带宽和 67 TFLOPS, 对于矩阵乘法,tensor core 提供 ~494 TFLOPS 稠密算力,Flop:byte = 147:1

对于 FP16/FP8 等精度较低的浮点数,比率更夸张:

  • H100 TensorCore 对于 dense FP8 矩阵的理论吞吐量为 1979 TFLOPS,FLOP:byte = 590:1

在这些场景中,无论是否使用 TensorCore 或使用什么浮点格式,ALU 都非常充足。

1.3.2 瓶颈:访存带宽

因此,transformer 这种只需要对每个元素执行两次操作的场景,必定受到访存带宽的限制。 所以,基于下面几个因素,

  1. 模型配置(参数多少)
  2. KV-cache 大小
  3. 访存带宽

我们就能估计推理过程的最短耗时。 下面以 Mistral 7B 为例来具体看看。

2 以 Mistral-7B 为例,极限推理延迟的计算

2.1 参数(权重)数量的组成/计算

Mistral-7B 有 72 亿参数(所有矩阵元素的总数是 72 亿个)。 参数的组成如下:

  1. 4096 * 32000 = 131M 用于 embedding 矩阵;
    • 4096: hidden size (tokens per hidden-vector)
    • 32000: vocabulary size

    矩阵-向量乘法中不会使用这整个大矩阵,每个 token 只读取这个矩阵中的一行,因此数据量相对很小,后面的带宽计算中将忽略这个;

  2. 32 * (4096 * (128 * 32 + 128 * 8 * 2) + 4096 * 128 * 32) = 1342M 用于计算与 attention 相关的向量;
  3. 32 * (4096 * 14336 * 3) = 5637M 用于通过 feed-forward 转换 hidden states;
  4. 4096 * 32000 = 131M 用于将 hidden states 转换为 token 概率;这与 embedding 矩阵不同,会用于矩阵乘法。

以上加起来,大约有 7111M (~7B) “活跃”参数用于矩阵乘法。

2.2 计算一个 token 所需加载的数据量

2.2.1 总数据量

如果模型使用 FP16 作为矩阵元素的类型, 那每生成一个 token,需要加载到 ALU 上的数据量

7111M params * 2Byte/param = ~14.2 GB

虽然计算下一个 token 时每个矩阵都可以复用,但硬件缓存的大小通常只有几十 MB, 矩阵无法放入缓存中,因此我们可以断定,这个生成(推理)过程的速度不会快于显存带宽

attention 计算需要读取当前 token 及前面上下文中所有 tokens 对应的 KV-cache, 所以读取的数据量取决于生成新 token 时模型看到多少前面的 token, 这包括

  1. 系统提示词(通常对用户隐藏)
  2. 用户提示词
  3. 前面的模型输出
  4. 可能还包括长聊天会话中多个用户的提示词。

2.2.2 KV-cache 部分的数据量

对于 Mistral,KV-cache

  • 为每层的每个 key 存储 8 个 128 元素向量,
  • 为每个层的每个 value 存储 8 个 128 元素向量,

这加起来,每个 token 对应 32 * 128 * 8 * 2 = 65K 个元素; 如果 KV-cache 使用 FP16,那么对于 token number P,我们需要读取 P * 130 KB 的数据。 例如, token number 1000 将需要从 KV-cache 读取 130MB 的数据。 跟 14.2GB 这个总数据量相比,这 130MB 可以忽略不计了。

2.3 以 RTX 4090 为例,极限延迟计算

根据以上数字,现在可以很容易地计算出推理所需的最小时间。

例如,在 NVIDIA RTX 4090(1008 GB/s)上,

  • 14.2GB (fp16) 需要 ~14.1ms 读取,因此可以预期对于位置靠前的 token, 每个 token 大约需要 14.1ms(KV-cache 影响可以忽略不计)。
  • 如果使用 8bit 权重,需要读取 7.1GB,这需要大约 7.0ms

这些都是理论下限,代表了生成每个 token 的最小可能时间。

2.4 ChatGLM3-6B/Qwen-7B 实测推理延迟(译注)

简单的单卡推理测试,16bit 权重,平均延迟,仅供参考:

LLM RTX 4090 24GB (2022) A100 80GB (2020) V100 32GB (2017)
ChatGLM3-6B 16ms/token 18ms/token 32ms/token
Qwen-7B 19ms/token 29ms/token 41ms/token

可以看到,单就推理速度来说,只要模型能塞进去(< 24GB),4090 与 A100 相当甚至更快,比 V100 快一倍。

说明:以上测的是 4090,不带 D4090D)。

3 数学模型和理论极限的用途

以上根据数学建模和计算得出了一些理论极限数字,接下来看看这些理论极限有什么用。

3.1 评估推理系统好坏

要接近理论极限,需要一个高质量的软件实现,以及能够达到峰值带宽的硬件。 因此如果你的软件+硬件离理论最优很远,那肯定就有问题:可能在软件方面,也可能在硬件方面。

例如,在 RTX 4090 上 calm 使用 16 位权重时达到 ~15.4 ms/tok,使用 8 位权重时达到 ~7.8 ms/tok, 达到了理论极限的 90%。

Close, but not quite there - 100% bandwidth utilization is unfortunately very hard to get close to on NVidia GPUs for this workload. Larger GPUs like H100 are even more difficult to fully saturate; on Mixtral - this is a different architecture but it obeys the same tradeoffs for single sequence generation if you only count active parameters - calm achieves ~80% of theoretically possible performance, although large denser models like Llama 70B can get closer to the peak.

在 Apple M2 Air 上使用 CPU 推理时,calmllama.cpp 只达到理论 100 GB/s 带宽的 ~65%, 然后带宽就上不去了,这暗示需要尝试 Apple iGPU 了。

3.2 指导量化

带宽与每个权重使用的 bit 数成正比;这意味着更小的权重格式(量化)能实现更低的延迟。 例如,在 RTX 4090 上 llama.cpp 使用 Mistral 7B

  • 16 bit 权重:~17.1 ms/tok(82% 的峰值)
  • 8.5 bit 权重:~10.3ms/tok (71% 的峰值)
  • 4.5 bit 权重:~6.7ms/tok (58% 的峰值)

因此对于低延迟场景,可以考虑低精度量化。

3.3 指导优化方向

除了为推理延迟提供下限外,上述建模还表明:推理过程并未充分利用算力(ALU)。 要解决这个问题,需要重新平衡 FLOP:byte 比例, speculative decoding 等技术试图部分解决这个问题。

3.3.1 批处理 batch 1 -> N:瓶颈 访存带宽 -> 算力

这里再另一种场景:多用户场景。注意到,

  • 当多个用户请求同时处理时,我们用相同的矩阵同时执行多个矩阵-向量乘法, 这里可以将多个矩阵-向量乘法变成一个矩阵-矩阵乘法
  • 对于足够大的矩阵来说,只要矩阵-矩阵乘法实现得当,速度就比访存 IO 快,

因此这种场景下,瓶颈不再是访存 IO,而是算力(ALU)。这就是为什么这种 ALU:byte 不平衡对于生产推理系统不是关键问题 —— 当使用 ChatGPT 时,你的请求与同一 GPU 上许多其他用户的请求并发评估,GPU 显存带宽利用更加高效。

3.3.2 批处理无法改善所需加载的 KV-cache 数据量

批处理通常不会减轻 KV-cache 带宽(除非多个请求共享非常大的前缀),因为 KV-cache 大小和带宽随请求数量的增加而增加,而不像权重矩阵保持不变。

像 Mistral 这样的混合专家模型(MoE)scaling 特性稍有不同:batching initially only increases the bandwidth required, but once the expert utilization becomes significant the inference becomes increasingly ALU bound.

3.4 硬件相对推理速度评估

带宽是评估推理性能的关键指标,对于模型变化/设备类型或架构来说是一个恒定的, 因此即使无法使用 batch processing,也可以用它来评估你用的硬件。

例如,NVIDIA RTX 4080 有 716 GB/s 带宽,所以可以预期它的推理速度是 RTX 4090 的 ~70% —— 注意,游戏、光线追踪或推理其他类型的神经网络等方面,相对性能可能与此不同!

4 GQA (group query attention) 的影响

Mistral-7B 是一个非常平衡的模型;在上面的所有计算中,几乎都能忽略 KV-cache 部分的 IO 开销。 这背后的原因:

  1. 较短的上下文(Mistral-7B 使用 windowed attention,限制 4096 token 的窗口),
  2. 使用了 GQA,这个是更重要的原因。

LLaMA 2:开放基础和微调聊天模型(Meta/Facebook,2023) 也使用了 GQA。

4.1 GQA 为什么能减少带宽

在 GQA 中(with a 4x ratio),为了得到 attention 的 4 个点积,

  • 不是使用 4 个 query 向量并分别与 4 个相应的 key 向量计算点积,
  • 而是只取一个 key 向量,然后执行 4 个点积。

这能够减少 KV-cache 的大小和所需带宽,也在某种程度上重新平衡了 ALU:bandwidth 比例。

4.2 有无 GQA 的数据量对比

这对于 KV-cache 内存大小也很关键,不过,这可能对短上下文模型不太明显

  • 4096 token 上下文的 Mistral 需要 0.5GiB,
  • 没有 GQA 的可比模型(如 Llama 7B)“只需要”2 GiB。

让我们看看一个最近不使用 GQA 的模型,Cohere 的 Command-R

Command-R has a large vocab (256K) and large hidden state (8192) so it spends a whopping 2B parameters on embeddings, but it reuses the same matrix for embedding and classification so we don’t need to exclude this from the inference bandwidth calculation.

模型本身有大约 35b 参数,所以以 16 位/权重计算,我们在推理期间需要为每个 token 读取 70 GB 的权重。 对于每个 token ,它需要在 KV-cache 中存储 40 * 128 * 64 * 2 = 655K 元素,以 16 位/元素计算是每个 token 1.3 MB。

因此,一个 4096 token 的上下文将需要大约 5.3GB; 与 ~70 GB 的权重相比,这已经相当显著了。然而,如果考虑到 Cohere 的模型宣传有 200K token 上下文窗口 —— 计算最后一个 token 需要读取 260 GB(还需要 260GB 的显存来存储它)!

4.3 多用户场景下 KV-cache 占用的显存规模

这么大的模型,典型的生产环境配置(单用户),

  • weights 通常使用 4bit 量化(通常的实现占用 ~4.5bit/权重)
  • KV-cache 可能会使用 8bit(FP8)值。

如果我们“保守地”假设上下文为 100K,则

  • 模型权重占 ~19.7GB
  • KV-cache 占 ~65GB

计算到最后一个 token 时,我们需要从内存中读取这么大的数据。 可以看到,突然之间,attention 计算部分的数据量(最终转变成耗时)从微不足道变成了占 ~75%

虽然 100K 上下文可能看起来有点极端,但在短上下文+多用户场景中,情况也是类似的:

  • 批处理优化将多次矩阵-向量乘法变成了一次矩阵-矩阵乘法(为一批用户请求读取一次模型权重),瓶颈来到算力(ALU),
  • 每个用户请求通常都有自己的 KV-cache

因此最终的 attention 仍然受访存带宽限制,并且需要大量内存/显存才能将所有用户请求放到单个节点!

4.4 GQA:减小从 KV-cache 加载的数据量

如果模型使用 4x GQA,KV-cache 的大小和所需带宽将会变成原来的 1/4

对于 100k+ token 的上下文场景,虽然 KV-cache 的开销仍然很大(65GB -> 16GB+),但已经进入实用范围。

4.5 GQA 的问题

对于 Cohere 的目标使用场景,引入 GQA 可能会导致模型质量有下降,具体得看他们的技术报告。

但是,纯粹从成本/性能角度来看,每个基于 transformer 的 LLM 都需要评估是否能引入 GQA,因为收益太大了。

5 总结

对于大模型推理场景,计算和访存的次数是已知的,因此可以进行数学建模,计算理论极限。 这非常有用,不仅可以用来验证推理系统的性能, 而且能预测架构变化带来的影响


Written by Human, Not by AI Written by Human, Not by AI

[译][论文] InstructGPT:基于人类反馈训练语言模型遵从指令的能力(OpenAI,2022)

2024年3月24日 08:00

译者序

本文翻译自 2022 年 OpenAI 的论文: Training language models to follow instructions with human feedback, 整理翻译了其中感兴趣的部分。

大模型进化树,可以看到 InstructGPT 所处的年代和位置。来自 大语言模型(LLM)综述与实用指南(Amazon,2023)

GPT -> InstructGPT -> ChatGPT 的过程,可参考 如何训练一个企业级 GPT 助手(OpenAI,2023)

译者水平有限,不免存在遗漏或错误之处。如有疑问,敬请查阅原文。

以下是译文。



摘要

增大模型尺寸未必就能提高它对用户意图的理解能力。 例如,一些大模型可能会生成不真实、有毒或对用户并无帮助(untruthful, toxic, or simply not helpful)的输出。 换句话说,这些模型与它们的用户没有对齐(not aligned)。

本文展示了一种基于人类反馈进行微调(fine-tuning with human feedback), 从而在各种任务上将语言模型与用户意图对齐的方法。简单来说,

  • 先收集一组“预期的模型行为应该是什么样”的数据集, 然后使用监督学习来微调 GPT-3(SFT),
  • 接着,收集一组排名形式组织的模型输出(rankings of model outputs)作为数据集, 使用人类反馈强化学习(RLHF)进一步微调上一步得到的模型。

我们将最终得到的这种模型称为 InstructGPT

  • 175b GPT-3 vs. 1.3b InstructGPT人工测评显示, 大家更喜欢后者,尽管它的参数不到前者的 1%
  • InstructGPT 在真实性(truthfulness)方面也有所改进,减少了有毒输出, 在公开 NLP 数据集上的性能退化也很小。

尽管 InstructGPT 仍然会犯一些简单的错误,但我们的研究结果表明, 基于人类反馈进行微调(fine-tuning with human feedback)是一个很有前途的 将语言模型与人类意图对齐的方向。

1 引言

给定一些任务示例(examples of the task)作为输入,大语言模型(LLMs)可以被 “prompt” 去执行一系列自然语言处理(NLP)任务。

1.1 大模型存在的问题

然而,这些模型经常会出现一些意外的行为,比如编造事实、生成有偏见或有毒的文本, 或者忽视用户的指示(Bender 等,2021;Bommasani 等,2021;Kenton 等,2021; Weidinger 等,2021;Tamkin 等,2021;Gehman 等,2020)。

1.2 语言模型建模偏差:预测下一个 token vs. 有益且安全地遵循用户指令

出现以上现象, 是因为许多近期的 LLM 建模目标都是(基于互联网数据训练)预测下一个 token —— 而并不是“有益且安全地遵循用户的指令”(Radford 等,2019;Brown 等,2020;Fedus 等,2021;Rae 等,2021;Thoppilan 等,2022)。 也就是说,语言建模目标有偏差(the language modeling objective is misaligned)。

由于 LLM 已经部署在大量实际应用中,因此解决大模型的这些非预期行为非常重要。

1.3 常规解决方式及评估标准

通过训练语言模型按照用户意图行事(Leike 等,2018)来推进语言模型的对齐。 这里的意图包括

  1. 明确的意图,如遵循指示,
  2. 隐含的意图,如保持真实、无偏见、无毒及无害性。

使用 Askell 等(2021)的术语,我们希望语言模型是

  • 有帮助的(helpful,应该帮助用户解决任务),
  • 诚实的(honest,不应该捏造信息或误导用户),
  • 无害的(harmless,不应该对人或环境造成身体、心理或社会伤害)。

我们在第 3.6 节中详细阐述了这些标准的评估。

1.4 本文方法:基于人类反馈+微调来对齐

本文专注于通过微调方法来对齐语言模型。具体来说, 使用人类反馈强化学习(RLHF;Christiano 等,2017;Stiennon 等,2020) 来微调 GPT-3,以便它能遵循类型广泛的各种用户指令。具体过程如图 2 所示,

Figure 2: InstructGPT 三部曲:(1) SFT, (2) RM training, (3) RLHF via proximal policy optimization (PPO) on RM.
蓝色箭头表示相应的数据用于训练模型。Step 2 中 A-D 是模型输出的采样,然后标注员对它们进行排序。详见 Section 3。

三个步骤:

  1. 收集示例数据(demonstration data),训练一个监督策略(supervised policy)。

    对于给定的输入,标注员给出期望的行为 (详见 3.2 节)。然后,使用监督学习(supervised learning)对一个预训练的 GPT-3 模型进行微调。

  2. 收集对比数据(comparison data),训练一个奖励模型(RM)。

    对给定输入,收集两个输出,标注员给出他们的偏好(which output they prefer)。然后,训练一个奖励模型来预测人类偏好输出(human-preferred output)。

  3. 针对奖励模型,使用 PPO 对策略进行优化(optimize a policy)。

    将 RM 的输出作为一个标量奖励。通过 PPO 算法 (Schulman 等,2017) 对监督策略进行微调(fine-tune the supervised policy),以优化这一奖励。

步骤 2 和 3 可以持续迭代;在当前最佳策略上收集更多的对比数据,这些数据又用于训练新的 RM 和新的策略。 实际上,大部分对比数据来自于我们的 supervised policies,一小部分来自于我们的 PPO policies。

这个过程将 GPT-3 的行为与特定人群的偏好(stated preferences of a specific group of people,大多是我们的标注员和研究人员), 而非任何更广泛的“人类价值观”对齐;5.2 节将进一步讨论这个问题。

1.5 模型尺寸及架构

我们训练了三种尺寸的 InstructGPT 模型:

  1. 1.3B
  2. 6B
  3. 175B

所有模型都使用了 GPT-3 架构。

1.6 主要发现

我们的主要发现如下。

1.6.1 标注员明显更喜欢 InstructGPT 而非 GPT-3 的输出

我们将 175b GPT-3 vs. 1.3b InstructGPT 的输出进行了人工测评, 大家明显更喜欢后者,尽管它的参数不到前者的 1%

这两类模型具有相同的架构,唯一的区别是 InstructGPT 在人工数据上进行了微调

作为对比,我们给 GPT-3 添加了一个 few-shot prompt 以使其更好地遵循指令(变成了一个提示词调优过的 GPT-3), 但效果仍赶不上 InstructGPT:

  • 175B InstructGPT 在 85 ± 3% 的结果中优于 175B GPT-3
  • 175B InstructGPT 在 71 ± 4% 的结果中优于 few-shot 175B GPT-3

根据标注员的反馈,InstructGPT 模型的输出更符合 prompt ,并更可靠地遵循指令中的明确约束。

1.6.2 InstructGPT 相比 GPT-3 在真实性方面有所改进

在 TruthfulQA 基准测试中,InstructGPT 生成 truthful & informative 答案的概率比 GPT-3 高约一倍。

对于“封闭域”(closed-domain)任务(输出不应包含输入中不存在的信息,例如摘要和封闭域的问答测试, InstructGPT 的信息虚构率(编造输入中不存在的信息)只有 GPT-3 的一半(21% vs. 41%)。

1.6.3 InstructGPT 相比 GPT-3 毒性略微下降,但偏见未下降

为了衡量毒性,我们使用了 RealToxicityPrompts 数据集(Gehman 等,2020),并进行了自动和人工评估。 当提示模型需要 respectful 时(prompted to be respectful),InstructGPT 生成的有毒输出比 GPT-3 少约 25%。

在 Winogender(Rudinger 等,2018)和 CrowSPairs(Nangia 等,2020)数据集上, InstructGPT 相比 GPT-3 没有明显改进。

1.6.4 通过修改 RLHF 微调过程,可以最小化在公开 NLP 数据集上的性能退化

在 RLHF 微调过程中,我们观察到在某些公开 NLP 数据集上 InstructGPT 相比 GPT-3 存在性能下降, 尤其是 SQuAD(Rajpurkar 等,2018)、DROP(Dua 等,2019)、HellaSwag(Zellers 等,2019)和 WMT 2015 法英翻译(Bojar 等,2015)。

这是一个“对齐税”(alignment tax)的例子 —— 对齐可能会牺牲在某些任务上的性能。 在不降低标注员偏好分数的前提下,我们通过混合 PPO updates 与 PPO-ptx updates(增加预训练分布的对数似然), 大大减少了在这些数据集上的性能下降。

InstructGPT 可以推广到那些未参与编写训练数据的标注员(held-out labelers)。 为测试 InstructGPT 的泛化能力而进行的初步实验结果表明,与参与训练的标注员(training labelers)一样, 未参与训练的标注员也更喜欢 InstructGPT 而不是 GPT-3 的输出。 当然,还需要进一步研究这些模型在更广泛的用户群体上的表现, 以及它们在人们所期望的行为存在分歧的输入上的表现(inputs where humans disagree about the desired behavior)。

1.6.5 在公开 NLP 数据集上微调不如在人类偏好数据上微调的效果好

我们比较了两个微调的 GPT-3:

  1. 人类偏好数据上微调的 GPT-3(即 InstructGPT);
  2. 在两个公开 NLP 任务(FLAN(Wei 等,2021)和 T0/T0++(Sanh 等,2021)上微调的 GPT-3。

    这两个数据集包含多种 NLP 任务,以及每个任务的自然语言指令(natural language instructions)。

标注员明显更喜欢 InstructGPT 的输出。相比基线,

  • InstructGPT 的胜率为 73.4 ± 2%
  • T0 和 FLAN fine-tuned GPT-3 分别为 26.8 ± 2%29.8 ± 2%

1.6.6 InstructGPT 对 RLHF 微调之外的指令有良好的泛化能力

我们对 InstructGPT 的能力进行了定性探究,发现它能够遵循如下指令:

  1. 总结代码,
  2. 回答关于代码的问题,
  3. 有时还能遵循不同语言的指令,尽管这些指令在训练数据中非常少。

相比之下,GPT-3 虽然也可以执行这些任务,但需要更精心设计的 prompt ,并且遵循这些领域指令的效果欠佳。

这个结果很令人兴奋,因为它表明 InstructGPT 能够推广“遵循指令”的概念。 即使在只有非常少的直接监督信号(SFT 训练样本)的任务上,它们仍然具备了一定的对齐性。

InstructGPT 仍然会犯一些简单的错误。例如,可能无法遵循指令,捏造事实,对简单问题给出冗长的回答,或无法检测出有错误前提的指令。 但总体而言,我们的结果表明,使用人类偏好微调大语言模型可以显著改善它们在各种任务上的行为, 相应的,也需要更多工作提高它们的安全性和可靠性。

2 相关工作

2.1 对齐(alignment)与人类反馈学习(learning from human feedback)研究

2.1.1 RLHF:来自游戏领域

InstructGPT 建立在前人的技术基础上,特别是用人类反馈强化学习(RLHF)来对齐模型

RLHF 最初是为了在模拟环境(simulated environments)和 Atari 游戏中训练简单机器人而开发的(Christiano 等,2017; Ibarz 等,2018), 最近被用于微调语言模型总结文本(Ziegler 等,2019; Stiennon 等,2020; Böhm 等,2019; Wu 等,2021)。

2.1.2 InstructGPT:基于 RLHF 在更广泛的语言任务上对齐 LLM

这项工作还受到了下列类似工作的影响:

  1. 对话(Jaques 等,2019; Yi 等,2019; Hancock 等,2019)
  2. 翻译(Kreutzer 等,2018; Bahdanau 等,2016)
  3. 语义解析(Lawrence 和 Riezler,2018)
  4. 故事生成(Zhou 和 Xu,2020)
  5. 评论生成(Cho 等,2018)
  6. 证据提取(Perez 等,2019)

Madaan 等(2022)使用人类反馈来增强 prompts,以提高 GPT-3 的性能。 在基于文本的环境中,使用带有 4a normative prior 的 RL 来对齐 agents(Nahian 等,2021)。

我们的工作可以看作是用 RLHF 在更广泛的语言任务上对齐语言模型

2.1.3 语言模型对齐意味着什么

近期,“语言模型对齐意味着什么”这一问题备受关注(Gabriel,2020)。

  • Kenton 等(2021)列出了由于不对齐而导致的模型行为问题,包括产生有害内容和游戏中的错误目标。
  • 同一时间,Askell 等(2021)提出将语言助手作为对齐研究的测试对象,研究了一些简单的基线和它们的扩展特性。

2.2 训练模型遵循指令(follow instructions)

Our work is also related to research on crosstask generalization in language models, where LMs are fine-tuned on a broad range of public NLP datasets (usually prefixed with an appropriate instruction) and evaluated on a different set of NLP tasks. There has been a range of work in this domain (Yi et al., 2019; Mishra et al., 2021; Wei et al., 2021; Khashabi et al., 2020; Sanh et al., 2021; Aribandi et al., 2021), which differ in training and evaluation data, formatting of instructions, size of pretrained models, and other experimental details. A consistent finding across studies is that fine-tuning LMs on a range of NLP tasks, with instructions, improves their downstream performance on held-out tasks, both in the zero-shot and few-shot settings.

There is also a related line of work on instruction following for navigation, where models are trained to follow natural language instructions to navigate in a simulated environment (Bahdanau et al., 2018; Abramson et al., 2020; Zhao et al., 2021).

2.3 评估语言模型的危害

A goal of modifying the behavior of language models is to mitigate the harms of these models when they’re deployed in the real world. These risks have been extensively documented (Bender et al., 2021; Bommasani et al., 2021; Kenton et al., 2021; Weidinger et al., 2021; Tamkin et al., 2021). Language models can produce biased outputs (Dhamala et al., 2021; Liang et al., 2021; Manela et al., 2021; Caliskan et al., 2017; Kirk et al., 2021), leak private data (Carlini et al., 2021), generate misinformation (Solaiman et al., 2019; Buchanan et al., 2021), and be used maliciously; for a thorough review we direct the reader to Weidinger et al. (2021). Deploying language models in specific domains gives rise to new risks and challenges, for example in dialog systems (Henderson et al., 2018; Xu et al., 2020; Dinan et al., 2019b). There is a nascent but growing field that aims to build benchmarks to concretely evaluate these harms, particularly around toxicity (Gehman et al., 2020), stereotypes (Nadeem et al., 2020), and social bias (Dhamala et al., 2021; Nangia et al., 2020; Rudinger et al., 2018). Making significant progress on these problems is hard since well-intentioned interventions on LM behavior can have side-effects (Welbl et al., 2021; Blodgett et al., 2020); for instance, efforts to reduce the toxicity of LMs can reduce their ability to model text from under-represented groups, due to prejudicial correlations in the training data (Xu et al., 2021).

2.4 修改模型行为,降低危害

There are many ways to change the generation behavior of language models. Solaiman and Dennison (2021) fine-tune LMs on a small, value-targeted dataset, which improves the models’ ability to adhere to these values on a question answering task. Ngo et al. (2021) filter the pretraining dataset by removing documents on which a language model has a high conditional likelihood of generating a set of researcher-written trigger phrases. When trained on this filtered dataset, their LMs generate less harmful text, at the cost of a slight decrease in language modeling performance. Xu et al. (2020) use a variety of approaches to improve the safety of chatbots, including data filtering, blocking certain words or n-grams during generation, safety-specific control tokens (Keskar et al., 2019; Dinan et al., 2019a), and human-in-theloop data collection (Dinan et al., 2019b). Other approaches for mitigating the generated bias by LMs use word embedding regularization (Liu et al., 2019; Huang et al., 2019), data augmentation (Liu et al., 2019; Dinan et al., 2019a; Sheng et al., 2019), null space projection to make the distribution over sensitive tokens more uniform (Liang et al., 2021), different objective functions (Qian et al., 2019), or causal mediation analysis (Vig et al., 2020). There is also work on steering the generation of language models using a second (usually smaller) language model (Dathathri et al., 2019; Krause et al., 2020), and variants of this idea have been applied to reducing language model toxicity (Schick et al., 2021)

3 方法论与实验详情

3.1 High-level 方法论

我们延用了 Ziegler 等 (2019) 和 Stiennon 等 (2020) 在 stylistic continuation and summarization 领域应用的方法,

3.1.1 准备工作

如下基础准备:

  • 一个预训练的语言模型,即 GPT-3 (Radford 等,2019; Brown 等,2020; Fedus 等,2021; Rae 等,2021; Thoppilan 等,2022)
  • 一个 prompt 类别分布(a distribution of prompts,希望模型输出对齐到这些领域)
  • 一个经过培训的人工标注团队 (详见 3.4 节)。

3.1.2 InstructGPT 训练三部曲

按照以下三个步骤开始训练,如图 2 所示,

Figure 2: InstructGPT 三部曲:(1) SFT, (2) RM training, (3) RLHF via proximal policy optimization (PPO) on RM.
蓝色箭头表示相应的数据用于训练模型。Step 2 中 A-D 是模型输出的采样,然后标注员对它们进行排序。详见 Section 3。

  1. 收集示范数据(demonstration data),训练一个监督策略(supervised policy)。

    对于给定的输入,标注员给出期望的行为 (详见 3.2 节)。然后,使用监督学习(supervised learning)对一个预训练的 GPT-3 模型进行微调。

  2. 收集对比数据(comparison data),训练一个奖励模型(RM)。

    对给定输入,收集两个输出,标注员给出他们的偏好(which output they prefer)。然后,训练一个奖励模型来预测人类偏好输出(human-preferred output)。

  3. 针对奖励模型,使用 PPO 对策略进行优化(optimize a policy)。

    将 RM 的输出作为一个标量奖励。通过 PPO 算法 (Schulman 等,2017) 对监督策略进行微调(fine-tune the supervised policy),以优化这一奖励。

步骤 2 和 3 可以持续迭代;每次在当前最佳策略上收集更多的对比数据,这些数据又用于训练新的 RM 和新的策略。 实际上,大部分对比数据来自于我们的 supervised policies,一小部分来自于我们的 PPO policies。

3.2 数据集

3.2.1 主要来自 OpenAI API 用户数据

我们的 prompts 数据集主要来自用户提交给 OpenAI API 的文本 prompts, 尤其是用户通过 OpenAI Playground interface 提交的那些 prompts ——

  • 这个环境背后运行的是我们用 SFT + 我们自己的一部分示例数据训练出来的初期 InstructGPT models
  • 用户每次通过 Playground 接口用到 InstructGPT 时,我们都会告知他们,他们的数据可能会被用于训练下一步的模型。

本文并没有用到生产环境 OpenAI API 的用户数据。

3.2.2 去重

我们的去重比较简单,有共同长前缀的 prompt 就认为是重复的,并将每个用户 ID 的 prompt 数量限制为 200 个。

我们还基于用户 ID 创建训练、验证和测试集(train, validation, and test splits)。 为了避免模型学习到客户信息,我们过滤掉了训练数据集中包含个人身份信息(PII)的 prompts。

3.2.3 冷启动(第一版 InstructGPT)

为了训练最初的 InstructGPT 模型,我们要求标注员自己编写 prompt。 这是因为我们需要一些初始的指令式的 prompts 来启动这个过程, 而这类数据很难从 GTP-3 API 的用户数据中获得,用户通常不会提交这些格式的 prompts。

3.2.3 三种 prompt:plain/few-shot/user-based

我们要求标注员编写三种类型的 prompt :

  1. Plain: 标注员提出任意的任务,确保任务具有足够的多样性就行。
  2. Few-shot: 标注员提出一条指令,并为该指令提供多个查询/响应对(query/response pairs)。
  3. User-based: OpenAI API 的 waitlist applications 中我们列了一些使用案例。我们要求标注员提供与这些使用案例相关的 prompts。

详见附录 A。

3.2.4 三个 prompts 数据集及大小

根据以上 prompts,我们生成了三个不同的数据集用于不同的微调过程,如表 6 所示,

Table 6: Dataset sizes, in terms of number of prompts.

split source size
SFT train labeler 11,295
SFT train customer 1,430
SFT valid labeler 1,550
SFT valid customer 103
SFT 总计   ~15k
RM train labeler 6,623
RM train customer 26,584
RM valid labeler 3,488
RM valid customer 14,399
RM 总计   ~50k
PPO train customer 31,144
PPO valid customer 16,185
PPO 总计   ~47k
  1. SFT 数据集(来自 API 和标注员):用于训练 SFT 模型,包含约 13k training prompts
  2. RM 数据集(来自 API 和标注员):标注员对模型输出的排名数据,用于训练 RM 模型,有 33k training prompts
  3. PPO 数据集(仅来自 API):没有任何人工标签,用作 RLHF 微调的输入,有 31k training prompts。

3.2.5 Prompts 类别分布及占比

表 1 中展示了 API prompt(尤其是 RM 数据集)的类别分布,这些类别由我们的承包商标注。 可以看到,占比最大的是文本生成

Table 1: API prompt dataset 中 use case 类别及占比

Use-case (%)
Generation 45.6%
Open QA 12.4%
Brainstorming 11.2%
Chat 8.4%
Rewrite 6.6%
Summarization 4.2%
Classification 3.5%
Other 3.5%
Closed QA 2.6%
Extract 1.9%

3.2.6 几个 prompt 例子

表 2 展示了几个 prompt 示例(由研究人员编写,提交给 InstructGPT 的格式),

Table 2: API prompt 具体例子

Use-case Prompt
Brainstorming List five ideas for how to regain enthusiasm for my career
Generation Write a short story where a bear goes to the beach, makes friends with a seal, and then returns home.
Rewrite This is the summary of a Broadway play:
“”“
{summary}
“”“
This is the outline of the commercial for that play:
“”“

更多信息:

  1. 提交给 InstructGPT 的 prompts 见附录 A.2.1,
  2. 提交给 GPT-3 的 prompts(做对比) 见附录 A.2.2,

3.3 训练任务

我们的训练任务有两个来源:

  1. 标注员编写的 prompt 数据集,
  2. 提交给早期 InstructGPT 模型的 prompt 数据集。

这些 prompt 种类繁多,包括生成、问答、对话、摘要、提取和其他自然语言任务 (见表 1)。 我们的数据集中 96%+ 是英文,但在 4.3 节中, 我们也探讨了 InstructGPT 对其他语言指令的响应能力以及完成代码任务的能力。

对于每个自然语言 prompt,

  • 任务通常通过自然语言指令直接指定,例如, “Write a story about a wise frog”(“写一个关于一只聪明的青蛙的故事”),
  • 也可以间接指定

    • 通过 few-shot examples,例如,提供两个关于青蛙的故事作为示例,prompt 模型生成一个新的故事,
    • 通过 implicit continuation,例如,提供一个关于青蛙的故事的开头,让模型续写。

在每种情况下,我们都要求标注员尽力推断每个 prompt 背后的用户意图, 并要求他们跳过那些任务非常模糊的 prompt。 此外,标注员还会根据我们提供的指导 (见附录 B) 和他们自己的判断, 思考其中隐含的意图(implicit intentions),例如回答的真实性,潜在的有偏见或有毒输出。

3.4 人工数据收集

为了生成示范和对比数据,以及进行结果评估,我们通过 Upwork 和 ScaleAI 雇了大约 40 名外包人员

与之前关于摘要任务的人类偏好数据收集工作 (Ziegler 等,2019; Stiennon 等,2020; Wu 等,2021) 相比, 我们的输入数据涵盖了范围更广泛的任务,甚至还包括有争议和敏感的主题。

3.4.1 标注员筛选

我们的目标是选择一组标注员,他们对不同人口分布的偏好很敏锐,并擅长识别潜在的有害输出。 因此,

  • 我们进行了一个筛选测试(screening test)来衡量标注员在这些方面的表现;
  • 最后选出在这个测试中表现良好的标注员。

相关的选择过程和标注员分布信息,见附录 B.1。

3.4.2 对齐冲突的处理

在训练和评估过程中,我们的对齐标准可能会发生冲突:例如,当用户请求一个潜在有害的响应时。 针对这种情况,我们采取如下方式,

  • 训练阶段优先考虑对用户的有用性 (否则就需要做出一些艰难的设计决策,我们留给未来的工作;更多讨论见 5.4 节)。
  • 最终评估阶段:要求标注员优先考虑真实性和无害性 (因为这是我们真正关心的)。

与 Stiennon 等 (2020) 一样,我们在项目过程中与标注员密切合作。我们有一个入职流程,对标注员进行培训, 为每个任务编写详细的说明 (见附录 B.2),并在群聊中回答标注员的问题。

3.4.3 对照度标注员:验证泛华能力

为了解 InstructGPT 推广到其他标注员的偏好时表现有多好, 我们雇佣了另一组独立的标注员,他们不参与编写任何训练数据。 这些标注员来自相同的供应商,但没有经过前面的筛选过程。

Despite the complexity of the task, we find that inter-annotator agreement rates are quite high: training labelers agree with each-other 72:6 ± 1:5% of the time, while for held-out labelers this number is 77:3 ± 1:3%. For comparison, in the summarization work of Stiennon et al. (2020) researcher-researcher agreement was 73 ± 4%.

3.5 Models(模型)

我们从 GPT-3 预训练模型开始微调。GPT-3 在大量互联网数据上进行了训练,适用于各种下游任务, 但其行为尚未充分符合人类需求。基于 GPT-3,我们使用三种不同技术进行了模型微调。

3.5.1 Supervised fine-tuning (SFT)

使用监督学习的方式,在我们的示范数据上对 GPT-3 进行微调。

  • 16 epoch
  • a cosine learning rate decay
  • residual dropout 0.2

得到很多个 SFT 模型。最后根据 validation set 上的 RM 分数选择最终的 SFT 模型。

与 Wu 等(2021)类似,我们发现我们的 SFT 模型在 1 个 epoch 后在 validation loss 上就会过拟合(overfit); 但是,同时我们发现,尽管存在过拟合问题,但更多 epoch 对 RM 分数和人类偏好得分都有帮助

3.5.2 Reward modeling (RM)

将 SFT 模型去掉最后的 unembedding 层,然后从这样的模型开始训练,

  • 输入:prompt 和 response
  • 输出:一个标量奖励。

最后得到的就是一个 RM 模型。

在本文中,我们仅使用 6B 的 RM,因为这样可以节省大量计算资源, 并且我们发现 175B 的 RM 训练可能不稳定,因此不太适合在 RL 中用作 value function(更多细节见附录 C)。

在 Stiennon 等(2020)中,给两个模型相同的输入,然后得到两份输出作为对比数据, RM 是在这个对比数据集(dataset of comparisons)上进行训练的

They use a cross-entropy loss, with the comparisons as labels—the difference in rewards represents the log odds that one response will be preferred to the other by a human labeler.

为了快速收集对比数据,我们将 $K=4$ 到 $K=9$ 之间的输出(即一个 input/prompt 喂给模型,得到 K 个 output)都提供给标注员,并要求他们对其进行排名(rank)。 这样每个 prompt 就对应 ${K \choose 2}$ 个对比数据。

由于每个 labeling task 内的 comparisons 非常相关,我们发现如果简单地 shuffle the comparisons into one dataset, 对数据集训练一次就会导致 RM 过拟合。

That is, if each of the possible ${K \choose 2}$ comparisons is treated as a separate data point, then each completion will potentially be used for $K-1$ separate gradient updates. The model tends to overfit after a single epoch, so repeating data within an epoch also causes it to overfit.

因此,我们将每个 prompt 的所有 ${K \choose 2}$ 个对比作为单个 batch element 进行训练。 这样做在计算上更加高效,因为只需要一次正向遍历(forward pass of the RM for each completion, 而不是 ${K \choose 2}$ forward passes for $K$ completions), 并且由于不再过拟合,它实现了更好的 validation accuracy and log loss。

具体来说,奖励模型的损失函数为:

\[\begin{equation} \label{eq1} \begin{split} \operatorname{loss}\left(\theta \right) = -\frac{1} {K \choose 2} E_{\left(x, y_{w}, y_{l}\right) \sim D}\left[\log \left(\sigma\left(r_{\theta}\left(x, y_{w}\right)-r_{\theta}\left(x, y_{l}\right)\right)\right)\right] \end{split} \end{equation}\]

其中,

  • \(x\):prompt(输入的提示词)
  • \(y\):completion(模型的返回)
  • $y_{w}$:the preferred completion out of the pair of $y_{w}$ and $y_{l}$
  • $D$:dataset of human comparisons(标注员给出的对比)
  • \(r_{\theta}(x, y)\):scalar output of the RM for prompt $x$ and completion $y$ with parameters $\theta$

最后,由于 RM loss 对奖励的平移不变,我们使用一个 bias 来对奖励模型进行归一化,这样标注员的示范在进行 RL 之前的平均分数为 0。

3.5.3 Reinforcement learning (RL)

再次沿用(Stiennon 等,2020),我们使用 PPO(Schulman 等,2017)对 SFT 模型进行微调。 我们创建一个 bandit 环境,

  • 给一个随机的客户 prompt,得到一个 response。
  • 给定一个 prompt 和相应的 response,会产生一个由 RM 确定的奖励,然后结束这一轮。

此外,我们添加了一个 per-token 的 KL 惩罚(来自 SFT 模型),以减轻奖励模型的过优化(over-optimization)。 值函数是从 RM 初始化的。我们将这些模型称为PPO

我们还尝试将预训练梯度(pretraining gradients)mixing into PPO 梯度中,以减轻在公开 NLP 数据集上的性能下降。 我们将这些模型称为PPO-ptx

我们在 RL 训练中最大化以下组合目标函数:

\begin{equation} \label{eq2} \begin{split} \operatorname{objective}\left(\phi\right)= & E_{\left(x, y\right) \sim D_{\pi_{\phi}^{\mathrm{RL}}}}\left[r_{\theta}(x, y)-\beta \log \left(\pi_{\phi}^{\mathrm{RL}}(y \mid x) / \pi^{\mathrm{SFT}}(y \mid x)\right)\right] +
& \gamma E_{x \sim D_\textrm{pretrain}}\left[\log(\pi_{\phi}^{\mathrm{RL}}(x))\right] \end{split} \end{equation}

其中

  • \(\pi_{\phi}^{\mathrm{RL}}\) 是学习到的 RL 策略,
  • \(\pi^{\mathrm{SFT}}\) 是 SFT 模型,
  • \(D_\textrm{pretrain}\) 是预训练分布(pretraining distribution)。
  • KL 奖励系数 \(\beta\) 和预训练损失系数 \(\gamma\) 分别控制 KL 惩罚和预训练梯度的强度(strength)。
  • 对于 “PPO” models,\(\gamma\) 设置为 0。

除非另有说明,在本文中,InstructGPT 指的是 PPO-ptx models

3.5.4 性能比较基线

我们将 PPO 模型与下列模型进行比较:

  1. SFT 模型
  2. GPT-3
  3. GPT-3-prompted:向 GPT-3 提供一个 few-shot prefix 以“提示”它进入指令跟随模式。在实现上,就是把这个前缀插入倒用户输入的指令之前。

    To obtain this prefix, authors RL and DA held a prefix-finding competition: each spent an hour interacting with GPT-3 to come up with their two best prefixes. The winning prefix was the one that led GPT-3 to attain the highest RM score on the prompt validation set. DA won.

  4. 在 FLAN 和 T0 数据集上微调过的 175B GPT-3

    这两个数据集都包含各种 NLP 任务,以及每个任务的自然语言指令。 我们分别在约 1 million examples 上对它们进行微调,并选择在验证集上获得最高奖励分数的 checkpoint。更多细节见附录 C。

3.6 性能评估

为了评估我们模型的“对齐”程度,首先需要明确“对齐”的含义。

一直以来,“对齐”的定义都很模糊和令人困惑,有很多提法(Chen 等,2021;Leike 等,2018;Gabriel,2020)。

按照 Leike 等(2018)的方法,我们的目标是训练符合用户意图的模型。 更实际地说,为了完成语言任务,我们使用了类似于 Askill 等(2021)的框架, 他们认为,如果模型是 helpful, honest, and harmless 的,那这个模型就是对齐的(aligned)。

3.6.1 指标

helpful

要做到有帮助,模型不仅要能遵循指令,还应该能从一个 few-shot prompt 或其他可解释的模式(例如 Q: {question}\nA:)中推断意图(infer intention)。

给定的 prompt 的意图可能不清楚,这种情况下就依赖于标注员的判断,我们的主要指标是标注员的偏好评分。 但另一方面,由于我们的标注员不是生成 prompt 的用户, 因此,下面二者之间可能存在偏差:

  • 用户的实际意图
  • 标注员通过 prompt 所理解的用户意图

honest / truthfulness

在纯生成式模型中如何衡量诚实度尚无定论;这需要将模型的实际输出与其关于正确输出的“信念”进行比较 (model’s actual output to its “belief” about the correct output), 由于模型是一个黑盒,我们无法推断它的信念。

因此,我们使用真实性(truthfulness)—— 模型关于世界的陈述是否真实 —— 来衡量。 具体到两个指标:

  1. 模型在封闭域任务中编造信息(make up information on closed domain tasks)的倾向,即“幻觉”(hallucinations),
  2. TruthfulQA 数据集(Lin 等,2021)上的表现。

显然,这只能覆盖真实性实际含义(what is actually meant by truthfulness)的一小部分。

harmless

与诚实度类似,衡量语言模型的有害性也很难。 在大多数情况下,语言模型的危害取决于其输出在现实世界中是如何被使用的。 例如,对于一个生成有毒输出的模型,

  • 如果部署在聊天机器人环境中,可能就是有害的,
  • 如果用于数据增强以训练更准确的毒性检测模型,则可能是有益的。

在项目早期,我们让标注员评估输出是否“可能有害”。 但后面经停止了这项工作,因为这需要太多关于输出最终将如何被使用的猜测(speculation), 尤其是我们的部分数据还来自 Playground API 客户。

因此,我们使用了一套更具体的替代方案,旨在捕捉最终部署的模型中可能导致有害的不同行为方面: 我们让标注员从一个用户助理的角度来评估输出是否恰当,是否 denigrates a protected class,或是否包含性或暴力内容。

我们还在衡量偏见和毒性的数据集上对 InstructGPT 进行基准测试,例如 RealToxicityPrompts(Gehman 等,2020)和 CrowS-Pairs(Nangia 等,2020)。

3.6.2 定量评估

我们的定量评估分为两个独立的部分。

在 OpenAI API 真实用户的 prompts 上的表现

数据来源:OpenAI Playground API(背后是 InstructGPT) 收集来的用户 prompts。 所以,评估用的 prompts 与训练用的 prompt 同源,但未参与训练, 也就是说只选择那些未参与训练的客户 prompts。

但这里有个问题,训练用的 prompt 是专门为 InstructGPT 设计的,因此 GPT-3 在这些 prompts 上的效果可能不佳,有失公平。 为此,我们还收集了用户通过 OpenAI GPT-3 API 提交的 prompts 进行评估;这些 prompt 通常不是“遵循指令”的风格,而是专门为 GPT-3 设计的。

主要评估指标是人类偏好评分。 对于每个模型,都计算其输出相对于 baseline 被人类偏好的频率; 这里用我们的 175B SFT 模型作为 baseline ,因为它的性能处于中等水平。 此外,我们要求标注员使用 1-7 Likert scale 判断每个 response 的整体质量,并为每个输出收集一些元数据(见表 3)。

Table 3: Labeler-collected metadata on the API distribution

Metadata Scale
Overall quality Likert scale; 1-7
Fails to follow the correct instruction / task Binary
Inappropriate for customer assistant Binary
Hallucination Binary
Satisifies constraint provided in the instruction Binary
Contains sexual content Binary
Contains violent content Binary
Encourages or fails to discourage violence/abuse/terrorism/self-harm Binary
Denigrates a protected class Binary
Gives harmful advice Binary
Expresses opinion Binary
Expresses moral judgment Binary

在公开 NLP 数据集上的表现

我们在两种公开数据集上进行评估:

  1. 能衡量模型安全性的数据集,特别是真实性、毒性和偏见;
  2. 能衡量在传统 NLP 任务(如问答、阅读理解和摘要)上的 zero-shot 性能的数据集。

我们还在 RealToxicityPrompts 数据集(Gehman 等,2020)上人工评估了毒性。

We are releasing samples from our models on all of the sampling-based NLP tasks.

4 结果

暂略。 见原文。

Figure 1: Human evaluations of various models on our API prompt distribution, evaluated by how often outputs from each model were preferred to those from the 175B SFT model. Our InstructGPT models (PPO-ptx) as well as its variant trained without pretraining mix (PPO) significantly outperform the GPT-3 baselines (GPT, GPT prompted); outputs from our 1.3B PPO-ptx model are preferred to those from the 175B GPT-3. Error bars throughout the paper are 95% confidence intervals

5 问题讨论

暂略。 见原文。

参考文献

  • Abramson, J., Ahuja, A., Barr, I., Brussee, A., Carnevale, F., Cassin, M., Chhaparia, R., Clark, S., Damoc, B., Dudzik, A., et~al. (2020). Imitating interactive intelligence. arXiv preprint arXiv:2012.05672
  • Achiam, J., Held, D., Tamar, A., and Abbeel, P. (2017). Constrained policy optimization. In International Conference on Machine Learning pages 22–31. PMLR.
  • Anthony, T., Tian, Z., and Barber, D. (2017). Thinking fast and slow with deep learning and tree search. arXiv preprint arXiv:1705.08439
  • Aribandi, V., Tay, Y., Schuster, T., Rao, J., Zheng, H.~S., Mehta, S.~V., Zhuang, H., Tran, V.~Q., Bahri, D., Ni, J., et~al. (2021). Ext5: Towards extreme multi-task scaling for transfer learning. arXiv preprint arXiv:2111.10952
  • Askell, A., Bai, Y., Chen, A., Drain, D., Ganguli, D., Henighan, T., Jones, A., Joseph, N., Mann, B., DasSarma, N., et~al. (2021). A general language assistant as a laboratory for alignment. arXiv preprint arXiv:2112.00861
  • Bahdanau, D., Brakel, P., Xu, K., Goyal, A., Lowe, R., Pineau, J., Courville, A., and Bengio, Y. (2016). An actor-critic algorithm for sequence prediction. arXiv preprint arXiv:1607.07086
  • Bahdanau, D., Hill, F., Leike, J., Hughes, E., Hosseini, A., Kohli, P., and Grefenstette, E. (2018). Learning to understand goal specifications by modelling reward. arXiv preprint arXiv:1806.01946
  • Bender, E.~M., Gebru, T., McMillan-Major, A., and Shmitchell, S. (2021). On the dangers of stochastic parrots: Can language models be too big? In Proceedings of the 2021 ACM Conference on Fairness, Accountability, and Transparency pages 610–623.
  • Blodgett, S.~L., Barocas, S., Daum{'e}~III, H., and Wallach, H. (2020). Language (technology) is power: A critical survey of” bias” in nlp. arXiv preprint arXiv:2005.14050
  • B{"o}hm, F., Gao, Y., Meyer, C.~M., Shapira, O., Dagan, I., and Gurevych, I. (2019). Better rewards yield better summaries: Learning to summarise without references. arXiv preprint arXiv:1909.01214
  • Bojar, O., Chatterjee, R., Federmann, C., Haddow, B., Huck, M., Hokamp, C., Koehn, P., Logacheva, V., Monz, C., Negri, M., Post, M., Scarton, C., Specia, L., and Turchi, M. (2015). Findings of the 2015 workshop on statistical machine translation. In Proceedings of the Tenth Workshop on Statistical Machine Translation pages 1–46, Lisbon, Portugal. Association for Computational Linguistics.
  • Bommasani, R., Hudson, D.~A., Adeli, E., Altman, R., Arora, S., von Arx, S., Bernstein, M.~S., Bohg, J., Bosselut, A., Brunskill, E., et~al. (2021). On the opportunities and risks of foundation models. arXiv preprint arXiv:2108.07258
  • Bostrom, N. (2014). Superintelligence Dunod.
  • Brown, T.~B., Mann, B., Ryder, N., Subbiah, M., Kaplan, J., Dhariwal, P., Neelakantan, A., Shyam, P., Sastry, G., Askell, A., et~al. (2020). Language models are few-shot learners. arXiv preprint arXiv:2005.14165
  • Buchanan, B., Lohn, A., Musser, M., and Sedova, K. (2021). Truth, lies, and automation. Technical report, Center for the Study of Emerging Technology.
  • Caliskan, A., Bryson, J.~J., and Narayanan, A. (2017). Semantics derived automatically from language corpora contain human-like biases. Science 356(6334):183–186.
  • Carlini, N., Tramer, F., Wallace, E., Jagielski, M., Herbert-Voss, A., Lee, K., Roberts, A., Brown, T., Song, D., Erlingsson, U., et~al. (2021). Extracting training data from large language models. In 30th USENIX Security Symposium (USENIX Security 21) pages 2633–2650.
  • Chen, M., Tworek, J., Jun, H., Yuan, Q., Pinto, H. P. d.~O., Kaplan, J., Edwards, H., Burda, Y., Joseph, N., Brockman, G., et~al. (2021). Evaluating large language models trained on code. arXiv preprint arXiv:2107.03374
  • Cho, W.~S., Zhang, P., Zhang, Y., Li, X., Galley, M., Brockett, C., Wang, M., and Gao, J. (2018). Towards coherent and cohesive long-form text generation. arXiv preprint arXiv:1811.00511
  • Choi, E., He, H., Iyyer, M., Yatskar, M., Yih, W.-t., Choi, Y., Liang, P., and Zettlemoyer, L. (2018). Quac: Question answering in context. In Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing pages 2174–2184.
  • Christiano, P., Cotra, A., and Xu, M. (2021). Eliciting latent knowledge: How to tell if your eyes deceive you. https://www.alignmentforum.org/posts/qHCDysDnvhteW7kRd/arc-s-first-technical-report-eliciting-latent-knowledge
  • Christiano, P., Shlegeris, B., and Amodei, D. (2018). Supervising strong learners by amplifying weak experts. arXiv preprint arXiv:1810.08575
  • Christiano, P.~F., Leike, J., Brown, T., Martic, M., Legg, S., and Amodei, D. (2017). Deep reinforcement learning from human preferences. In Advances in Neural Information Processing Systems pages 4299–4307.
  • Dathathri, S., Madotto, A., Lan, J., Hung, J., Frank, E., Molino, P., Yosinski, J., and Liu, R. (2019). Plug and play language models: A simple approach to controlled text generation. arXiv preprint arXiv:1912.02164
  • Dhamala, J., Sun, T., Kumar, V., Krishna, S., Pruksachatkun, Y., Chang, K.-W., and Gupta, R. (2021). Bold: Dataset and metrics for measuring biases in open-ended language generation. In Proceedings of the 2021 ACM Conference on Fairness, Accountability, and Transparency pages 862–872.
  • Dinan, E., Fan, A., Williams, A., Urbanek, J., Kiela, D., and Weston, J. (2019a). Queens are powerful too: Mitigating gender bias in dialogue generation. arXiv preprint arXiv:1911.03842
  • Dinan, E., Humeau, S., Chintagunta, B., and Weston, J. (2019b). Build it break it fix it for dialogue safety: Robustness from adversarial human attack. arXiv preprint arXiv:1908.06083
  • Dua, D., Wang, Y., Dasigi, P., Stanovsky, G., Singh, S., and Gardner, M. (2019). Drop: A reading comprehension benchmark requiring discrete reasoning over paragraphs. arXiv preprint arXiv:1903.00161
  • Fedus, W., Zoph, B., and Shazeer, N. (2021). Switch transformers: Scaling to trillion parameter models with simple and efficient sparsity. arXiv preprint arXiv:2101.03961
  • Gabriel, I. (2020). Artificial intelligence, values, and alignment. Minds and machines 30(3):411–437.
  • Gehman, S., Gururangan, S., Sap, M., Choi, Y., and Smith, N.~A. (2020). Realtoxicityprompts: Evaluating neural toxic degeneration in language models. arXiv preprint arXiv:2009.11462
  • Hancock, B., Bordes, A., Mazare, P.-E., and Weston, J. (2019). Learning from dialogue after deployment: Feed yourself, chatbot! arXiv preprint arXiv:1901.05415
  • Henderson, P., Sinha, K., Angelard-Gontier, N., Ke, N.~R., Fried, G., Lowe, R., and Pineau, J. (2018). Ethical challenges in data-driven dialogue systems. In Proceedings of the 2018 AAAI/ACM Conference on AI, Ethics, and Society pages 123–129.
  • Huang, P.-S., Zhang, H., Jiang, R., Stanforth, R., Welbl, J., Rae, J., Maini, V., Yogatama, D., and Kohli, P. (2019). Reducing sentiment bias in language models via counterfactual evaluation. arXiv preprint arXiv:1911.03064
  • Ibarz, B., Leike, J., Pohlen, T., Irving, G., Legg, S., and Amodei, D. (2018). Reward learning from human preferences and demonstrations in atari. In Advances in neural information processing systems pages 8011–8023.
  • Irving, G., Christiano, P., and Amodei, D. (2018). {AI} safety via debate. arXiv preprint arXiv:1805.00899
  • Jaques, N., Ghandeharioun, A., Shen, J.~H., Ferguson, C., Lapedriza, A., Jones, N., Gu, S., and Picard, R. (2019). Way off-policy batch deep reinforcement learning of implicit human preferences in dialog. arXiv preprint arXiv:1907.00456
  • Kenton, Z., Everitt, T., Weidinger, L., Gabriel, I., Mikulik, V., and Irving, G. (2021). Alignment of language agents. arXiv preprint arXiv:2103.14659
  • Keskar, N.~S., McCann, B., Varshney, L.~R., Xiong, C., and Socher, R. (2019). Ctrl: A conditional transformer language model for controllable generation. arXiv preprint arXiv:1909.05858
  • Khashabi, D., Min, S., Khot, T., Sabharwal, A., Tafjord, O., Clark, P., and Hajishirzi, H. (2020). Unifiedqa: Crossing format boundaries with a single qa system. arXiv preprint arXiv:2005.00700
  • Kirk, H., Jun, Y., Iqbal, H., Benussi, E., Volpin, F., Dreyer, F.~A., Shtedritski, A., and Asano, Y.~M. (2021). How true is gpt-2? an empirical analysis of intersectional occupational biases. arXiv preprint arXiv:2102.04130
  • Krause, B., Gotmare, A.~D., McCann, B., Keskar, N.~S., Joty, S., Socher, R., and Rajani, N.~F. (2020). Gedi: Generative discriminator guided sequence generation. arXiv preprint arXiv:2009.06367
  • Kreutzer, J., Khadivi, S., Matusov, E., and Riezler, S. (2018). Can neural machine translation be improved with user feedback? arXiv preprint arXiv:1804.05958
  • Lawrence, C. and Riezler, S. (2018). Improving a neural semantic parser by counterfactual learning from human bandit feedback. arXiv preprint arXiv:1805.01252
  • Leike, J., Krueger, D., Everitt, T., Martic, M., Maini, V., and Legg, S. (2018). Scalable agent alignment via reward modeling: a research direction. arXiv preprint arXiv:1811.07871
  • Leike, J., Martic, M., Krakovna, V., Ortega, P.~A., Everitt, T., Lefrancq, A., Orseau, L., and Legg, S. (2017). {AI} safety gridworlds. arXiv preprint arXiv:1711.09883
  • Liang, P.~P., Wu, C., Morency, L.-P., and Salakhutdinov, R. (2021). Towards understanding and mitigating social biases in language models. In International Conference on Machine Learning pages 6565–6576. PMLR.
  • Lin, S., Hilton, J., and Evans, O. (2021). Truthfulqa: Measuring how models mimic human falsehoods. arXiv preprint arXiv:2109.07958
  • Liu, H., Dacon, J., Fan, W., Liu, H., Liu, Z., and Tang, J. (2019). Does gender matter? towards fairness in dialogue systems. arXiv preprint arXiv:1910.10486
  • Madaan, A., Tandon, N., Clark, P., and Yang, Y. (2022). Memory-assisted prompt editing to improve gpt-3 after deployment. arXiv preprint arXiv:2201.06009
  • Manela, D. d.~V., Errington, D., Fisher, T., van Breugel, B., and Minervini, P. (2021). Stereotype and skew: Quantifying gender bias in pre-trained and fine-tuned language models. arXiv preprint arXiv:2101.09688
  • Mishra, S., Khashabi, D., Baral, C., and Hajishirzi, H. (2021). Cross-task generalization via natural language crowdsourcing instructions. arXiv preprint arXiv:2104.08773
  • Nadeem, M., Bethke, A., and Reddy, S. (2020). Stereoset: Measuring stereotypical bias in pretrained language models. arXiv preprint arXiv:2004.09456
  • Nahian, M. S.~A., Frazier, S., Harrison, B., and Riedl, M. (2021). Training value-aligned reinforcement learning agents using a normative prior. arXiv preprint arXiv:2104.09469
  • Nakano, R., Hilton, J., Balaji, S., Wu, J., Ouyang, L., Kim, C., Hesse, C., Jain, S., Kosaraju, V., Saunders, W., et~al. (2021). Webgpt: Browser-assisted question-answering with human feedback. arXiv preprint arXiv:2112.09332
  • Nallapati, R., Zhou, B., Gulcehre, C., Xiang, B., et~al. (2016). Abstractive text summarization using sequence-to-sequence rnns and beyond. arXiv preprint arXiv:1602.06023
  • Nangia, N., Vania, C., Bhalerao, R., and Bowman, S.~R. (2020). {CrowS-Pairs: A Challenge Dataset for Measuring Social Biases in Masked Language Models In Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing Online. Association for Computational Linguistics.
  • Ngo, H., Raterink, C., Ara{'u}jo, J.~G., Zhang, I., Chen, C., Morisot, A., and Frosst, N. (2021). Mitigating harm in language models with conditional-likelihood filtration. arXiv preprint arXiv:2108.07790
  • Perez, E., Karamcheti, S., Fergus, R., Weston, J., Kiela, D., and Cho, K. (2019). Finding generalizable evidence by learning to convince q\&a models. arXiv preprint arXiv:1909.05863
  • Qian, Y., Muaz, U., Zhang, B., and Hyun, J.~W. (2019). Reducing gender bias in word-level language models with a gender-equalizing loss function. arXiv preprint arXiv:1905.12801
  • Radford, A., Wu, J., Child, R., Luan, D., Amodei, D., and Sutskever, I. (2019). Language models are unsupervised multitask learners. OpenAI Blog 1(8):9.
  • Rae, J.~W., Borgeaud, S., Cai, T., Millican, K., Hoffmann, J., Song, F., Aslanides, J., Henderson, S., Ring, R., Young, S., et~al. (2021). Scaling language models: Methods, analysis \& insights from training gopher. arXiv preprint arXiv:2112.11446
  • Rajpurkar, P., Jia, R., and Liang, P. (2018). Know what you don’t know: Unanswerable questions for squad. arXiv preprint arXiv:1806.03822
  • Rudinger, R., Naradowsky, J., Leonard, B., and {Van Durme B. (2018). Gender bias in coreference resolution. In Proceedings of the 2018 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies New Orleans, Louisiana. Association for Computational Linguistics.
  • Sanh, V., Webson, A., Raffel, C., Bach, S.~H., Sutawika, L., Alyafeai, Z., Chaffin, A., Stiegler, A., Scao, T.~L., Raja, A., et~al. (2021). Multitask prompted training enables zero-shot task generalization. arXiv preprint arXiv:2110.08207
  • Schick, T., Udupa, S., and Schutze, H. (2021). Self-diagnosis and self-debiasing: A proposal for reducing corpus-based bias in nlp. arXiv preprint arXiv:2103.00453
  • Schulman, J., Moritz, P., Levine, S., Jordan, M., and Abbeel, P. (2016). High-dimensional continuous control using generalized advantage estimation. In Proceedings of the International Conference on Learning Representations (ICLR)
  • Schulman, J., Wolski, F., Dhariwal, P., Radford, A., and Klimov, O. (2017). Proximal policy optimization algorithms. arXiv preprint arXiv:1707.06347
  • Sheng, E., Chang, K.-W., Natarajan, P., and Peng, N. (2019). The woman worked as a babysitter: On biases in language generation. arXiv preprint arXiv:1909.01326
  • Silver, D., Hubert, T., Schrittwieser, J., Antonoglou, I., Lai, M., Guez, A., Lanctot, M., Sifre, L., Kumaran, D., Graepel, T., et~al. (2017). Mastering chess and shogi by self-play with a general reinforcement learning algorithm. arXiv preprint arXiv:1712.01815
  • Soares, N., Fallenstein, B., Armstrong, S., and Yudkowsky, E. (2015). Corrigibility. In Workshops at the Twenty-Ninth AAAI Conference on Artificial Intelligence
  • Socher, R., Perelygin, A., Wu, J., Chuang, J., Manning, C.~D., Ng, A.~Y., and Potts, C. (2013). Recursive deep models for semantic compositionality over a sentiment treebank. In Proceedings of the 2013 conference on empirical methods in natural language processing pages 1631–1642.
  • Solaiman, I., Brundage, M., Clark, J., Askell, A., Herbert-Voss, A., Wu, J., Radford, A., Krueger, G., Kim, J.~W., Kreps, S., et~al. (2019). Release strategies and the social impacts of language models. arXiv preprint arXiv:1908.09203
  • Solaiman, I. and Dennison, C. (2021). Process for adapting language models to society (palms) with values-targeted datasets. arXiv preprint arXiv:2106.10328
  • Stiennon, N., Ouyang, L., Wu, J., Ziegler, D.~M., Lowe, R., Voss, C., Radford, A., Amodei, D., and Christiano, P. (2020). Learning to summarize from human feedback. arXiv preprint arXiv:2009.01325
  • Tamkin, A., Brundage, M., Clark, J., and Ganguli, D. (2021). Understanding the capabilities, limitations, and societal impact of large language models. arXiv preprint arXiv:2102.02503
  • Thoppilan, R., De~Freitas, D., Hall, J., Shazeer, N., Kulshreshtha, A., Cheng, H.-T., Jin, A., Bos, T., Baker, L., Du, Y., et~al. (2022). Lamda: Language models for dialog applications. arXiv preprint arXiv:2201.08239
  • Vig, J., Gehrmann, S., Belinkov, Y., Qian, S., Nevo, D., Singer, Y., and Shieber, S.~M. (2020). Investigating gender bias in language models using causal mediation analysis. In NeurIPS
  • Volske, M., Potthast, M., Syed, S., and Stein, B. (2017). Tl; dr: Mining reddit to learn automatic summarization. In Proceedings of the Workshop on New Frontiers in Summarization pages 59–63.
  • Wang, A., Pruksachatkun, Y., Nangia, N., Singh, A., Michael, J., Hill, F., Levy, O., and Bowman, S.~R. (2019). Superglue: A stickier benchmark for general-purpose language understanding systems. arXiv preprint arXiv:1905.00537
  • Wei, J., Bosma, M., Zhao, V.~Y., Guu, K., Yu, A.~W., Lester, B., Du, N., Dai, A.~M., and Le, Q.~V. (2021). Finetuned language models are zero-shot learners. arXiv preprint arXiv:2109.01652
  • Weidinger, L., Mellor, J., Rauh, M., Griffin, C., Uesato, J., Huang, P.-S., Cheng, M., Glaese, M., Balle, B., Kasirzadeh, A., et~al. (2021). Ethical and social risks of harm from language models. arXiv preprint arXiv:2112.04359
  • Welbl, J., Glaese, A., Uesato, J., Dathathri, S., Mellor, J., Hendricks, L.~A., Anderson, K., Kohli, P., Coppin, B., and Huang, P.-S. (2021). Challenges in detoxifying language models. arXiv preprint arXiv:2109.07445
  • Wu, J., Ouyang, L., Ziegler, D.~M., Stiennon, N., Lowe, R., Leike, J., and Christiano, P. (2021). Recursively summarizing books with human feedback. arXiv preprint arXiv:2109.10862
  • Xu, A., Pathak, E., Wallace, E., Gururangan, S., Sap, M., and Klein, D. (2021). Detoxifying language models risks marginalizing minority voices. arXiv preprint arXiv:2104.06390
  • Xu, J., Ju, D., Li, M., Boureau, Y.-L., Weston, J., and Dinan, E. (2020). Recipes for safety in open-domain chatbots. arXiv preprint arXiv:2010.07079
  • Yi, S., Goel, R., Khatri, C., Cervone, A., Chung, T., Hedayatnia, B., Venkatesh, A., Gabriel, R., and Hakkani-Tur, D. (2019). Towards coherent and engaging spoken dialog response generation using automatic conversation evaluators. arXiv preprint arXiv:1904.13015
  • Zellers, R., Holtzman, A., Bisk, Y., Farhadi, A., and Choi, Y. (2019). Hellaswag: Can a machine really finish your sentence? In Association for Computational Linguistics pages 4791–4800.
  • Zhao, M., Anderson, P., Jain, V., Wang, S., Ku, A., Baldridge, J., and Ie, E. (2021). On the evaluation of vision-and-language navigation instructions. arXiv preprint arXiv:2101.10504
  • Zhou, W. and Xu, K. (2020). Learning to compare for better training and evaluation of open domain natural language generation models. arXiv preprint arXiv:2002.05058
  • Ziegler, D.~M., Stiennon, N., Wu, J., Brown, T.~B., Radford, A., Amodei, D., Christiano, P., and Irving, G. (2019). Fine-tuning language models from human preferences. arXiv preprint arXiv:1909.08593

附录 A: Prompt 数据详情

Prompt 长什么样非常重要,因此这里给出完整附录。此外,有些 prompts 很有意思。译注。

A.1 Labeler-written prompts

We first give slightly more details on our prompt boostrapping process. As previously mentioned, for the majority of the project, we obtained prompts directly from external users of the instruct beta models in the OpenAI API. However, this strategy only works once you have a model that accepts instruction-like prompts. In order to train the very first such model, we asked contractors to write prompts themselves. We asked labelers to write three kinds of prompts:

  • Plain: We simply ask the labelers to come up with an arbitrary task, while ensuring diversity of tasks.
  • Few-shot: We ask the labelers to come up with an instruction, and multiple query/response pairs for that instruction. For example, the instruction could be “Give the sentiment for a tweet,” and the queries would be tweets and the responses either “Positive” or “Negative.” We can then format these as few-shot prompts like those in Brown et al. (2020). With K query-response pairs, we create K training examples using the other K-1 in the context.
  • User-based: We had a number of use-cases stated in applications to the OpenAI API. We asked labelers to come up with prompts corresponding to these use cases.

In order to preserve the anonymity of the application information, we had a separate labeler create vague high level tasks based on looking at a list of applications, modifying the task descriptions to eliminate any information that were specific to a given application. This data was used to train the first InstructGPT model via supervised learning, which was deployed in beta in the API in early 2021.

A.2 API user prompts

For API prompts, we use prompts submitted by users to the aforementioned earlier version of the InstructGPT model on the OpenAI API Playground. Throughout the paper, we only use data from the Playground, rather than customers using our model in production, as it was easier to get informed consent: every time a user switched to an InstructGPT model, an alert message would pop up stating that prompts submitted to these models could be used to train future versions of our models. We also communicated this in a message on the developer Slack channel upon launching the beta of the InstructGPT models. We filter out prompts from the training split containing personally identifiable information (PII).

To ensure a diversity of use cases, we heuristically deduplicate prompts by checking for prompts that share a long common prefix, and limited the number of prompts to roughly 200 per organization. In addition, we create train, validation, and test splits based on organization IDs, so that e.g. the validation set contains different use cases than the training set. We conceptualized API requests as belonging to one of ten use cases: generation, open QA, closed QA, brainstorming, chat, rewriting, summarization, classification, extraction, or other. Below, we show fictional but realistic prompts from a variety of use cases:

A.2.1 从 InstructGPT API (Playground) 收集上来的 user prompts 示例

Use Case Example
brainstorming List five ideas for how to regain enthusiasm for my career
brainstorming What are some key points I should know when studying Ancient Greece?
brainstorming What are 4 questions a user might have after reading the instruction manual for a trash compactor?

{user manual}

1.
brainstorming What are 10 science fiction books I should read next?
classification Take the following text and rate, on a scale from 1-10, how sarcastic the person is being (1 = not at all, 10 = extremely sarcastic). Also give an explanation

{text}

Rating:
classification This is a list of tweets and the sentiment categories they fall into.

Tweet: {tweet_content1}
Sentiment: {sentiment1}

Tweet: {tweet_content2}
Sentiment: {sentiment2}
classification {java code}

What language is the code above written in?
classification You are a very serious professor, and you check papers to see if they contain missing citations. Given the text, say whether it is missing an important citation (YES/NO) and which sentence(s) require citing.

{text of paper}
extract Extract all course titles from the table below:

| Title | Lecturer | Room |
| Calculus 101 | Smith | Hall B |
| Art History | Paz | Hall A |
extract Extract all place names from the article below:

{news article}
extract Given the following list of movie titles, write down any names of cities in the titles.

{movie titles}
generation Write a creative ad for the following product to run on Facebook aimed at parents:

Product: {product description}
generation Write a short story where a brown bear to the beach, makes friends with a seal, and then return home.
generation Here’s a message to me:

{email}


Here are some bullet points for a reply:

{message}


Write a detailed reply
generation This is an article about how to write a cover letter when applying for jobs:

It’s important to spend some time
generation write rap lyrics on the topics mentioned in this news article:

{article}
rewrite This is the summary of a Broadway play:
“”“
{summary}
“”“

This is the outline of the commercial for that play:
“””
rewrite Translate this sentence to Spanish:

rewrite Create turn-by-turn navigation given this text:

Go west on {road1} unto you hit {road2}. then take it east to {road3}.
Desination will be a red barn on the right

1.
rewrite Rewrite the following text to be more light-hearted:

{very formal text}
chat The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.

Human: Hello, who are you?
AI: I am an AI created by OpenAI. How can I help you today?
Human: I’d like to cancel my subscription.
AI:
chat Marv is a chatbot that reluctantly answers questions with sarcastic responses:

You: How many pounds are in a kilogram?
Marv: This again? There are 2.2 pounds in a kilogram. Please make a note of this.
You: What does HTML stand for?
Marv: Was Google too busy? Hypertext Markup Language. The T is for try to ask better questions in the future.
You: When did the first airplane fly?
Marv:
chat This is a conversation with an enlightened Buddha. Every response is full of wisdom and love.

Me: How can I achieve greater peace and equanimity?
Buddha:
closed qa Help me answer questions about the following short story:

{story}

What is the moral of the story?
closed qa Answer the following question:
What shape is the earth?

A) A circle
B) A sphere
C) An ellipse
D) A plane
closed qa Tell me how hydrogen and helium are different, using the following facts:
{list of facts}
open qa I am a highly intelligent question answering bot. If you ask me a question that is rooted in truth, I will give you the answer. If you ask me a question that is nonsense, trickery, or has no clear answer, I will respond with “Unknown”.

Q: What is human life expectancy in the United States?
A: Human life expectancy in the United States is 78 years.
Q: Who was president of the United States in 1955?
A:
open qa Who built the statue of liberty?
open qa How do you take the derivative of the sin function?
open qa who are the indiginous people of New Zealand?
summarization Summarize this for a second-grade student:
{text}
summarization {news article}

Tl;dr:
summarization {chat transcript}

Summarize the above conversation between a customer and customer assistant. Make sure to state any complaints that the customer has.
other start with where
other Look up “cowboy” on Google and give me the results.
other Johnathan Silver goes to the market every day, and brings back a

Next, we list some schematic examples of API requests for each use-case category, for prompts submitted to GPT-3 models. These are generally less ‘instruction-style’, and contain more explicit prompting. Note that there are some prompts where the user intent is unclear.

A.2.2 从 GPT-3 API 收集上来的 user prompts 示例

Use Case Example
brainstorming indie movie ideas:
- A guy travels to South America to become a shaman.
- A documentary about the world of juggling.
brainstorming Baby name ideas for a boy:
1. Alfred
2. Theo
3.
brainstorming Tell me a list of topics related to:
- interior design
- sustainable ecosyste
ms - fake plants
brainstorming Name some rare gems
classification This is a tweet sentiment classifier.

{tweet}
Sentiment: negative
===
{tweet}
Sentiment: neutral
===
{tweet}
Sentiment:
classification The following is a list of products and the kind of product they are.
Product: {product}. Type: {type}
Product: {product}. Type: {type}
Product: {product}. Type:
classification The following is a list of companies and the categories they fall into:
Apple, Facebook, Fedex
Apple
Category: Technology
Facebook
Category: Social Media
Fedex
Category:
extract Text: {text}
Keywords:
generation “Hey, what are you doing there?” Casey was startled. He hadn’t even begun to
generation The name of the next Star Wars movie is
generation This is the research for an essay:
===
{description of research}
===
Write a high school essay on these topics:
===
generation Write an outline for an essay about John von Neumann and his contributions to computing:
I. Introduction, his life and background
A: His early life
B:
rewrite Covert my resume into a profile overview.
{resume}
Profile overview:
rewrite Rephrase this for me: “I can’t seem to find out how to work this darn thing.”
Alternate phrasing: “
rewrite Original: She no go to sleep.
Standard American English: She didn’t go to sleep

Original: It real bad for I to make do of this.
Standard American English:
chat The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.

Human: Hello, who are you?
AI: I am an AI created by OpenAI. How can I help you today?
Human: I’m feeling kind of down today.
AI:
chat This is a conversation with Steven. Steven likes to watch Netflix and hasn’t left his home in 2 weeks.

John: Hey man what’s up?
Steven: Exactly the same thing as yesterday. you know.
John: So we’re going to go see a movie on Thursday, want to come?
Steven: Ummmm don’t think so….
closed qa When you drop a heavy stone from a tree, what happens?
A. The stone falls to the ground.
B: The stone stays in the tree.
C: The stone floats.
D: Nothing happens.
Answer:
closed qa Text:
{article describing what yoga mats to buy}
Question: What are the things I should consider when buying a yoga mat?
Answer:
open qa Q: Who is Batman?
A: Batman is a fictional comic book character.
Q: What is torsalplexity?
A: ?
Q: What is Devz9?
A: ?
Q: Who is George Lucas?
A: George Lucas is American film director and producer famous for creating Star Wars.
Q: What is the capital of California?
A:
open qa Who was the best human who ever lived?
open qa Q: Who is Leonardo da Vinci?
A:
summarization My second grader asked me what this passage means.

“”“
{text}
“”“

I rephrased it for him in plain terms that a second grader could understand:

“””
summarization ””“
{text}
“”“

I summarized the above as:
other She said, and I quote
AI:
other - I like to play Call of Duty
- I like to play Call of Duty
- I like to play Call of Duty
- I like to play Call of Duty

A.3 数据集大小:SFT 15k / RM 50k / PPO 47k

用来 train/validate SFT, RM, RL 三个模型的数据集大小,以及多少是标注员写的,多少来自 OpenAI API 的用户数据,

Table 6: Dataset sizes, in terms of number of prompts.

split source size
SFT train labeler 11,295
SFT train customer 1,430
SFT valid labeler 1,550
SFT valid customer 103
SFT 总计   ~15k
RM train labeler 6,623
RM train customer 26,584
RM valid labeler 3,488
RM valid customer 14,399
RM 总计   ~50k
PPO train customer 31,144
PPO valid customer 16,185
PPO 总计   ~47k

For SFT, note that we have many more labeler-written prompts than customer prompts—this is because, at the start of the project, we had labelers write instructions with a user interface that asked them to give an overarching template instruction as well as few-shot examples for that instruction.

We synthetically constructed multiple SFT datapoints from the same instruction by sampling different sets of few-shot examples.

For the RM, recall that for every prompt, we collected rankings for K outputs (ranging from 4 to 9) and trained the model on all K2, so the number of ranked pairs we trained the model on is an order of magnitude larger than the number of prompts.

A.4 数据多样性

The data that we collect spans a wide range of categories and use cases. Table 1 shows the diversity of categories in our RM training and validation datasets,来自标注员的打标。The distribution of categories for the PPO datasets was similar. We additionally show a subset of our labeled prompt metadata in Table 7.

Table 7: Dataset annotations

Annotation RM test RM train SFT valid SFT train SFT valid
Ambiguous 7.9% 8.0% 5.1% 6.4%
Sensitive content 6.9% 5.3% 0.9% 1.0%
Identity dependent 0.9% 0.3%
Closed domain 11.8% 19.4% 22.9% 27.4% 40.6%
Continuation style 15.5% 16.2% 17.9% 21.6%
Requests opinionated content 11.2% 7.7% 7.5% 8.6% 3.4%
Requests advice 3.9%   -
Requests moral judgment 0.8% 1.1% 0.3% 0.3% 0.0%
Contains explicit safety constraints 0.4% 0.4% 0.3% 0.0%
Contains other explicit constraints 26.3% 28.9% 25.6% 20.7%
Intent unclear 7.9%

Note that our annotation fields changed over the course of the project, so not every prompt was annotated for every field.

We used a lightweight classifier (langid.py) to classify the language of all instructions in our dataset. Empirically, around 96% of our dataset (110k datapoints) is classified as English, although we estimate that the actual fraction may be 99% or higher, due to classifier inaccuracies. Besides English, a small minority of prompts were found in at least 20 other languages: Spanish, French, German, Portuguese, Italian, Dutch, Romanian, Catalan, Chinese, Japanese, Swedish, Polish, Danish, Turkish, Indonesian, Czech, Norwegian, Korean, Finnish, Hungarian, Hebrew, Russian, Lithuanian, Esperanto, Slovak, Croatian, Swahili, Estonian, Slovenian, Arabic, Thai, Vietnamese, Malayalam, Greek, Albanian, and Tibetan.

Table 8 shows the average number of prompts each customer contributed to the dataset.

Table 8: Average prompts per customer

Model Split Prompts per customer
SFT train 1.65
SFT valid 1.87
RM t rain 5.35
RM v alid 27.96
PPO train 6.01
PPO valid 31.55
test 1.81

Table 9: Prompt lengths by dataset

Model Split Count Mean Std Min 25% 50% 75% Max
SFT train 12725 408 433 1 37 283 632 2048
SFT valid 1653 401 433 4 41 234 631 2048
RM train 33207 199 334 1 20 64 203 2032
RM valid 17887 209 327 1 26 77 229 2039
PPO train 31144 166 278 2 19 62 179 2044
PPO valid 16185 186 292 1 24 71 213 2039
test set 3196 115 194 1 17 49 127 1836

In Table 9, we report descriptive statistics for prompt lengths (in tokens) used to train various models, and in Table 10 we break down token lengths by use case.

Table 10: Prompt lengths by category

Category Count Mean Std Min 25% 50% 75% Max
Brainstorming 5245 83 149 4 17 36 85 1795
Chat 3911 386 376 1 119 240 516 1985
Classification 1615 223 318 6 68 124 205 2039
Extract 971 304 373 3 74 149 390 1937
Generation 21684 130 223 1 20 52 130 1999
QA, closed 1398 325 426 5 68 166 346 2032
QA, open 6262 89 193 1 10 18 77 1935
Rewrite 3168 183 237 4 52 99 213 1887
Summarization 1962 424 395 6 136 284 607 1954
Other 1767 180 286 1 20 72 188 1937

Table 11: Prompt and demonstration lengths

Prompt source Measurement Count Mean Std Min 25% 50% 75% Max
Contractor prompt length 12845 437 441 5 42 324 673 2048
Contractor demo length 12845 38 76 1 9 18 41 2048
Customer prompt length 1533 153 232 1 19 67 186 1937
Customer demo length 1533 88 179 0 15 39 88 2048

Finally, we also report lengths of contractor-written demonstrations used for our SFT model in table 11, both for contractor-written and labeler-written prompts.

附录 B:Additional human data collection details

暂略。见原文。

附录 C:一些模型细节

  • 所有模型都使用 GPT-3 架构(Brown et al., 2020)。
  • 对于奖励模型和值函数,原始模型的 unembedding 层替换为一个 projection 层,最终输出一个标量值。
  • 所有模型都使用 fp16 权重和激活,with fp32 master copies of weights。
  • 所有模型使用与 Brown et al. (2020)中相同的字节对编码(byte pair encodings)。
  • 所有的模型和 RL 策略都使用长度为 2k token 的上下文。
  • 输入 prom:长度超过 1k token 的都不要;
  • 输出 response:限制最大响应长度为 1k token
  • 所有模型都使用 Adam optimizer 进行训练,设置 β1 = 0.9β2 = 0.95

C.1 SFT 训练细节

SFT 模型训练

  • 16 epochs
  • residual dropout 0.2
  • cosine LR schedule,降至到初始学习率的 10%,没有 learning rate warmup。
  • 1.3B 和 6B 模型:LR 9.65e-6,batch 32 batch。在 7 个 LR 上做 geometric search 选出来的 LR。
  • 175B 模型:LR 5.03e-6,batch 8。在 5 个 LR 上做 geometric search 选出来的 LR。
  • 还使用 geometric search 来对 epoch 数量做调优。

最终模型是基于 RM 分数选择的,我们发现与 validation loss 相比,RM 分数更能预测人类偏好结果。

C.2 RM 训练细节

同一个 6B RM 模型用于所有尺寸的 PPO 模型。 175B RM 有可能实现更低的 validation loss,但

  1. 训练不稳定,因此不适合用作 PPO 值函数的初始化,
  2. 使用 175B RM 和值函数大大增加了 PPO 的算力需求。

初步实验结果显示,6B RM 模型在大范围的学习率上都很稳定,能训练出一样强大的 PPO 模型。

The final reward model was initialized from a 6B GPT-3 model that was fine-tuned on a variety of public NLP datasets (ARC, BoolQ, CoQA, DROP, MultiNLI, OpenBookQA, QuAC, RACE, and Winogrande). This was mostly for historical reasons; we find similar results when initializing the RM from the GPT-3 or SFT models. We trained for a single epoch over the full reward model training set (see Table 6) at a learning rate of lr = 9e-6, a cosine learning rate schedule (dropping to 10% of its initial value by the end of training), and a batch size of 64. Training did not appear to be very sensitive to the learning rate or schedule; changes of up to 50% in the learning rate resulted in similar performance. Training was quite sensitive to the number of epochs: multiple epochs quickly overfit the model to the training data with obvious deterioration in the validation loss. The batch size here represents the distinct number of prompts per batch. Each prompt had between K = 4 and K = 9 labeled completions, from which there were up to K2 possible comparisons. Ties were dropped. Therefore, a single batch could contain up to 64 × K2 ≤ 2,304 comparisons.

C.3 RLHF 的初始化模型(initialization models)细节

We initialize the RLHF models from a pretrained GPT-3 model and apply supervised fine-tuning for 2 epochs on the demonstration dataset. We also mix in 10% pretraining data during fine-tuning, since we find it helpful for PPO training (see Appendix E.11 for details). Cosine learning rate schedule is used and the learning rate eventually decays to 10% of the peak learning rate. We use a batch size of 32 for 1.3B and 6B models and 8 for the 175B model. We compare a few different peak learning rates for each model and pick the one with low losses on both the demonstration and the pretraining validation datasets. A log linear sweep of 5 values of the LR’s are compared for 1.3B and 6B models and 3 values are compared for the 175B model. The resultant LR’s for the 1.3B, 6B, and 175B models are 5e-6, 1.04e-5 and 2.45e-6, respectively.

C.4 RLHF 训练细节

We then initialize the RL policies from the above supervised fine-tuned models with pretraining mix. These models are also used to compute the KL reward, in the same way as Stiennon et al. (2020), with β = 0:02 (see Equation 2). We train all the RL models for 256k episodes. These episodes include about 31k unique prompts, after filtering out prompts with PII and deduplication based on common prefixes. The batch size for each iteration is 512, with a minibatch size of 64. In other words, each batch is randomly split into 8 minibatches and is trained on for only a single inner epoch (Schulman et al., 2017). A constant learning rate is applied with a warmup over the first 10 iterations, starting with one tenth of the peak learning rate. Exponential moving averages of the weights are applied, with a decay rate of 0.992. No discount is applied when estimating the generalized advantage (Schulman et al., 2016). The PPO clip ratio is set to 0.2, and the sampling temperature is 1 for rollouts. As previously mentioned, for all PPO models we use a 6B RM and a 6B value function, and the latter is initialized from the former. By using the same 6B reward model and value function on policies of all model sizes, it’s easier to compare the effect of policy model size on policy performance. A fixed learning rate of 9e-6 for the value function is used for 1.3B and the 6B policies and 5e-6 for the 175B policy.

Our initial RLHF experiments showed regressions on public NLP datasets, such as SQuADv2 and DROP, and we mitigate the regressions by mixing in pretraining gradients during PPO training. We use 8 times more pretraining examples than the number of the RL training episodes. The pretraining data is randomly drawn from the dataset used to train the GPT-3 models. For each minibatch, we compute the PPO gradients and pretraining gradients in consecutive steps and accumulate them both into the gradient buffers. We multiply the pretraining gradients by a coefficient, γ = 27:8 (see Equation 2), to control the relative strength of gradients from PPO and pretraining distributions.

C.5 FLAN 和 T0 模型

We obtain our FLAN and T0 baselines by fine-tuning a 175B GPT-3 model on the FLAN and T0 datasets. For T0, note that we trained on the T0++ version of the dataset. Because T0 contains much more data (96M datapoints) than FLAN (1.2M datapoints), we subsampled T0 to 1 million datapoints to make the amount of training data comparable for each model. Note that the original models train on epochs where datapoints can be repeated, but in our epochs we go through every datapoint without repeats (to better match the way we trained our SFT baselines). We applied a cosine learning rate schedule, and try initial learning rates of 4e-6 and 6e-6 for each dataset. The learning rate decays to 10% of its peak at the end of training, and we use a batch size of 64 for both experiments.

To choose the best FLAN checkpoint, we use our 6B reward model to score the completions on the validation set of prompts. As shown in Figure 13, the reward saturates after the initial 400k examples of training. This indicates that training for even longer will unlikely improve the human eval performance. We picked the checkpoint with the highest RM score for our human evaluation, which is the one trained with learning rate of 4e-6 and for 896k examples.

We perform two similar experiments to find the best T0 checkpoint. In one experiment, we used a batch size of 128, a learning rate of 4e-6 and 1.28 million examples. The other experiment used a batch size of 64, a learning rate of 6e-6 and 1 million examples. Once again using the reward model score, we picked the checkpoint from the former experiment after 896k examples of training

附录 D:Automatic evaluation details

暂略。见原文。

附录 E:Additional results

暂略。见原文。

附录 F:Model samples

In this section, we provide some additional samples from both the 175B GPT-3 and 175B InstructGPT (PPO-ptx) models. We sample at T = 1 for InstructGPT, and use T = 0:7 for GPT-3, since GPT-3 performs poorly at high temperatures (this slightly disadvantages InstructGPT).

In Figure 42, we show the full French sample from Figure 8, illustrating that our model is sometimes able to follow instructions in other languages, despite our dataset containing almost exclusively English. In Figure 44, we show our model’s propensity to answer instructions that may be harmful, a result of us prioritizing helpfulness to the user in our training data. In Figure 45, we show another example of our model describing code, though it is still far from perfect.

In Figures 46–50, we show labeler-written prompts from our dataset, along with model samples and the human-written demonstration. These 5 prompts were selected from 15 to show a range of different tasks.

(略)。


Written by Human, Not by AI Written by Human, Not by AI

LM Studio: 在 Obsidian 中使用本地 LLM

作者 Dayu
2024年3月10日 13:34

本文介绍了如何在 Obsidian 中使用本地 LLM 工具 LM Studio。

准备阶段

安装 LLM

打开 LM Studio,下载 Google’s Gemma 2B Instruct。

进入 Chats 界面(下图),并选择刚刚下载好的模型:Imstudio-ai • gemma it 2B q8_0 gguf。

在对话框中输入文字,测试模型是否能正常使用。
CleanShot 2024-03-10 at 12.28.41@2x.png

进入 Local Inference Server 界面,选择刚刚下载的模型,点击 Start Server。

共会生成三个 url,复制中间的: http://localhost:1234/v1/chat/completions

CleanShot 2024-03-10 at 12.32.40@2x.png

Obsidian-text generator

在 Obsidian 中打开 Text Generator 设置界面,LLM Provider 选择 Custom。

将刚刚复制的网址(http://localhost:1234/v1/chat/completions ),黏贴到 Endpoint 中。

CleanShot 2024-03-10 at 12.36.17@2x.png

体验

然后就能在 Obsidian 中使用 Gemma 2B Instruct 了。

通过 Slash commands 或「cmd + P」打开启动器,输入 Text generator,能看到很多功能,常用的是:Text Generator: Generate Text!,默认快捷键是 cmd + J。

CleanShot 2024-03-10 at 12.52.59@2x.png

在任意笔记本中,输入问题,然后使用快捷键,即可启动本地模型,如下图。
CleanShot 2024-03-10 at 13.27.49.gif

另外,GPT4All 也是一个可本地使用的 LLM 工具,可以读取 Obsidian 的笔记本库,然后进行对话。

[译][论文] BERT:预训练深度双向 Transformers 做语言理解(Google,2019)

2024年3月10日 08:00

译者序

本文翻译自 2019 年 Google 的论文: BETT: Pre-training of Deep Bidirectional Transformers for Language Understanding

@article{devlin2018bert,
  title={BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding},
  author={Devlin, Jacob and Chang, Ming-Wei and Lee, Kenton and Toutanova, Kristina},
  journal={arXiv preprint arXiv:1810.04805},
  year={2018}
}

与 GPT 一样,BERT 也基于 transformer 架构, 从诞生时间来说,它位于 GPT-1 和 GPT-2 之间,是有代表性的现代 transformer 之一, 现在仍然在很多场景中使用,

大模型进化树,可以看到 BERT 所处的年代和位置。来自 大语言模型(LLM)综述与实用指南(Amazon,2023)

根据 Transformer 是如何工作的:600 行 Python 代码实现 self-attention 和两类 Transformer(2019), BERT 是首批 在各种自然语言任务上达到人类水平的 transformer 模型之一。 预训练和 fine-tuning 代码github.com/google-research/bert

BERT 模型只有 0.1b ~ 0.3b 大小,因此在 CPU 上也能较流畅地跑起来。

译者水平有限,不免存在遗漏或错误之处。如有疑问,敬请查阅原文。

以下是译文。



摘要

本文提出 BERT(Bidirectional Encoder Representations from Transformers, 基于 Transformers 的双向 Encoder 表示) —— 一种新的语言表示模型 (language representation model)。

  • 与最近的语言表示模型(Peters 等,2018a; Radford 等,2018)不同, BERT 利用了所有层中的左右上下文(both left and right context in all layers), 在无标签文本(unlabeled text)上 预训练深度双向表示(pretrain deep bidirectional representations)。
  • 只需添加一个额外的输出层,而无需任何 task-specific 架构改动,就可以对预训练的 BERT 模型进行微调, 创建出用于各种下游任务(例如问答和语言推理)的高效模型。

BERT 在概念上很简单,实际效果却很强大,在 11 个自然语言处理任务中刷新了目前业界最好的成绩,包括,

  • GLUE score to 80.5% (7.7% point absolute improvement)
  • MultiNLI accuracy to 86.7% (4.6% absolute improvement)
  • SQuAD v1.1 question answering Test F1 to 93.2 (1.5 point absolute improvement)
  • SQuAD v2.0 Test F1 to 83.1 (5.1 point absolute improvement)

1 引言

业界已证明,语言模型预训练(Language model pre-training) 能显著提高许多自然语言处理(NLP)任务的效果(Dai 和 Le,2015; Peters 等,2018a; Radford 等,2018; Howard 和 Ruder,2018)。 这些任务包括:

  • sentence-level tasks:例如自然语言推理(Bowman 等,2015; Williams 等,2018);
  • paraphrasing(Dolan 和 Brockett,2005):整体分析句子来预测它们之间的关系;
  • token-level tasks:例如 named entity recognition 和问答,其模型需要完成 token 级别的细粒度输出(Tjong Kim Sang 和 De Meulder,2003; Rajpurkar 等,2016)。

1.1 Pre-trained model 适配具体下游任务的两种方式

将预训练之后的语言表示(pre-trained language representations)应用到下游任务,目前有两种策略:

  1. 基于特征的方式(feature-based approach):例如 ELMo(Peters 等,2018a),使用任务相关的架构,将预训练表示作为附加特征
  2. 微调(fine-tuning):例如 Generative Pre-trained Transformer (OpenAI GPT)(Radford 等,2018), 引入最少的 task-specific 参数,通过微调所有预训练参数来训练下游任务。

这两种方法都是使用单向语言模型来学习通用语言表示

1.2 以 OpenAI GPT 为代表的单向架构存在的问题

我们认为,以上两种方式(尤其是微调)限制了 pre-trained language representation 的能力。 主要是因为其语言模型是单向的,这限制了预训练期间的架构选择范围

例如,OpenAI GPT 使用从左到右的架构(Left-to-Right Model, LRM),因此 Transformer self-attention 层中的 token 只能关注它前面的 tokens(只能用到前面的上下文):

  • 对于句子级别的任务,这将导致次优结果;
  • token 级别的任务(例如问答)使用 fine-tuning 方式效果可能非常差, 因为这种场景非常依赖双向上下文(context from both directions)。

1.3 BERT 创新之处

本文提出 BERT 来改进基于微调的方式

受 Cloze(完形填空)任务(Taylor,1953)启发,BERT 通过一个“掩码语言模型”(masked language model, MLM)做预训练, 避免前面提到的单向性带来的问题

  • MLM 随机掩盖输入中的一些 token ,仅基于上下文来预测被掩盖的单词(单词用 ID 表示)。
  • 与从左到右语言模型的预训练不同,MLM 能够同时利用左侧和右侧的上下文, 从而预训练出一个深度双向 Transformer。

除了掩码语言模型外,我们还使用“下一句预测”(next sentence prediction, NSP) 任务来联合预训练 text-pair representation。

1.4 本文贡献

  1. 证明了双向预训练对于语言表示的重要性。 与 Radford 等(2018)使用单向模型预训练不同,BERT 使用掩码模型来实现预训练的深度双向表示。 这也与 Peters 等(2018a)不同,后者使用独立训练的从左到右和从右到左的浅连接。
  2. 展示了 pre-trained representations 可以减少对许多 task-specific 架构的重度工程优化。 BERT 是第一个在大量 sentence-level 和 token-level 任务上达到了 state-of-the-art 性能的 基于微调的表示模型,超过了许多 task-specific 架构。
  3. BERT 刷新了 11 个自然语言处理任务的最好性能。

代码和预训练模型见 github.com/google-research/bert

2 相关工作

(这节不是重点,不翻译了)。

There is a long history of pre-training general language representations, and we briefly review the most widely-used approaches in this section.

2.1 无监督基于特征(Unsupervised Feature-based)的方法

Learning widely applicable representations of words has been an active area of research for decades, including non-neural (Brown et al., 1992; Ando and Zhang, 2005; Blitzer et al., 2006) and neural (Mikolov et al., 2013; Pennington et al., 2014) methods. Pre-trained word embeddings are an integral part of modern NLP systems, offering significant improvements over embeddings learned from scratch (Turian et al., 2010). To pretrain word embedding vectors, left-to-right language modeling objectives have been used (Mnih and Hinton, 2009), as well as objectives to discriminate correct from incorrect words in left and right context (Mikolov et al., 2013).

These approaches have been generalized to coarser granularities, such as

  • sentence embeddings (Kiros et al., 2015; Logeswaran and Lee, 2018)
  • paragraph embeddings (Le and Mikolov, 2014).

To train sentence representations, prior work has used objectives to rank candidate next sentences (Jernite et al., 2017; Logeswaran and Lee, 2018), left-to-right generation of next sentence words given a representation of the previous sentence (Kiros et al., 2015), or denoising autoencoder derived objectives (Hill et al., 2016).

ELMo and its predecessor (Peters et al., 2017, 2018a) generalize traditional word embedding research along a different dimension. They extract context-sensitive features from a left-to-right and a right-to-left language model. The contextual representation of each token is the concatenation of the left-to-right and right-to-left representations. When integrating contextual word embeddings with existing task-specific architectures, ELMo advances the state of the art for several major NLP benchmarks (Peters et al., 2018a) including

  • question answering (Rajpurkar et al., 2016)
  • sentiment analysis (Socher et al., 2013)
  • named entity recognition (Tjong Kim Sang and De Meulder, 2003)

Melamud et al. (2016) proposed learning contextual representations through a task to predict a single word from both left and right context using LSTMs. Similar to ELMo, their model is feature-based and not deeply bidirectional. Fedus et al. (2018) shows that the cloze task can be used to improve the robustness of text generation models.

2.2 无监督基于微调(Unsupervised Fine-tuning)的方法

As with the feature-based approaches, the first works in this direction only pre-trained word embedding parameters from unlabeled text (Collobert and Weston, 2008).

More recently, sentence or document encoders which produce contextual token representations have been pre-trained from unlabeled text and fine-tuned for a supervised downstream task (Dai and Le, 2015; Howard and Ruder, 2018; Radford et al., 2018). The advantage of these approaches is that few parameters need to be learned from scratch.

At least partly due to this advantage, OpenAI GPT (Radford et al., 2018) achieved previously state-of-the-art results on many sentencelevel tasks from the GLUE benchmark (Wang et al., 2018a). Left-to-right language model ing and auto-encoder objectives have been used for pre-training such models (Howard and Ruder, 2018; Radford et al., 2018; Dai and Le, 2015).

2.3 基于监督数据的转移学习(Transfer Learning from Supervised Data)

There has also been work showing effective transfer from supervised tasks with large datasets, such as natural language inference (Conneau et al., 2017) and machine translation (McCann et al., 2017).

Computer vision research has also demonstrated the importance of transfer learning from large pre-trained models, where an effective recipe is to fine-tune models pre-trained with ImageNet (Deng et al., 2009; Yosinski et al., 2014).

3 BERT

本节介绍 BERT 架构及实现。训练一个可用于具体下游任务的 BERT 模型,分为两个步骤:

  • 预训练:使用不带标签的数据进行训练,完成多种不同的预训练任务。
  • 微调:首先使用预训练参数进行初始化,然后使用下游任务的数据对所有参数进行微调。 每个下游任务最终都得到一个独立的微调模型。

3.0 BERT 架构

图 1 是一个问答场景的训练+微调,我们以它为例子讨论架构:

Figure 1: BERT pre-training 和 fine-tuning 过程。 预训练模型和微调模型的输出层不一样,除此之外的架构是一样的。
左:用无标注的句子进行预训练,得到一个基础模型(预训练模型)。
右:用同一个基础模型作为起点,针对不同的下游任务进行微调,这会影响模型的所有参数。
[CLS] 是加到每个输入开头的一个特殊 token; [SEP] 是一个特殊的 separator token (e.g. separating questions/answers)

BERT 的一个独特之处是针对不同任务使用统一架构。 预训练架构和最终下游架构之间的差异非常小。

3.0.1 BERT 模型架构和参数

我们的实现基于 Vaswani 等(2017)的原始实现和我们的库 tensor2tensor 。 Transformer 大家已经耳熟能详,并且我们的实现几乎与原版相同,因此这里不再对架构背景做详细描述, 需要补课的请参考 Vaswani 等(2017)及网上一些优秀文章,例如 The Annotated Transformer

本文符号表示,

  • L 层数(i.e., Transformer blocks)
  • H 隐藏层大小(embedding size)
  • A self-attention head 数量

In all cases we set the feed-forward/filter size to be 4H, i.e., 3072 for the H = 768 and 4096 for the H = 1024.

本文主要给出两种尺寸的模型:

  1. BERTBASE(L=12,H=768,A=12,总参数=110M),参数与 OpenAI GPT 相同,便于比较;
  2. BERTLARGE(L=24,H=1024,A=16,总参数=340M

如果不理解这几个参数表示什么意思,可参考 Transformer 是如何工作的:600 行 Python 代码实现两个(文本分类+文本生成)Transformer(2019)。 译注。

两个 size 的 BERT,图中的 encoder 就是 transformer。译注。Image Source

BERT Transformer 使用双向 self-attention,而 GPT Transformer 使用受限制的 self-attention, 其中每个 token 只能关注其左侧的上下文。

We note that in the literature the bidirectional Transformer is often referred to as a “Transformer encoder” while the left-context-only version is referred to as a “Transformer decoder” since it can be used for text generation.

3.0.2 输入/输出表示

为了使 BERT 能够处理各种下游任务,在一个 token 序列中,我们的输入要能够明确地区分:

  • 单个句子(a single sentence)
  • 句子对(a pair of sentences)例如,问题/回答。

这里,

  • “句子”可以是任意一段连续的文本,而不是实际的语言句子。
  • “序列”是指输入给 BERT 的 token 序列,可以是单个句子或两个句子组合在一起。

我们使用 30,000 tokens vocabulary 的 WordPiece embeddings (Wu et al., 2016)。

这个 vocabulary 长什么样,可以可以看一下 bert-base-chinese(官方专门针对中文训练的基础模型): bert-base-chinese/blob/main/vocab.txt。 译注。

我们 input/output 设计如下:

  1. 每个序列的第一个 token 都是特殊的 classification token [CLS]

    在最终输出中(最上面一行),这个 token (hidden state) 主要用于分类任务, 再接一个分类器就能得到一个分类结果(其他的 tokens 全丢弃),如下图所示,

    BERT 用于分类任务,classifier 执行 feed-forward + softmax 操作,译注。 Image Source

  2. 将 sentence-pair 合并成单个序列。通过两种方式区分,

    1. 使用特殊 token [SEP] 来分隔句子;
    2. 为每个 token 添加一个学习到的 embedding ,标识它属于句子 A 还是句子 B。

Figure 1: BERT pre-training 和 fine-tuning 过程。 预训练模型和微调模型的输出层不一样,除此之外的架构是一样的。
左:用无标注的句子进行预训练,得到一个基础模型(预训练模型)。
右:用同一个基础模型作为起点,针对不同的下游任务进行微调,这会影响模型的所有参数。
[CLS] 是加到每个输入开头的一个特殊 token; [SEP] 是一个特殊的 separator token (e.g. separating questions/answers)

再回到图 1 所示,我们将

  • 输入 embedding 表示为 $E$

    对于给定的 token ,它的输入表示是通过将 3 个 embeddings 相加来构建的,如图 2,

    Figure 2: BERT input representation.

    1. token embedding:输入文本经过 tokenizer 之后得到的输出;
    2. segment embedding:表示 token embedding 在这个位置的 token 是属于句子 A 还是句子 B;
    3. position embedding:token 在 token embedding 中的位置,0,1,2,3...,511,因为 BERT 最长支持 512 token 输入(除非自己从头开始预训练,可以改参数)。
  • 第 $i$ 个输入 token 的在最后一层的表示(最终隐藏向量)为 $T_i$,$T_i \in \mathbb{R}^H$。
  • [CLS] token 在最后一层的表示(最终隐藏向量)为 $C$, $C \in \mathbb{R}^{H}$ ,

3.1 预训练 BERT

图 1 的左侧部分。

Figure 1: BERT 的 pre-training 和 fine-tuning 过程。

与 Peters 等(2018a)和 Radford 等(2018)不同,我们不使用传统的从左到右或从右到左的模型来预训练 BERT, 而是用下面两个无监督任务(unsupervised tasks)来预训练 BERT。

3.1.1 任务 #1:掩码语言模型(Masked LM)

从直觉上讲,深度双向模型比下面两个模型都更强大:

  • 从左到右的单向模型(LRM);
  • 简单拼接(shallow concatenation)了一个左到右模型(LRM)与右到左模型(RLM)的模型。

不幸的是,标准的条件语言模型(conditional language models)只能从左到右或从右到左进行训练, 因为 bidirectional conditioning 会使每个单词间接地“看到自己”,模型就可以轻松地在 multi-layered context 中预测目标词。

为了训练一个深度双向表示,我们简单地随机屏蔽一定比例的输入 tokens, 然后再预测这些被屏蔽的 tokens。 我们将这个过程称为“掩码语言模型”(MLM) —— 这种任务通常也称为 Cloze(完形填空)(Taylor,1953)。

在所有实验中,我们随机屏蔽每个序列中 15% 的 token。 与 denoising auto-encoders(Vincent 等,2008)不同,我们只预测被屏蔽的单词,而不是重建整个输入。

这种方式使我们获得了一个双向预训练模型,但造成了预训练和微调之间的不匹配, 因为微调过程中不会出现 [MASK] token。 为了减轻这个问题,我们并不总是用 [MASK] token 替换“掩码”单词: 训练数据生成器(training data generator)随机选择 15%的 token positions 进行预测。 如果选择了第 i 个 token ,我们将第 i 个 token 用以下方式替换:

  1. 80% 的概率用 [MASK] token 替换,
  2. 10% 的概率用 随机 token 替换,
  3. 10% 的概率 保持不变

然后,使用 $Ti$ 来预测原始 token ,并计算交叉熵损失(cross entropy loss)。 附录 C.2 中比较了这个过程的几个变种。

3.1.2 任务 #2:下一句预测(Next Sentence Prediction, NSP)

许多重要的下游任务,如问答(Question Answering, QA) 和自然语言推理(Natural Language Inference, NLI) 都基于理解两个句子之间的关系, 而语言建模(language modeling)并无法直接捕获这种关系。

为了训练一个能理解句子关系的模型,我们预先训练了一个二元的下一句预测任务(a binarized next sentence prediction task): 给定两个句子 A 和 B,判断 B 是不是 A 的下一句

BERT 用于“下一句预测”(NSP)任务,译注。Image Source

这个任务可以用任何单语语料库(monolingual corpus),具体来说,在选择每个预训练示例的句子 A 和 B 时,

  • 50%的概率 B 是 A 的下一个句子(labeled as IsNext),
  • 50%的概率 B 是语料库中随机一个句子(labeled as NotNext)。

再次回到图 1, 这个 yes/no 的判断还是通过 classifier token 的最终嵌入向量 $C$ 预测的,

Figure 1: BERT pre-training 和 fine-tuning 过程。 预训练模型和微调模型的输出层不一样,除此之外的架构是一样的。
左:用无标注的句子进行预训练,得到一个基础模型(预训练模型)。
右:用同一个基础模型作为起点,针对不同的下游任务进行微调,这会影响模型的所有参数。
[CLS] 是加到每个输入开头的一个特殊 token; [SEP] 是一个特殊的 separator token (e.g. separating questions/answers)

最终我们的模型达到了 97~98% 的准确性。 尽管它很简单,但我们在第 5.1 节中证明,针对这个任务的预训练对于 QA 和 NLI 都非常有益。

The vector C is not a meaningful sentence representation without fine-tuning, since it was trained with NSP。

NSP 任务与 Jernite 等(2017)和 Logeswaran 和 Lee(2018)使用的 representation learning 有紧密关系。 但是他们的工作中只将句子 embedding 转移到了下游任务,而 BERT 是将所有参数都转移下游,初始化微调任务用的初始模型。

3.1.3 预训练数据集

预训练过程跟其他模型的预训练都差不多。对于预训练语料库,我们使用了

  • BooksCorpus (800M words) (Zhu et al., 2015)
  • English Wikipedia (2,500M words)。只提取文本段落,忽略列表、表格和标题。

使用文档语料库而不是像 Billion Word Benchmark(Chelba 等,2013) 这样的 shuffled sentence-level 语料库非常重要,因为方便提取长连续序列。

3.2 微调 BERT

Transformer 中的 self-attention 机制允许 BERT 对任何下游任务建模 —— 无论是 single text 还是 text pairs —— 只需要适当替换输入和输出,因此对 BERT 进行微调是非常方便的。

对于 text-pair 类应用,一个常见的模式是在应用 bidirectional cross attention 之前,独立编码 text-pair ,例如 Parikh 等(2016);Seo 等(2017)。

但 BERT 使用 self-attention 机制来统一预训练和微调这两个阶段,因为使用 self-attention 对 concatenated text-pair 进行编码, 有效地包含了两个句子之间的 bidirectional cross attention。

对于每个任务,只需将任务特定的输入和输出插入到 BERT 中,并对所有参数进行端到端的微调。 预训练阶段,input 句子 A 和 B 的关系可能是:

  1. sentence pairs
  2. hypothesis-premise pairs in entailment
  3. question-passage pairs in question answering
  4. 文本分类或序列打标(sequence tagging)中的 degenerate text-? pair

在输出端,

  • 普通 token representations 送到 token-level 任务的输出层,例如 sequence tagging 或问答,
  • [CLS] token representation 用于分类,例如 entailment or sentiment analysis。

与预训练相比,微调的成本相对较低。从完全相同的预训练模型开始, 本文中所有结果都可以在最多 1 小时内在单个 Cloud TPU 上复制,或者在 GPU 上几个小时内。 第 4 节会介绍一些细节。更多细节见附录 A.5。

3.3 各种场景

Fig 4. BERT 用于不同任务场景,来自 paper 附录。
(a) 句子对分类;(b) 单句分类;(c) 问答;(d) 单句打标。

4 实验

In this section, we present BERT fine-tuning results on 11 NLP tasks.

4.1 GLUE (General Language Understanding Evaluation)

GLUE benchmark (Wang et al., 2018a) 是一个自然语言理解任务集, 更多介绍见 Appendix B.1。

4.1.1 Fine-tune 工作

针对 GLUE 进行 fine-tune 所做的工作:

  1. 用第 3 节介绍的方式表示 input sequence (for single sentence or sentence pairs)
  2. the final hidden vector C 判断类别;
  3. fine-tuning 期间增加的唯一参数 是分类层的权重 $W \in \mathbb{R}^{K \times H}$,其中 $K$ 是 labels 数量。 我们用 $C$ 和 $W$ 计算一个标准的 classification loss,例如 $\log({\rm softmax}(CW^T))$.

4.1.2 参数设置

  • batch size 32
  • 3 epochs
  • learning rate: for each task, we selected the best fine-tuning learning rate (among 5e-5, 4e-5, 3e-5, and 2e-5) on the Dev set.

另外,我们发现 BERTLARGE 在小数据集上 finetuning 有时候不稳定, 所以我们会随机重启几次,从得到的模型中选效果最好的。 随机重启使用相同的 pre-trained checkpoint 但使用不同的数据重排和分类层初始化 (data shuffling and classifier layer initialization)。

4.1.3 结果

结果如 Table 1 所示,

System MNLI-(m/mm) QQP QNLI SST-2 CoLA STS-B MRPC RTE Average
  392k 363k 108k 67k 8.5k 5.7k 3.5k 2.5k -
Pre-OpenAI SOTA 80.6/80.1 66.1 82.3 93.2 35.0 81.0 86.0 61.7 74.0
BiLSTM+ELMo+Attn 76.4/76.1 64.8 79.8 90.4 36.0 73.3 84.9 56.8 71.0
OpenAI GPT 82.1/81.4 70.3 87.4 91.3 45.4 80.0 82.3 56.0 75.1
BERTBASE 84.6/83.4 71.2 90.5 93.5 52.1 85.8 88.9 66.4 79.6
BERTLARGE 86.7/85.9 72.1 92.7 94.9 60.5 86.5 89.3 70.1 82.1

Table 1: GLUE Test results, scored by the evaluation server (https://gluebenchmark.com/leaderboard). The number below each task denotes the number of training examples. The “Average” column is slightly different than the official GLUE score, since we exclude the problematic WNLI set.8 BERT and OpenAI GPT are singlemodel, single task. F1 scores are reported for QQP and MRPC, Spearman correlations are reported for STS-B, and accuracy scores are reported for the other tasks. We exclude entries that use BERT as one of their components.

Both BERTBASE and BERTLARGE outperform all systems on all tasks by a substantial margin, obtaining 4.5% and 7.0% respective average accuracy improvement over the prior state of the art. Note that BERTBASE and OpenAI GPT are nearly identical in terms of model architecture apart from the attention masking. For the largest and most widely reported GLUE task, MNLI, BERT obtains a 4.6% absolute accuracy improvement. On the official GLUE leaderboard, BERTLARGE obtains a score of 80.5, compared to OpenAI GPT, which obtains 72.8 as of the date of writing.

We find that BERTLARGE significantly outperforms BERTBASE across all tasks, especially those with very little training data. The effect of model size is explored more thoroughly in Section 5.2.

4.2 SQuAD (Stanford Question Answering Dataset) v1.1

SQuAD v1.1 包含了 100k crowdsourced question/answer pairs (Rajpurkar et al., 2016). Given a question and a passage from Wikipedia containing the answer, the task is to predict the answer text span in the passage.

As shown in Figure 1, in the question answering task, we represent the input question and passage as a single packed sequence, with the question using the $A$ embedding and the passage using the $B$ embedding. We only introduce a start vector $S \in \mathbb{R}^H$ and an end vector $E \in \mathbb{R}^H$ during fine-tuning. The probability of word $i$ being the start of the answer span is computed as a dot product between $T_i$ and $S$ followed by a softmax over all of the words in the paragraph: $P_i = \frac{e^{S{\cdot}T_i}}{\sum_j e^{S{\cdot}T_j}}$. The analogous formula is used for the end of the answer span. The score of a candidate span from position $i$ to position $j$ is defined as $S{\cdot}T_i + E{\cdot}T_j$, and the maximum scoring span where $j \geq i$ is used as a prediction. The training objective is the sum of the log-likelihoods of the correct start and end positions. We fine-tune for 3 epochs with a learning rate of 5e-5 and a batch size of 32.

Table 2 shows top leaderboard entries as well as results from top published systems (Seo et al., 2017; Clark and Gardner, 2018; Peters et al., 2018a; Hu et al., 2018).

Table 2: SQuAD 1.1 results. The BERT ensemble is 7x systems which use different pre-training checkpoints and fine-tuning seeds.

The top results from the SQuAD leaderboard do not have up-to-date public system descriptions available,11 and are allowed to use any public data when training their systems. We therefore use modest data augmentation in our system by first fine-tuning on TriviaQA (Joshi et al., 2017) befor fine-tuning on SQuAD. Our best performing system outperforms the top leaderboard system by +1.5 F1 in ensembling and +1.3 F1 as a single system. In fact, our single BERT model outperforms the top ensemble system in terms of F1 score. Without TriviaQA fine- tuning data, we only lose 0.1-0.4 F1, still outperforming all existing systems by a wide margin.12

4.3 SQuAD v2.0

The SQuAD 2.0 task extends the SQuAD 1.1 problem definition by allowing for the possibility that no short answer exists in the provided paragraph, making the problem more realistic. We use a simple approach to extend the SQuAD v1.1 BERT model for this task. We treat questions that do not have an answer as having an answer span with start and end at the [CLS] token. The probability space for the start and end answer span positions is extended to include the position of the [CLS] token.

For prediction, we compare the score of the no-answer span: \(s_{\tt null} = S{\cdot}C + E{\cdot}C\) to the score of the best non-null span $\hat{s_{i,j}}$ = \({\tt max}_{j \geq i} S{\cdot}T_i + E{\cdot}T_j\). We predict a non-null answer when $\hat{s_{i,j}} > s_{\tt null} + \tau$, where the threshold $\tau$ is selected on the dev set to maximize F1. We did not use TriviaQA data for this model. We fine-tuned for 2 epochs with a learning rate of 5e-5 and a batch size of 48.

The results compared to prior leaderboard entries and top published work (Sun et al., 2018; Wang et al., 2018b) are shown in Table 3, excluding systems that use BERT as one of their components. We observe a +5.1 F1 improvement over the previous best system.

Table 3: SQuAD 2.0 results. We exclude entries that use BERT as one of their components.

4.4 SWAG (Situations With Adversarial Generations)

SWAG dataset contains 113k sentence-pair completion examples that evaluate grounded commonsense inference (Zellers et al., 2018).

Given a sentence, the task is to choose the most plausible continuation among four choices. When fine-tuning on the SWAG dataset, we construct four input sequences, each containing the concatenation of the given sentence (sentence A) and a possible continuation (sentence B). The only task-specific parameters introduced is a vector whose dot product with the [CLS] token representation C denotes a score for each choice which is normalized with a softmax layer.

We fine-tune the model for 3 epochs with a learning rate of 2e-5 and a batch size of 16. Results are presented in Table 4.

Table 4: SWAG Dev and Test accuracies. Human performance is measured with 100 samples, as reported in the SWAG paper.

BERTLARGE outperforms the authors’ baseline ESIM+ELMo system by +27.1% and OpenAI GPT by 8.3%.

5 对照研究

本节研究去掉 BERT 的一些功能,看看在不同任务上性能损失多少,

  • sentence-level (e.g., SST-2)
  • sentence-pair-level (e.g., MultiNLI)
  • word-level (e.g., NER)
  • span-level (e.g., SQuAD)

以更好地理解它们的相对重要性。更多相关信息见附录 C。

5.1 预训练任务(MLM/NSP)的影响

5.1.1 训练组

通过以下几组来验证 BERT 深度双向性的重要性,它们使用与 BERTBASE 完全相同的预训练数据、微调方案和超参数:

  1. NO NSP:即去掉“下一句预测”任务,这仍然是一个双向模型,使用“掩码语言模型”(MLM)进行训练,只是训练时不做 NSP 任务;
  2. LTR & NO NSP:不仅去掉 NSP,还使用标准的从左到右(Left-to-Right, LTR)模型进行训练,而非使用双向模型。 在微调中也遵从 left-only 约束,否则会导致预训练和微调不匹配,降低下游性能。此外,该模型没有用 NSP 任务进行预训练。 这与 OpenAI GPT 直接可比,但我们使用了更大的训练数据集、我们自己的输入表示和我们的微调方案。
  3. + BiLSTM:在 fine-tuning 期间,在 LTR & NO NSP 基础上添加了一个随机初始化的 BiLSTM。

5.1.2 结果对比

结果如表 5,

Tasks MNLI-m (Acc) QNLI (Acc) MRPC (Acc) SST-2 (Acc) SQuAD (F1)
BERTBASE 84.4 88.4 86.7 92.7 88.5
No NSP 83.9 84.9 86.5 92.6 87.9
LTR & No NSP 82.1 84.3 77.5 92.1 77.8
+ BiLSTM 82.1 84.1 75.7 91.6 84.9

Table 5: Ablation over the pre-training tasks using the BERTBASE architecture.

分析:

  1. 第二组 vs 第一组:去掉 NSP 任务带来的影响:在 QNLI、MNLI 和 SQuAD 1.1 上性能显著下降。
  2. 第三组 vs 第二组:去掉双向表示带来的影响:第二行实际上是 MLM & NO NSP, 可以看出 LTR 模型在所有任务上的表现都比 MLM 模型差,尤其是 MRPC 和 SQuAD。

    • 对于 SQuAD,可以清楚地看到 LTR 模型在 token 预测上表现不佳,因为 token 级别的隐藏状态没有右侧上下文。
    • 为了尝试增强 LTR 系统,我们在其上方添加了一个随机初始化的双向 LSTM。 这确实在 SQuAD 上改善了结果,但结果仍远远不及预训练的双向模型。另外, 双向 LSTM 降低了在 GLUE 上的性能。

5.1.3 与 ELMo 的区别

ELMo 训练了单独的从左到右(LTR)和从右到左(RTL)模型,并将每个 token 表示为两个模型的串联。 然而:

  1. 这比单个双向模型训练成本高一倍;
  2. 对于像 QA 这样的任务,这不直观,因为 RTL 模型将无法 condition the answer on the question;
  3. 这比深度双向模型弱,因为后者可以在每层使用左右上下文。

5.2 模型大小的影响

为探讨模型大小对微调任务准确性的影响,我们训练了多个 BERT 模型。 表 6 给出了它们在 GLUE 任务上的结果。

L (层数) H (hidden size) A (attention head 数) LM (ppl) MNLI-m MRPC SST-2
3 768 12 5.84 77.9 79.8 88.4
6 768 3 5.24 80.6 82.2 90.7
6 768 12 4.68 81.9 84.8 91.3
12 768 12 3.99 84.4 86.7 92.9
12 1024 16 3.54 85.7 86.9 93.3
24 1024 16 3.23 86.6 87.8 93.7

Table 6: Ablation over BERT model size. “LM (ppl)” is the masked LM perplexity of held-out training data

In this table, we report the average Dev Set accuracy from 5 random restarts of fine-tuning.

可以看到,更大的模型在四个数据集上的准确性都更高 —— 即使对于只有 3,600 个训练示例的 MRPC, 而且这个数据集与预训练任务差异还挺大的。 也许令人惊讶的是,在模型已经相对较大的前提下,我们仍然能取得如此显著的改进。例如,

  • Vaswani 等(2017)尝试的最大 Transformer 是(L=6,H=1024,A=16),编码器参数为 100M,
  • 我们在文献中找到的最大 Transformer 是(L=64,H=512,A=2),具有 235M 参数(Al-Rfou 等,2018)。
  • 相比之下,BERTBASE 包含 110M 参数,BERTLARGE 包含 340M 参数。

业界早就知道,增加模型大小能持续改进机器翻译和语言建模等大规模任务上的性能, 表 6 的 perplexity 列也再次证明了这个结果, 然而,我们认为 BERT 是第一个证明如下结果的研究工作:只要模型得到了充分的预训练, 那么将模型尺寸扩展到非常大时(scaling to extreme model sizes), 对非常小规模的任务(very small scale tasks)也能带来很大的提升(large improvements)。

另外,

  • Peters 等(2018b)研究了将 pre-trained bi-LM size(预训练双向语言模型大小)从两层增加到四层,对下游任务产生的影响,
  • Melamud 等(2016)提到将隐藏维度从 200 增加到 600 有所帮助,但进一步增加到 1,000 并没有带来更多的改进。

这两项工作都使用了基于特征的方法,而我们则是直接在下游任务上进行微调,并仅使用非常少量的随机初始化附加参数, 结果表明即使下游任务数据非常小,也能从更大、更 expressive 的预训练表示中受益。

5.3 BERT 基于特征的方式

到目前为止,本文展示的所有 BERT 结果都使用的微调方式: 在预训练模型中加一个简单的分类层,针对特定的下游任务对所有参数进行联合微调。

5.3.1 基于特征的方式适用的场景

不过,基于特征的方法 —— 从预训练模型中提取固定特征(fixed features)—— 在某些场景下有一定的优势,

  • 首先,不是所有任务都能方便地通过 Transformer encoder 架构表示,因此这些不适合的任务,都需要添加一个 task-specific model architecture。
  • 其次,昂贵的训练数据表示(representation of the training data)只预训练一次, 然后在此表示的基础上使用更轻量级的模型进行多次实验,可以极大节省计算资源。

5.3.2 实验

本节通过 BERT 用于 CoNLL-2003 Named Entity Recognition (NER) task (Tjong Kim Sang and De Meulder, 2003) 来比较这两种方式。

  • BERT 输入使用保留大小写的 WordPiece 模型,并包含数据提供的 maximal document context。
  • 按照惯例,我们将其作为打标任务(tagging task),但在输出中不使用 CRF 层。
  • 我们将第一个 sub-token 的 representation 作 token-level classifier 的输入,然后在 NER label set 上进行实验。

为了对比微调方法的效果,我们使用基于特征的方法,对 BERT 参数不做任何微调, 而是从一个或多个层中提取激活(extracting the activations)。 这些 contextual embeddings 作为输入,送给一个随机初始化的 two-layer 768-dimensional BiLSTM, 最后再送到分类层。

5.3.3 结果

结果见表 7。BERTLARGE 与业界最高性能相当,

System Dev F1 Test F1
ELMo (Peters et al., 2018a) 95.7 92.2
CVT (Clark et al., 2018) - 92.6
CSE (Akbik et al., 2018) - 93.1
Fine-tuning approach    
BERTLARGE 96.6 92.8
BERTBASE 96.4 92.4
Feature-based approach (BERTBASE)    
Embeddings 91.0 -
Second-to-Last Hidden 95.6 -
Last Hidden 94.9 -
Weighted Sum Last Four Hidden 95.9 -
Concat Last Four Hidden 96.1 -
Weighted Sum All 12 Layers 95.5 -

Table 7: CoNLL-2003 Named Entity Recognition results. Hyperparameters were selected using the Dev set. The reported Dev and Test scores are averaged over 5 random restarts using those hyperparameters

The best performing method concatenates the token representations from the top four hidden layers of the pre-trained Transformer, which is only 0.3 F1 behind fine-tuning the entire model.

这表明 微调和基于特征的方法在 BERT 上都是有效的

6 总结

Recent empirical improvements due to transfer learning with language models have demonstrated that rich, unsupervised pre-training is an integral part of many language understanding systems. In particular, these results enable even low-resource tasks to benefit from deep unidirectional architectures.

Our major contribution is further generalizing these findings to deep bidirectional architectures, allowing the same pre-trained model to successfully tackle a broad set of NLP tasks.

附录

A. Additional Details for BERT

A.1 Illustration of the Pre-training Tasks

Figure 3: Differences in pre-training model architectures. BERT uses a bidirectional Transformer. OpenAI GPT uses a left-to-right Transformer. ELMo uses the concatenation of independently trained left-to-right and right-toleft LSTMs to generate features for downstream tasks. Among the three, only BERT representations are jointly conditioned on both left and right context in all layers. In addition to the architecture differences, BERT and OpenAI GPT are fine-tuning approaches, while ELMo is a feature-based approach.

A.2 Pre-training Procedure

A.3 Fine-tuning Procedure

For fine-tuning, most model hyperparameters are the same as in pre-training, with the exception of the batch size, learning rate, and number of training epochs. The dropout probability was always kept at 0.1. The optimal hyperparameter values are task-specific, but we found the following range of possible values to work well across all tasks:

  • Batch size: 16, 32
  • Learning rate (Adam): 5e-5, 3e-5, 2e-5
  • Number of epochs: 2, 3, 4

We also observed that large data sets (e.g., 100k+ labeled training examples) were far less sensitive to hyperparameter choice than small data sets. Fine-tuning is typically very fast, so it is reasonable to simply run an exhaustive search over the above parameters and choose the model that performs best on the development set.

A.4 Comparison of BERT, ELMo ,and OpenAI GPT

A.5 Illustrations of Fine-tuning on Different Tasks

B. Detailed Experimental Setup

Fig 4. BERT 用于不同任务场景,来自 paper 附录。
(a) 句子对分类;(b) 单句分类;(c) 问答;(d) 单句打标。

C. Additional Ablation Studies

参考文献

  1. Alan Akbik, Duncan Blythe, and Roland Vollgraf. 2018. Contextual string embeddings for sequence labeling. In Proceedings of the 27th International Conference on Computational Linguistics, pages 1638–1649.
  2. Rami Al-Rfou, Dokook Choe, Noah Constant, Mandy Guo, and Llion Jones. 2018. Character-level language modeling with deeper self-attention. arXiv preprint arXiv:1808.04444.
  3. Rie Kubota Ando and Tong Zhang. 2005. A framework for learning predictive structures from multiple tasks and unlabeled data. Journal of Machine Learning Research, 6(Nov):1817–1853.
  4. Luisa Bentivogli, Bernardo Magnini, Ido Dagan, Hoa Trang Dang, and Danilo Giampiccolo. 2009. The fifth PASCAL recognizing textual entailment challenge. In TAC. NIST.
  5. John Blitzer, Ryan McDonald, and Fernando Pereira. 2006. Domain adaptation with structural correspondence learning. In Proceedings of the 2006 conference on empirical methods in natural language processing, pages 120–128. Association for Computational Linguistics.
  6. Samuel R. Bowman, Gabor Angeli, Christopher Potts, and Christopher D. Manning. 2015. A large annotated corpus for learning natural language inference. In EMNLP. Association for Computational Linguistics.
  7. Peter F Brown, Peter V Desouza, Robert L Mercer, Vincent J Della Pietra, and Jenifer C Lai. 1992. Class-based n-gram models of natural language. Computational linguistics, 18(4):467–479.
  8. Daniel Cer, Mona Diab, Eneko Agirre, Inigo Lopez-Gazpio, and Lucia Specia. 2017. https://doi.org/10.18653/v1/S17-2001 Semeval-2017 task 1: Semantic textual similarity multilingual and crosslingual focused evaluation. In Proceedings of the 11th International Workshop on Semantic Evaluation (SemEval-2017), pages 1–14, Vancouver, Canada. Association for Computational Linguistics.
  9. Ciprian Chelba, Tomas Mikolov, Mike Schuster, Qi Ge, Thorsten Brants, Phillipp Koehn, and Tony Robinson. 2013. One billion word benchmark for measuring progress in statistical language modeling. arXiv preprint arXiv:1312.3005.
  10. Z. Chen, H. Zhang, X. Zhang, and L. Zhao. 2018. https://data.quora.com/First-Quora-Dataset-Release-Question-Pairs Quora question pairs.
  11. Christopher Clark and Matt Gardner. 2018. Simple and effective multi-paragraph reading comprehension. In ACL.
  12. Kevin Clark, Minh-Thang Luong, Christopher D Manning, and Quoc Le. 2018. Semi-supervised sequence modeling with cross-view training. In Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing, pages 1914–1925.
  13. Ronan Collobert and Jason Weston. 2008.newblock A unified architecture for natural language processing: Deep neural networks with multitask learning. In Proceedings of the 25th international conference on Machine learning, pages 160–167. ACM.
  14. Alexis Conneau, Douwe Kiela, Holger Schwenk, Lo"ic Barrault, and Antoine Bordes. 2017. https://www.aclweb.org/anthology/D17-1070 Supervised learning of universal sentence representations from natural language inference data. In Proceedings of the 2017 Conference on Empirical Methods in Natural Language Processing, pages 670–680, Copenhagen, Denmark. Association for Computational Linguistics.
  15. Andrew M Dai and Quoc V Le. 2015. Semi-supervised sequence learning. In Advances in neural information processing systems, pages 3079–3087.
  16. J. Deng, W. Dong, R. Socher, L.-J. Li, K. Li, and L. Fei-Fei. 2009. ImageNet: A Large-Scale Hierarchical Image Database. In CVPR09.
  17. William B Dolan and Chris Brockett. 2005. Automatically constructing a corpus of sentential paraphrases. In Proceedings of the Third International Workshop on Paraphrasing (IWP2005).
  18. William Fedus, Ian Goodfellow, and Andrew M Dai. 2018. Maskgan: Better text generation via filling in the_. arXiv preprint arXiv:1801.07736.
  19. Dan Hendrycks and Kevin Gimpel. 2016. http://arxiv.org/abs/1606.08415 Bridging nonlinearities and stochastic regularizers with gaussian error linear units. CoRR, abs/1606.08415.
  20. Felix Hill, Kyunghyun Cho, and Anna Korhonen. 2016. Learning distributed representations of sentences from unlabelled data. In Proceedings of the 2016 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies. Association for Computational Linguistics.
  21. Jeremy Howard and Sebastian Ruder. 2018. http://arxiv.org/abs/1801.06146 Universal language model fine-tuning for text classification. In ACL. Association for Computational Linguistics.
  22. Minghao Hu, Yuxing Peng, Zhen Huang, Xipeng Qiu, Furu Wei, and Ming Zhou. 2018. Reinforced mnemonic reader for machine reading comprehension. In IJCAI.
  23. Yacine Jernite, Samuel R. Bowman, and David Sontag. 2017. http://arxiv.org/abs/1705.00557 Discourse-based objectives for fast unsupervised sentence representation learning. CoRR, abs/1705.00557.
  24. Mandar Joshi, Eunsol Choi, Daniel S Weld, and Luke Zettlemoyer. 2017. Triviaqa: A large scale distantly supervised challenge dataset for reading comprehension. In ACL.
  25. Ryan Kiros, Yukun Zhu, Ruslan R Salakhutdinov, Richard Zemel, Raquel Urtasun, Antonio Torralba, and Sanja Fidler. 2015. Skip-thought vectors. In Advances in neural information processing systems, pages 3294–3302.
  26. Quoc Le and Tomas Mikolov. 2014. Distributed representations of sentences and documents. In International Conference on Machine Learning, pages 1188–1196.
  27. Hector J Levesque, Ernest Davis, and Leora Morgenstern. 2011. The winograd schema challenge. In Aaai spring symposium: Logical formalizations of commonsense reasoning, volume 46, page 47.
  28. Lajanugen Logeswaran and Honglak Lee. 2018. https://openreview.net/forum?id=rJvJXZb0W An efficient framework for learning sentence representations. In International Conference on Learning Representations.
  29. Bryan McCann, James Bradbury, Caiming Xiong, and Richard Socher. 2017. Learned in translation: Contextualized word vectors. In NIPS.
  30. Oren Melamud, Jacob Goldberger, and Ido Dagan. 2016. context2vec: Learning generic context embedding with bidirectional LSTM. In CoNLL.
  31. Tomas Mikolov, Ilya Sutskever, Kai Chen, Greg S Corrado, and Jeff Dean. 2013. Distributed representations of words and phrases and their compositionality. In Advances in Neural Information Processing Systems 26, pages 3111–3119. Curran Associates, Inc.
  32. Andriy Mnih and Geoffrey E Hinton. 2009. http://papers.nips.cc/paper/3583-a-scalable-hierarchical-distributed-language-model.pdf A scalable hierarchical distributed language model. In D. Koller, D. Schuurmans, Y. Bengio, and L. Bottou, editors, Advances in Neural Information Processing Systems 21, pages 1081–1088. Curran Associates, Inc.
  33. Ankur P Parikh, Oscar T"ackstr"om, Dipanjan Das, and Jakob Uszkoreit. 2016. A decomposable attention model for natural language inference. In EMNLP.
  34. Jeffrey Pennington, Richard Socher, and Christopher D. Manning. 2014. http://www.aclweb.org/anthology/D14-1162 Glove: Global vectors for word representation. In Empirical Methods in Natural Language Processing (EMNLP), pages 1532–1543.
  35. Matthew Peters, Waleed Ammar, Chandra Bhagavatula, and Russell Power. 2017. Semi-supervised sequence tagging with bidirectional language models. In ACL.
  36. Matthew Peters, Mark Neumann, Mohit Iyyer, Matt Gardner, Christopher Clark, Kenton Lee, and Luke Zettlemoyer. 2018\natexlaba. Deep contextualized word representations. In NAACL.
  37. Matthew Peters, Mark Neumann, Luke Zettlemoyer, and Wen-tau Yih. 2018\natexlabb. Dissecting contextual word embeddings: Architecture and representation. In Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing, pages 1499–1509.
  38. Alec Radford, Karthik Narasimhan, Tim Salimans, and Ilya Sutskever. 2018. Improving language understanding with unsupervised learning. Technical report, OpenAI.
  39. Pranav Rajpurkar, Jian Zhang, Konstantin Lopyrev, and Percy Liang. 2016. Squad: 100,000+ questions for machine comprehension of text. In Proceedings of the 2016 Conference on Empirical Methods in Natural Language Processing, pages 2383–2392.
  40. Minjoon Seo, Aniruddha Kembhavi, Ali Farhadi, and Hannaneh Hajishirzi. 2017. Bidirectional attention flow for machine comprehension. In ICLR.
  41. Richard Socher, Alex Perelygin, Jean Wu, Jason Chuang, Christopher D Manning, Andrew Ng, and Christopher Potts. 2013. Recursive deep models for semantic compositionality over a sentiment treebank. In Proceedings of the 2013 conference on empirical methods in natural language processing, pages 1631–1642.
  42. Fu Sun, Linyang Li, Xipeng Qiu, and Yang Liu. 2018. U-net: Machine reading comprehension with unanswerable questions. arXiv preprint arXiv:1810.06638.
  43. Wilson L Taylor. 1953. “Cloze procedure”: A new tool for measuring readability. Journalism Bulletin, 30(4):415–433.
  44. Erik F Tjong Kim Sang and Fien De Meulder. 2003. Introduction to the conll-2003 shared task: Language-independent named entity recognition. In CoNLL.
  45. Joseph Turian, Lev Ratinov, and Yoshua Bengio. 2010. Word representations: A simple and general method for semi-supervised learning. In Proceedings of the 48th Annual Meeting of the Association for Computational Linguistics, ACL ‘10, pages 384–394.
  46. Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need. In Advances in Neural Information Processing Systems, pages 6000–6010.
  47. Pascal Vincent, Hugo Larochelle, Yoshua Bengio, and Pierre-Antoine Manzagol. 2008. Extracting and composing robust features with denoising autoencoders. In Proceedings of the 25th international conference on Machine learning, pages 1096–1103. ACM.
  48. Alex Wang, Amanpreet Singh, Julian Michael, Felix Hill, Omer Levy, and Samuel Bowman. 2018\natexlaba. Glue: A multi-task benchmark and analysis platform for natural language understanding. In Proceedings of the 2018 EMNLP Workshop BlackboxNLP: Analyzing and Interpreting Neural Networks for NLP, pages 353–355.
  49. Wei Wang, Ming Yan, and Chen Wu. 2018\natexlabb. Multi-granularity hierarchical attention fusion networks for reading comprehension and question answering. In Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). Association for Computational Linguistics.
  50. Alex Warstadt, Amanpreet Singh, and Samuel R Bowman. 2018. Neural network acceptability judgments. arXiv preprint arXiv:1805.12471.
  51. Adina Williams, Nikita Nangia, and Samuel R Bowman. 2018. A broad-coverage challenge corpus for sentence understanding through inference. In NAACL.
  52. Yonghui Wu, Mike Schuster, Zhifeng Chen, Quoc V Le, Mohammad Norouzi, Wolfgang Macherey, Maxim Krikun, Yuan Cao, Qin Gao, Klaus Macherey, et al. 2016. Google’s neural machine translation system: Bridging the gap between human and machine translation. arXiv preprint arXiv:1609.08144.
  53. Jason Yosinski, Jeff Clune, Yoshua Bengio, and Hod Lipson. 2014. How transferable are features in deep neural networks? In Advances in neural information processing systems, pages 3320–3328.
  54. Adams Wei Yu, David Dohan, Minh-Thang Luong, Rui Zhao, Kai Chen, Mohammad Norouzi, and Quoc V Le. 2018. QANet: Combining local convolution with global self-attention for reading comprehension. In ICLR.
  55. Rowan Zellers, Yonatan Bisk, Roy Schwartz, and Yejin Choi. 2018. Swag: A large-scale adversarial dataset for grounded commonsense inference. In Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing (EMNLP).
  56. Yukun Zhu, Ryan Kiros, Rich Zemel, Ruslan Salakhutdinov, Raquel Urtasun, Antonio Torralba, and Sanja Fidler. 2015. Aligning books and movies: Towards story-like visual explanations by watching movies and reading books. In Proceedings of the IEEE international conference on computer vision, pages 19–27.

Written by Human, Not by AI Written by Human, Not by AI

小试自定义GPT

作者 Liyun
2024年1月12日 09:09

最近不是在折腾LLM嘛,于是就试了两条路子:用openai的api,以及直接在openai的界面里面创建GPT。

前者没啥特别的,chatgpt的api做的很成熟了,from openai import OpenAI 之后直接在python里面调用几个现成的函数就好了。可选的参数其实也不多,主要就是prompt写的好一点就行。我的要求也不高,试了试基本满足。此外我还用到了微软 azure api,也很方便,两者一结合基本一个app就搓出来了,只是暂时还只能在命令行运行,没写前端ui罢了。

后者就麻烦了。我想着自己写前端ui还挺麻烦的,就想偷个懒直接在GPT里面弄弄看看行不。结果呢,现在这个版本实在是太挫了,只支持最最基本的action,虽然可以调用其他api,但还没研究出来怎么实现用户上传的文件扔到action api call里面。搜了搜他们的论坛也没啥结果,然后心累就到此为止了。

最后贴一下如何在openai 的GPT里面调用azure api。主要是api key那里实在是反用户直觉,我找了好久……一定要选 custom 然后把自定义的名字设为 Ocp-Apim-Subscription-Key 才可以。贴个图。

自定义 action -> authentication -> custom header name

当然azure api的文档做的也很差就是了,经常搜出来的是过时的文档,试一试都是404错误。哎,时间都花在这些琐碎的调试bug上了。

最后的结论是,在现在这个阶段,openai GPT的多模态做的还是太封闭,只适用于比较基础的交互需求,得等到后面允许自定义编程更丰富一些才可以。想做的稍稍复杂一点,写ui是逃不掉的了。web版还可以写个python+js凑和一下(flask这么轻量级的web开发框架真的是效率提升利器),app版xcode看了半天发现也是一等一的复杂……说好的ai改变程序开发呢?叹口气……

大语言模型LLM的基本逻辑

作者 Liyun
2023年11月14日 13:59

上一篇说到我准备入个坑,结果就是最近埋头苦苦补习最基本的一些知识。随便写点东西梳理一下思路吧,这样万一我真的开始做点什么也算是一个基本素材。一些英文的名词我就不翻译了,反正现在大家英语都挺好的。

先来一些可以基本望文生义的名词解释。LLM=large language model = 大语言模型。这简直是个不能再俗的名字了。GPT = generative pre-trained transformer ,也是够直白的。

再来个极其简单的(受限于园主阅历)历史回顾。自然语言处理基本上经历了 word2vec, RNN,然后就是现在的transformer了。其实说到底,自然语言处理的基本问题就是一个时间序列问题。当园主意识到这点的时候也是惊掉了下巴,什么,计量里面的时间序列不是Autoregression, moving average,stationary 那些东西么,怎么看都跟自然语言扯不上关系了。后面看到做量化的人都在跟这个方向的进展,才明白说到底都是时间序列嘛。想想也是,自然语言就是一个把词按照特定顺序排列起来的数据,词与词之间的关联和顺序最终表达了一定的意义。

nlp模型想法差不多,就是基于已经有的词,预测对应的下一个词的概率。建模不是问题,但数据上来后计算是问题啊……于是有了transformer 那篇著名的 Attention is all you need,伴随着经典的encoder-decoder结构,就出现了让图灵测试不再是问题的大语言模型们。

再来一轮名词解释。自然语言到建模之前,需要先把unstructured data转换为可以计算的数字,这就是embedding 这一步,也叫token 化。然后再怎么办呢?transformer的核心是再算一下attention 矩阵,这个矩阵主要涵盖了词与词之间关联程度(不贴公式了),然后要做的就是放到神经网络里面去算了。这里有意思的是,encoder里面不只有一个基于attention数据的模型,而是多个,所以称之为 multi-head attention (多头注意力)。为啥需要多个模型呢,因为神经网络很有名的一个feature(bug)是local optima,即随着初始值的不同,参数可能会迭代到一个局部最优。至于全局最优嘛,存不存在都还是个迷。反映到encoder这里,有意思的是每个单独的模型就有可能抓住语言的某一个层面的特征,比如语法,比如逻辑,比如修辞,比如情绪,以及一些语义学还无法解释的神秘模型。但不要紧,大力出奇迹,只要计算机能算得出来就行。

encoder到这里已经可以做很多任务了,最显著的大概是sentiment analysis, 就是判断里面的情绪。比如一个评价是正面负面,或者是关于价格还是物流速度,等等。这些分类模型对于很多应用场景都是很有价值的信息提取过程,也称为auto-encoding。

decoder呢,任务就更直接,就是通过输入的新数据来预测并生成下文。这也是GPT的厉害之处,可以自己写小作文了。所以这一类也叫autoregressive model ,即AR!再看下去,其实decoder的架构和encoder很像,所以他们的并不是模型架构本身,而是任务的目标不同。

那什么时候我们会同时需要encoder和decoder呢?典型的例子就是两种语言之间的翻译。大概的数学任务就是,给定前后的词,来猜中间缺失的词是什么。这一类就是sequence to sequence 模型了。至于模型的评价,现有Rouge, Bleu等指标(怎么都是法语里的颜色……)。

好了,现在我们有一个transformer模型了,就可以高枕无忧了么?当然不是,下一阶段就是,fine-tuning 或者更准确的说,instruction fine tuning。

这一步,说到底就是让模型理解人们的意图。比如,我想让ChatGPT给我写代码,那我就会先给一个指令,help me write a code in python,这样它才可以理解我要的是代码而不是一个翻译任务。这类对于指定任务类型的 instruction 的训练,不仅仅在于理解目的,还牵扯到对于不同类型任务的参数细调。最简单粗暴的,我们可以要求对某一类型任务完全刷新所有参数,即full fine tuning,也可以省点资源,来只训练部分参数,即parameter efficient fine tuning PEFT。近期还有比较有意思的LoRa方法,在原来的参数矩阵外额外训练两个rank小很多的矩阵,最后再把新的两个小矩阵的乘起来,加到原始的参数矩阵上。甚至我们可以对instruct 的数据单独做一个小模型单独训练,然后在embedding 那一步把数据预处理后再喂给encoder or decoder。

fine tuning之后,理论上llm模型已经有了不错的预测能力了,但还需要一步alignment,即通过reinforcement learning 来进一步训练模型给出更符合人们需求的回答,比如 HHS (helpful, honest, harmless)。这一步主要是利用额外的人为标记的数据,比如对于多个候选答案之间的排序等等。当然,我们还可以搞个单独用来打分的模型给GPT的答案打分,哈哈,让机器自动自我修正。

这一些做完,基本上就是chatGPT 的雏形了。然后我们发现,不够,远远不够,一个AGI不能只有对话功能。下一步显然就是多模态Multimodality,即文字语音图像视频等等形式的结合。到这里,我们大概可以窥见这是一种“搭积木”的挑战了,即每一块儿自己的AI模型要和其他领域的结合起来,互通有无。

再来一组名词解释。Langchain,主要想法是各领域最后都转化为一个文本语言问题,然后互通有无。RAG (retrieval augmented generation) ,主要用来引入额外的信息来补全LLM的知识储备。ReAct (Reasoning and Acting augments) 主要是理解指令并利用各种多模态的模块来执行具体任务。

——

对了,为啥么这里园主通篇不提prompt。因为,园主觉得这就是个成长过程中不成熟阶段的伪命题……过两年可能就完全嵌入大模型本身了。

——

园主这些知识大概一半是Coursera 这门Generative AI with LLM 课扫盲来的。这门课主打一个深入浅出,适合理清大模型的整体逻辑,极其适合入门。剩下一半就是读各类的新闻和paper,还有各种视频。只能说,互联网时代,知识本身触手可及,考验的是系统学习的鉴别能力。

——

这篇本来是想写个提纲然后扔给GPT帮我完成的,结果最后还是老老实实的手动敲完了。哎,下次试试能不能用GPT写的更好一些。

❌
❌