You are on page 1of 55

AI 新宠:Prompt Learning

用提示学习调教大模型
李烨
微软亚洲互联网工程院
首席算法工程师

仅供个人学习使用
李烨

微软(亚洲)互联网工程院
首席算法工程师

− 20年科技行业从业经验
− 曾在SUN、EMC等跨国IT公司的核心研发部门工作
− 微软商用人工智能团队 AI 架构师
− 专家领域:知识图谱、智能对话、自然语言处理、机器学习
− 微软AI Talent Program(ATP)创始人
− 科普作家,出版《机器学习极简入门》、《算法第一步》等书

仅供个人学习使用
微软 AI Talent Program
我们将微软内部具备人工智能技术落地经验的算法工程师、研发工程师和产品经理组织在一起,为在校大学生、职场新
人提供丰富的人工智能课程实训和工程项目实践机会。大家可以从这里得到人工智能领域从入门到顶尖科技企业实习、
实践的机会,为求职就业打下坚实的基础,向自己梦想的岗位迈出踏实的一步。

零售业 金融业
专注于
培养人工智能技术落地
教育业 保险业 最后“一公里”的应用型人才!

交通运输 房地产

更多课程信息请
通讯业 制造业
关注官方公众号

…… “智汇AI”
医疗

仅供个人学习使用
ATP助力系统化学习AI 驱动企业AI转型 提升企业AI能力

AI工程师 AI专项深度
AI商学院
提升营 训练营

大学生 教师AI
AI训练营 素养课
大学生自我提升 教师AI素养提升

AI Talent Program 更多精彩课程

专家讲座
大学生AI 教师AI素 AI商学院 AI工程师LLM 技术直播
训练营 养课 课程 专题课程

初识AI
仅供个人学习使用
内容提纲

• AI 范式的变迁
• 提示学习(Prompt Learning)
• 提示(Prompting)与示例

仅供个人学习使用
AI 范式的变迁

仅供个人学习使用
认识 AI 模型

训练数据集
(成对的输入输出数据)


输入 语言模型 输出
x (Language Model, LM) y
P(y|x; θ)

仅供个人学习使用
从零开始训练(Training From Scratch)
训练

参数初 神经网络 模型
始化

训练数据集

仅供个人学习使用
模型越来越大

仅供个人学习使用
大模型的成本要素

• 算力和存储(硬件)

• 人工(算法科学家&工程师)

• 时间(数据准备&训练)

仅供个人学习使用
巨额成本倒逼训练范式
Training From Scratch
• 算法
• 神经网络架构
• 训练运行

• 算力
• GPU
参数初 神经网络 模型
始化 • 存储
• 训练框架

• 数据
• 数据收集
• 数据标注
训练数据集 • 数据处理

仅供个人学习使用
激发通用大模型的潜能

仅供个人学习使用
迁移学习(Transfer Learning)
• 把已学训练好的模型参数迁移到新的模型,来帮助新模型训练
• 基于预训练模型
• 针对特定的任务,有监督训练而成
• 相关概念
• 预训练(Pretrain)
• 预训练模型(Pretrained Model)
• 调整(Tuning)
• 微调(Fine Tune)
• 相对概念
• 从零训练(Training From Scratch)

仅供个人学习使用
微调(Fine-tuning)
Training Fine-tuning
From Scratch 网络结构
改变

N
NN-1 NN-1 N- 最终模型
预训练模型 2 (特定任务)

参数改变

Pretrain Tuning
Dataset Dataset

仅供个人学习使用
微调示例
• 基于BERT的Fine-tuning

仅供个人学习使用
提示学习

仅供个人学习使用
提示学习(Prompt Learning)

• prompt-based learning

• 让模型通过接收明确的提示来改进表现
• 不更新预训练模型的结构参数

• 向输入增加“提示信息”

• 将下游任务改为文本生成任务(LLM)

仅供个人学习使用
基于预训练语言模型的提示学习

传统语言模型 预训练语言模型
• 用于估计基于输入的输出产生的概 • 用于估计输入自身的概率:P(x; θ)
率:P(y|x; θ)
• 运行时根据输入 x 自身的概率预测
• 运行时根据基于输入 x 的条件概率, 输出 y 的概率
推理出输出 y

仅供个人学习使用
提示学习的形式化表达
• 原始输入 x
• 设计模板 …… [X] ………… [Z] ……
• 将 x 填入模板的 [X] 生成 x’
• 构建数据集 Z(Z 被称为答案空间,answer space),其中包含所有可能填写到 x’ 中
[Z] 处的值
• 将 Z 中的每一个 z 填入 x’ 的 [Z]处,生成填充提示(filled prompt),当 z 为真是答
案时,生成的填充提示称为答案提示
• 用 pretrained LM 处理每一个填充提示,计算其存在概率,选出概率最高的 z’

• 将 z’ 映射为答案 y’ (当多个对等答案共存时)

仅供个人学习使用
提示学习实力

• 输入(x): 我喜欢这部电影

提示模板: [X] 这部电影 [Z]


生成提示(x’): 我喜欢这部电影。这部电影[Z]

Z = {“优秀”, “好看”, “还行”, “不行”, “太差劲”}


Y = {正向,正向,中性,负向,负向}

仅供个人学习使用
预训练模型选择

GPT 1, 2, 3 BERT UniLM 1-2 T5


PanGu-α RoBERTA ERNIE-M BART
Ernie-3 ERNIE MASS

仅供个人学习使用
提示工程(Prompt Engineering)
• 手动模板工程(Manual Template Engineering)
• 自动化模板工程(Automated Template Engineering)
• 离散提示(Discrete Prompts):构建自然语言形式的提示
• 提示挖掘 (Prompt Mining)
• 提示改写 (Prompt Paraphrasing)
• 提示生成 (Prompt Generation)
• 提示打分 (Prompt Scoring)
• ……
• 连续提示(Continuous Prompts/Soft Prompts):构建文本嵌入形式的提示
• 前缀调整 (Prefix Tuning)
• 硬-软混合调整(Hard-Soft Prompt Hybrid Tuning)
• ……
仅供个人学习使用
答案工程(Answer Engineering)
• 手动设计答案空间(Answer Space Manual
Design)
• 无限空间
• 有限空间
• 离散答案搜索(Discrete Answer Search)
• 答案改写(Answer Paraphrasing)
• 标签分解(Label Decomposition)
• ……
• 连续答案搜索(Continuous Answer Search)

仅供个人学习使用
提示学习的研究领域:
多重提示学习(Multi-Prompt Learning)

• 提示集成(Prompt Ensembling)

• 提示增强(Prompt Augmentation)

• 提示合成(Prompt Composition)

• 提示分解(Prompt Decomposition)

仅供个人学习使用
思维链 (Chain of Thought, CoT)

仅供个人学习使用
微调和提示学习的联合使用

仅供个人学习使用
Few-shot/One-shot/Zero-shot

Few-shot

Learning

One-
shot
Prompting

Zero-
shot

仅供个人学习使用
*-shot Learning

• 利用语言模型执行任务的方法

• Zero-shot Learning
• 模型在没有某个特定类别的训练数据的情况下,仍然可以识别和分类这个类别

• One-shot Learning
• 模型在某个特定类别仅有一个样本的时候,可以识别和分类这个类别

• Few-shot Learning
• 模型在某个特定类别仅有几个样本的时候,可以识别和分类这个类别

仅供个人学习使用
*-shot Prompting

• Zero-shot (0S) Prompting


• One-shot (1S) Prompting
• Few-shot (FS) Prompting

仅供个人学习使用
Prompting 示例

仅供个人学习使用
ChatGPT Prompting

仅供个人学习使用
ChatGPT 生成数据可视化代码

仅供个人学习使用
仅供个人学习使用
利用ChatGPT + SmartKG 构建知识图谱

• 知识图谱题材:《哈利波特》
• 实体:人物

• 关系:人物间关系

• SmartKG
关注“智汇AI”
• 开源知识图谱引擎
输入“提示学习”
• https://github.com/microsoft/smartkg
领取相关资料
• 代码及截图
• https://github.com/juliali/AITechCampus/tree/master/ChatGPTKG
仅供个人学习使用
通过提问ChatGPT获得实体

仅供个人学习使用
通过提问ChatGPT获得关系

仅供个人学习使用
要求ChatGPT生成Python脚本处理实体数据

仅供个人学习使用
要求ChatGPT生成Python脚本处理关系数据

仅供个人学习使用
修改脚本并生成具体功能点

• 手动修改代码
• 自然语言描述 vs 程序语言描述

• 广度 vs 精度

• 在手动基础上ChatGPT生成细小
功能点

仅供个人学习使用
SmartKG 效果

仅供个人学习使用
基于SmartKG的搜索和对话

仅供个人学习使用
基于SmartKG的搜索和对话

仅供个人学习使用
提示学习产生的 IP 资产

• 提示学习带来的新问题

• 提示学习生成资料 vs 数据 & 算法

• IP 与资产 关注“智汇AI”
输入“提示学习”
领取相关资料
“哈利波特”知识图谱相关资料:
https://github.com/juliali/AITechCampus/tree/master/ChatGPTKG

仅供个人学习使用
Images Generation

仅供个人学习使用
A picture book generated by stable-diffusion
On a stormy night, a group of soldiers were engaged
in a fierce battle with a terrifying monster. This
monstrous creature possessed three heads and
twelve limbs, making it a formidable opponent.

The soldiers, tired, thirsty, and hungry, knew that


their survival was crucial to the safety of their
kingdom, as the monster was under the control of
an evil master who would surely destroy the land if
they were to fall.

Just as the fight was reaching a critical point, the


chief of the soldiers was gravely injured, bleeding
profusely from his mouth and wound. His assistants
attempted to move him to safety, but their efforts
were in vain.

The monster, sensing an opportunity, loomed over


them with its sharp teeth bared, ready to strike.

In the midst of this dire situation, a brave young girl


with bright, wide eyes and black hair, dressed in a
bold red outfit and wielding a kitchen knife,
suddenly appeared on the battlefield. With her
unwavering courage and loyalty, she stepped in
front of the monster, blocking its view of the
vulnerable soldiers.

With a shout that rang through the night, she


challenged the monster, her voice filled with
determination and bravery. Her actions inspired the
soldiers, who rallied behind her, unleashing a fierce
attack on the monster.

Though the battle was long and difficult, the soldiers


emerged victorious, thanks in no small part to the
bravery and quick thinking of the young girl in the
red dress. 仅供个人学习使用
Images generated by Intuition

仅供个人学习使用
Images generated by simple guided prompting
• Stable-Diffusion
• A dream of sailing boat floating on the ocean under the moon,
concept art, matte painting, HQ, 4k

仅供个人学习使用
Images generated by simple guided prompting
• Dall-E-2
• photograph of a gentle Old English Sheepdog in a wooden floor room
• An Impressionist oil painting of Tulipa gesneriana in a blue vase
• a big ship floating on the ocean, ukiyo-e
• 3D render of a cute smiling shark in the ocean, anime style, digital art, HQ

仅供个人学习使用
Images generated by simple guided prompting
• Midjourney
• a beautiful young queen, a in palace, dressed wedding dress, anime, big eyes, long hair, a
lot of jewelry
• logo, cartoon pixar style, A super cute happy baby manul, sitting on its own tail 8K
• a little Asian girl is playing with an Old English Sheepdog on the ground, photograph, 4K

仅供个人学习使用
Video Generation

仅供个人学习使用
仅供个人学习使用
仅供个人学习使用
仅供个人学习使用
仅供个人学习使用
谢谢

仅供个人学习使用

You might also like