Commit 0de349d0 by wangchenglong

Update AML textbook merged notes

parent f136e8b3
...@@ -9,3 +9,5 @@ ...@@ -9,3 +9,5 @@
*.run.xml *.run.xml
*.synctex.gz *.synctex.gz
*.toc *.toc
*.xdv
.DS_Store
# 中文课程讲义 LaTeX 模板 # 高级机器学习教材合订版
主文件是 `aml_notes.tex`,建议使用 XeLaTeX 编译: 主文件是 `main.tex`,建议使用 XeLaTeX 编译:
```powershell ```powershell
xelatex aml_notes.tex xelatex main.tex
xelatex aml_notes.tex bibtex main
xelatex main.tex
xelatex main.tex
``` ```
第二次编译用于生成完整目录和交叉引用。图片可以放在 `figures/``images/` 目录中,然后用 `\includegraphics` 插入。 章节内容分别位于 `section1``section5` 目录中,合订版通过 `main.tex` 统一引入各章正文、图片与参考文献。
常用修改位置:
- 课程信息:修改 `\coursename``\semester``\teacher``\school`
- 新增章节:使用 `\chapter{章节标题}`
- 新增小节:使用 `\section{小节标题}``\subsection{小节标题}`
- 插入表格:参考模板中的 `table``longtable` 示例。
- 插入图片:把图片放入 `figures/`,参考模板中的 `figure` 示例。
如果使用 Overleaf,也请选择 XeLaTeX 作为编译器。
File added
% !TeX program = xelatex
% Advanced Machine Learning textbook master file.
\documentclass[UTF8,a4paper,zihao=-4,oneside,openany]{ctexbook}
\usepackage[
top=2.6cm,
bottom=2.6cm,
left=2.7cm,
right=2.7cm,
headheight=15pt
]{geometry}
\usepackage{setspace}
\setstretch{1.25}
\setlength{\parindent}{2em}
\setlength{\parskip}{0.2em}
\setlength{\emergencystretch}{3em}
\sloppy
\usepackage{amsmath,amssymb,bm}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{array}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[normalem]{ulem}
\usepackage[mode=tex]{standalone}
\usepackage{import}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{
arrows.meta,
automata,
backgrounds,
calc,
chains,
decorations.pathmorphing,
decorations.pathreplacing,
fillbetween,
fit,
intersections,
matrix,
patterns,
positioning,
shadows,
shapes,
shapes.arrows,
shapes.geometric
}
\usepackage[most]{tcolorbox}
\usepackage{xcolor}
\definecolor{CourseInk}{HTML}{1F2933}
\definecolor{CourseBlue}{HTML}{24577A}
\definecolor{CourseGreen}{HTML}{2D6A4F}
\definecolor{CourseGold}{HTML}{A16207}
\definecolor{CourseGray}{HTML}{F4F6F8}
\definecolor{CourseLine}{HTML}{D9E2EC}
\definecolor{CoursePurple}{HTML}{6D28D9}
\definecolor{ugreen}{HTML}{2E8B57}
\definecolor{kellygreen}{HTML}{4CBB17}
\definecolor{lightcyan}{HTML}{D9F5F7}
\definecolor{lightgreen}{HTML}{DFF3D8}
\definecolor{lightpink}{HTML}{F7D9E8}
\definecolor{lolgreen}{RGB}{103,213,181}
\definecolor{lolorange}{RGB}{246,179,82}
\definecolor{lolpurple}{RGB}{200,158,196}
\definecolor{axiscolor}{HTML}{334155}
\definecolor{positioncolor}{HTML}{2563EB}
\definecolor{velocitycolor}{HTML}{D97706}
\definecolor{guidecolor}{HTML}{94A3B8}
\definecolor{positionbg}{HTML}{F5F8FF}
\definecolor{velocitybg}{HTML}{FFF9F0}
\definecolor{stateblue}{HTML}{2563EB}
\definecolor{velocityteal}{HTML}{0F766E}
\definecolor{markorange}{HTML}{EA580C}
\definecolor{axisgray}{HTML}{374151}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\newcommand{\mathbbm}[1]{\mathbf{#1}}
\graphicspath{
{section1/}{section2/}{section3/}{section4/}{section5/}
{section1/figures/}{section2/figures/}{section3/figures/}{section4/figures/}{section5/figures/}
}
\makeatletter
\def\input@path{%
{section1/}{section2/}{section3/}{section4/}{section5/}%
{section1/figures/}{section2/figures/}{section3/figures/}{section4/figures/}{section5/figures/}%
{section1/chapters/}{section3/section/}{section5/chapters/}%
}
\newcommand{\AMLFigure}[2][0.96\linewidth]{%
\begingroup
\let\AtBeginDocument\@gobble
\def\input@path{{section4/figures/}{figures/}{./}}%
\input{section4/figures/figure_style.tex}%
\includestandalone[width=#1]{section4/figures/#2}%
\endgroup
}
\makeatother
\tcbset{
enhanced,
boxrule=0.6pt,
arc=2mm,
left=3mm,
right=3mm,
top=2mm,
bottom=2mm,
breakable
}
\newtcolorbox{learninggoals}{
colback=CourseGray,
colframe=CourseBlue,
title=学习目标,
fonttitle=\bfseries
}
\newtcolorbox{importantnote}{
colback=white,
colframe=CourseGreen,
title=重点提示,
fonttitle=\bfseries
}
\newtcolorbox{examplebox}[1][]{
colback=white,
colframe=CourseGold,
title=例题 #1,
fonttitle=\bfseries
}
\newtcolorbox{exercisebox}{
colback=CourseGray,
colframe=CourseLine,
title=课后练习,
fonttitle=\bfseries\color{CourseInk}
}
\newtcolorbox{intuition}{
colback=CourseGray,
colframe=CoursePurple,
title=直观理解,
fonttitle=\bfseries
}
\newcommand{\coursename}{高级机器学习}
\newcommand{\semester}{2026}
\newcommand{\teacher}{授课教师}
\newcommand{\school}{学校 / 机构名称}
\newcommand{\lessondate}{\today}
\newcommand{\blankline}{\par\noindent\rule{\linewidth}{0.4pt}\par}
\newcommand{\keyword}[1]{\textbf{\color{CourseBlue}#1}}
\newcommand{\mindex}[1]{\textit{#1}}
\newcommand{\imageplaceholder}[2][5cm]{%
\fbox{%
\begin{minipage}[c][#1][c]{0.78\linewidth}
\centering
\textbf{图片占位}\\[0.45em]
{\small #2}
\end{minipage}%
}%
}
\newcommand{\todochen}[1]{%
\par\noindent
{\color{CourseGold}\small$\blacktriangleright$\ \textbf{[待陈丹补充 / 语音音频]}\ #1}\par}
\newcommand{\chapterinfo}[2][]{%
\begin{tcolorbox}[
colback=CourseGray,
colframe=CourseLine,
boxrule=0.5pt,
arc=1.5mm,
left=3mm,
right=3mm,
top=1.5mm,
bottom=1.5mm,
before skip=-0.4em,
after skip=1.0em
]
{\small\noindent\textbf{\color{CourseBlue}作者:}#2%
\if\relax\detokenize{#1}\relax
\else\\[0.25em]\textbf{\color{CourseBlue}参考:}#1%
\fi}
\end{tcolorbox}
}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\small 高级机器学习}
\fancyhead[R]{\small \leftmark}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0pt}
\usepackage[numbers,sort&compress]{natbib}
\bibliographystyle{plainnat}
\usepackage[
colorlinks=true,
linkcolor=CourseBlue,
urlcolor=CourseGreen,
citecolor=CourseGold
]{hyperref}
\ctexset{
chapter = {
name = {第,章},
number = \chinese{chapter},
format = \huge\bfseries\color{CourseBlue},
beforeskip = 0pt,
afterskip = 22pt
},
section = {
format = \Large\bfseries\color{CourseInk}
},
subsection = {
format = \large\bfseries\color{CourseInk}
}
}
\begin{document}
\begin{titlepage}
\centering
\vspace*{1.5cm}
{\zihao{1}\bfseries\color{CourseBlue}\coursename\par}
\vspace{0.8cm}
{\zihao{3}\bfseries 教材合订版\par}
\vspace{1.2cm}
\vfill
\begin{tabular}{rl}
\textbf{教师:} & \teacher \\
\textbf{学期:} & \semester \\
\textbf{单位:} & \school \\
\textbf{日期:} & \lessondate \\
\end{tabular}
\vspace*{1.2cm}
\end{titlepage}
\frontmatter
\pagestyle{plain}
\tableofcontents
\cleardoublepage
\mainmatter
\pagestyle{fancy}
\subimport{section1/}{chapter.tex}
\subimport{section2/}{chapter.tex}
\subimport{section3/}{chapter.tex}
\subimport{section4/}{chapter.tex}
\subimport{section5/}{chapter.tex}
\backmatter
% \renewcommand{\bibname}{参考文献}
% \addcontentsline{toc}{chapter}{参考文献}
\bibliography{references_merged}
\end{document}
This source diff could not be displayed because it is too large. You can view the blob instead.
# 中文课程讲义 LaTeX 模板
主文件是 `aml_notes.tex`,建议使用 XeLaTeX 编译:
```powershell
xelatex aml_notes.tex
xelatex aml_notes.tex
```
第二次编译用于生成完整目录和交叉引用。图片可以放在 `figures/``images/` 目录中,然后用 `\includegraphics` 插入。
常用修改位置:
- 课程信息:修改 `\coursename``\semester``\teacher``\school`
- 新增章节:使用 `\chapter{章节标题}`
- 新增小节:使用 `\section{小节标题}``\subsection{小节标题}`
- 插入表格:参考模板中的 `table``longtable` 示例。
- 插入图片:把图片放入 `figures/`,参考模板中的 `figure` 示例。
如果使用 Overleaf,也请选择 XeLaTeX 作为编译器。
% !TeX program = xelatex
% 高级机器学习中文讲义
% 编译建议:XeLaTeX 或 LuaLaTeX。Windows / macOS / Linux 均可使用 ctex 自动处理中文字体。
% \documentclass[UTF8,fontset=none,a4paper,zihao=-4,oneside,openany]{ctexbook}
\documentclass[UTF8,a4paper,zihao=-4,oneside,openany]{ctexbook}
% 页面与基础排版
\usepackage[
top=2.6cm,
bottom=2.6cm,
left=2.7cm,
right=2.7cm,
headheight=15pt
]{geometry}
\usepackage{setspace}
\setstretch{1.25}
\setlength{\parindent}{2em}
\setlength{\parskip}{0.2em}
% 数学、表格、图片
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{
arrows.meta,
automata,
backgrounds,
calc,
chains,
decorations.pathmorphing,
decorations.pathreplacing,
fit,
matrix,
patterns,
positioning,
shadows,
shapes,
shapes.arrows,
shapes.geometric
}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{array}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{caption}
\usepackage{subcaption}
\graphicspath{{figures/}{images/}}
% 与“参考样例”保持一致:数字编号引用,并对同一处的多篇文献排序、压缩
\usepackage[numbers,sort&compress]{natbib}
\bibliographystyle{plainnat}
% 颜色与强调框
\usepackage[most]{tcolorbox}
\usepackage{xcolor}
\definecolor{CourseInk}{HTML}{1F2933}
\definecolor{CourseBlue}{HTML}{24577A}
\definecolor{CourseGreen}{HTML}{2D6A4F}
\definecolor{CourseGold}{HTML}{A16207}
\definecolor{CourseGray}{HTML}{F4F6F8}
\definecolor{CourseLine}{HTML}{D9E2EC}
\definecolor{ugreen}{HTML}{2E8B57}
\definecolor{kellygreen}{HTML}{4CBB17}
\definecolor{lightcyan}{HTML}{D9F5F7}
\definecolor{lightgreen}{HTML}{DFF3D8}
\definecolor{lightpink}{HTML}{F7D9E8}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\tcbset{
enhanced,
boxrule=0.6pt,
arc=2mm,
left=3mm,
right=3mm,
top=2mm,
bottom=2mm,
breakable
}
\newtcolorbox{learninggoals}{
colback=CourseGray,
colframe=CourseBlue,
title=学习目标,
fonttitle=\bfseries
}
\newtcolorbox{importantnote}{
colback=white,
colframe=CourseGreen,
title=重点提示,
fonttitle=\bfseries
}
\newtcolorbox{examplebox}[1][]{
colback=white,
colframe=CourseGold,
title=例题 #1,
fonttitle=\bfseries
}
\newtcolorbox{exercisebox}{
colback=CourseGray,
colframe=CourseLine,
title=课后练习,
fonttitle=\bfseries\color{CourseInk}
}
% 页眉页脚与链接
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\small 高级机器学习讲义}
\fancyhead[R]{\small \leftmark}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0pt}
\usepackage[
colorlinks=true,
linkcolor=CourseBlue,
urlcolor=CourseGreen,
citecolor=CourseGold
]{hyperref}
% 章节样式
\ctexset{
chapter = {
name = {第,讲},
number = \chinese{chapter},
format = \huge\bfseries\color{CourseBlue},
beforeskip = 0pt,
afterskip = 22pt
},
section = {
format = \Large\bfseries\color{CourseInk}
},
subsection = {
format = \large\bfseries\color{CourseInk}
}
}
% 常用命令
\newcommand{\coursename}{高级机器学习}
\newcommand{\semester}{2026}
\newcommand{\teacher}{授课教师}
\newcommand{\school}{学校 / 机构名称}
\newcommand{\lessondate}{\today}
\newcommand{\blankline}{\par\noindent\rule{\linewidth}{0.4pt}\par}
\newcommand{\keyword}[1]{\textbf{\color{CourseBlue}#1}}
\begin{document}
% 封面
\begin{titlepage}
\centering
\vspace*{1.5cm}
{\zihao{1}\bfseries\color{CourseBlue}\coursename\par}
\vspace{0.8cm}
{\zihao{3}\bfseries 第一部分:机器学习数学基础\par}
\vspace{1.2cm}
\IfFileExists{figures/cover.jpg}{
\includegraphics[width=0.78\linewidth,height=0.32\textheight,keepaspectratio]{figures/cover.jpg}
}{
\fbox{
\begin{minipage}[c][0.24\textheight][c]{0.78\linewidth}
\centering
将封面图命名为 \texttt{cover.jpg} 并放入 \texttt{figures/} 目录后,会自动显示在这里。
\end{minipage}
}
}
\vfill
\begin{tabular}{rl}
\textbf{教师:} & \teacher \\
\textbf{学期:} & \semester \\
\textbf{单位:} & \school \\
\textbf{日期:} & \lessondate \\
\end{tabular}
\vspace*{1.2cm}
\end{titlepage}
% 目录
\frontmatter
\pagestyle{plain}
\tableofcontents
\cleardoublepage
% 正文
\mainmatter
\pagestyle{fancy}
\input{chapters/part1_ml_foundations}
\backmatter
\renewcommand{\bibname}{参考文献}
\addcontentsline{toc}{chapter}{参考文献}
\bibliography{references}
\end{document}
% !TeX root = ../main.tex
% 第一章正文入口。
\input{chapters/part1_ml_foundations}
This source diff could not be displayed because it is too large. You can view the blob instead.
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
\foreach \r/\dy in {5.2/0.70,4.2/0.50,3.2/0.33,2.3/0.20,1.4/0.10,0.65/0.02}{
\draw[FigureLine,rotate=25,line width=.8pt] (0,\dy) ellipse ({\r} and {\r*0.45});
}
\coordinate (p1) at (-4.4,-2.0);
\coordinate (p2) at (-3.5,-1.0);
\coordinate (p3) at (-2.5,-1.35);
\coordinate (p4) at (-1.8,-0.65);
\coordinate (p5) at (-1.15,-0.82);
\coordinate (p6) at (-0.70,-0.38);
\coordinate (p7) at (-0.35,-0.30);
\coordinate (p8) at (-0.08,-0.08);
\foreach \i in {1,...,7}{
\draw[-stealth,FigureCoral,line width=1.2pt] (p\i) -- (p\the\numexpr\i+1\relax);
}
\fill[FigureInk] (0,0) ellipse (0.10 and 0.05);
\node[font=\footnotesize,fill=FigureWarm,rounded corners=1pt,inner sep=2pt] at (0.72,0.25){最小值};
\node[font=\footnotesize,FigureCoral] at (-3.8,-2.25){参数更新路径};
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}
\begin{scope}
\begin{axis}[
width=4.7cm,height=4.7cm,
title={\small 线性可分},
xlabel={$x_1$}, ylabel={$x_2$},
xmin=0,xmax=8,ymin=0,ymax=8,
xtick={0,2,4,6,8}, ytick={0,2,4,6,8},
title style={yshift=-0.4em},
ylabel style={yshift=-0.4em},
]
\addplot[only marks,blue,very thick,mark=square] coordinates{
(1.0,3.3) (1.4,3.8) (1.8,4.6) (2.3,5.1) (2.7,5.8)
(3.1,6.3) (3.5,5.7) (3.9,6.9) (4.4,6.4) (4.9,7.1)};
\addplot[only marks,red,very thick,mark=x,mark size=4pt] coordinates{
(2.7,0.9) (3.2,1.6) (3.8,2.0) (4.4,2.5) (5.0,2.9)
(5.6,3.4) (6.1,3.8) (6.7,4.3) (7.2,5.0)};
\addplot[black,semithick,domain=0:8]{0.72*x+1.3};
\end{axis}
\end{scope}
\begin{scope}[xshift=5.2cm]
\begin{axis}[
width=4.7cm,height=4.7cm,
title={\small 圆形边界},
xlabel={$x_1$}, ylabel={$x_2$},
xmin=0,xmax=8,ymin=0,ymax=8,
xtick={0,2,4,6,8}, ytick={0,2,4,6,8},
title style={yshift=-0.4em},
ylabel style={yshift=-0.4em},
]
\addplot[black,semithick,domain=0:360,samples=80] ({4+1.7*cos(x)},{4+1.7*sin(x)});
\addplot[only marks,red,very thick,mark=x,mark size=4pt] coordinates{
(3.2,3.6) (3.5,4.6) (4.0,5.0) (4.6,4.7) (5.0,3.9)
(4.5,3.1) (3.8,3.0) (3.1,4.2)};
\addplot[only marks,blue,very thick,mark=square] coordinates{
(0.9,6.1) (1.4,6.5) (2.2,6.8) (3.0,7.1) (4.1,7.3)
(5.0,7.0) (6.4,6.4) (7.2,5.6) (7.0,2.0) (6.4,1.2)
(5.3,0.8) (4.1,0.6) (2.8,1.0) (1.6,2.1) (1.0,3.4)};
\end{axis}
\end{scope}
\begin{scope}[xshift=10.4cm]
\begin{axis}[
width=4.7cm,height=4.7cm,
title={\small 复杂边界},
xlabel={$x_1$}, ylabel={$x_2$},
xmin=0,xmax=8,ymin=0,ymax=8,
xtick={0,2,4,6,8}, ytick={0,2,4,6,8},
title style={yshift=-0.4em},
ylabel style={yshift=-0.4em},
]
\addplot[black,semithick,smooth] coordinates{
(1.0,4.3) (1.7,4.1) (2.5,3.7) (3.1,3.0) (3.9,2.8)
(4.6,3.3) (5.4,3.1) (6.2,3.6) (6.8,4.2)};
\addplot[black,semithick,smooth] coordinates{
(2.2,3.7) (3.0,4.4) (3.9,5.1) (4.8,5.5) (5.7,5.2)};
\addplot[only marks,red,very thick,mark=x,mark size=4pt] coordinates{
(2.0,3.8) (2.8,3.5) (3.4,4.1) (4.0,3.6) (4.8,4.5)
(5.3,3.8) (5.6,5.1)};
\addplot[only marks,blue,very thick,mark=square] coordinates{
(0.9,5.1) (1.5,5.7) (2.4,6.4) (3.3,6.0) (4.3,6.7)
(5.4,6.2) (6.5,6.9) (7.3,5.8) (1.2,2.0) (2.2,1.1)
(3.2,1.7) (4.3,2.2) (5.5,2.0) (6.6,2.6) (7.2,3.4)};
\end{axis}
\end{scope}
\end{tikzpicture}
\end{center}
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
\begin{axis}[
aml axis,
width=8.4cm,height=8.0cm,
xlabel={$x_1$}, ylabel={$x_2$},
xmin=0,xmax=8,ymin=0,ymax=8,
xtick={0,1,2,3,4,5,6,7,8},
ytick={0,1,2,3,4,5,6,7,8},
legend style={at={(0.5,1.08)},anchor=south,draw=none,legend columns=2},
]
\addplot[only marks,blue,very thick,mark=square] coordinates{
(0.6,5.7) (0.9,4.8) (1.1,6.8) (1.5,5.2) (1.8,7.1)
(2.2,6.0) (2.4,7.5) (2.8,5.6) (3.2,6.4) (3.5,7.8)
(3.7,6.9) (4.0,7.3) (4.3,6.2) (4.6,7.6)};
\addlegendentry{食物类}
\addplot[only marks,red,very thick,mark=x,mark size=4pt] coordinates{
(3.0,0.8) (3.4,1.5) (3.8,2.1) (4.1,1.1) (4.5,2.4)
(4.9,1.7) (5.2,0.9) (5.6,2.7) (6.0,1.3) (6.5,3.0)
(6.9,1.1) (7.3,4.1)};
\addlegendentry{非食物类}
\addplot[FigureInk,semithick,domain=0:8]{1.55*x-1.0};
\addplot[FigureTeal,semithick,dashed,domain=0:8]{0.55*x+1.6};
\addplot[FigureGold,semithick,dash dot,domain=0:8]{-0.35*x+5.0};
\node[rotate=56,anchor=south] at (axis cs:2.0,2.2) {\footnotesize 超平面 1};
\node[rotate=29,anchor=south] at (axis cs:5.7,4.9) {\footnotesize 超平面 2};
\node[rotate=-20,anchor=south] at (axis cs:2.3,4.0) {\footnotesize 超平面 3};
\end{axis}
\end{tikzpicture}
\end{center}
% Shared visual language for figures in Chapter 1.
\providecolor{FigureInk}{HTML}{263640}
\providecolor{FigureBlue}{HTML}{347A9E}
\providecolor{FigureTeal}{HTML}{3B8176}
\providecolor{FigureCoral}{HTML}{C65F54}
\providecolor{FigureGold}{HTML}{C49332}
\providecolor{FigureLilac}{HTML}{746DA3}
\providecolor{FigureMist}{HTML}{EDF3F5}
\providecolor{FigureWarm}{HTML}{F8F0E4}
\providecolor{FigureLine}{HTML}{B8C5CC}
\providecolor{FigureMuted}{HTML}{6F7F88}
\tikzset{
aml figure/.style={
line cap=round,
line join=round,
>={Stealth[length=5pt,width=4pt]},
every node/.append style={text=FigureInk}
},
aml soft node/.style={
draw=FigureInk!65,
fill=FigureMist,
line width=.7pt,
rounded corners=2pt
},
aml warm node/.style={
draw=FigureGold!75!FigureInk,
fill=FigureWarm,
line width=.7pt,
rounded corners=2pt
},
aml flow/.style={->,draw=FigureInk!75,line width=.8pt},
aml accent flow/.style={->,draw=FigureTeal,line width=1pt}
}
\pgfplotsset{
aml axis/.style={
axis line style={draw=FigureInk!75},
tick style={draw=FigureInk!65},
tick label style={text=FigureInk},
label style={text=FigureInk},
grid=major,
grid style={draw=FigureLine!55,line width=.25pt},
legend style={draw=none,fill=white,fill opacity=.88,text opacity=1}
}
}
% Remap the standard colors used by the source figures inside each picture.
\providecommand{\amlfigurepalette}{%
\colorlet{blue}{FigureBlue}%
\colorlet{red}{FigureCoral}%
\colorlet{green}{FigureTeal}%
\colorlet{ugreen}{FigureTeal}%
\colorlet{orange}{FigureGold}%
\colorlet{purple}{FigureLilac}%
\colorlet{cyan}{FigureBlue}%
\colorlet{gray}{FigureLine}%
}
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
\begin{axis}[
aml axis,
width=11cm,height=5.6cm,
xlabel={模型复杂度},
ylabel={误差},
xmin=0,xmax=10,ymin=0,ymax=20,
xmajorticks=false,
ymajorticks=false,
legend style={at={(0.70,0.78)},anchor=west,draw=none},
]
\addplot[blue,line width=1.5pt,smooth] coordinates{
(0,18) (0.7,9) (1.5,5.5) (2.5,3.6) (4,2.2)
(5.5,1.5) (7,1.0) (9.5,0.8)};
\addlegendentry{训练误差}
\addplot[red,line width=1.5pt,smooth] coordinates{
(0,18) (0.7,10) (1.5,6.5) (2.5,4.4) (4,3.2)
(5.5,3.1) (7,4.6) (8.5,6.7) (9.5,8.5)};
\addlegendentry{验证误差}
\draw[dashed,FigureTeal,line width=.8pt] (axis cs:5.1,0) -- (axis cs:5.1,20);
\node[anchor=south,FigureTeal] at (axis cs:5.1,17.2) {\footnotesize 合适复杂度};
\end{axis}
\end{tikzpicture}
\end{center}
x y
5.124345363663242 1
2.8882435863499247 1
2.9718282477365445 1
2.4270313778438295 1
4.365407629324679 1
1.1984613031197173 1
5.24481176421648 1
2.738793099104897 1
3.8190390960570983 1
3.25062962452259 1
4.9621079370449745 1
1.439859290502346 1
3.1775827959864924 1
3.1159456453315846 1
4.633769442335438 1
2.400108732685969 1
3.3275717924495645 1
2.622141582078628 1
3.5422137467155927 1
4.082815213715822 1
2.3993808227870788 1
4.644723709839614 1
4.401590720592796 1
4.002494338901868 1
4.400855949264412 1
2.816272140825667 1
3.377109774481352 1
2.5642305657409312 1
3.232111920373984 1
4.030355466738186 1
2.8083392482746907 1
3.1032464731440226 1
2.8128272998804005 1
2.6547943585012805 1
2.828753869163181 1
3.487335401081099 1
2.382689651364722 1
3.7344156978170924 1
5.159802177109871 1
4.242044160577335 1
3.308164447638385 1
2.6123710359151637 1
2.7528417062491624 1
5.192454601027746 1
3.550807754776029 1
2.8630043534306466 1
3.690915484667466 1
5.600255136478842 1
3.620158952481629 1
4.117203109707419 1
3.8001703199558277 1
3.1477501535064816 1
2.35748180197786 1
3.1506572775871224 1
3.291105766625222 1
4.0866231911821975 1
4.338983413874505 1
4.431102081303557 1
3.7855873252542587 1
4.385141164270728 1
2.745602059003347 1
4.752868155233288 1
4.012929820418009 1
3.2019071648972846 1
3.988518146537497 1
3.4244282869789444 1
4.631629387451427 1
5.019816816422199 1
5.685575406533161 1
2.1035036645118623 1
2.0558861945704106 1
2.9955341370535487 1
3.6600370694478306 1
4.376168921116225 1
3.8156349472416053 1
1.477798784175997 1
3.193795987371628 1
4.327974642607246 1
3.7300947353643834 1
4.262011180312025 1
3.277671857389641 1
3.2992419310700027 1
3.686561390988284 1
3.9100516472082565 1
3.6982997201267698 1
3.6190086458074586 1
2.829337713710969 1
3.8775637863209194 1
3.621821270991437 1
4.629483907911919 1
4.698917879901507 1
3.685156417483944 1
3.1247150499098857 1
2.861269592545778 1
3.923494354064113 1
3.577340068348559 1
3.1561463244289243 1
3.543596856834247 1
2.8799991560518707 1
4.198032034072219 1
x y
2.052871435214002 2
3.724507704805499 2
2.9034916417908 2
3.093578523237067 2
1.4050881542589582 2
2.669382433058668 2
3.2405564510962748 2
1.5462993981920654 2
2.233781493996378 2
2.5326145466933587 2
1.1268826797532443 2
2.815159392042292 2
3.3461606475850334 2
1.6404840591680137 2
2.8505459786641074 2
1.1877165887625682 2
2.4613044907339487 2
0.8842276452967053 2
3.621417708235664 2
2.9089005379368276 2
2.4753830441242215 2
1.7248383808308403 2
3.7737559301587766 2
4.467101749254734 2
0.6420181355532479 2
3.7361640304528203 2
4.127650753148906 2
2.8380116965744757 2
1.300731967664814 2
3.3633453175440216 2
2.3190796979218495 2
1.8960793722067426 2
1.2699418643330382 2
3.0505374959762155 2
3.2928068659193475 2
1.8764692703202084 2
3.0205763370733707 2
1.3556586103768573 2
3.301861031871345 2
2.5465672984241454 2
2.3134302280926513 2
2.398254127470855 2
3.3688861570058677 2
3.2504116398650083 2
3.029465324352709 2
2.637701209997386 2
2.577821127912706 2
3.1183802619985244 2
2.732494559178738 2
3.1825514068644853 2
2.18988322648194 2
0.06516223589286119 2
3.538824601859414 2
4.686979646974258 2
2.941364443568582 2
2.3998447667165004 2
2.3635552561039668 2
2.3809458122251903 2
2.5174094083000047 2
1.3779812712531117 2
1.982905542079772 2
1.502973172349737 2
2.7487991613877707 2
2.2033588476291373 2
2.9952113239779603 2
2.325296840257499 2
3.486335187821242 2
2.713533901335442 2
4.690699728969733 2
0.6036390771089075 2
1.853083311745092 2
3.401486891648711 2
5.028325706806398 2
2.25136522228454 2
2.543668993178389 2
2.273685757486395 2
3.831457112587592 2
2.212692136523981 2
3.1800698398781044 2
2.180198401101329 2
1.2274412447540057 2
2.8135477204634323 2
3.0031848134353263 2
3.793225882532262 2
2.3895529735826835 2
1.882637936287639 2
3.062761096619026 2
2.7407370922377323 2
2.780665077122639 2
2.426887296252722 2
3.66033856999377 2
2.869492716375724 2
4.404658708340981 2
3.6110566985605046 2
3.15904979610021 2
0.8725616593837426 2
3.102319280295629 2
2.9202822036470595 2
3.3109516728035557 2
3.544442094707259 2
x y
6.599121808211073 3
7.824005618450408 3
6.43769456898091 3
8.954878075009034 3
5.668048333482751 3
5.239311439601217 3
5.3492787341759 3
6.109444415836951 3
5.880884601440272 3
8.956078903703641 3
6.673500501921816 3
5.657324210622564 3
8.114382976779792 3
6.413476061178407 3
5.763146623458603 3
7.8758389276492995 3
7.623362176578032 3
6.565043317044772 3
8.407540000241228 3
7.129101579710725 3
8.6169495988573 3
7.502740881999904 3
8.55880554061986 3
7.109402696425428 3
5.780255603020967 3
9.449368649061398 3
6.454225832017432 3
6.8011621371111035 3
6.299601495078745 3
6.796605551035442 3
7.2426694410817944 3
7.201830178874004 3
7.661020287598693 3
8.792158208975566 3
6.879535428211493 3
5.766879264553573 3
5.817681873490367 3
6.334245481800873 3
5.325804192381068 3
7.825029824438986 3
6.5017864363689215 3
6.689015021697149 3
6.998108517161996 3
5.603379575404568 3
6.138683639223958 3
7.6747115256879725 3
7.618539130786293 3
6.556828069299362 3
8.810534914125457 3
5.694273077442262 3
6.655012789845021 3
6.769160256864531 3
4.20691499985346 3
8.93752881361608 3
7.366332014540058 3
5.955410618092208 3
9.051173442857444 3
7.585662000172382 3
7.429526140021965 3
6.393001601799954 3
7.106222724035217 3
5.474319683770642 3
7.795026094424845 3
6.625561681156779 3
7.134048196554623 3
8.202054862199706 3
7.284748110849058 3
7.262467445463269 3
7.276499304822184 3
6.266728396104687 3
7.836004719434269 3
8.543359110804484 3
7.758805660097931 3
7.884908814464883 3
6.122718481081812 3
6.132212777127075 3
5.55912397570816 3
8.232253070828436 3
6.745820132392632 3
8.399843942480986 3
6.2180883173131996 3
6.5624910171714195 3
7.095425087191257 3
7.921450068659511 3
7.060750195799507 3
7.211124755007717 3
7.016527567305616 3
7.177187720275961 3
5.883529982115256 3
7.080927100973279 3
6.813421006488534 3
6.943175519114153 3
7.492336555936649 3
6.319321858991114 3
6.91549197259537 3
6.702638117226496 3
7.417302004974863 3
7.78477065101559 3
6.044574737626311 3
7.585910431102616 3
x y
0.5 0.0044318484119380075
0.6224489795918368 0.0063513463131534146
0.7448979591836735 0.008966748444677233
0.8673469387755102 0.012470747171139728
0.9897959183673469 0.017085915686358193
1.1122448979591837 0.023060692208763875
1.2346938775510203 0.030661593077694442
1.3571428571428572 0.04016108044090312
1.4795918367346939 0.051820828999617916
1.6020408163265305 0.06587060018907169
1.7244897959183674 0.08248351897454742
1.846938775510204 0.10174920793009684
1.9693877551020407 0.12364688773086303
2.0918367346938775 0.14802110351235884
2.2142857142857144 0.1745630742814002
2.336734693877551 0.20280068705640536
2.4591836734693877 0.2320997956849513
2.5816326530612246 0.2616787100740331
2.704081632653061 0.29063660944969527
2.826530612244898 0.31799518388693393
2.9489795918367347 0.3427512620862568
3.071428571428571 0.3639367217154836
3.193877551020408 0.3806808152989237
3.316326530612245 0.3922693707583609
3.4387755102040813 0.3981952754136922
3.561224489795918 0.39819527541369226
3.683673469387755 0.3922693707583609
3.806122448979592 0.3806808152989237
3.9285714285714284 0.3639367217154837
4.051020408163265 0.3427512620862568
4.173469387755102 0.31799518388693404
4.295918367346939 0.29063660944969527
4.418367346938775 0.2616787100740331
4.540816326530612 0.23209979568495137
4.663265306122449 0.20280068705640536
4.785714285714286 0.1745630742814002
4.908163265306122 0.14802110351235895
5.030612244897959 0.12364688773086303
5.153061224489796 0.10174920793009688
5.275510204081632 0.08248351897454748
5.3979591836734695 0.06587060018907169
5.520408163265306 0.051820828999617916
5.642857142857142 0.040161080440903156
5.76530612244898 0.030661593077694442
5.887755102040816 0.023060692208763875
6.0102040816326525 0.017085915686358217
6.13265306122449 0.012470747171139728
6.255102040816326 0.008966748444677233
6.377551020408163 0.00635134631315342
6.5 0.0044318484119380075
x y
-0.5 0.0044318484119380075
-0.37755102040816324 0.0063513463131534146
-0.25510204081632654 0.00896674844467722
-0.13265306122448983 0.012470747171139728
-0.010204081632653073 0.017085915686358193
0.11224489795918369 0.023060692208763875
0.23469387755102034 0.030661593077694442
0.3571428571428571 0.04016108044090312
0.47959183673469385 0.051820828999617916
0.6020408163265305 0.06587060018907169
0.7244897959183674 0.08248351897454742
0.846938775510204 0.10174920793009684
0.9693877551020407 0.12364688773086303
1.0918367346938775 0.14802110351235884
1.2142857142857142 0.17456307428140014
1.336734693877551 0.2028006870564054
1.4591836734693877 0.2320997956849513
1.5816326530612246 0.2616787100740331
1.704081632653061 0.29063660944969527
1.8265306122448979 0.31799518388693393
1.9489795918367347 0.3427512620862568
2.071428571428571 0.3639367217154836
2.193877551020408 0.3806808152989237
2.316326530612245 0.3922693707583609
2.4387755102040813 0.3981952754136922
2.561224489795918 0.39819527541369226
2.683673469387755 0.3922693707583609
2.806122448979592 0.3806808152989237
2.9285714285714284 0.3639367217154837
3.0510204081632653 0.3427512620862568
3.173469387755102 0.31799518388693393
3.2959183673469385 0.2906366094496954
3.4183673469387754 0.2616787100740331
3.540816326530612 0.23209979568495137
3.663265306122449 0.20280068705640536
3.7857142857142856 0.1745630742814002
3.908163265306122 0.14802110351235895
4.030612244897959 0.12364688773086303
4.153061224489796 0.10174920793009688
4.275510204081632 0.08248351897454748
4.3979591836734695 0.06587060018907169
4.520408163265306 0.051820828999617916
4.642857142857142 0.040161080440903156
4.76530612244898 0.030661593077694442
4.887755102040816 0.023060692208763875
5.0102040816326525 0.017085915686358217
5.13265306122449 0.012470747171139728
5.255102040816326 0.008966748444677233
5.377551020408163 0.00635134631315342
5.5 0.0044318484119380075
x y
4.0 0.0044318484119380075
4.122448979591836 0.006351346313153403
4.244897959183674 0.008966748444677233
4.36734693877551 0.012470747171139728
4.489795918367347 0.017085915686358172
4.612244897959184 0.023060692208763875
4.73469387755102 0.030661593077694442
4.857142857142857 0.040161080440903094
4.979591836734694 0.051820828999617916
5.1020408163265305 0.06587060018907169
5.224489795918368 0.08248351897454748
5.346938775510204 0.10174920793009688
5.469387755102041 0.12364688773086303
5.591836734693878 0.14802110351235895
5.714285714285714 0.1745630742814002
5.836734693877551 0.20280068705640536
5.959183673469388 0.23209979568495137
6.081632653061225 0.2616787100740331
6.204081632653061 0.29063660944969527
6.326530612244898 0.31799518388693404
6.448979591836735 0.3427512620862568
6.571428571428571 0.3639367217154836
6.6938775510204085 0.3806808152989237
6.816326530612245 0.3922693707583609
6.938775510204081 0.3981952754136922
7.061224489795919 0.3981952754136922
7.183673469387755 0.3922693707583609
7.3061224489795915 0.3806808152989237
7.428571428571429 0.3639367217154836
7.551020408163265 0.3427512620862568
7.673469387755102 0.31799518388693404
7.795918367346939 0.29063660944969527
7.918367346938775 0.2616787100740331
8.040816326530612 0.23209979568495137
8.16326530612245 0.20280068705640536
8.285714285714285 0.17456307428140042
8.408163265306122 0.14802110351235895
8.53061224489796 0.12364688773086303
8.653061224489797 0.10174920793009673
8.775510204081632 0.08248351897454748
8.89795918367347 0.06587060018907169
9.020408163265305 0.051820828999618006
9.142857142857142 0.040161080440903156
9.26530612244898 0.030661593077694442
9.387755102040817 0.023060692208763823
9.510204081632653 0.017085915686358217
9.63265306122449 0.012470747171139728
9.755102040816325 0.008966748444677257
9.877551020408163 0.00635134631315342
10.0 0.0044318484119380075
\input{figures/figure-theme.tex}
\begingroup
\begin{tikzpicture}[aml figure]
\amlfigurepalette
\def\nodesep{1.5em}
\def\noderowsep{2em}
\tikzstyle{pnode} = [minimum width=6.5em,minimum height=2em,inner sep=3pt,draw=FigureInk!65,fill=FigureMist,line width=.7pt,rounded corners=2pt]
%%%%%%%%%%
%% CBOW
\begin{scope}
%% word vectors
\node [pnode,anchor=east] (e2) at (-\nodesep,0) {\footnotesize{$\mathbf{e}_{i-1}=\boldsymbol{1}_{w_{i-1}} \mathbf{C}$}};
\node [pnode,anchor=east] (e1) at ([xshift=-\nodesep]e2.west) {\footnotesize{$\mathbf{e}_{i-2}=\boldsymbol{1}_{w_{i-2}} \mathbf{C}$}};
\node [pnode,anchor=west] (e4) at (\nodesep,0) {\footnotesize{$\mathbf{e}_{i+1}=\boldsymbol{1}_{w_{i+1}} \mathbf{C}$}};
\node [pnode,anchor=west] (e5) at ([xshift=\nodesep]e4.east) {\footnotesize{$\mathbf{e}_{i+2}=\boldsymbol{1}_{w_{i+2}} \mathbf{C}$}};
%% input words
\node [anchor=north] (w1) at ([yshift=-0.8*\noderowsep]e1.south) {$w_{i-2}$};
\node [anchor=north] (w2) at ([yshift=-0.8*\noderowsep]e2.south) {$w_{i-1}$};
\node [anchor=north] (w4) at ([yshift=-0.8*\noderowsep]e4.south) {$w_{i+1}$};
\node [anchor=north] (w5) at ([yshift=-0.8*\noderowsep]e5.south) {$w_{i+2}$};
%% hidden states
\node [pnode,anchor=south,minimum width=18.0em,minimum height=2.5em] (hidden) at ([xshift=\nodesep,yshift=\noderowsep]e2.north east) {$\mathbf{h} = \frac{1}{4} \left( \sum_{j=i-2}^{i-1} \mathbf{e}_{j} + \sum_{j=i+1}^{i+2} \mathbf{e}_{j} \right)$};
%% softmax
\node [pnode,anchor=south,minimum width=18.0em,minimum height=2.5em] (softmax) at ([yshift=\noderowsep]hidden.north) {$\mathbf{y} = \mathrm{Softmax}(\mathbf{h} \mathbf{U} + \mathbf{b})$};
%% output
\node [anchor=south] (output) at ([yshift=0.8*\noderowsep]softmax.north) {$\Pr(w_{i}|w_{i-2},w_{i-1},w_{i+1},w_{i+2})=y(w_{i})$};
%% connections
\draw [->,thick] ([yshift=1pt]w1.north) -- ([yshift=-1pt]e1.south);
\draw [->,thick] ([yshift=1pt]w2.north) -- ([yshift=-1pt]e2.south);
\draw [->,thick] ([yshift=1pt]w4.north) -- ([yshift=-1pt]e4.south);
\draw [->,thick] ([yshift=1pt]w5.north) -- ([yshift=-1pt]e5.south);
\draw [->,thick] ([yshift=1pt]e1.north) .. controls +(north:2em) and +(south:1.5em) .. ([xshift=-5em,yshift=-1pt]hidden.south);
\draw [->,thick] ([yshift=1pt,xshift=-0em]e2.north) .. controls +(north:1.5em) and +(south:1.5em) .. ([xshift=-1.5em,yshift=-1pt]hidden.south);
\draw [->,thick] ([yshift=1pt,xshift=0em]e4.north) .. controls +(north:1.5em) and +(south:1.5em) .. ([xshift=1.5em,yshift=-1pt]hidden.south);
\draw [->,thick] ([yshift=1pt]e5.north) .. controls +(north:2em) and +(south:1.5em) .. ([xshift=5em,yshift=-1pt]hidden.south);
\draw [->,thick] ([yshift=1pt]hidden.north) -- ([yshift=-1pt]softmax.south);
\draw [->,thick] ([yshift=1pt]softmax.north) -- ([yshift=-1pt]output.south);
%% label
\node [anchor=north] (labela) at ([xshift=\nodesep,yshift=-1.6*\noderowsep]e2.south east) {(a) CBOW};
\node [anchor=south] (embeddinglabel) at (e1.north west) {\textbf{Embedding}};
\end{scope}
%%%%%%%%%%
%% skip-gram
\begin{scope}[yshift=-16em]
%% softmax
\node [pnode,anchor=east,align=left] (softmax2) at (-\nodesep,0) {\footnotesize{$\mathbf{y}_{-1}=\mathrm{Softmax}($}\\\footnotesize{$\mathbf{h} \mathbf{V}_{-1} + \mathbf{b}_{-1})$}};
\node [pnode,anchor=east,align=left] (softmax1) at ([xshift=-\nodesep]softmax2.west) {\footnotesize{$\mathbf{y}_{-2}=\mathrm{Softmax}($}\\\footnotesize{$\mathbf{h} \mathbf{V}_{-2} + \mathbf{b}_{-2})$}};
\node [pnode,anchor=west,align=left] (softmax4) at (\nodesep,0) {\footnotesize{$\mathbf{y}_{1}=\mathrm{Softmax}($}\\\footnotesize{$\mathbf{h} \mathbf{V}_{1} + \mathbf{b}_{1})$}};
\node [pnode,anchor=west,align=left] (softmax5) at ([xshift=\nodesep]softmax4.east) {\footnotesize{$\mathbf{y}_{2}=\mathrm{Softmax}($}\\\footnotesize{$\mathbf{h} \mathbf{V}_{2} + \mathbf{b}_{2})$}};
%% output
\node [anchor=south,align=left] (output1) at ([yshift=0.8*\noderowsep]softmax1.north) {\ \ \ \footnotesize{$\Pr(w_{i-2}|w_i)$}\\\footnotesize{$=y_{-2}(w_{i-2})$}};
\node [anchor=south,align=left] (output2) at ([yshift=0.8*\noderowsep]softmax2.north) {\ \ \ \footnotesize{$\Pr(w_{i-1}|w_i)$}\\\footnotesize{$=y_{-1}(w_{i-1})$}};
\node [anchor=south,align=left] (output4) at ([yshift=0.8*\noderowsep]softmax4.north) {\ \ \ \footnotesize{$\Pr(w_{i+1}|w_i)$}\\\footnotesize{$=y_{1}(w_{i+1})$}};
\node [anchor=south,align=left] (output5) at ([yshift=0.8*\noderowsep]softmax5.north) {\ \ \ \footnotesize{$\Pr(w_{i+2}|w_i)$}\\\footnotesize{$=y_{2}(w_{i+2})$}};
%% hidden states
\node [pnode,anchor=north] (hidden) at ([xshift=\nodesep,yshift=-1.2*\noderowsep]softmax2.south east) {$\mathbf{h} = w_i \mathbf{C}$};
%% input
\node [anchor=north] (input) at ([yshift=-0.8*\noderowsep]hidden.south) {$w_i$};
%% connections
\draw [->,thick] ([yshift=1pt]softmax1.north) -- ([yshift=-1pt]output1.south);
\draw [->,thick] ([yshift=1pt]softmax2.north) -- ([yshift=-1pt]output2.south);
\draw [->,thick] ([yshift=1pt]softmax4.north) -- ([yshift=-1pt]output4.south);
\draw [->,thick] ([yshift=1pt]softmax5.north) -- ([yshift=-1pt]output5.south);
\draw [->,thick] ([xshift=1em,yshift=1pt]hidden.north west) .. controls +(north:2em) and +(south:2.5em) .. ([yshift=-1pt]softmax1.south);
\draw [->,thick] ([xshift=-1em,yshift=1pt]hidden.north) .. controls +(north:2.5em) and +(south:1.5em) .. ([yshift=-1pt]softmax2.south);
\draw [->,thick] ([xshift=1em,yshift=1pt]hidden.north) .. controls +(north:2.5em) and +(south:1.5em) .. ([yshift=-1pt]softmax4.south);
\draw [->,thick] ([xshift=-1em,yshift=1pt]hidden.north east) .. controls +(north:2em) and +(south:2.5em) .. ([yshift=-1pt]softmax5.south);
\draw [->,thick] ([yshift=1pt]input.north) -- ([yshift=-1pt]hidden.south);
%% label
\node [anchor=north] (labelb) at ([xshift=0,yshift=-0.5em]input.south) {(b) Skip-gram};
\node [anchor=east] (embeddinglabel) at (hidden.west) {\textbf{Embedding}};
\end{scope}
\end{tikzpicture}
\endgroup
\begin{center}
\begin{tikzpicture}
%----------------------------------------------------------------------------------------
% variables
\def\blockw{0.49\textwidth}
\def\blockh{0.35\textwidth}
\def\itemrowsep{0}
\def\xlen{0}
\def\textw{10em}
\tikzstyle{wnode} = [minimum width=1.5em,minimum height=1.5em,very thick,rounded corners=3pt]
%----------------------------------------------------------------------------------------
% block of classification
\begin{scope}
\node [anchor=north west] (label-classifcation-null) at (0,0) {};
\node [anchor=center] (label-classifcation) at (0.5*\blockw,-0.9*\blockh) {(a) Classification};
\node [anchor=center,minimum width=11em,minimum height=3em,draw=blue,very thick] (classifier) at (0.5*\blockw,-0.5*\blockh+0.7em) {Classifier $f(\mathbf{x})$};
\node [anchor=north,minimum width=11em,minimum height=1.5em,draw=red,very thick,rounded corners=3pt] (sample) at ([yshift=-1.2em]classifier.south) {\small{Sample $\mathbf{x}$ (feature vector)}};
\node [anchor=south,minimum width=1em,minimum height=0.8em,fill=kellygreen] (bar1) at ([yshift=1.2em]classifier.north) {};
\node [anchor=south east,minimum width=1em,minimum height=1.5em,fill=kellygreen,draw=ugreen,thick] (bar2l) at ([xshift=-1pt]bar1.south west) {};
\node [anchor=south east,minimum width=1em,minimum height=0.6em,fill=kellygreen] (bar3l) at ([xshift=-1pt]bar2l.south west) {};
\node [anchor=south east,minimum width=1em,minimum height=0.3em,fill=kellygreen] (bar4l) at ([xshift=-1pt]bar3l.south west) {};
\node [anchor=south west,minimum width=1em,minimum height=1.1em,fill=kellygreen] (bar2r) at ([xshift=1pt]bar1.south east) {};
\node [anchor=south west,minimum width=1em,minimum height=0.7em,fill=kellygreen] (bar3r) at ([xshift=1pt]bar2r.south east) {};
\node [anchor=south west,minimum width=1em,minimum height=0.3em,fill=kellygreen] (bar4r) at ([xshift=1pt]bar3r.south east) {};
\node [anchor=south west] (classhat) at ([xshift=0.5em,yshift=0.8em]bar2l.north) {\small{class $\hat{c}$}};
\draw [<-] ([yshift=1pt]bar2l.north) .. controls +(north:0.8em) and +(south:0.7em) .. ([yshift=1pt]classhat.south);
\draw [->,very thick] ([yshift=1pt]sample.north) -- ([yshift=-1pt]classifier.south);
\draw [->,very thick] ([yshift=1pt]classifier.north) -- ([yshift=-1pt]bar1.south);
\end{scope}
%----------------------------------------------------------------------------------------
% block of sequence labeling
\begin{scope}[xshift=\blockw]
\node [anchor=north west] (label-sequence-labeling-null) at (0,0) {};
\node [anchor=center] (label-sequence-labeling) at (0.5*\blockw,-0.9*\blockh) {(b) Sequence Labeling};
\node [anchor=center,minimum width=15.5em,minimum height=3em,draw=blue,very thick,align=center] (sequence-labeler) at (0.5*\blockw,-0.5*\blockh+0.7em) {Sequence Labeler\\$f(\begin{bmatrix}\mathbf{x}_1&...&\mathbf{x}_n\end{bmatrix})$};
\node [anchor=north,wnode,draw=red] (input3) at ([yshift=-1.2em]sequence-labeler.south) {\footnotesize{$\mathbf{x}_3$}};
\node [anchor=north,wnode,draw=red] (input2) at ([yshift=-1.2em,xshift=-3em]sequence-labeler.south) {\footnotesize{$\mathbf{x}_2$}};
\node [anchor=north,wnode,draw=red] (input1) at ([yshift=-1.2em,xshift=-6em]sequence-labeler.south) {\footnotesize{$\mathbf{x}_1$}};
\node [anchor=north,wnode,draw=red] (input4) at ([yshift=-1.2em,xshift=3em]sequence-labeler.south) {\footnotesize{$\mathbf{x}_4$}};
\node [anchor=north,wnode,draw=red] (input5) at ([yshift=-1.2em,xshift=6em]sequence-labeler.south) {\footnotesize{$\mathbf{x}_5$}};
\draw [->,very thick] ([yshift=1pt]input1.north) -- ([xshift=-6em,yshift=-1pt]sequence-labeler.south);
\draw [->,very thick] ([yshift=1pt]input2.north) -- ([xshift=-3em,yshift=-1pt]sequence-labeler.south);
\draw [->,very thick] ([yshift=1pt]input3.north) -- ([xshift=-0em,yshift=-1pt]sequence-labeler.south);
\draw [->,very thick] ([yshift=1pt]input4.north) -- ([xshift=3em,yshift=-1pt]sequence-labeler.south);
\draw [->,very thick] ([yshift=1pt]input5.north) -- ([xshift=6em,yshift=-1pt]sequence-labeler.south);
\node [anchor=south,fill=orange,circle,minimum size=0.5em,inner sep=1pt] (p31) at ([yshift=1.5em]sequence-labeler.north) {};
\node [anchor=south,fill=cyan,circle,minimum size=0.5em,inner sep=1pt] (p32) at ([yshift=0.3em]p31.north) {};
\node [anchor=south,fill=purple,circle,minimum size=0.5em,inner sep=1pt] (p33) at ([yshift=0.3em]p32.north) {};
\node [draw,thin,inner sep=2pt,thick,rounded corners=2pt] [fit = (p31) (p33)] (p3label) {};
\node [anchor=south,fill=orange,circle,minimum size=0.5em,inner sep=1pt] (p21) at ([xshift=-3em,yshift=1.5em]sequence-labeler.north) {};
\node [anchor=south,fill=cyan,circle,minimum size=0.5em,inner sep=1pt] (p22) at ([yshift=0.3em]p21.north) {};
\node [anchor=south,fill= purple,circle,minimum size=0.5em,inner sep=1pt] (p23) at ([yshift=0.3em]p22.north) {};
\node [draw,thin,inner sep=2pt,thick,rounded corners=2pt] [fit = (p21) (p23)] (p2label) {};
\node [anchor=south,fill=orange,circle,minimum size=0.5em,inner sep=1pt] (p11) at ([xshift=-6em,yshift=1.5em]sequence-labeler.north) {};
\node [anchor=south,fill=cyan,circle,minimum size=0.5em,inner sep=1pt] (p12) at ([yshift=0.3em]p11.north) {};
\node [anchor=south,fill= purple,circle,minimum size=0.5em,inner sep=1pt] (p13) at ([yshift=0.3em]p12.north) {};
\node [draw,thin,inner sep=2pt,thick,rounded corners=2pt] [fit = (p11) (p13)] (p1label) {};
\node [anchor=south,fill=orange,circle,minimum size=0.5em,inner sep=1pt] (p41) at ([xshift=3em,yshift=1.5em]sequence-labeler.north) {};
\node [anchor=south,fill=cyan,circle,minimum size=0.5em,inner sep=1pt] (p42) at ([yshift=0.3em]p41.north) {};
\node [anchor=south,fill= purple,circle,minimum size=0.5em,inner sep=1pt] (p43) at ([yshift=0.3em]p42.north) {};
\node [draw,thin,inner sep=2pt,thick,rounded corners=2pt] [fit = (p41) (p43)] (p4label) {};
\node [anchor=south,fill=orange,circle,minimum size=0.5em,inner sep=1pt] (p51) at ([xshift=6em,yshift=1.5em]sequence-labeler.north) {};
\node [anchor=south,fill=cyan,circle,minimum size=0.5em,inner sep=1pt] (p52) at ([yshift=0.3em]p51.north) {};
\node [anchor=south,fill= purple,circle,minimum size=0.5em,inner sep=1pt] (p53) at ([yshift=0.3em]p52.north) {};
\node [draw,thin,inner sep=2pt,thick,rounded corners=2pt] [fit = (p51) (p53)] (p5label) {};
\draw [->,thick] ([yshift=1pt,xshift=-6em]sequence-labeler.north) -- ([yshift=-1pt]p1label.south);
\draw [->,thick] ([yshift=1pt,xshift=-3em]sequence-labeler.north) -- ([yshift=-1pt]p2label.south);
\draw [->,thick] ([yshift=1pt,xshift=-0em]sequence-labeler.north) -- ([yshift=-1pt]p3label.south);
\draw [->,thick] ([yshift=1pt,xshift=+3em]sequence-labeler.north) -- ([yshift=-1pt]p4label.south);
\draw [->,thick] ([yshift=1pt,xshift=+6em]sequence-labeler.north) -- ([yshift=-1pt]p5label.south);
\begin{pgfonlayer}{background}
\draw [->,very thick,dotted] ([xshift=-1em]p11.west) -- ([xshift=1em]p11.east) -- ([xshift=-1em]p22.west) -- ([xshift=1em]p22.east) -- ([xshift=-1em]p33.west) -- ([xshift=1em]p33.east) -- ([xshift=-1em]p41.west) -- ([xshift=1em]p41.east) -- ([xshift=-1em]p51.west) -- ([xshift=1.0em]p51.east);
\end{pgfonlayer}
\node [anchor=south] (prediction1) at ([yshift=0.0em]p1label.north) {\scriptsize{$\hat{l}_1=\textrm{\color{orange} B}$}};
\node [anchor=south] (prediction2) at ([yshift=0.0em]p2label.north) {\scriptsize{$\hat{l}_2=\textrm{\color{cyan} I}$}};
\node [anchor=south] (prediction3) at ([yshift=0.0em]p3label.north) {\scriptsize{$\hat{l}_3=\textrm{\color{purple} O}$}};
\node [anchor=south] (prediction4) at ([yshift=0.0em]p4label.north) {\scriptsize{$\hat{l}_4=\textrm{\color{orange} B}$}};
\node [anchor=south] (prediction5) at ([yshift=0.0em]p5label.north) {\scriptsize{$\hat{l}_5=\textrm{\color{orange} B}$}};
\end{scope}
%----------------------------------------------------------------------------------------
% block of language modeling
\begin{scope}[yshift=-\blockh]
\node [anchor=north west] (label-language-modeling-null) at (0,0) {};
\node [anchor=center] (label-language-modeling) at (0.5*\blockw,-0.9*\blockh) {(c) Language Modeling};
\node [anchor=center,minimum width=15.5em,minimum height=3em,draw=blue,very thick,align=center] (language-model) at (0.5*\blockw,-0.5*\blockh+0.7em) {Language Model\\$\Pr(w_i|w_1,...,w_{i-1})$};
\node [anchor=north,wnode,draw=red] (input3) at ([yshift=-1.2em]language-model.south) {\footnotesize{$w_3$}};
\node [anchor=north,wnode,draw=red] (input2) at ([yshift=-1.2em,xshift=-3em]language-model.south) {\footnotesize{$w_2$}};
\node [anchor=north,wnode,draw=red] (input1) at ([yshift=-1.2em,xshift=-6em]language-model.south) {\footnotesize{$w_1$}};
\node [anchor=north,wnode,draw=red] (input4) at ([yshift=-1.2em,xshift=+3em]language-model.south) {\footnotesize{$w_4$}};
\draw [->,very thick] ([yshift=1pt]input1.north) -- ([xshift=-6em,yshift=-1pt]language-model.south);
\draw [->,very thick] ([yshift=1pt]input2.north) -- ([xshift=-3em,yshift=-1pt]language-model.south);
\draw [->,very thick] ([yshift=1pt]input3.north) -- ([xshift=-0em,yshift=-1pt]language-model.south);
\draw [->,very thick] ([yshift=1pt]input4.north) -- ([xshift=3em,yshift=-1pt]language-model.south);
\node [anchor=south,minimum width=1em,minimum height=0.5em,fill=kellygreen] (bar1) at ([xshift=6em,yshift=1.2em]language-model.north) {};
\node [anchor=south east,minimum width=1em,minimum height=0.4em,fill=kellygreen,inner sep=0pt] (bar2) at ([xshift=-1pt]bar1.south west) {};
\node [anchor=south east,minimum width=1em,minimum height=0.8em,fill=kellygreen,draw=ugreen,thick] (bar3) at ([xshift=-1pt]bar2.south west) {};
\node [anchor=south east,minimum width=1em,minimum height=0.7em,fill=kellygreen] (bar4) at ([xshift=-1pt]bar3.south west) {};
\node [anchor=south east,minimum width=1em,minimum height=0.3em,fill=kellygreen,inner sep=0pt] (bar5) at ([xshift=-1pt]bar4.south west) {};
\draw [->,very thick] ([xshift=6em,yshift=1pt]language-model.north) -- ([yshift=-1pt]bar1.south);
\node [anchor=south,wnode,draw=red] (predictionlabel) at ([xshift=-0em,yshift=1.3em]bar1.north) {\small{$w_5$}};
\draw [<-] ([yshift=1pt]bar3.north) .. controls +(north:1.0em) and +(south:0.5em) .. ([yshift=-1pt]predictionlabel.south);
\end{scope}
%----------------------------------------------------------------------------------------
% block of sequence generation
\begin{scope}[yshift=-\blockh,xshift=\blockw]
\node [anchor=north west] (label-sequence-generation-null) at (0,0) {};
\node [anchor=center] (label-sequence-generation) at (0.5*\blockw,-0.9*\blockh) {(d) Sequence Generation (seq2seq)};
\node [anchor=east,minimum width=7.2em,minimum height=3em,draw=blue,very thick,align=center] (encoder) at (0.5*\blockw-0.6em,-0.5*\blockh+0.7em) {Encoder\\$\mathrm{Enc}(\mathbf{s})$};
\node [anchor=west,minimum width=7.2em,minimum height=3em,draw=blue,very thick,align=center] (decoder) at (0.5*\blockw+0.6em,-0.5*\blockh+0.7em) {Decoder\\\scriptsize{$\mathrm{Dec}(\mathrm{Enc},t_1...t_{i-1})$}};
\node [anchor=north,wnode,draw=red] (source2) at ([yshift=-1.2em]encoder.south) {\footnotesize{$s_2$}};
\node [anchor=north,wnode,draw=red] (source1) at ([yshift=-1.2em,xshift=-2.5em]encoder.south) {\footnotesize{$s_1$}};
\node [anchor=north,wnode,draw=red] (source3) at ([yshift=-1.2em,xshift=+2.5em]encoder.south) {\footnotesize{$s_3$}};
\draw [->,very thick] ([yshift=1pt]source1.north) -- ([xshift=-2.5em,yshift=-1pt]encoder.south);
\draw [->,very thick] ([yshift=1pt]source2.north) -- ([xshift=+0.0em,yshift=-1pt]encoder.south);
\draw [->,very thick] ([yshift=1pt]source3.north) -- ([xshift=+2.5em,yshift=-1pt]encoder.south);
\draw [->,very thick] ([yshift=1pt]encoder.north) -- ([yshift=0.8em]encoder.north) -- ([yshift=0.8em,xshift=0.6em]encoder.north east) -- ([yshift=0em,xshift=0.6em]encoder.east) -- ([yshift=0em,xshift=-1pt]decoder.west);
\node [anchor=north,wnode,draw=red] (target2) at ([yshift=-1.2em]decoder.south) {\footnotesize{$t_2$}};
\node [anchor=north,wnode,draw=red] (target1) at ([yshift=-1.2em,xshift=-2.5em]decoder.south) {\footnotesize{$t_1$}};
\draw [->,very thick] ([yshift=1pt]target1.north) -- ([xshift=-2.5em,yshift=-1pt]decoder.south);
\draw [->,very thick] ([yshift=1pt]target2.north) -- ([xshift=+0.0em,yshift=-1pt]decoder.south);
\node [anchor=south,minimum width=1em,minimum height=0.2em,fill=kellygreen,inner sep=0pt] (bar1) at ([xshift=2.5em,yshift=1.2em]decoder.north) {};
\node [anchor=south east,minimum width=1em,minimum height=0.4em,fill=kellygreen] (bar2) at ([xshift=-1pt]bar1.south west) {};
\node [anchor=south east,minimum width=1em,minimum height=0.8em,fill=kellygreen,draw=ugreen,thick] (bar3) at ([xshift=-1pt]bar2.south west) {};
\node [anchor=south east,minimum width=1em,minimum height=0.2em,fill=kellygreen,inner sep=0pt] (bar4) at ([xshift=-1pt]bar3.south west) {};
\node [anchor=south east,minimum width=1em,minimum height=0.7em,fill=kellygreen,inner sep=0pt] (bar5) at ([xshift=-1pt]bar4.south west) {};
\draw [->,very thick] ([xshift=2.5em,yshift=1pt]decoder.north) -- ([yshift=-1pt]bar1.south);
\node [anchor=south,wnode,draw=red] (predictionlabel) at ([xshift=-0em,yshift=1.7em]bar1.north) {\small{$t_3$}};
\draw [<-] ([yshift=1pt]bar3.north) .. controls +(north:1.0em) and +(south:0.5em) .. ([yshift=-1pt]predictionlabel.south);
\end{scope}
%----------------------------------------------------------------------------------------
% block of tree generation
\begin{scope}[yshift=-2*\blockh]
\node [anchor=north west] (label-tree-generation-null) at (0,0) {};
\node [anchor=center] (label-tree-generation) at (0.5*\blockw,-0.9*\blockh) {(e) Tree Generation (seq2tree)};
\node [anchor=east,minimum width=7.2em,minimum height=3em,draw=blue,very thick,align=center] (parser) at (0.5*\blockw-0.6em,-0.5*\blockh+0.7em) {Parser\\$\Pr(d|\mathbf{s})$};
\draw [->,very thick] ([yshift=1pt]parser.north) -- ([yshift=0.8em]parser.north) -- ([yshift=0.8em,xshift=0.6em]parser.north east) -- ([yshift=0em,xshift=0.6em]parser.east) -- ([yshift=0em,xshift=1.5em]parser.east);
\node [anchor=north,wnode,draw=red] (source2) at ([yshift=-1.2em]parser.south) {\footnotesize{$s_2$}};
\node [anchor=north,wnode,draw=red] (source1) at ([yshift=-1.2em,xshift=-2.5em] parser.south) {\footnotesize{$s_1$}};
\node [anchor=north,wnode,draw=red] (source3) at ([yshift=-1.2em,xshift=+2.5em] parser.south) {\footnotesize{$s_3$}};
\draw [->,very thick] ([yshift=1pt]source1.north) -- ([xshift=-2.5em,yshift=-1pt] parser.south);
\draw [->,very thick] ([yshift=1pt]source2.north) -- ([xshift=+0.0em,yshift=-1pt] parser.south);
\draw [->,very thick] ([yshift=1pt]source3.north) -- ([xshift=+2.5em,yshift=-1pt] parser.south);
\node [anchor=south west] (word1) at ([xshift=2em,yshift=-2em]parser.south east) {\scriptsize{$s_1$}};
\node [anchor=south west] (word2) at ([xshift=1em]word1.south east) {\scriptsize{$s_2$}};
\node [anchor=south west] (word3) at ([xshift=1em]word2.south east) {\scriptsize{$s_3$}};
\node [anchor=south,fill=orange!30,rounded corners=2pt] (POS1) at ([yshift=0.8em]word1.north) {\scriptsize{NNS}};
\node [anchor=south,fill=orange!30,rounded corners=2pt] (POS2) at ([yshift=0.8em]word2.north) {\scriptsize{VBD}};
\node [anchor=south,fill=orange!30,rounded corners=2pt] (POS3) at ([yshift=0.8em]word3.north) {\scriptsize{NN}};
\node [anchor=south,fill=orange!30,rounded corners=2pt] (treenode1) at ([yshift=0.8em]POS1.north) {\scriptsize{NP}};
\node [anchor=south,fill=orange!30,rounded corners=2pt] (treenode2) at ([yshift=0.8em]POS2.north east) {\scriptsize{VP}};
\node [anchor=south,fill=orange!30,rounded corners=2pt] (treenode3) at ([yshift=0.8em,xshift=-1em]treenode2.north west) {\scriptsize{S}};
\draw [-] ([yshift=1pt]word1.north) -- ([yshift=-1pt]POS1.south);
\draw [-] ([yshift=1pt]word2.north) -- ([yshift=-1pt]POS2.south);
\draw [-] ([yshift=1pt]word3.north) -- ([yshift=-1pt]POS3.south);
\draw [-] ([yshift=1pt]POS1.north) -- ([yshift=-1pt]treenode1.south);
\draw [-] ([yshift=1pt]POS2.north) -- ([xshift=-0.3em,yshift=-1pt]treenode2.south);
\draw [-] ([yshift=1pt]POS3.north) -- ([xshift=+0.3em,yshift=-1pt]treenode2.south);
\draw [-] ([yshift=1pt]treenode1.north) -- ([xshift=-0.3em,yshift=-1pt]treenode3.south);
\draw [-] ([yshift=1pt]treenode2.north) -- ([xshift=+0.3em,yshift=-1pt]treenode3.south);
\end{scope}
%----------------------------------------------------------------------------------------
% block of relevance modeling
\begin{scope}[yshift=-2*\blockh,xshift=\blockw]
\node [anchor=north west] (label-relevance-modeling-null) at (0,0) {};
\node [anchor=center] (label-relevance-modeling) at (0.5*\blockw,-0.9*\blockh) {(f) Relevance Modeling};
\node [anchor=east,minimum width=6em,minimum height=1.5em,draw=blue,very thick,align=center] (queryfunction) at (0.5*\blockw-0.6em,-0.5*\blockh-0.3em) {\footnotesize{$\mathbf{q} = Q(query)$}};
\node [anchor=west,minimum width=6em,minimum height=1.5em,draw=blue,very thick,align=center] (keyfunction) at (0.5*\blockw+0.6em,-0.5*\blockh-0.3em) {\footnotesize{$\mathbf{k} = K(key)$}};
\node [anchor=north,wnode,minimum width=4em,draw=red] (query) at ([yshift=-1.2em]queryfunction.south) {\footnotesize{$query$}};
\node [anchor=north,wnode,minimum width=4em,draw=red] (key) at ([yshift=-1.2em]keyfunction.south) {\footnotesize{$key$}};
\node [anchor=south west, minimum width=13.3em,minimum height=1.5em,draw=blue,very thick,align=center] (simfunction) at ([yshift=1.2em]queryfunction.north west) {\small{Similarity Func. $f(\mathbf{q},\mathbf{k})$} };
\draw [->,very thick] ([yshift=1pt]query.north) -- ([yshift=-1pt]queryfunction.south);
\draw [->,very thick] ([yshift=1pt]key.north) -- ([yshift=-1pt]keyfunction.south);
\draw [->,very thick] ([yshift=1pt]queryfunction.north) -- ([yshift=1.2em-1pt]queryfunction.north);
\draw [->,very thick] ([yshift=1pt]keyfunction.north) -- ([yshift=1.2em-1pt]keyfunction.north);
\node [fill=kellygreen,minimum width=3em] (simresult) at ([yshift=1.6em]simfunction.north) {{\color{white} 0.82}};
\draw [->,very thick] ([yshift=1pt]simfunction.north) -- ([yshift=-1pt]simresult.south);
\end{scope}
%----------------------------------------------------------------------------------------
% block of alignment
\begin{scope}[yshift=-3*\blockh]
\node [anchor=north west] (label-alignment-null) at (0,0) {};
\node [anchor=center] (label-alignment) at (0.5*\blockw,-0.9*\blockh) {(g) Linguistic Alignment};
\node [anchor=east,minimum width=7.2em,minimum height=3em,draw=blue,very thick,align=center] (aligner) at (0.5*\blockw-0.6em,-0.5*\blockh+0.7em) {Aligner\\$a(\mathbf{s},\mathbf{t})$};
\node [anchor=north,wnode,draw=red] (source2) at ([yshift=-1.2em]aligner.south) {\footnotesize{$s_2$}};
\node [anchor=north,wnode,draw=red] (source1) at ([yshift=-1.2em,xshift=-2.5em] aligner.south) {\footnotesize{$s_1$}};
\node [anchor=north,wnode,draw=red] (source3) at ([yshift=-1.2em,xshift=+2.5em] aligner.south) {\footnotesize{$s_3$}};
\draw [->,very thick] ([yshift=1pt]source1.north) -- ([xshift=-2.5em,yshift=-1pt] aligner.south);
\draw [->,very thick] ([yshift=1pt]source2.north) -- ([xshift=+0.0em,yshift=-1pt] aligner.south);
\draw [->,very thick] ([yshift=1pt]source3.north) -- ([xshift=+2.5em,yshift=-1pt] aligner.south);
\node [anchor=south,wnode,draw=red] (target2) at ([yshift=1.2em]aligner.north) {\footnotesize{$t_2$}};
\node [anchor=south,wnode,draw=red] (target1) at ([yshift=1.2em,xshift=-2.5em] aligner.north) {\footnotesize{$t_1$}};
\node [anchor=south,wnode,draw=red] (target3) at ([yshift=1.2em,xshift=+2.5em] aligner.north) {\footnotesize{$t_3$}};
\draw [->,very thick] ([yshift=-1pt]target1.south) -- ([xshift=-2.5em,yshift=1pt] aligner.north);
\draw [->,very thick] ([yshift=-1pt]target2.south) -- ([xshift=+0.0em,yshift=1pt] aligner.north);
\draw [->,very thick] ([yshift=-1pt]target3.south) -- ([xshift=+2.5em,yshift=1pt] aligner.north);
\draw [->,very thick] ([xshift=1pt]aligner.east) -- ([xshift=1.6em]aligner.east);
\node [anchor=south west,fill=kellygreen,inner sep=0pt,minimum size=0.7em] (node11) at ([xshift=2.2em]aligner.south east) {};
\node [anchor=center,fill=kellygreen,inner sep=0pt,minimum size=0.7em] (node12) at ([xshift=1.5em]node11.center) {\tiny{\color{white} .7}};
\node [anchor=center,fill=kellygreen,inner sep=0pt,minimum size=0.4em] (node13) at ([xshift=1.5em]node12.center) {};
\node [anchor=center,fill=kellygreen,inner sep=0pt,minimum size=1.0em] (node21) at ([yshift=1.5em]node11.center) {\tiny{\color{white} 1}};
\node [anchor=center,fill=kellygreen,inner sep=0pt,minimum size=0.4em] (node22) at ([xshift=1.5em]node21.center) {};
\node [anchor=center,fill=kellygreen,inner sep=0pt,minimum size=0.6em] (node23) at ([xshift=1.5em]node22.center) {\tiny{\color{white} .6}};
\node [anchor=center,fill=kellygreen,inner sep=0pt,minimum size=0.3em] (node31) at ([yshift=1.5em]node21.center) {};
\node [anchor=center,fill=kellygreen,inner sep=0pt,minimum size=0.5em] (node32) at ([xshift=1.5em]node31.center) {};
\node [anchor=center,fill=kellygreen,inner sep=0pt,minimum size=0.8em] (node33) at ([xshift=1.5em]node32.center) {\tiny{\color{white} .8}};
\node [anchor=south] (tm1) at ([yshift=0.6em]node31.center) {\scriptsize{$t_1$}};
\node [anchor=south] (tm2) at ([yshift=0.6em]node32.center) {\scriptsize{$t_2$}};
\node [anchor=south] (tm3) at ([yshift=0.6em]node33.center) {\scriptsize{$t_3$}};
\node [anchor=west] (sm1) at ([xshift=0.6em]node33.center) {\scriptsize{$s_1$}};
\node [anchor=west] (sm2) at ([xshift=0.6em]node23.center) {\scriptsize{$s_2$}};
\node [anchor=west] (sm3) at ([xshift=0.6em]node13.center) {\scriptsize{$s_3$}};
\end{scope}
%----------------------------------------------------------------------------------------
% block of extraction
\begin{scope}[yshift=-3*\blockh,xshift=\blockw]
\node [anchor=north west] (label-extraction-null) at (0,0) {};
\node [anchor=center] (label-extraction) at (0.5*\blockw,-0.9*\blockh) {(h) Information Extraction};
\node [anchor=center,minimum width=15.5em,minimum height=3em,draw=blue,very thick,align=center] (extractor) at (0.5*\blockw,-0.5*\blockh+0.7em) {Extractor\\\scriptsize{Extracte everything needed, e.g., a segment}};
\node [anchor=north,wnode,draw=red] (input3) at ([yshift=-1.2em]extractor.south) {\footnotesize{$s_3$}};
\node [anchor=north,wnode,draw=red] (input2) at ([yshift=-1.2em,xshift=-3em] extractor.south) {\footnotesize{$s_2$}};
\node [anchor=north,wnode,draw=red] (input1) at ([yshift=-1.2em,xshift=-6em] extractor.south) {\footnotesize{$s_1$}};
\node [anchor=north,wnode,draw=red] (input4) at ([yshift=-1.2em,xshift=3em] extractor.south) {\footnotesize{$s_4$}};
\node [anchor=north,wnode,draw=red] (input5) at ([yshift=-1.2em,xshift=6em] extractor.south) {\footnotesize{$s_5$}};
\draw [->,very thick] ([yshift=1pt]input1.north) -- ([xshift=-6em,yshift=-1pt] extractor.south);
\draw [->,very thick] ([yshift=1pt]input2.north) -- ([xshift=-3em,yshift=-1pt] extractor.south);
\draw [->,very thick] ([yshift=1pt]input3.north) -- ([xshift=-0em,yshift=-1pt] extractor.south);
\draw [->,very thick] ([yshift=1pt]input4.north) -- ([xshift=3em,yshift=-1pt] extractor.south);
\draw [->,very thick] ([yshift=1pt]input5.north) -- ([xshift=6em,yshift=-1pt] extractor.south);
\node [anchor=south,fill=kellygreen] (spanlabel) at ([yshift=1.2em]extractor.north) {\small{\color{white} $span=[2,4]$}};
\draw [->,very thick] ([yshift=1pt]extractor.north) -- ([yshift=-1pt]spanlabel.south);
\node [draw,dotted,inner sep=3pt,very thick,ugreen,rounded corners=2pt] [fit = (input2) (input4)] (extractedspan) {};
\end{scope}
\end{tikzpicture}
\end{center}
%\definecolor{ublue}{rgb}{0.152,0.250,0.545}
\begin{tikzpicture}
\begin{scope}
\begin{axis}[
width=6.5cm, height=5cm,
%symbolic x coords={1-15,16-25,26-35,>35},
ytick={0,4,8,...,20},
xtick={200,400,...,1200},
xlabel={Data size},
ylabel={Vocabulary size},
xlabel style={align=center},
ylabel style={yshift=-1.2em},
%y tick style={opacity=0},
x tick label style={font=\footnotesize},
y tick label style={font=\footnotesize},
tick align=inside,
%major grid style={draw=blue,dashed},
legend pos=outer north east,
%legend style={anchor=north west,yshift=-1cm},
%legend style={yshift=-4.5em,xshift=-6em,legend cell align=left,legend plot pos=right},
xmin=-20,
xmax=1220,
ymin=-2,
ymax=22]
%\addplot [sharp plot,very thick,ublue,mark=o] coordinates{(4.72,1.48) (9.45, 2.37) (14.18, 3.09) (18.91, 3.74) (23.64, 4.32) (28.37, 4.85) (33.09, 5.35) (37.82, 5.82) (42.55, 6.27) (47.28, 6.7) (51.58, 6.7) (55.88, 7.1) (60.17, 7.49) (64.47, 7.87) (68.77, 8.24) (73.07, 8.59) (77.37, 8.93) (81.67, 9.26) (85.97, 9.3)};
%\addplot [sharp plot,thick,blue,mark=square] coordinates{(6,1.69) (12, 2.64) (18, 3.40) (24, 4.04) (30, 4.6) (36, 5.1) (42, 5.55) (48, 5.96) (54, 6.34) (60, 6.7) (66, 7.0) (72, 7.34) (78, 7.62) (84, 7.88) (90, 8.12) (96, 8.35) (102, 8.56) (108, 8.75) (114, 8.94) (120, 9.12)};
\addplot [sharp plot,thick,blue,mark=square] coordinates{(0.1,0.04) (0.25, 0.09) (0.76, 0.21) (1.7, 0.36) (3.59, 0.58) (9.28, 1.07) (18.64, 1.74) (37.46, 2.83) (56.47, 3.73) (75.97, 4.5) (95.78, 5.19) (198.93, 8) (417.56, 12.03) (1102.31, 19.9)};
%\addplot [sharp plot,very thick,orange,mark=*] coordinates{(1-15,17.5) (16-25,24) (26-35,25) (>35,27)};
%\legend{\tiny{NMT},\tiny{SPB},\tiny{HPB},\tiny{PBSY}}
\end{axis}
\node[anchor=east] (ny) at (1.2em,9.8em){\footnotesize($\cdot 10^4$)};
\node[anchor=west] (nx) at (12.8em,0.4em){\footnotesize($\cdot 10^5$)};
\end{scope}
\begin{scope}[xshift=2.85in]
\begin{axis}[
width=6.5cm, height=5cm,
title={},
xlabel={Vocabulary size},
ylabel={OOV percentage},
ylabel style={yshift=-1.2em},
xmin=-1, xmax=21,
% ymin=0, ymax=10e-2,
ymin=0, ymax=100,
xtick={0,4,...,20},
% ytick={0,1e-2,2e-2,3e-2,4e-2,5e-2,6e-2,7e-2,8e-2,9e-2,10e-2},
ytick={20,40,...,100},
]
\addplot[
color=blue,
mark=square,
thick,
]
% coordinates {(27248,0.0867) (69248,0.0383) (100195,0.0268) (144777,0.0184) (180743,0.0148)};
% coordinates {(0.27,8.67) (0.38,6.27) (0.52,4.97) (0.69,3.83) (1.0,2.68) (1.44,1.84) (1.8,1.48)};
% coordinates {(0.27,60.1) (0.38,51.63) (0.52,41.22) (0.69,33.97) (1.0,28.64) (1.44,23.19) (1.8,1.48)};
coordinates {(0.04, 60.1) (0.09, 51.63) (0.21, 41.22) (0.36, 33.97) (0.58, 28.64) (1.07, 23.19) (1.74, 19.97) (2.83, 17.73) (3.73, 16.94) (4.5, 16.52) (5.19, 16.26) (8, 15.71) (12.03, 15.44) (19.9, 15.27)};
\end{axis}
\node[anchor=east] (ny) at (1.2em,9.8em){\footnotesize($\%$)};
\node[anchor=west] (nx) at (12.8em,0.4em){\footnotesize($\cdot 10^4$)};
\end{scope}
\end{tikzpicture}
%---------------------------------------------------------------------
\ No newline at end of file
\input{figures/figure-theme.tex}
\begin{tikzpicture}[aml figure,scale=1.0]
\amlfigurepalette
\tikzstyle{circlenode1}=[circle,minimum size=3pt, fill=blue, inner sep=0]
\tikzstyle{circlenode2}=[circle,minimum size=3pt, fill=red, inner sep=0]
\tikzstyle{circlenode3}=[circle,minimum size=2pt, fill=black, inner sep=0,black]
\begin{axis}[aml axis,ymin=0,ymax=14, ytick={0, 2,4,6,8,10,12,14},
xmin=0,xmax=12, xtick={0, 2,4,6,8,10,12},
scatter/classes={%
a={mark=*,draw=FigureInk,fill=FigureInk}}]
\addplot[scatter,only marks,%
scatter src=explicit symbolic]%
table[meta=label] {
x y label
0.6 3.3 a
2 2.5 a
4.3 12 a
4.7 3.3 a
4.5 1 a
5 6.8 a
6 10 a
8 1 a
3.6 13.5 a
1.5 8 a
7.5 9.8 a
};
% \addplot[domain=1.8:2,blue] {x*x};
% \addplot[domain=2:2.2,blue] {x*x+1};
% \draw[dotted] (axis cs:2,0) -- (axis cs:2,5);
% \addplot[holdot] coordinates{(2,4)};
% \addplot[soldot] coordinates{(2,5)};
% \draw[dotted] (axis cs:0,4.5) -- (axis cs:2,4.5);
% \draw[->] (axis cs:1.9, 1.9*1.9+0.1) -- (axis cs:1.95, 1.95*1.95+0.1);
\draw[->, thick] (axis cs:0, 0) -- (axis cs:5.5, 12.65) ;
\node[anchor=north west, font=\footnotesize] at (axis cs:6, 13) {Direction 1};
\node[circlenode3] (node3) at (axis cs:4.7, 5.7){};
\node[,anchor=north east,font=\footnotesize] at (axis cs:4.9, 5.7){Proj.};
\node[] (node1)at ($(axis cs:1.3, 0)!(axis cs:4.7, 5.7)!(axis cs:6.3, 11.5)$) {};
\draw[->,thick] (node3) -- (node1);
\draw[red,dotted] (axis cs:5, 6.8) -- ($(axis cs:0, 0)!(axis cs:5, 6.8)!(axis cs:5, 11.5)$);
\node[circlenode2] at ($(axis cs:0, 0)!(axis cs:5, 6.8)!(axis cs:5, 11.5)$){};
\draw[red,dotted] (axis cs:6, 10) -- ($(axis cs:0, 0)!(axis cs:6, 10)!(axis cs:5, 11.5)$);
\node[circlenode2] at ($(axis cs:0, 0)!(axis cs:6, 10)!(axis cs:5, 11.5)$){};
\draw[red,dotted] (axis cs:7.5, 9.8) -- ($(axis cs:0, 0)!(axis cs:7.5, 9.8)!(axis cs:5, 11.5)$);
\node[circlenode2] at ($(axis cs:0, 0)!(axis cs:7.5, 9.8)!(axis cs:5, 11.5)$){};
\draw[red,dotted] (axis cs:8, 1) -- ($(axis cs:0, 0)!(axis cs:8, 1)!(axis cs:5, 11.5)$);
\node[circlenode2] at ($(axis cs:0, 0)!(axis cs:8, 1)!(axis cs:5, 11.5)$){};
\draw[red,dotted] (axis cs:4.5, 1) -- ($(axis cs:0, 0)!(axis cs:4.5, 1)!(axis cs:5, 11.5)$);
\node[circlenode2] at ($(axis cs:0, 0)!(axis cs:4.5, 1)!(axis cs:5, 11.5)$){};
\draw[red,dotted] (axis cs:0.6, 3.3 ) -- ($(axis cs:0, 0)!(axis cs:0.6, 3.3)!(axis cs:5, 11.5)$);
\node[circlenode2] at ($(axis cs:0, 0)!(axis cs:0.6, 3.3)!(axis cs:5, 11.5)$){};
\draw[red,dotted] (axis cs:4.7, 3.3 ) -- ($(axis cs:0, 0)!(axis cs:4.7, 3.3)!(axis cs:5, 11.5)$);
\node[circlenode2] at ($(axis cs:0, 0)!(axis cs:4.7, 3.3)!(axis cs:5, 11.5)$){};
\draw[red,dotted] (axis cs:4.3, 12 ) -- ($(axis cs:0, 0)!(axis cs:4.3, 12)!(axis cs:5, 11.5)$);
\node[circlenode2] at ($(axis cs:0, 0)!(axis cs:4.3, 12)!(axis cs:5, 11.5)$){};
\draw[red,dotted] (axis cs:3.6, 13.5 ) -- ($(axis cs:0, 0)!(axis cs:3.6, 13.5)!(axis cs:5, 11.5)$);
\node[circlenode2] at ($(axis cs:0, 0)!(axis cs:3.6, 13.5)!(axis cs:5, 11.5)$){};
\draw[red,dotted] (axis cs:1.5, 8 ) -- ($(axis cs:0, 0)!(axis cs:1.5, 8)!(axis cs:5, 11.5)$);
\node[circlenode2] at ($(axis cs:0, 0)!(axis cs:1.5, 8)!(axis cs:5, 11.5)$){};
\draw[red,dotted] (axis cs:2,2.5 ) -- ($(axis cs:0, 0)!(axis cs:2,2.5)!(axis cs:5, 11.5)$);
\node[circlenode2] at ($(axis cs:0, 0)!(axis cs:2,2.5)!(axis cs:5, 11.5)$){};
\draw[->, thick] (axis cs:0, 0) -- (axis cs:11, 6) ;
\draw[blue,dotted] (axis cs:2, 2.5) -- ($(axis cs:0, 0)!(axis cs:2, 2.5)!(axis cs:11,6)$);
\node[circlenode1] at ($(axis cs:0, 0)!(axis cs:2, 2.5)!(axis cs:11, 6)$){};
\node[] (node2)at ($(axis cs:-3.5, 0)!(axis cs:4.7, 5.7)!(axis cs:7.5,6)$) {};
\draw[->,thick] (node3) -- (node2);
%\draw[blue,dotted] (axis cs:3.6, 5.6) -- ($(axis cs:0, 0)!(axis cs:3.6, 5.6)!(axis cs:11, 6)$);
%\node[circlenode1] at ($(axis cs:0, 0)!(axis cs:3.6, 5.6)!(axis cs:11, 6)$){};
\draw[blue,dotted] (axis cs:5, 6.8) -- ($(axis cs:0, 0)!(axis cs:5, 6.8)!(axis cs:11, 6)$);
\node[circlenode1] at ($(axis cs:0, 0)!(axis cs:5, 6.8)!(axis cs:11, 6)$){};
\draw[blue,dotted] (axis cs:6, 10) -- ($(axis cs:0, 0)!(axis cs:6, 10)!(axis cs:11, 6)$);
\node[circlenode1] at ($(axis cs:0, 0)!(axis cs:6, 10)!(axis cs:11, 6)$){};
\draw[blue,dotted] (axis cs:7.5, 9.8) -- ($(axis cs:0, 0)!(axis cs:7.5, 9.8)!(axis cs:11, 6)$);
\node[circlenode1] at ($(axis cs:0, 0)!(axis cs:7.5, 9.8)!(axis cs:11, 6)$){};
\draw[blue,dotted] (axis cs:4.5, 1) -- ($(axis cs:0, 0)!(axis cs:4.5, 1)!(axis cs:11, 6)$);
\node[circlenode1] at ($(axis cs:0, 0)!(axis cs:4.5, 1)!(axis cs:11, 6)$){};
\draw[blue,dotted] (axis cs:0.6, 3.3 ) -- ($(axis cs:0, 0)!(axis cs:0.6, 3.3)!(axis cs:11, 6)$);
\node[circlenode1] at ($(axis cs:0, 0)!(axis cs:0.6, 3.3)!(axis cs:11, 6)$){};
\draw[blue,dotted] (axis cs:8, 1 ) -- ($(axis cs:0, 0)!(axis cs:8, 1)!(axis cs:11, 6)$);
\node[circlenode1] at ($(axis cs:0, 0)!(axis cs:8, 1)!(axis cs:11, 6)$){};
\draw[blue,dotted] (axis cs:4.7, 3.3 ) -- ($(axis cs:0, 0)!(axis cs:4.7, 3.3)!(axis cs:11, 6)$);
\node[circlenode1] at ($(axis cs:0, 0)!(axis cs:4.7, 3.3)!(axis cs:11, 6)$){};
\draw[blue,dotted] (axis cs:4.3, 12 ) -- ($(axis cs:0, 0)!(axis cs:4.3, 12)!(axis cs:11, 6)$);
\node[circlenode1] at ($(axis cs:0, 0)!(axis cs:4.3, 12)!(axis cs:11, 6)$){};
\draw[blue,dotted] (axis cs:3.6, 13.5 ) -- ($(axis cs:0, 0)!(axis cs:3.6, 13.5)!(axis cs:11, 6)$);
\node[circlenode1] at ($(axis cs:0, 0)!(axis cs:3.6, 13.5)!(axis cs:11, 6)$){};
\draw[blue,dotted] (axis cs:1.5,8 ) -- ($(axis cs:0, 0)!(axis cs:1.5,8)!(axis cs:11, 6)$);
\node[circlenode1] at ($(axis cs:0, 0)!(axis cs:1.5,8)!(axis cs:11, 6)$){};
\node[anchor=north east,font=\footnotesize] at (axis cs:11.5,7.5) {Direction 2};
% \node[anchor=east] (source) at (axis cs:2.1,5){\text\ F(x)};
% \node[anchor=east] (source) at (axis cs:1.95,3.5){\text\ F(x)};
% \node[anchor=north](source) at (axis cs:1.8,4.6){$\omega$};
% \node[anchor=south](source) at (axis cs:2,3.2){$X^{+/-}(\omega)$};
\end{axis}
\end{tikzpicture}
\begin{center}
\begin{tikzpicture}
%----------------------------------------------------------------------------------------
% variables
\def\nodesize{1.2em}
\tikzstyle{nnode} = [circle,minimum size=\nodesize,inner sep=0,fill=ugreen!20]
%----------------------------------------------------------------------------------------
% code (or representation)
\node [nnode] (code1) at (0,0) {\scriptsize{0}};
\node [nnode,anchor=north] (code2) at ([yshift=-0.3*\nodesize]code1.south) {\scriptsize{5}};
\node [nnode,anchor=north] (code3) at ([yshift=-0.3*\nodesize]code2.south) {\scriptsize{-1}};
\node [nnode,anchor=north] (code4) at ([yshift=-0.3*\nodesize]code3.south) {\scriptsize{.8}};
\begin{pgfonlayer}{background}
\node [draw,inner sep=3pt,fill=white] [fit = (code1) (code4)] (codebox) {};
\end{pgfonlayer}
\node [anchor=south] (codelabel) at ([yshift=1.5em]codebox.north) {Code $\mathbf{h}$};
\node [anchor=north] (bottleneck) at ([yshift=2pt]codelabel.south) {(Bottleneck)};
%----------------------------------------------------------------------------------------
% encoder
\node [nnode,anchor=east,fill=blue!20] (input2) at ([xshift=-7*\nodesize]code1.west) {\scriptsize{3}};
\node [nnode,anchor=south,fill=blue!20] (input1) at ([yshift=0.3*\nodesize]input2.north) {\scriptsize{7}};
\node [nnode,anchor=north,fill=blue!20] (input3) at ([yshift=-0.3*\nodesize]input2.south) {\scriptsize{.1}};
\node [nnode,anchor=north,fill=blue!20] (input4) at ([yshift=-0.3*\nodesize]input3.south) {\scriptsize{2}};
\node [nnode,anchor=north,fill=blue!20] (input5) at ([yshift=-0.3*\nodesize]input4.south) {\scriptsize{0}};
\node [nnode,anchor=north,fill=blue!20] (input6) at ([yshift=-0.3*\nodesize]input5.south) {\scriptsize{.1}};
\begin{pgfonlayer}{background}
\node [draw,inner sep=3pt,fill=white] [fit = (input1) (input6)] (inputbox) {};
\end{pgfonlayer}
\node [anchor=south] (inputlabel) at ([yshift=0.5em]inputbox.north) {Input $\mathbf{x}$};
\filldraw [fill=blue!20,draw=white] ([xshift=-5pt]codebox.south west) -- ([xshift=-5pt]codebox.north west) -- ([xshift=5pt]inputbox.north east) -- ([xshift=5pt]inputbox.south east) -- ([xshift=-5pt]codebox.south west);
\node [anchor=east] (encoderlabel) at ([xshift=-1.8em,yshift=0.5em]codebox.west) {\Large{Encoder}};
\node [anchor=north west] (encodereqn) at ([xshift=0em]encoderlabel.south west) {$\mathbf{h}=\mathrm{Enc}_{\theta}(\mathbf{x})$};
%----------------------------------------------------------------------------------------
% decoder
\node [nnode,anchor=west,fill=red!20] (output2) at ([xshift=7*\nodesize]code1.east) {\scriptsize{3}};
\node [nnode,anchor=south,fill=red!20] (output1) at ([yshift=0.3*\nodesize]output2.north) {\scriptsize{7}};
\node [nnode,anchor=north,fill=red!20] (output3) at ([yshift=-0.3*\nodesize]output2.south) {\scriptsize{.1}};
\node [nnode,anchor=north,fill=red!20] (output4) at ([yshift=-0.3*\nodesize]output3.south) {\scriptsize{0}};
\node [nnode,anchor=north,fill=red!20] (output5) at ([yshift=-0.3*\nodesize]output4.south) {\scriptsize{1}};
\node [nnode,anchor=north,fill=red!20] (output6) at ([yshift=-0.3*\nodesize]output5.south) {\scriptsize{.5}};
\begin{pgfonlayer}{background}
\node [draw,inner sep=3pt,fill=white] [fit = (output1) (output6)] (outputbox) {};
\end{pgfonlayer}
\node [anchor=south] (outputlabel) at ([yshift=0.5em]outputbox.north) {Reconstruction $\widetilde{\mathbf{x}}$};
\filldraw [fill=red!20,draw=white] ([xshift=5pt]codebox.south east) -- ([xshift=5pt]codebox.north east) -- ([xshift=-5pt]outputbox.north west) -- ([xshift=-5pt]outputbox.south west) -- ([xshift=5pt]codebox.south east);
\node [anchor=west] (decoderlabel) at ([xshift=1.2em,yshift=0.5em]codebox.east) {\Large{Decoder}};
\node [anchor=north west] (decodereqn) at ([xshift=0em]decoderlabel.south west) {$\widetilde{\mathbf{x}}=\mathrm{Dec}_{\omega}(\mathbf{h})$};
%----------------------------------------------------------------------------------------
% the training objective
\node [anchor=south] (objective) at ([yshift=6em,xshift=-2em]codebox.north) {$(\hat{\theta},\hat{\omega}) = \argmin\limits_{(\theta,\omega)} \sum\limits_{i=1}^{m} L ($};
\node [anchor=west,inner sep=2pt,minimum height=1.5em,fill=blue!20] (obj2) at ([yshift=4pt]objective.east) {$\mathbf{x}^{(i)}$};
\node [anchor=west,inner sep=3pt,minimum height=1.5em] (obj3) at ([yshift=-4pt]obj2.east) {$,$};
\node [anchor=west,inner sep=3pt,minimum height=1.5em,fill=red!20] (obj4) at ([yshift=4pt]obj3.east) {$\widetilde{\mathbf{x}}^{(i)}$};
\node [anchor=west,inner sep=2pt,minimum height=1.5em] (obj5) at ([yshift=-2pt]obj4.east) {$)$};
\draw [<-] (inputlabel.north) .. controls +(north:3em) and +(south:3em) .. (obj2.south);
\draw [<-] (outputlabel.north) .. controls +(north:3em) and +(south:3em) .. (obj4.south);
\node [anchor=south west] (objlabel) at ([yshift=-0.5em]objective.north west) {Training Objective:};
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}[scale=1]
\begin{axis}[
axis x line=bottom,
axis y line=left,
line width=1.2pt,
xlabel near ticks,
width=12cm, height=6cm,
xlabel={Complexity},
y label style={font=\large},
xmin=-0.1,xmax=10,
ymin=0,ymax=20.5,
xmajorticks=false,
ymajorticks=false,
legend style={xshift=-1em,yshift=1em,anchor=north east,legend cell align=left},
]
\addplot[-latex,color=blue, line width=4pt, smooth,line cap=round,opacity=.55] coordinates {(0.2,20)(0.5,14.2)(1,10)(1.5,7.6)(2,6.04)(3,4.5)(4,3.7)(5,3.2)(6,2.8)(7,2.7)(8,2.6)(9.5,2.6)};
\addplot[-latex,color=ugreen, line width=4pt, smooth,line cap=round,opacity=.55] coordinates {(0.2,2.5)(1,2.6)(2,2.7)(3,3)(4,3.5)(5,4)(6,5.1)(7,6.5)(8,8.3)(8.8,10)(9.5,11.8)};
\addplot[-latex,color=red, line width=4pt, smooth,line cap=round,opacity=.55] coordinates {(0.35,20)(0.54,16.4)(1,11.88)(1.5,9.5)(2,8.2)(3,6.6)(4,5.7)(5,5.4)(6,6)(7,7.4)(8,9.2)(9.5,12.6)};
%\addplot[color=red, mark=x,draw=none,line width=1.6pt] coordinates {(0,1)(0.5,1)(1,1)(2,1)(3,1)(4,1)(5,1)(6,1)(7,1)(8,1)(9,1)};
\end{axis}
\node[color=blue,opacity=.6] (bias) at (2em,4.5em) {Bias};
\node[color=red,opacity=.6] (error) at (18em,5.3em) {Test Error};
\node[color=ugreen,opacity=.6] (variance) at (22em,3.2em) {Variance};
\end{tikzpicture}
\end{center}
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
\begin{scope}
\node [anchor=south west,minimum width=20em] (source) at (0,0) {Source-side sequence: $\mathbf{x}=x_1...x_m$};
\node [anchor=south west,minimum width=20em] (target) at ([yshift=16em]source.north west) {Target-side sequence: $\mathbf{y}=y_1...y_n$};
\node [anchor=center,minimum width=9.6em,minimum height=1.8em,draw=FigureInk!65,fill=FigureMist,line width=.7pt,rounded corners=0.3em] (hidden) at ([yshift=8em]source.north) {};
\node [anchor=west,minimum width=1.5em,minimum size=1.5em,fill=ugreen!20] (cell01) at ([xshift=0.2em]hidden.west) {\footnotesize{.2}};
\node [anchor=west,minimum width=1.5em,minimum size=1.5em,fill=ugreen!10] (cell02) at (cell01.east) {\footnotesize{-1}};
\node [anchor=west,minimum width=1.5em,minimum size=1.5em,fill=ugreen!70] (cell03) at (cell02.east) {\footnotesize{6}};
\node [anchor=west,minimum width=1.5em,minimum size=1.5em,fill=ugreen!50] (cell04) at (cell03.east) {\footnotesize{5}};
\node [anchor=west,minimum width=1.5em,minimum size=1.5em,fill=ugreen!30] (cell05) at (cell04.east) {\footnotesize{.7}};
\node [anchor=west,minimum width=1.5em,minimum size=1.5em,fill=ugreen!10] (cell06) at (cell05.east) {\footnotesize{-2}};
\node [anchor=west] (representation) at ([xshift=0.5em]cell06.east) {Representation: $\mathbf{H}$};
\filldraw [fill=FigureCoral!16,draw=white] (source.north west) -- (source.north east) -- ([xshift=-0.2em,yshift=-0.1em]hidden.south east) -- ([xshift=0.2em,yshift=-0.1em]hidden.south west);
\filldraw [fill=FigureBlue!16,draw=white] (target.south west) -- (target.south east) -- ([xshift=-0.2em,yshift=0.1em]hidden.north east) -- ([xshift=0.2em,yshift=0.1em]hidden.north west);
\draw [->,thick] (source.north west) -- ([xshift=0.2em,yshift=-0.1em]hidden.south west);
\draw [->,thick] (source.north east) -- ([xshift=-0.2em,yshift=-0.1em]hidden.south east);
\draw [->,thick] ([xshift=0.2em,yshift=0.1em]hidden.north west) -- (target.south west);
\draw [->,thick] ([xshift=-0.2em,yshift=0.1em]hidden.north east) -- (target.south east);
\node [anchor=south,align=center] (enclabel) at ([yshift=2em]source.north) {\Large{\textbf{Encoder}}\\($\mathbf{H}=\mathrm{Encode}(\mathbf{x})$)};
\node [anchor=north,align=center] (declabel) at ([yshift=-2em]target.south) {\Large{\textbf{Decoder}}\\($\mathbf{y}=\mathrm{Decode}(\mathbf{H})$)};
\end{scope}
\end{tikzpicture}
\end{center}
% !Mode:: "TeX:UTF-8"
% !TEX encoding = UTF-8 Unicode
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
%----------------------------------------------------------------------------------------
% variables
\def\rowsep{0.5cm}
\def\colsep{0.4cm}
\def\ballsize{0.1cm}
\def\stepsize{0.5cm}
\tikzstyle{pnode} = [circle,minimum size=\ballsize,inner sep=2pt,fill=FigureMuted,draw=FigureInk!45,line width=.4pt]
%----------------------------------------------------------------------------------------
% rnn
\begin{scope}
\node [anchor=west,pnode] (p1) at (0,0) {};
\node [anchor=west,pnode] (p2) at ([xshift=\stepsize]p1.east) {};
\node [anchor=west,pnode] (p3) at ([xshift=\stepsize]p2.east) {};
\node [anchor=west,pnode] (p4) at ([xshift=\stepsize]p3.east) {};
\node [anchor=west,pnode,fill=blue] (p5) at ([xshift=\stepsize]p4.east) {};
\draw [->,thick] ([yshift=0.1em,xshift=-0.1em]p2.north) .. controls +(north:0.5cm) and +(north:0.5cm) .. ([yshift=0.1em]p1.north);
\draw [->,thick] ([yshift=0.1em,xshift=-0.1em]p3.north) .. controls +(north:0.5cm) and +(north:0.5cm) .. ([yshift=0.1em,xshift=0.1em]p2.north);
\draw [->,thick] ([yshift=0.1em,xshift=-0.1em]p4.north) .. controls +(north:0.5cm) and +(north:0.5cm) .. ([yshift=0.1em,xshift=0.1em]p3.north);
\draw [->,thick,blue] ([yshift=0.1em,xshift=-0.1em]p5.north) .. controls +(north:0.5cm) and +(north:0.5cm) .. ([yshift=0.1em,xshift=0.1em]p4.north);
\node [anchor=north] (rnnlabel) at ([yshift=-1em]p3.south) {(a) RNN};
\end{scope}
%----------------------------------------------------------------------------------------
% cnn
\begin{scope} [xshift=1.8in]
\node [anchor=west,pnode] (p1) at (0,0) {};
\node [anchor=west,pnode] (p2) at ([xshift=\stepsize]p1.east) {};
\node [anchor=west,pnode,fill=blue] (p3) at ([xshift=\stepsize]p2.east) {};
\node [anchor=west,pnode] (p4) at ([xshift=\stepsize]p3.east) {};
\node [anchor=west,pnode] (p5) at ([xshift=\stepsize]p4.east) {};
\draw [->,thick,blue] ([yshift=0.1em,xshift=-0.1em]p3.north) .. controls +(north:0.5cm) and +(north:0.5cm) .. ([yshift=0.1em]p2.north);
\draw [->,thick,blue] ([yshift=0.1em,xshift=0.1em]p3.north) .. controls +(north:0.5cm) and +(north:0.5cm) .. ([yshift=0.1em]p4.north);
\node [anchor=north] (rnnlabel) at ([yshift=-1em]p3.south) {(b) CNN ($r=3$)};
\end{scope}
%----------------------------------------------------------------------------------------
% self-attention
\begin{scope} [xshift=3.6in]
\node [anchor=west,pnode] (p1) at (0,0) {};
\node [anchor=west,pnode] (p2) at ([xshift=\stepsize]p1.east) {};
\node [anchor=west,pnode] (p3) at ([xshift=\stepsize]p2.east) {};
\node [anchor=west,pnode] (p4) at ([xshift=\stepsize]p3.east) {};
\node [anchor=west,pnode,fill=blue] (p5) at ([xshift=\stepsize]p4.east) {};
\draw [->,thick,blue] ([yshift=0.1em,xshift=-0.2em]p5.north) .. controls +(north:0.2cm) and +(north:0.2cm) .. ([yshift=0.1em]p4.north);
\draw [->,thick,blue] ([yshift=0.1em,xshift=-0.1em]p5.north) .. controls +(north:0.35cm) and +(north:0.35cm) .. ([yshift=0.1em]p3.north);
\draw [->,thick,blue] ([yshift=0.1em,xshift=0.1em]p5.north) .. controls +(north:0.5cm) and +(north:0.5cm) .. ([yshift=0.1em]p2.north);
\draw [->,thick,blue] ([yshift=0.1em,xshift=0.2em]p5.north) .. controls +(north:0.65cm) and +(north:0.65cm) .. ([yshift=0.1em]p1.north);
\node [anchor=north] (rnnlabel) at ([yshift=-1em]p3.south) {(c) Self-attention};
\end{scope}
\end{tikzpicture}
\end{center}
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure,scale=0.45]
\amlfigurepalette
%-----small lr
\begin{scope}[xshift=0cm]
\foreach \a/\y in {0.5/0, 1/0.08, 1.6/0.15, 2.4/0.3, 3/0.34, 4/0.5, 5/0.7}{
\draw[FigureLine,rotate=25,line width=0.7pt] (0,\y) ellipse ({\a} and {\a*0.5});
}
\coordinate (n1) at (-4.4,-2);
\coordinate (n2) at (-4,-1.5);
\coordinate (n3) at (-3.4,-1.8);
\coordinate (n4) at (-3.3,-1.4);
\coordinate (n5) at (-2.8,-1.6);
\coordinate (n6) at (-2.7,-1.3);
\coordinate (n7) at (-2.3,-1.4);
\coordinate (n8) at (-2.2,-1.12);
\coordinate (n9) at (-1.85,-1.25);
\coordinate (n10) at (-1.65,-1.04);
\coordinate (n11) at (-1.25,-1.05);
\foreach \i in {1,...,10}{
\draw[-stealth,FigureGold,line width=0.75pt] ([xshift=0.06em]n\i) -- (n\the\numexpr\i+1\relax);
}
\draw[color=FigureInk,fill=FigureInk,rotate=25] (0,0) ellipse (0.1 and 0.05);
\node[anchor=center] (l1) at (-0.5,-4){(a) 学习率过小};
\end{scope}
%-----large lr
\begin{scope}[xshift=10.5cm]
\foreach \a/\y in {0.5/0, 1/0.08, 1.6/0.15, 2.4/0.3, 3/0.34, 4/0.5, 5/0.7}{
\draw[FigureLine,rotate=25,line width=0.7pt] (0,\y) ellipse ({\a} and {\a*0.5});
}
\coordinate (n1) at (-4.4,-2);
\coordinate (n2) at (-3,0.1);
\coordinate (n3) at (-1.5,-1.6);
\coordinate (n4) at (-1.1,0.8);
\coordinate (n5) at (0,-1.1);
\coordinate (n6) at (-0.5,0.9);
\coordinate (n7) at (0.2,-1);
\coordinate (n8) at (-0.3,0.9);
\coordinate (n9) at (0.3,-0.9);
\coordinate (n10) at (-0.1,0.9);
\foreach \i in {1,...,9}{
\draw[-stealth,FigureCoral,line width=0.75pt] ([xshift=0.06em]n\i) -- (n\the\numexpr\i+1\relax);
}
\draw[color=FigureInk,fill=FigureInk,rotate=25] (0,0) ellipse (0.1 and 0.05);
\node[anchor=center] (l2) at (-0.5,-4){(b) 学习率过大};
\end{scope}
%-----proper lr
\begin{scope}[xshift=21cm]
\foreach \a/\y in {0.5/0, 1/0.08, 1.6/0.15, 2.4/0.3, 3/0.34, 4/0.5, 5/0.7}{
\draw[FigureLine,rotate=25,line width=0.7pt] (0,\y) ellipse ({\a} and {\a*0.5});
}
\coordinate (n1) at (-4.4,-2);
\coordinate (n2) at (-3.6,-1);
\coordinate (n3) at (-2.4,-1.4);
\coordinate (n4) at (-2.1,-0.6);
\coordinate (n5) at (-1.4,-0.9);
\coordinate (n6) at (-1.3,-0.4);
\coordinate (n7) at (-0.92,-0.6);
\coordinate (n8) at (-0.74,-0.3);
\coordinate (n9) at (-0.46,-0.4);
\coordinate (n10) at (-0.28,-0.2);
\foreach \i in {1,...,9}{
\draw[-stealth,FigureTeal,line width=0.75pt] ([xshift=0.06em]n\i) -- (n\the\numexpr\i+1\relax);
}
\draw[color=FigureInk,fill=FigureInk,rotate=25] (0,0) ellipse (0.1 and 0.05);
\node[anchor=center] (l3) at (-0.5,-4){(c) 学习率合适};
\end{scope}
\end{tikzpicture}
\end{center}
% !Mode:: "TeX:UTF-8"
% !TEX encoding = UTF-8 Unicode
\input{figures/figure-theme.tex}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
\def\nodewidth{0.5cm}
\def\nodeheight{0.5cm}
\def\nodestep{0.6*\nodewidth}
\tikzstyle{enode} = [minimum width=\nodewidth,minimum height=\nodeheight,inner sep=2pt,fill=gray!50,draw=FigureInk!28,line width=.35pt,rounded corners=.8pt]
%%% causal language modeling
\begin{scope}
\node [enode,anchor=west,fill=blue!80] (node00) at (0,0) {};
\node [enode,anchor=west] (node01) at ([xshift=\nodestep]node00.east) {};
\node [enode,anchor=west] (node02) at ([xshift=\nodestep]node01.east) {};
\node [enode,anchor=west] (node03) at ([xshift=\nodestep]node02.east) {};
\node [enode,anchor=west] (node04) at ([xshift=\nodestep]node03.east) {};
\node [enode,anchor=north,fill=blue!80] (node10) at ([yshift=-\nodestep]node00.south) {};
\node [enode,anchor=west,fill=blue!80] (node11) at ([xshift=\nodestep]node10.east) {};
\node [enode,anchor=west] (node12) at ([xshift=\nodestep]node11.east) {};
\node [enode,anchor=west] (node13) at ([xshift=\nodestep]node12.east) {};
\node [enode,anchor=west] (node14) at ([xshift=\nodestep]node13.east) {};
\node [enode,anchor=north,fill=blue!80] (node20) at ([yshift=-\nodestep]node10.south) {};
\node [enode,anchor=west,fill=blue!80] (node21) at ([xshift=\nodestep]node20.east) {};
\node [enode,anchor=west,fill=blue!80] (node22) at ([xshift=\nodestep]node21.east) {};
\node [enode,anchor=west] (node23) at ([xshift=\nodestep]node22.east) {};
\node [enode,anchor=west] (node24) at ([xshift=\nodestep]node23.east) {};
\node [enode,anchor=north,fill=blue!80] (node30) at ([yshift=-\nodestep]node20.south) {};
\node [enode,anchor=west,fill=blue!80] (node31) at ([xshift=\nodestep]node30.east) {};
\node [enode,anchor=west,fill=blue!80] (node32) at ([xshift=\nodestep]node31.east) {};
\node [enode,anchor=west,fill=blue!80] (node33) at ([xshift=\nodestep]node32.east) {};
\node [enode,anchor=west] (node34) at ([xshift=\nodestep]node33.east) {};
\node [enode,anchor=north,fill=blue!80] (node40) at ([yshift=-\nodestep]node30.south) {};
\node [enode,anchor=west,fill=blue!80] (node41) at ([xshift=\nodestep]node40.east) {};
\node [enode,anchor=west,fill=blue!80] (node42) at ([xshift=\nodestep]node41.east) {};
\node [enode,anchor=west,fill=blue!80] (node43) at ([xshift=\nodestep]node42.east) {};
\node [enode,anchor=west,fill=blue!80] (node44) at ([xshift=\nodestep]node43.east) {};
\foreach \i in {0,...,4}{
\node [anchor=south] (hx\i) at ([yshift=0.2em]node0\i.north) {\small{$x_{\i}$}};
\node [anchor=east] (vx\i) at ([xshift=-0.2em]node\i0.west) {\small{$x_{\i}$}};
\draw [->,double] ([xshift=1.0cm]node\i4.east) -- ([xshift=1.6cm]node\i4.east);
}
\node [anchor=west] (pr0) at ([xshift=2.4cm]node04.east) {$\Pr(x_0)=1$};
\node [anchor=west] (pr1) at ([xshift=2.4cm]node14.east) {$\Pr(x_1|\mathbf{e}_0)$};
\node [anchor=west] (pr2) at ([xshift=2.4cm]node24.east) {$\Pr(x_2|\mathbf{e}_0,\mathbf{e}_1)$};
\node [anchor=west] (pr3) at ([xshift=2.4cm]node34.east) {$\Pr(x_3|\mathbf{e}_0,\mathbf{e}_1,\mathbf{e}_2)$};
\node [anchor=west] (pr4) at ([xshift=2.4cm]node44.east) {$\Pr(x_4|\mathbf{e}_0,\mathbf{e}_1,\mathbf{e}_2,\mathbf{e}_3)$};
\node [anchor=north] (caption) at ([yshift=-0.5cm,xshift=0.8cm]node44.south east) {(a) Causal Language Modeling (order: $x_0 \to x_1 \to x_2 \to x_3 \to x_4$)};
\end{scope}
%%% masked language modeling
\begin{scope}[yshift=-6.5cm]
\node [enode,anchor=west,fill=blue!80] (node00) at (0,0) {};
\node [enode,anchor=west,fill=blue!80] (node01) at ([xshift=\nodestep]node00.east) {};
\node [enode,anchor=west,fill=blue!80] (node02) at ([xshift=\nodestep]node01.east) {};
\node [enode,anchor=west,fill=blue!80] (node03) at ([xshift=\nodestep]node02.east) {};
\node [enode,anchor=west,fill=blue!80] (node04) at ([xshift=\nodestep]node03.east) {};
\node [enode,anchor=north,fill=blue!80] (node10) at ([yshift=-\nodestep]node00.south) {};
\node [enode,anchor=west,fill=blue!80] (node11) at ([xshift=\nodestep]node10.east) {};
\node [enode,anchor=west,fill=blue!80] (node12) at ([xshift=\nodestep]node11.east) {};
\node [enode,anchor=west,fill=blue!80] (node13) at ([xshift=\nodestep]node12.east) {};
\node [enode,anchor=west,fill=blue!80] (node14) at ([xshift=\nodestep]node13.east) {};
\node [enode,anchor=north,fill=blue!80] (node20) at ([yshift=-\nodestep]node10.south) {};
\node [enode,anchor=west,fill=blue!80] (node21) at ([xshift=\nodestep]node20.east) {};
\node [enode,anchor=west,fill=blue!80] (node22) at ([xshift=\nodestep]node21.east) {};
\node [enode,anchor=west,fill=blue!80] (node23) at ([xshift=\nodestep]node22.east) {};
\node [enode,anchor=west,fill=blue!80] (node24) at ([xshift=\nodestep]node23.east) {};
\node [enode,anchor=north,fill=blue!80] (node30) at ([yshift=-\nodestep]node20.south) {};
\node [enode,anchor=west,fill=blue!80] (node31) at ([xshift=\nodestep]node30.east) {};
\node [enode,anchor=west,fill=blue!80] (node32) at ([xshift=\nodestep]node31.east) {};
\node [enode,anchor=west,fill=blue!80] (node33) at ([xshift=\nodestep]node32.east) {};
\node [enode,anchor=west,fill=blue!80] (node34) at ([xshift=\nodestep]node33.east) {};
\node [enode,anchor=north,fill=blue!80] (node40) at ([yshift=-\nodestep]node30.south) {};
\node [enode,anchor=west,fill=blue!80] (node41) at ([xshift=\nodestep]node40.east) {};
\node [enode,anchor=west,fill=blue!80] (node42) at ([xshift=\nodestep]node41.east) {};
\node [enode,anchor=west,fill=blue!80] (node43) at ([xshift=\nodestep]node42.east) {};
\node [enode,anchor=west,fill=blue!80] (node44) at ([xshift=\nodestep]node43.east) {};
\foreach \i in {0,...,4}{
\node [anchor=south] (hx\i) at ([yshift=0.2em]node0\i.north) {\small{$x_{\i}$}};
\node [anchor=east] (vx\i) at ([xshift=-0.2em]node\i0.west) {\small{$x_{\i}$}};
\draw [->,double] ([xshift=1.0cm]node\i4.east) -- ([xshift=1.6cm]node\i4.east);
}
\node [anchor=south] (hmasked3) at ([yshift=-0.1cm]hx3.north) {\footnotesize{masked}};
\node [anchor=south] (hmasked1) at ([yshift=-0.1cm]hx1.north) {\footnotesize{masked}};
\node [anchor=north east] (vmasked3) at ([yshift=0.2cm]vx3.south east) {\footnotesize{masked}};
\node [anchor=north east] (vmasked1) at ([yshift=0.2cm]vx1.south east) {\footnotesize{masked}};
\node [anchor=west] (pr0) at ([xshift=2.4cm]node04.east) {$1$};
\node [anchor=west] (pr1) at ([xshift=2.4cm]node14.east) {$\Pr(x_1|\mathbf{e}_0,\mathbf{e}_{\mathrm{mask}},\mathbf{e}_2,\mathbf{e}_{\mathrm{mask}},\mathbf{e}_4)$};
\node [anchor=west] (pr2) at ([xshift=2.4cm]node24.east) {$1$};
\node [anchor=west] (pr3) at ([xshift=2.4cm]node34.east) {$\Pr(x_3|\mathbf{e}_0,\mathbf{e}_{\mathrm{mask}},\mathbf{e}_2,\mathbf{e}_{\mathrm{mask}},\mathbf{e}_4)$};
\node [anchor=west] (pr4) at ([xshift=2.4cm]node44.east) {$1$};
\node [anchor=north] (caption) at ([yshift=-0.5cm,xshift=0.8cm]node44.south east) {(b) Masked Language Modeling (order: $x_0, \mathrm{[MASK]}, x_2, \mathrm{[MASK]}, x_4 \to x_1, x_3$)};
\end{scope}
%%% permuted language modeling
\begin{scope}[yshift=-13cm]
\node [enode,anchor=west,fill=blue!80] (node00) at (0,0) {};
\node [enode,anchor=west] (node01) at ([xshift=\nodestep]node00.east) {};
\node [enode,anchor=west] (node02) at ([xshift=\nodestep]node01.east) {};
\node [enode,anchor=west] (node03) at ([xshift=\nodestep]node02.east) {};
\node [enode,anchor=west] (node04) at ([xshift=\nodestep]node03.east) {};
\node [enode,anchor=north,fill=blue!80] (node10) at ([yshift=-\nodestep]node00.south) {};
\node [enode,anchor=west,fill=blue!80] (node11) at ([xshift=\nodestep]node10.east) {};
\node [enode,anchor=west,fill=blue!80] (node12) at ([xshift=\nodestep]node11.east) {};
\node [enode,anchor=west] (node13) at ([xshift=\nodestep]node12.east) {};
\node [enode,anchor=west,fill=blue!80] (node14) at ([xshift=\nodestep]node13.east) {};
\node [enode,anchor=north,fill=blue!80] (node20) at ([yshift=-\nodestep]node10.south) {};
\node [enode,anchor=west] (node21) at ([xshift=\nodestep]node20.east) {};
\node [enode,anchor=west,fill=blue!80] (node22) at ([xshift=\nodestep]node21.east) {};
\node [enode,anchor=west] (node23) at ([xshift=\nodestep]node22.east) {};
\node [enode,anchor=west,fill=blue!80] (node24) at ([xshift=\nodestep]node23.east) {};
\node [enode,anchor=north,fill=blue!80] (node30) at ([yshift=-\nodestep]node20.south) {};
\node [enode,anchor=west,fill=blue!80] (node31) at ([xshift=\nodestep]node30.east) {};
\node [enode,anchor=west,fill=blue!80] (node32) at ([xshift=\nodestep]node31.east) {};
\node [enode,anchor=west,fill=blue!80] (node33) at ([xshift=\nodestep]node32.east) {};
\node [enode,anchor=west,fill=blue!80] (node34) at ([xshift=\nodestep]node33.east) {};
\node [enode,anchor=north,fill=blue!80] (node40) at ([yshift=-\nodestep]node30.south) {};
\node [enode,anchor=west] (node41) at ([xshift=\nodestep]node40.east) {};
\node [enode,anchor=west] (node42) at ([xshift=\nodestep]node41.east) {};
\node [enode,anchor=west] (node43) at ([xshift=\nodestep]node42.east) {};
\node [enode,anchor=west,fill=blue!80] (node44) at ([xshift=\nodestep]node43.east) {};
\foreach \i in {0,...,4}{
\node [anchor=south] (hx\i) at ([yshift=0.2em]node0\i.north) {\small{$x_{\i}$}};
\node [anchor=east] (vx\i) at ([xshift=-0.2em]node\i0.west) {\small{$x_{\i}$}};
\draw [->,double] ([xshift=1.0cm]node\i4.east) -- ([xshift=1.6cm]node\i4.east);
}
\node [anchor=west] (pr0) at ([xshift=2.4cm]node04.east) {$\Pr(x_0)=1$};
\node [anchor=west] (pr1) at ([xshift=2.4cm]node14.east) {$\Pr(x_1|\mathbf{e}_0,\mathbf{e}_4,\mathbf{e}_2)$};
\node [anchor=west] (pr2) at ([xshift=2.4cm]node24.east) {$\Pr(x_2|\mathbf{e}_0,\mathbf{e}_4)$};
\node [anchor=west] (pr3) at ([xshift=2.4cm]node34.east) {$\Pr(x_3|\mathbf{e}_0,\mathbf{e}_4,\mathbf{e}_2,\mathbf{e}_1)$};
\node [anchor=west] (pr4) at ([xshift=2.4cm]node44.east) {$\Pr(x_4|\mathbf{e}_0)$};
\node [anchor=north] (caption) at ([yshift=-0.5cm,xshift=0.8cm]node44.south east) {(c) Permuted Language Modeling (order: $x_0 \to x_4 \to x_2 \to x_1 \to x_3$)};
\end{scope}
\end{tikzpicture}
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
%----------------------------------------------------------------------------------------
% variables
\def\nodesep{1.5em}
\def\noderowsep{7.0em}
\def\snodesep{0.2em}
\tikzstyle{nnode} = [circle,minimum size=2.2em,draw=FigureInk!65,line width=.7pt]
\tikzstyle{snode} = [circle,minimum size=0.7em,inner sep=0pt]
%----------------------------------------------------------------------------------------
% layer 1 (hidden 1)
\node [anchor=center,nnode,fill=blue!30] (n11) at (0,0) {};
\node [anchor=west,nnode,fill=blue!30] (n12) at ([xshift=\nodesep]n11.east) {};
\node [anchor=west,nnode,fill=blue!30] (n13) at ([xshift=\nodesep]n12.east) {};
\node [anchor=west,nnode,fill=blue!30] (n14) at ([xshift=\nodesep]n13.east) {};
%----------------------------------------------------------------------------------------
% layer 0 (input)
\node [anchor=north,nnode,fill=red!30] (n01) at ([xshift=0.5*\nodesep,yshift= -\noderowsep]n11.east) {$x_1$};
\node [anchor=west,nnode,fill=red!30] (n02) at ([xshift=\nodesep]n01.east) {$x_2$};
\node [anchor=west,nnode,fill=red!30] (n03) at ([xshift=\nodesep]n02.east) {$x_3$};
%----------------------------------------------------------------------------------------
% layer 2 (hidden 2)
\node [anchor=south,nnode,fill=ugreen!30] (n21) at ([xshift=0.5*\nodesep,yshift= \noderowsep]n11.east) {};
\node [anchor=west,nnode,fill=ugreen!30] (n22) at ([xshift=\nodesep]n21.east) {};
\node [anchor=west,nnode,fill=ugreen!30] (n23) at ([xshift=\nodesep]n22.east) {};
%----------------------------------------------------------------------------------------
% layer 2 (bias 2)
\node [anchor=west,nnode,fill=purple!30] (b2) at ([xshift=\nodesep]n23.east) {$\bf{1}$};
%----------------------------------------------------------------------------------------
% layer 3 (hidden 3)
\node [anchor=south,nnode,fill=orange!30] (n31) at ([xshift=0,yshift= \noderowsep]n21.center) {};
\node [anchor=west,nnode,fill= orange!30] (n32) at ([xshift=\nodesep]n31.east) {};
\node [anchor=west,nnode,fill= orange!30] (n33) at ([xshift=\nodesep]n32.east) {};
%----------------------------------------------------------------------------------------
% output
\node [anchor=south,circle,fill=cyan!30,minimum size=1.2em] (n41) at ([xshift=0,yshift= \noderowsep]n31.center) {\large{$y_1$}};
\node [anchor=south,circle,fill=cyan!30,minimum size=1.2em] (n42) at ([xshift=0,yshift= \noderowsep]n32.center) {\large{$y_2$}};
\node [anchor=south,circle,fill=cyan!30,minimum size=1.2em] (n43) at ([xshift=0,yshift= \noderowsep]n33.center) {\large{$y_3$}};
%----------------------------------------------------------------------------------------
% connections
% input -> layer 1
\draw [->,thick] ([yshift=1pt]n01.100) -- ([yshift=-1pt]n11.-90);
\draw [->,thick] ([yshift=1pt]n01.90) -- ([yshift=-1pt]n12.-100);
\draw [->,thick] ([yshift=1pt]n01.80) -- ([yshift=-1pt]n13.-100);
\draw [->,thick] ([yshift=1pt]n01.70) -- ([yshift=-1pt]n14.-110);
\draw [->,thick] ([yshift=1pt]n02.105) -- ([yshift=-1pt]n11.-80);
\draw [->,thick] ([yshift=1pt]n02.95) -- ([yshift=-1pt]n12.-90);
\draw [->,thick] ([yshift=1pt]n02.85) -- ([yshift=-1pt]n13.-90);
\draw [->,thick] ([yshift=1pt]n02.75) -- ([yshift=-1pt]n14.-100);
\draw [->,thick] ([yshift=1pt]n03.110) -- ([yshift=-1pt]n11.-70);
\draw [->,thick] ([yshift=1pt]n03.100) -- ([yshift=-1pt]n12.-80);
\draw [->,thick] ([yshift=1pt]n03.90) -- ([yshift=-1pt]n13.-80);
\draw [->,thick] ([yshift=1pt]n03.80) -- ([yshift=-1pt]n14.-90);
% layer 1 -> layer 2
\draw [->,thick] ([yshift=1pt]n11.90) -- ([yshift=-1pt]n21.-105);
\draw [->,thick] ([yshift=1pt]n11.80) -- ([yshift=-1pt]n22.-115);
\draw [->,thick] ([yshift=1pt]n11.70) -- ([yshift=-1pt]n23.-125);
\draw [->,thick] ([yshift=1pt]n12.95) -- ([yshift=-1pt]n21.-90);
\draw [->,thick] ([yshift=1pt]n12.85) -- ([yshift=-1pt]n22.-100);
\draw [->,thick] ([yshift=1pt]n12.75) -- ([yshift=-1pt]n23.-110);
\draw [->,thick] ([yshift=1pt]n13.100) -- ([yshift=-1pt]n21.-80);
\draw [->,thick] ([yshift=1pt]n13.90) -- ([yshift=-1pt]n22.-85);
\draw [->,thick] ([yshift=1pt]n13.80) -- ([yshift=-1pt]n23.-95);
\draw [->,thick] ([yshift=1pt]n14.105) -- ([yshift=-1pt]n21.-60);
\draw [->,thick] ([yshift=1pt]n14.95) -- ([yshift=-1pt]n22.-70);
\draw [->,thick] ([yshift=1pt]n14.85) -- ([yshift=-1pt]n23.-80);
% layer 2 -> layer 3
\draw [->,thick] ([yshift=1pt]n21.90) -- ([yshift=-1pt]n31.-90);
\draw [->,thick] ([yshift=1pt]n21.80) -- ([yshift=-1pt]n32.-100);
\draw [->,thick] ([yshift=1pt]n21.70) -- ([yshift=-1pt]n33.-110);
\draw [->,thick] ([yshift=1pt]n22.100) -- ([yshift=-1pt]n31.-80);
\draw [->,thick] ([yshift=1pt]n22.90) -- ([yshift=-1pt]n32.-90);
\draw [->,thick] ([yshift=1pt]n22.80) -- ([yshift=-1pt]n33.-100);
\draw [->,thick] ([yshift=1pt]n23.110) -- ([yshift=-1pt]n31.-70);
\draw [->,thick] ([yshift=1pt]n23.100) -- ([yshift=-1pt]n32.-80);
\draw [->,thick] ([yshift=1pt]n23.90) -- ([yshift=-1pt]n33.-90);
\draw [->,thick] ([yshift=1pt]b2.120) -- ([yshift=-1pt]n31.-55);
\draw [->,thick] ([yshift=1pt]b2.110) -- ([yshift=-1pt]n32.-65);
\draw [->,thick] ([yshift=1pt]b2.100) -- ([yshift=-1pt]n33.-75);
% layer 3 -> output
\draw [->,thick] ([yshift=1pt]n31.90) -- ([yshift=-1pt]n41.-90);
\draw [->,thick] ([yshift=1pt]n32.90) -- ([yshift=-1pt]n42.-90);
\draw [->,thick] ([yshift=1pt]n33.90) -- ([yshift=-1pt]n43.-90);
% layer 0 (input) variables
\node [anchor=west,snode,fill=red!30] (l01) at ([xshift=5.5em]n03.east) {};
\node [anchor=west,snode,fill=red!30] (l02) at ([xshift= \snodesep]l01.east) {};
\node [anchor=west,snode,fill=red!30] (l03) at ([xshift= \snodesep]l02.east) {};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (l01) (l03)] (inputvar) {};
\end{pgfonlayer}
\node [anchor=south] (l0label) at (inputvar.north) {\small{$\mathbf{x}$}};
\node [anchor=north west,inner sep=0] (l0labelbelow) at ([yshift=-0.5em]inputvar.south west) {\footnotesize{\bf{Layer 0 (Input)}}};
% layer 1 states
\node [anchor=south,snode,fill=blue!30] (l11) at ([yshift= \noderowsep+0.4em]l01.north) {};
\node [anchor=west,snode,fill=blue!30] (l12) at ([xshift= \snodesep]l11.east) {};
\node [anchor=west,snode,fill=blue!30] (l13) at ([xshift= \snodesep]l12.east) {};
\node [anchor=west,snode,fill=blue!30] (l14) at ([xshift= \snodesep]l13.east) {};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (l11) (l14)] (layer1var) {};
\end{pgfonlayer}
\node [anchor=west] (l1eqn) at ([xshift=0.1em]layer1var.east) {$= \rm{ReLU} \Big($};
\node [anchor=west,snode,fill=red!30] (l01copy) at ([xshift=0.3em]l1eqn.east) {};
\node [anchor=west,snode,fill=red!30] (l02copy) at ([xshift= \snodesep]l01copy.east) {};
\node [anchor=west,snode,fill=red!30] (l03copy) at ([xshift= \snodesep]l02copy.east) {};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (l01copy) (l03copy)] (inputvarcopy) {};
\end{pgfonlayer}
\node [anchor=west] (l1times) at ([xshift=0.1em]inputvarcopy.east) {\Large{$\times$}};
\node [anchor=west,snode,fill=gray!30] (w121) at ([xshift=2.0em]inputvarcopy.east) {};
\node [anchor=west,snode,fill=gray!30] (w122) at ([xshift=\snodesep]w121.east) {};
\node [anchor=west,snode,fill=gray!30] (w123) at ([xshift=\snodesep]w122.east) {};
\node [anchor=west,snode,fill=gray!30] (w124) at ([xshift=\snodesep]w123.east) {};
\node [anchor=north,snode,fill=gray!30] (w131) at ([yshift=-\snodesep]w121.south) {};
\node [anchor=west,snode,fill=gray!30] (w132) at ([xshift=\snodesep]w131.east) {};
\node [anchor=west,snode,fill=gray!30] (w133) at ([xshift=\snodesep]w132.east) {};
\node [anchor=west,snode,fill=gray!30] (w134) at ([xshift=\snodesep]w133.east) {};
\node [anchor=south,snode,fill=gray!30] (w111) at ([yshift=\snodesep]w121.north) {};
\node [anchor=west,snode,fill=gray!30] (w112) at ([xshift=\snodesep]w111.east) {};
\node [anchor=west,snode,fill=gray!30] (w113) at ([xshift=\snodesep]w112.east) {};
\node [anchor=west,snode,fill=gray!30] (w114) at ([xshift=\snodesep]w113.east) {};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (w111) (w134)] (w1box) {};
\end{pgfonlayer}
\node [anchor=north] (w1label) at (w1box.south) {\small{$\mathbf{w}_1$}};
\node [anchor=west] (l1eqn2) at ([xshift=0.3em]w124.east) {$\Big)$};
\node [anchor=north west,inner sep=0] (l1labelbelow) at ([yshift=-0.5em]layer1var.south west) {\footnotesize{\bf{Layer 1}}};
% layer 2 states
\node [anchor=south,snode,fill=ugreen!30] (l21) at ([yshift= \noderowsep+0.4em]l11.north) {};
\node [anchor=west,snode,fill=ugreen!30] (l22) at ([xshift= \snodesep]l21.east) {};
\node [anchor=west,snode,fill=ugreen!30] (l23) at ([xshift= \snodesep]l22.east) {};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (l21) (l23)] (layer2var) {};
\end{pgfonlayer}
\node [anchor=west] (l2eqn) at ([xshift=0.1em]layer2var.east) {$= \rm{Sigmoid} \Big($};
\node [anchor=west,snode,fill=blue!30] (l11copy) at ([xshift=0.3em]l2eqn.east) {};
\node [anchor=west,snode,fill=blue!30] (l12copy) at ([xshift= \snodesep]l11copy.east) {};
\node [anchor=west,snode,fill=blue!30] (l13copy) at ([xshift= \snodesep]l12copy.east) {};
\node [anchor=west,snode,fill=blue!30] (l14copy) at ([xshift= \snodesep]l13copy.east) {};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (l11copy) (l14copy)] (l1varcopy) {};
\end{pgfonlayer}
\node [anchor=west] (l2times) at ([xshift=0.1em]l1varcopy.east) {\Large{$\times$}};
\node [anchor=west,snode,fill=gray!30] (w221) at ([xshift=2.0em,yshift=0.5*\snodesep+0.35em]l1varcopy.east) {};
\node [anchor=west,snode,fill=gray!30] (w222) at ([xshift=\snodesep]w221.east) {};
\node [anchor=west,snode,fill=gray!30] (w223) at ([xshift=\snodesep]w222.east) {};
\node [anchor=south,snode,fill=gray!30] (w211) at ([yshift=\snodesep]w221.north) {};
\node [anchor=west,snode,fill=gray!30] (w212) at ([xshift=\snodesep]w211.east) {};
\node [anchor=west,snode,fill=gray!30] (w213) at ([xshift=\snodesep]w212.east) {};
\node [anchor=west,snode,fill=gray!30] (w231) at ([xshift=2.0em,yshift=-0.5*\snodesep-0.35em]l1varcopy.east) {};
\node [anchor=west,snode,fill=gray!30] (w232) at ([xshift=\snodesep]w231.east) {};
\node [anchor=west,snode,fill=gray!30] (w233) at ([xshift=\snodesep]w232.east) {};
\node [anchor=north,snode,fill=gray!30] (w241) at ([yshift=-\snodesep]w231.south) {};
\node [anchor=west,snode,fill=gray!30] (w242) at ([xshift=\snodesep]w241.east) {};
\node [anchor=west,snode,fill=gray!30] (w243) at ([xshift=\snodesep]w242.east) {};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (w211) (w243)] (w2box) {};
\end{pgfonlayer}
\node [anchor=west] (l2eqn2) at ([xshift=0.1em]w2box.east) {$\Big)$};
\node [anchor=north] (w2label) at (w2box.south) {\small{$\mathbf{w}_2$}};
\node [anchor=north west,inner sep=0] (l2labelbelow) at ([yshift=-0.5em]layer2var.south west) {\footnotesize{\bf{Layer 2}}};
% layer 3 states
\node [anchor=south,snode,fill=orange!30] (l31) at ([yshift= \noderowsep+0.4em]l21.north) {};
\node [anchor=west,snode,fill=orange!30] (l32) at ([xshift= \snodesep]l31.east) {};
\node [anchor=west,snode,fill=orange!30] (l33) at ([xshift= \snodesep]l32.east) {};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (l31) (l33)] (layer3var) {};
\end{pgfonlayer}
\node [anchor=west] (l3eqn) at ([xshift=0.1em]layer3var.east) {$= \rm{Softmax} \Big($};
\node [anchor=west,snode,fill=ugreen!30] (l21copy) at ([xshift=0.3em]l3eqn.east) {};
\node [anchor=west,snode,fill=ugreen!30] (l22copy) at ([xshift=\snodesep]l21copy.east) {};
\node [anchor=west,snode,fill=ugreen!30] (l23copy) at ([xshift=\snodesep]l22copy.east) {};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (l21copy) (l23copy)] (l2varcopy) {};
\end{pgfonlayer}
\node [anchor=west] (l3times) at ([xshift=0.1em]l2varcopy.east) {\Large{$\times$}};
\node [anchor=west,snode,fill=gray!30] (w321) at ([xshift=2.0em]l2varcopy.east) {};
\node [anchor=west,snode,fill=gray!30] (w322) at ([xshift=\snodesep]w321.east) {};
\node [anchor=west,snode,fill=gray!30] (w323) at ([xshift=\snodesep]w322.east) {};
\node [anchor=north,snode,fill=gray!30] (w331) at ([yshift=-\snodesep]w321.south) {};
\node [anchor=west,snode,fill=gray!30] (w332) at ([xshift=\snodesep]w331.east) {};
\node [anchor=west,snode,fill=gray!30] (w333) at ([xshift=\snodesep]w332.east) {};
\node [anchor=south,snode,fill=gray!30] (w311) at ([yshift=\snodesep]w321.north) {};
\node [anchor=west,snode,fill=gray!30] (w312) at ([xshift=\snodesep]w311.east) {};
\node [anchor=west,snode,fill=gray!30] (w313) at ([xshift=\snodesep]w312.east) {};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (w311) (w333)] (w3box) {};
\end{pgfonlayer}
\node [anchor=south] (w3label) at (w3box.north) {\small{$\mathbf{w}_3$}};
\node [anchor=north west] (l3sum) at ([yshift=-2.5em,xshift=-0.7em]l21copy.south) {\Large{$+$}};
\node [anchor=west,snode,fill=purple!30] (const1) at ([xshift=0.5em]l3sum.east) {\tiny{$\bf{1}$}};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (const1)] (const1box) {};
\end{pgfonlayer}
\node [anchor=west] (l3times2) at ([xshift=0.1em]const1box.east) {\Large{$\times$}};
\node [anchor=west,snode,fill=gray!30] (b31) at ([xshift=0.2em]l3times2.east) {};
\node [anchor=west,snode,fill=gray!30] (b32) at ([xshift=\snodesep]b31.east) {};
\node [anchor=west,snode,fill=gray!30] (b33) at ([xshift=\snodesep]b32.east) {};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (b31) (b33)] (b3box) {};
\end{pgfonlayer}
\node [anchor=west] (l3eqn2) at ([xshift=0.3em]b3box.east) {$\Big)$};
\node [anchor=south] (b3label) at ([yshift=-0.2em]b3box.north) {\small{$\mathbf{b}_3$}};
\node [anchor=north west,inner sep=0] (l3labelbelow) at ([yshift=-0.5em]layer3var.south west) {\footnotesize{\bf{Layer 3 (Output)}}};
% layer 3 (output) variables
\node [anchor=south,snode,fill=cyan!30] (l41) at ([yshift= \noderowsep+0.4em]l31.north) {};
\node [anchor=west,snode,fill=cyan!30] (l42) at ([xshift= \snodesep]l41.east) {};
\node [anchor=west,snode,fill=cyan!30] (l43) at ([xshift= \snodesep]l42.east) {};
\begin{pgfonlayer}{background}
\node [draw,inner sep=2pt,fill=white] [fit = (l41) (l43)] (outputvar) {};
\end{pgfonlayer}
\node [anchor=south] (l3label) at (outputvar.north) {\small{$\mathbf{y}$}};
% figure connections
\draw [->,thick,dotted] ([xshift=1em,yshift=2pt]inputvar.north) .. controls +(north:6em) and +(south:6em) .. ([yshift=-2pt]inputvarcopy.south);
\draw [->,thick,dotted] ([xshift=0,yshift=2pt]layer1var.north) .. controls +(north:6em) and +(south:6em) .. ([yshift=-2pt]l1varcopy.south);
\draw [->,thick,dotted] ([xshift=0,yshift=2pt]layer2var.north) .. controls +(north:6em) and +(south:5em) .. ([yshift=-2pt,xshift=-0.0em]l2varcopy.south);
\draw [->,thick,dotted] ([xshift=0,yshift=2pt]layer3var.north) -- ([yshift=-2pt,xshift=0]outputvar.south);
\end{tikzpicture}
\end{center}
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
%----------------------------------------------------------------------------------------
% variables
\def\nodesep{4em}
\def\noderowsep{3.0em}
\tikzstyle{nnode} = [circle,minimum size=3em,draw=FigureBlue,line width=1pt,fill=FigureMist]
%----------------------------------------------------------------------------------------
% layer 1 (hidden 1)
\node [anchor=center,nnode] (neuron) at (0,0) {};
\node [anchor=north] (x2) at ([yshift=-\noderowsep]neuron.south) {$x_2$};
\node [anchor=east] (x1) at ([xshift=-\nodesep]x2.west) {$x_1$};
\node [anchor=west] (x3) at ([xshift=\nodesep]x2.east) {$1$};
\node [anchor=south] (y) at ([yshift=\noderowsep]neuron.north) {$y$};
\draw [->,very thick] ([yshift=0pt]x1.north east) -- ([yshift=-2pt]neuron.-120) node [pos=0.5,left,xshift=-0.3em] (w1) {$w_1$};
\draw [->,very thick] ([yshift=1pt]x2.north) -- ([yshift=-2pt]neuron.-90) node [pos=0.5,left,xshift=0.0em] (w1) {$w_2$};
\draw [->,very thick] ([yshift=0pt]x3.north west) -- ([yshift=-2pt]neuron.-60) node [pos=0.5,right,xshift=0.5em] (b) {$b$};
\draw [->,very thick] ([yshift=2pt]neuron.north) -- ([yshift=0pt]y.south);
\node [anchor=north west] (label01) at ([xshift=6em,yshift=-2em]y.north east) {\textbf{neuron}: };
\node [anchor=north west] (eqn) at (label01.south west) {${\displaystyle y = \begin{cases} 1 & x_1 \cdot w_1 + x_2 \cdot w_2 + b> 0 \\ 0 & \textrm{otherwise} \end{cases}}$};
\begin{pgfonlayer}{background}
\node [draw=FigureGold!80!FigureInk,inner sep=4pt,fill=FigureWarm,line width=.7pt,rounded corners=2pt] [fit = (label01) (eqn)] (neuronbox) {};
\end{pgfonlayer}
\draw [->,FigureTeal,line width=.9pt,dotted] ([yshift=2em]neuronbox.west) .. controls +(west:4em) and +(east:5em) .. ([yshift=0.5em,xshift=-1em]neuron.east);
\end{tikzpicture}
\end{center}
\input{figures/figure-theme.tex}
\begin{tikzpicture}[aml figure,node distance = 0,scale = 1]
\amlfigurepalette
\begin{scope}[xshift=0cm,yshift=0cm]
\tikzstyle{txt} = [minimum width = 1.6cm,minimum height=1cm,text width=2cm,align=left]
\node(row1)[minimum width = 3.1cm, text width=2cm,align=right]{Tokens};
\node(row2)[minimum width = 3.1cm, below of = row1, yshift = -1cm,text width=2cm,align=right]{POS tags};
\node(row3)[minimum width = 3.1cm, below of = row2, yshift = -1.1cm,text width=2cm,align=right]{Chunk tags};
\node(text1_1)[txt]at([xshift = 0.9cm]row1.east){Most};
\node(text1_2)[txt]at([xshift = 0.2cm,yshift = -0.05cm]text1_1.east){are};
\node(text1_3)[txt]at([xshift = -0.1cm,yshift = 0.02cm]text1_2.east){expected};
\node(text1_4)[txt]at([xshift = 0.6cm,yshift = 0.02cm]text1_3.east){to};
\node(text1_5)[txt]at([xshift = -0.2cm,yshift = 0.02cm]text1_4.east){fall};
\node(text1_6)[txt]at([xshift = 0cm]text1_5.east){below};
\node(text1_7)[txt]at([xshift = 0.4cm,yshift = -0.2cm]text1_6.east){previous-month};
\node(text1_8)[txt]at([xshift = 0.6cm,yshift = 0.2cm]text1_7.east){levels};
\node(text1_9)[txt]at([xshift = 0.1cm,yshift = -0.08cm]text1_8.east){.};
\node(text2_1)[txt, below of = text1_1, yshift = -1cm]{JJS};
\node(text2_2)[txt, below of = text1_2, yshift = -0.95cm]{VBP};
\node(text2_3)[txt, below of = text1_3, yshift = -0.97cm]{VBN};
\node(text2_4)[txt, below of = text1_4, yshift = -0.99cm]{TO};
\node(text2_5)[txt, below of = text1_5, yshift = -1.01cm]{VB};
\node(text2_6)[txt, below of = text1_6, yshift = -1.01cm]{IN};
\node(text2_7)[txt, below of = text1_7, yshift = -0.81cm]{JJ};
\node(text2_8)[txt, below of = text1_8, yshift = -1.01cm]{NNS};
\node(text2_9)[txt, below of = text1_9, yshift = -0.93cm]{.};
\node(text3_1)[txt, below of = text2_1, yshift = -1cm]{B-NP};
\node(text3_2)[txt, below of = text2_2, yshift = -1cm]{B-VP};
\node(text3_3)[txt, below of = text2_3, yshift = -1cm]{I-VP};
\node(text3_4)[txt, below of = text2_4, yshift = -1cm]{I-VP};
\node(text3_5)[txt, below of = text2_5, yshift = -1cm]{I-VP};
\node(text3_6)[txt, below of = text2_6, yshift = -1cm]{B-PP};
\node(text3_7)[txt, below of = text2_7, yshift = -1cm]{B-NP};
\node(text3_8)[txt, below of = text2_8, yshift = -1cm]{I-NP};
\node(text3_9)[txt, below of = text2_9, yshift = -1cm]{O};
\node(text4_1)[txt, below of = text3_1, yshift = -0.4cm, xshift = 0.2cm]{\small{NP}};
\node(text4_2)[txt, below of = text3_3, yshift = -0.4cm, xshift = 0.9cm]{\small{VP}};
\node(text4_3)[txt, below of = text3_6, yshift = -0.4cm, xshift = 0.2cm]{\small{PP}};
\node(text4_4)[txt, below of = text3_7, yshift = -0.4cm, xshift = 1cm]{\small{NP}};
\begin{pgfonlayer}{background}
\node[fit=(text1_1)(text1_9),fill=FigureMist,rounded corners=2pt,inner sep=3pt] {};
\node[fit=(text2_1)(text2_9),fill=FigureBlue!11,rounded corners=2pt,inner sep=3pt] {};
\node[fit=(text3_1)(text3_9),fill=FigureTeal!12,rounded corners=2pt,inner sep=3pt] {};
\end{pgfonlayer}
\node(sign1)at([xshift = -0.4cm]row1.east){:};
\node(sign2)at([xshift = -0.4cm]row2.east){:};
\node(sign3)at([xshift = -0.4cm,yshift = 0.0cm]row3.east){:};
\draw[-,FigureGold,line width=1.2pt]([xshift=-1.1cm,yshift=0.3cm]text3_1.south)to([xshift=-0.1cm,yshift=0.3cm]text3_1.south);
\draw[-,FigureGold,line width=1.2pt]([xshift=-1.1cm,yshift=0.3cm]text3_2.south)to([xshift=-0.2cm,yshift=0.3cm]text3_5.south);
\draw[-,FigureGold,line width=1.2pt]([xshift=-1.1cm,yshift=0.3cm]text3_6.south)to([xshift=-0.1cm,yshift=0.3cm]text3_6.south);
\draw[-,FigureGold,line width=1.2pt]([xshift=-1.cm,yshift=0.3cm]text3_7.south)to([xshift=-0.1cm,yshift=0.3cm]text3_8.south);
\end{scope}
\end{tikzpicture}
% !Mode:: "TeX:UTF-8"
% !TEX encoding = UTF-8 Unicode
\input{figures/figure-theme.tex}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
\def\nodewidth{0.55cm}
\def\nodeheight{0.55cm}
\tikzstyle{enode} = [minimum width=\nodewidth,minimum height=\nodeheight,inner sep=2pt,draw=FigureLine,fill=FigureMist,line width=.45pt,rounded corners=1pt]
%%% pre-training encoders
\begin{scope}
\node [enode,anchor=west] (w0) at (0,0) {$x_0$};
\node [enode,anchor=west] (w1) at ([xshift=\nodewidth]w0.east) {$x_1$};
\node [enode,anchor=west] (w2) at ([xshift=\nodewidth]w1.east) {$x_2$};
\node [enode,anchor=west] (w3) at ([xshift=\nodewidth]w2.east) {$x_3$};
\node [enode,anchor=west] (w4) at ([xshift=\nodewidth]w3.east) {$x_4$};
\node [enode,anchor=north] (w3mask) at ([yshift=0.4em]w3.south) {\footnotesize{(masked)}};
\node [enode,anchor=south] (e0) at ([yshift=\nodeheight]w0.north) {$\mathbf{e}_0$};
\node [enode,anchor=west] (e1) at ([xshift=\nodewidth]e0.east) {$\mathbf{e}_1$};
\node [enode,anchor=west] (e2) at ([xshift=\nodewidth]e1.east) {$\mathbf{e}_2$};
\node [enode,anchor=west] (e3) at ([xshift=\nodewidth]e2.east) {$\mathbf{e}_3$};
\node [enode,anchor=west] (e4) at ([xshift=\nodewidth]e3.east) {$\mathbf{e}_4$};
\draw [->] (w0.north) -- ([yshift=0]e0.south);
\draw [->] (w1.north) -- ([yshift=0]e1.south);
\draw [->] (w2.north) -- ([yshift=0]e2.south);
\draw [->] (w3.north) -- ([yshift=0]e3.south);
\draw [->] (w4.north) -- ([yshift=0]e4.south);
\node [anchor=south,minimum width=10*\nodewidth,minimum height=2*\nodeheight,draw=FigureBlue,fill=FigureBlue!13,line width=.8pt,rounded corners=2pt] (encoder) at ([yshift=\nodeheight]e2.north) {\large{Encoder}};
\draw [->] ([yshift=0pt]e0.north) -- ([yshift=\nodeheight-2pt]e0.north);
\draw [->] ([yshift=0pt]e1.north) -- ([yshift=\nodeheight-2pt]e1.north);
\draw [->] ([yshift=0pt]e2.north) -- ([yshift=\nodeheight-2pt]e2.north);
\draw [->] ([yshift=0pt]e3.north) -- ([yshift=\nodeheight-2pt]e3.north);
\draw [->] ([yshift=0pt]e4.north) -- ([yshift=\nodeheight-2pt]e4.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e0.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e0.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e1.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e1.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e2.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e2.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e3.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e3.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e4.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e4.north);
\node [anchor=south,minimum width=10*\nodewidth,minimum height=\nodeheight,draw=FigureGold,fill=FigureWarm,line width=.8pt,rounded corners=2pt] (softmax) at ([yshift=1.2*\nodeheight]encoder.north) {Softmax};
\node [anchor=south west] (supervision1) at ([yshift=1.5*\nodeheight]softmax.north west) {\footnotesize{model reconstructs the masked token}};
\node [anchor=south west] (supervision2) at ([yshift=-0.4em]supervision1.north west) {\footnotesize{E.g., evaluate how well the}};
\node [anchor=south west] (supervision3) at ([yshift=-0.3em]supervision2.north west) {Self-supervision};
\draw [->,thick] ([yshift=1.5*\nodeheight,xshift=-0.8cm]softmax.north) .. controls +(south:0.6cm) and +(north:0.7cm) .. ([yshift=2pt]softmax.north);
\node [anchor=north] (caption) at ([yshift=-\nodeheight]w2.south) {\small{(a) Pre-training}};
\end{scope}
%%% applying pre-trained encoders
\begin{scope}[xshift=7cm]
\node [enode,anchor=west] (w0) at (0,0) {$x_0$};
\node [enode,anchor=west] (w1) at ([xshift=\nodewidth]w0.east) {$x_1$};
\node [enode,anchor=west] (w2) at ([xshift=\nodewidth]w1.east) {$x_2$};
\node [enode,anchor=west] (w3) at ([xshift=\nodewidth]w2.east) {$x_3$};
\node [enode,anchor=west] (w4) at ([xshift=\nodewidth]w3.east) {$x_4$};
\node [enode,anchor=south] (e0) at ([yshift=\nodeheight]w0.north) {$\mathbf{e}_0$};
\node [enode,anchor=west] (e1) at ([xshift=\nodewidth]e0.east) {$\mathbf{e}_1$};
\node [enode,anchor=west] (e2) at ([xshift=\nodewidth]e1.east) {$\mathbf{e}_2$};
\node [enode,anchor=west] (e3) at ([xshift=\nodewidth]e2.east) {$\mathbf{e}_3$};
\node [enode,anchor=west] (e4) at ([xshift=\nodewidth]e3.east) {$\mathbf{e}_4$};
\draw [->] (w0.north) -- ([yshift=0]e0.south);
\draw [->] (w1.north) -- ([yshift=0]e1.south);
\draw [->] (w2.north) -- ([yshift=0]e2.south);
\draw [->] (w3.north) -- ([yshift=0]e3.south);
\draw [->] (w4.north) -- ([yshift=0]e4.south);
\node [anchor=south,minimum width=10*\nodewidth,minimum height=2*\nodeheight,draw=FigureTeal,fill=FigureTeal!13,line width=.8pt,rounded corners=2pt] (encoder) at ([yshift=\nodeheight]e2.north) {\large{Pre-trained Encoder}};
\draw [->] ([yshift=0]e0.north) -- ([yshift=\nodeheight-2pt]e0.north);
\draw [->] ([yshift=0]e1.north) -- ([yshift=\nodeheight-2pt]e1.north);
\draw [->] ([yshift=0]e2.north) -- ([yshift=\nodeheight-2pt]e2.north);
\draw [->] ([yshift=0]e3.north) -- ([yshift=\nodeheight-2pt]e3.north);
\draw [->] ([yshift=0]e4.north) -- ([yshift=\nodeheight-2pt]e4.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e0.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e0.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e1.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e1.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e2.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e2.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e3.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e3.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e4.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e4.north);
\node [anchor=south,minimum width=10*\nodewidth,minimum height=\nodeheight,draw=FigureCoral,fill=FigureCoral!12,line width=.8pt,rounded corners=2pt] (softmax) at ([yshift=1.2*\nodeheight]encoder.north) {Prediction Network};
\node [anchor=south] (supervision1) at ([yshift=1.5*\nodeheight]softmax.north) {Output for Downstream Tasks};
\draw [->,thick] ([yshift=2pt]softmax.north) -- (supervision1.south);
\node [anchor=north] (caption) at ([yshift=-\nodeheight]w2.south) {\small{(b) Applying the Pre-trained Encoder}};
\end{scope}
\end{tikzpicture}
% !Mode:: "TeX:UTF-8"
% !TEX encoding = UTF-8 Unicode
\input{figures/figure-theme.tex}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
\tikzstyle{cnode} = [minimum width=6em,minimum height=4em,inner sep=5pt,fill=FigureWarm,rounded corners=2pt]
\tikzstyle{snode} = [minimum width=8em,minimum height=8em,inner sep=5pt,fill=FigureMist,rounded corners=3pt]
%% embedding table
\node [cnode,anchor=center,draw=FigureCoral,line width=.9pt] (C) at (0,0) {{\color{FigureInk} $\mathbf{C} = \scriptsize{\begin{bmatrix} \bullet & \bullet \\ \bullet & \bullet \\ \bullet & \bullet \end{bmatrix}}$}};
%% system A
\node [snode,anchor=south,draw=FigureBlue,line width=1pt] (systemA) at ([yshift=-0.6em]C.south) {};
\node [anchor=south] (systemAfunc) at ([yshift=0.7em]C.north) {\Large{$F_{\theta,\mathbf{C}}(\cdot)$}};
\node [anchor=south] (systemAlabel) at ([yshift=0.2em]systemA.north) {\small{via language modeling}};
\node [anchor=south] (systemAlabel2) at ([yshift=-0.2em]systemAlabel.north) {\small{Optimizing $\mathbf{C}$ and $\theta$}};
\node [anchor=north] (systemAcaption) at ([yshift=-0.5em]systemA.south) {{\large Training Word Vectors}};
%% optimized embedding table
\node [cnode,anchor=west,draw=FigureCoral,line width=.9pt] (oC) at ([xshift=12em]C.east) {{\color{FigureInk} $\widehat{\mathbf{C}} = \scriptsize{\begin{bmatrix} \bullet & \bullet \\ \bullet & \bullet \\ \bullet & \bullet \end{bmatrix}}$}};
%% system B
\node [snode,anchor=south,draw=FigureTeal,line width=1pt] (systemB) at ([yshift=-0.6em]oC.south) {};
\node [anchor=south] (systemBfunc) at ([yshift=0.7em]oC.north) {\Large{$G_{\pi,\widehat{\mathbf{C}}}(\cdot)$}};
\node [anchor=south] (systemBlabel) at ([yshift=0.3em]systemB.north) {\small{on the new task}};
\node [anchor=south] (systemBlabel2) at ([yshift=-0.2em]systemBlabel.north) {\small{Using optimized $\widehat{\mathbf{C}}$}};
\node [anchor=north] (systemBcaption) at ([yshift=-0.5em]systemB.south) {{\large Applying Word Vectors}};
%% arrow
\draw [->,very thick,dotted] ([xshift=0.2em]C.east) -- ([xshift=-0.2em]oC) node [pos=0.5,above] {\scriptsize{optimized word vectors $\widehat{\mathbf{C}}$}};
\end{tikzpicture}
\begin{center}
\begin{tikzpicture}[scale=1]
\begin{axis}[
xlabel near ticks,
width=10cm, height=6cm,
xlabel={Performance},
xmin=0,xmax=10,
ymin=0,ymax=3.5,
xmajorticks=false,
ymajorticks=false,
legend style={xshift=-1em,yshift=-0.8em,legend cell align=left},
]
\addplot[color=blue,only marks,mark size=1.0pt,] table {figures/nlpbook/data/f1.dat};
\addplot[color=blue,fill=blue!50,opacity=0.5] table {figures/nlpbook/data/ff1.dat};
\addplot[color=blue,only marks,mark size=2.0pt] coordinates {(3.5,1)};
\addplot[color=blue!50] coordinates {(3.5,0.4)(3.5,1)};
\addplot[color=red,only marks,mark size=1.0pt,] table {figures/nlpbook/data/f2.dat};
\addplot[color=red,fill=red!50,opacity=0.5] table {figures/nlpbook/data/ff2.dat};
\addplot[color=red,only marks,mark size=2.0pt] coordinates {(2.5,2)};
\addplot[color=red!50] coordinates {(2.5,0.4)(2.5,2)};
\addplot[color=ugreen,only marks,mark size=1.0pt,] table {figures/nlpbook/data/f3.dat};
\addplot[color=ugreen,fill=ugreen!50,opacity=0.5] table {figures/nlpbook/data/ff3.dat};
\addplot[color=ugreen,only marks,mark size=2.0pt] coordinates {(7,3)};
\addplot[color=ugreen!50] coordinates {(7,0.4)(7,3)};
\end{axis}
\node[inner sep=0pt] (a) at (-1,1.25){system {\color{blue} A}};
\node[anchor=south,inner sep=0pt] (b) at ([yshift=2.3em]a.north){system {\color{red} B}};
\node[anchor=south,inner sep=0pt] (b) at ([yshift=2.3em]b.north){system {\color{ugreen} C}};
\end{tikzpicture}
\end{center}
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
%----------------------------------------------------------------------------------------
% variables
\def\nodesep{1.7em}
\def\noderowsep{2.5em}
\tikzstyle{nnode} = [minimum width=1.5em,fill=FigureBlue!62,inner sep=0pt,label=above:#1,rounded corners=.8pt]
\begin{scope}
\node[nnode=0,minimum height=0.05em] (n1) at (0,0){};
\node[nnode=0,minimum height=0.05em,anchor=south] (n2) at ([xshift=\nodesep]n1.south){};
\node[nnode=1,minimum height=4.1em,anchor=south] (n3) at ([xshift=\nodesep]n2.south){};
\node[nnode=0,minimum height=0.05em,anchor=south] (n4) at ([xshift=\nodesep]n3.south){};
\node[nnode=0,minimum height=0.05em,anchor=south] (n5) at ([xshift=\nodesep]n4.south){};
\node[nnode=0,minimum height=0.05em,anchor=south] (n6) at ([xshift=\nodesep]n5.south){};
\node[nnode=0,minimum height=0.05em,anchor=south] (n7) at ([xshift=\nodesep]n6.south){};
\node[nnode=0,minimum height=0.05em,anchor=south] (n8) at ([xshift=\nodesep]n7.south){};
\node[nnode=0,minimum height=0.05em,anchor=south] (n9) at ([xshift=\nodesep]n8.south){};
\node[nnode=0,minimum height=0.05em,anchor=south] (n10) at ([xshift=\nodesep]n9.south){};
\node[anchor=east] at ([xshift=-1em,yshift=0.2em]n1.west){$p_k$};
\end{scope}
\begin{scope}[yshift=-10em]
\node[nnode=.02,minimum height=0.2em,fill=black!50] (n1) at (0,0){};
\node[nnode=.02,minimum height=0.2em,anchor=south,fill=black!50] (n2) at ([xshift=\nodesep]n1.south){};
\node[nnode,minimum height=3.1em,anchor=south] (n31) at ([xshift=\nodesep]n2.south){};
\node[nnode,minimum height=0.2em,anchor=south,fill=black!50] (n32) at ([xshift=\nodesep]n2.south){};
\node[nnode=.82,minimum height=1em,minimum width=1.45em,anchor=south,fill=white,draw=blue!40,dash pattern=on 2pt off 1pt] (n33) at ([xshift=\nodesep,yshift=3.1em]n2.south){};
\node[nnode=.02,minimum height=0.2em,anchor=south,fill=black!50] (n4) at ([xshift=\nodesep]n31.south){};
\node[nnode=.02,minimum height=0.2em,anchor=south,fill=black!50] (n5) at ([xshift=\nodesep]n4.south){};
\node[nnode=.02,minimum height=0.2em,anchor=south,fill=black!50] (n6) at ([xshift=\nodesep]n5.south){};
\node[nnode=.02,minimum height=0.2em,anchor=south,fill=black!50] (n7) at ([xshift=\nodesep]n6.south){};
\node[nnode=.02,minimum height=0.2em,anchor=south,fill=black!50] (n8) at ([xshift=\nodesep]n7.south){};
\node[nnode=.02,minimum height=0.2em,anchor=south,fill=black!50] (n9) at ([xshift=\nodesep]n8.south){};
\node[nnode=.02,minimum height=0.2em,anchor=south,fill=black!50] (n10) at ([xshift=\nodesep]n9.south){};
\node[anchor=east] at ([xshift=-1em,yshift=0.2em]n1.west){$\hat{p}_k(\epsilon=0.2)$};
\node[inner sep=0pt,anchor=west] (eps) at ([xshift=2em,yshift=1em]n33.east){$\epsilon$};
\node[inner sep=0pt,anchor=west] (arrow) at ([xshift=0.2em]eps.east){$\to$};
\node[minimum width=1.5em, minimum height=0.2em, inner sep=0pt,fill=black!50,anchor=west] (n1) at ([xshift=0.5em,]arrow.east) {};
\node[minimum width=1.2em, minimum height=0.2em, inner sep=0pt,anchor=west] (n2) at ([xshift=0.5em]n1.east) {...};
\node[minimum width=1.5em, minimum height=0.2em, inner sep=0pt,fill=black!50,anchor=west] (n3) at ([xshift=0.5em]n2.east) {};
\draw[decorate,decoration={brace,amplitude=3mm}] ([yshift=0.1em]n1.north west) -- node[yshift=1.5em] {$\frac{\epsilon}{n} \times n$}([yshift=0.1em]n3.north east);
\begin{pgfonlayer}{background}
\node [draw=FigureGold!80!FigureInk,inner sep=6pt,fill=FigureWarm,line width=.7pt,rounded corners=2pt,minimum height=3em,yshift=1em] [fit = (eps) (n3)] (box) {};
\end{pgfonlayer}
\draw [->, thick,dotted] (n33.center) .. controls +(east:2em) and +(west:1.5em) .. ([xshift=-0.1em]box.west);
\draw [->, thick,dotted] ([yshift=-0.1em]n3.south) .. controls +(south:2em) and +(north:2em) .. ([yshift=1.2em]n10.north);
\end{scope}
\end{tikzpicture}
\end{center}
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
%----------------------------------------------------------------------------------------
% variables
\def\rowsep{1.0em}
\def\colsep{1.0em}
\tikzstyle{cnode} = [rectangle,inner sep=1pt,minimum width=1.8em,minimum height=1.8em,draw=FigureInk!65,line width=.7pt,fill=FigureMist,rounded corners=2pt]
%----------------------------------------------------------------------------------------
% queries, keys, and values
\begin{scope}
% queries
\node [cnode,anchor=west] (q1base) at (0,0) {$\mathbf{q}_1$};
\node [cnode,anchor=west] (q2base) at ([xshift=\colsep]q1base.east) {$\mathbf{q}_2$};
\node [cnode,anchor=west] (q3base) at ([xshift=\colsep]q2base.east) {$\mathbf{q}_3$};
\node [anchor=south] (qlabel) at ([yshift=0.3em]q2base.north) {Queries (e.g., $\{\mathbf{s}_1,...,\mathbf{s}_n\}$)};
\draw[decoration={brace,amplitude=6pt},decorate,thick] ([yshift=-3pt]q3base.south east) -- ([yshift=-3pt]q1base.south west);
\node [anchor=north] (qdef) at ([yshift=-\rowsep]q2base.south east) {$\mathbf{Q}= \begin{bmatrix} \colorbox{blue!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{q}_1$}} \\ \colorbox{blue!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{q}_2$}} \\ \colorbox{blue!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{q}_3$}} \end{bmatrix}$};
% keys
\node [cnode,anchor=west] (k1base) at ([xshift=5*\colsep]q3base.east) {$\mathbf{k}_1$};
\node [cnode,anchor=west] (k2base) at ([xshift=\colsep]k1base.east) {$\mathbf{k}_2$};
\node [cnode,anchor=west] (k3base) at ([xshift=\colsep]k2base.east) {$\mathbf{k}_3$};
\node [anchor=south] (klabel) at ([yshift=0.3em]k2base.north) {Keys (e.g., $\{\mathbf{h}_1,...,\mathbf{h}_m\}$)};
\draw[decoration={brace,amplitude=6pt},decorate,thick] ([yshift=-3pt]k3base.south east) -- ([yshift=-3pt]k1base.south west);
\node [anchor=north] (kdef) at ([yshift=-\rowsep]k2base.south east) {$\mathbf{K}= \begin{bmatrix} \colorbox{red!30}{\parbox{1.4cm}{\hspace{0.5cm}\scriptsize{$\mathbf{k}_1$}}} \\ \colorbox{red!30}{\parbox{1.4cm}{\hspace{0.5cm}\scriptsize{$\mathbf{k}_2$}}} \\ \colorbox{red!30}{\parbox{1.4cm}{\hspace{0.5cm}\scriptsize{$\mathbf{k}_3$}}} \end{bmatrix}$};
% values
\node [cnode,anchor=west] (v1base) at ([xshift=5*\colsep]k3base.east) {$\mathbf{v}_1$};
\node [cnode,anchor=west] (v2base) at ([xshift=\colsep]v1base.east) {$\mathbf{v}_2$};
\node [cnode,anchor=west] (v3base) at ([xshift=\colsep]v2base.east) {$\mathbf{v}_3$};
\node [anchor=south] (vlabel) at ([yshift=0.3em]v2base.north) {Values (e.g., $\{\mathbf{h}_1,...,\mathbf{h}_m\}$)};
\draw[decoration={brace,amplitude=6pt},decorate,thick] ([yshift=-3pt]v3base.south east) -- ([yshift=-3pt]v1base.south west);
\node [anchor=north] (vdef) at ([yshift=-\rowsep]v2base.south east) {$\mathbf{V}= \begin{bmatrix} \colorbox{green!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{v}_1$}} \\ \colorbox{green!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{v}_2$}} \\ \colorbox{green!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{v}_3$}} \end{bmatrix}$};
% Q K^T
\node[anchor=north east] (Q) at ([yshift=-5*\rowsep,xshift=-0.3cm]qdef.south east) {$\begin{matrix} \colorbox{blue!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{q}_1$}} \\ \colorbox{blue!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{q}_2$}} \\ \colorbox{blue!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{q}_3$}} \end{matrix}$};
\node [anchor=west] (otimes) at ([xshift=1em]Q.east) {\LARGE{$\times$}};
\node[anchor=west,minimum height=1.5cm,minimum width=0.3cm,inner sep=1pt,fill=red!30] (K1) at ([xshift=1em]otimes.east) {$\mathbf{k}_1^{\mathrm{T}}$};
\node[anchor=west,minimum height=1.5cm,minimum width=0.3cm,inner sep=1pt,fill=red!30] (K2) at ([xshift=0.3em]K1.east) {$\mathbf{k}_2^{\mathrm{T}}$};
\node[anchor=west,minimum height=1.5cm,minimum width=0.3cm,inner sep=1pt,fill=red!30] (K3) at ([xshift=0.3em]K2.east) {$\mathbf{k}_3^{\mathrm{T}}$};
\node [anchor=west] (oeq) at ([xshift=1em]K3.east) {\LARGE{$=$}};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK21) at ([xshift=1em]oeq.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK22) at ([xshift=0.3em]QK21.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK23) at ([xshift=0.3em]QK22.east) {};
\node[anchor=south,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK11) at ([yshift=0.3em]QK21.north) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK12) at ([xshift=0.3em]QK11.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK13) at ([xshift=0.3em]QK12.east) {};
\node[anchor=north,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK31) at ([yshift=-0.3em]QK21.south) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK32) at ([xshift=0.3em]QK31.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK33) at ([xshift=0.3em]QK32.east) {};
\draw [<-,thick,dotted] ([yshift=1pt]Q.north) -- ([yshift=5*\rowsep + 1pt]Q.north);
\draw [<-,thick,dotted] ([yshift=1pt]K2.north) .. controls +(north:1.6cm) and +(south:1.6cm) .. ([yshift=0.3pt,xshift=0.5cm]kdef.south);
\node [anchor=south] (QKLabel) at (QK12.north) {$\mathbf{Q} \mathbf{K}^{\mathrm{T}}$};
\node [anchor=north] (QKexample) at ([yshift=-0.5em]QK33.south) {\scriptsize{$\mathbf{q}_3 \mathbf{k}_3^{\mathrm{T}}$}};
\draw [->] ([yshift=-1pt]QKexample.north) -- (QK33.center);
% Softmax
\node[anchor=north,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK21b) at ([yshift=-7*\rowsep]K1.south) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK22b) at ([xshift=0.3em]QK21b.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK23b) at ([xshift=0.3em]QK22b.east) {};
\node[anchor=south,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK11b) at ([yshift=0.3em]QK21b.north) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK12b) at ([xshift=0.3em]QK11b.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK13b) at ([xshift=0.3em]QK12b.east) {};
\node[anchor=north,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK31b) at ([yshift=-0.3em]QK21b.south) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK32b) at ([xshift=0.3em]QK31b.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=gray!30] (QK33b) at ([xshift=0.3em]QK32b.east) {};
\draw [very thick] ([yshift=-0.4em,xshift=-0.2em]QK31b.south west) -- ([yshift=-0.4em,xshift=0.2em]QK33b.south east);
\node [anchor=north] (beta) at ([yshift=-0.4em]QK32b.south) {\Large{$\beta$}};
\node [anchor=east] (softmax) at (QK31b.south west) {\Large{$\mathrm{Softmax}\Big($}};
\node [anchor=west] (softamx2) at (QK33b.south east) {\Large{$\Big)\ \ =$}};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK21c) at ([xshift=1em]softamx2.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK22c) at ([xshift=0.3em]QK21c.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK23c) at ([xshift=0.3em]QK22c.east) {};
\node[anchor=south,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK11c) at ([yshift=0.3em]QK21c.north) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK12c) at ([xshift=0.3em]QK11c.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK13c) at ([xshift=0.3em]QK12c.east) {};
\node[anchor=north,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK31c) at ([yshift=-0.3em]QK21c.south) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK32c) at ([xshift=0.3em]QK31c.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK33c) at ([xshift=0.3em]QK32c.east) {};
\node [anchor=south] (alphalabel) at (QK12c.north) {\LARGE{$\alpha$}};
\node [anchor=north] (alphaexample) at ([yshift=-0.5em]QK33c.south) {\scriptsize{$\alpha_{3,3}$}};
\draw [->] ([yshift=-1pt]alphaexample.north) -- (QK33c.center);
\draw [<-,thick,dotted] ([yshift=1pt]QK12b.north) .. controls +(north:2.0cm) and +(south:2cm) .. ([yshift=-1pt,xshift=0cm]QK31.south);
% results
\node [anchor=north] (V) at ([yshift=-7*\rowsep]QK32b.south) {$\begin{matrix} \colorbox{green!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{v}_1$}} \\ \colorbox{green!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{v}_2$}} \\ \colorbox{green!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{v}_3$}} \end{matrix}$};
\node [anchor=east] (otimes2) at ([xshift=-0.8em]V.west) {\LARGE{$\times$}};
\node[anchor=east,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK23d) at ([xshift=-1em]otimes2.west) {};
\node[anchor=east,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK22d) at ([xshift=-0.3em]QK23d.west) {};
\node[anchor=east,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK21d) at ([xshift=-0.3em]QK22d.west) {};
\node[anchor=south,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK11d) at ([yshift=0.3em]QK21d.north) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK12d) at ([xshift=0.3em]QK11d.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK13d) at ([xshift=0.3em]QK12d.east) {};
\node[anchor=north,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK31d) at ([yshift=-0.3em]QK21d.south) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK32d) at ([xshift=0.3em]QK31d.east) {};
\node[anchor=west,minimum height=0.4cm,minimum width=0.4cm,inner sep=1pt,fill=orange!30] (QK33d) at ([xshift=0.3em]QK32d.east) {};
\node [anchor=west] (oeq2) at ([xshift=1em]V.east) {\LARGE{$=$}};
\node [anchor=west] (cvectors) at ([xshift=1em]oeq2.east) {$\begin{matrix} \colorbox{purple!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{c}_1$}} \\ \colorbox{purple!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{c}_2$}} \\ \colorbox{purple!30}{\parbox{1.4cm}{\hspace{0.5cm}$\mathbf{c}_3$}} \end{matrix}$};
\node [anchor=south] (resultlabel) at (cvectors.north) {Returned Values $\mathbf{C}$};
\draw [<-,thick,dotted] ([yshift=-1pt,xshift=1em]V.north) .. controls +(north:2.0cm) and +(south:3.5cm) .. ([yshift=-7cm,xshift=0.0cm]vdef.south) -- ([yshift=0.3em,xshift=0.0cm]vdef.south);
\draw [<-,thick,dotted] ([yshift=1pt]QK12d.north) .. controls +(north:2.0cm) and +(south:1.2cm) .. ([yshift=-1pt,xshift=0cm]QK31c.south);
\node [anchor=west] (cvectorlabel) at ([xshift=1em]cvectors.east) {$\mathbf{c}_i = \sum_{j=1}^{3} \alpha_{i,j} \mathbf{v}_j$};
\end{scope}
\end{tikzpicture}
\end{center}
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure,scale=1]
\amlfigurepalette
\pgfmathsetseed{10}
\begin{scope}
\node [anchor=south west,minimum height=1em,minimum width=1em,inner sep=0.1pt] (n1) at (0,0) {};
\end{scope}
\begin{scope}[xshift=0cm]
\begin{axis}[
aml axis,
width=5.1cm, height=5.1cm,
title={Underfitting},
xlabel={$x_1$},
ylabel={$x_2$},
y label style={at={(axis description cs:0.25,.5)},anchor=south},
xmin=0, xmax=8,
ymin=0, ymax=8,
xtick={0,2,4,6,8},
ytick={0,2,4,6,8},
]
\addplot[only marks,color=blue,very thick, mark=square,samples=12, domain=0.5:7.5,]{(0.5*x+3+rand};
\addplot[only marks,color=red, very thick, mark=x,samples=12, domain=0.5:7.5, mark size=4pt]{(0.5*x+0.6+rand};
\addplot[only marks,color=blue,very thick, mark=square,samples=12, domain=1:7]{(0.5*x+5+rand};
\addplot[only marks,color=red, very thick, mark=x,samples=12, domain=1:7, mark size=4pt]{(0.5*x-1.4+rand};
\addplot[color=red, very thick, mark=x,mark size=4pt,draw=none] coordinates {(1.3,1.9)(1.4,3.4)(4.8,5.6)(7.4,5)};
\addplot[color=blue, very thick, mark=square] coordinates {(3.4,2.4)};
\addplot[color=black, very thick, smooth] coordinates {(0,1.4)(3,1.8)(6,3)(8,7)};
\end{axis}
\end{scope}
\begin{scope}[xshift=4.8cm]
\pgfmathsetseed{10}
\begin{axis}[
aml axis,
width=5.1cm, height=5.1cm,
title={Fitting just right},
xlabel={$x_1$},
ylabel={$x_2$},
y label style={at={(axis description cs:0.25,.5)},anchor=south},
xmin=0, xmax=8,
ymin=0, ymax=8,
xtick={0,2,4,6,8},
ytick={0,2,4,6,8},
]
\addplot[only marks,color=blue, very thick, mark=square,samples=12, domain=0.5:7.5,]{(0.5*x+3+rand};
\addplot[only marks,color=red, very thick, mark=x,samples=12, domain=0.5:7.5, mark size=4pt]{(0.5*x+0.6+rand};
\addplot[only marks,color=blue,very thick, mark=square,samples=12, domain=1:7,]{(0.5*x+5+rand};
\addplot[only marks,color=red, very thick, mark=x,samples=12, domain=1:7, mark size=4pt]{(0.5*x-1.4+rand};
\addplot[color=red, very thick, mark=x,mark size=4pt,draw=none] coordinates {(1.3,1.9)(1.4,3.4)(4.8,5.6)(7.4,5)};
\addplot[color=blue, very thick, mark=square] coordinates {(3.4,2.4)};
\addplot[color=black, very thick, smooth] coordinates {(0,2.5)(3,3)(5,3.9)(8,6.2)};
\end{axis}
\end{scope}
\begin{scope}[xshift=9.6cm]
\pgfmathsetseed{10}
\begin{axis}[
aml axis,
width=5.1cm, height=5.1cm,
title={Overfitting},
xlabel={$x_1$},
ylabel={$x_2$},
y label style={at={(axis description cs:0.25,.5)},anchor=south},
xmin=0, xmax=8,
ymin=0, ymax=8,
xtick={0,2,4,6,8},
ytick={0,2,4,6,8},
]
\addplot[only marks,color=blue,very thick, mark=square,samples=12, domain=0.5:7.5,]{(0.5*x+3+rand};
\addplot[only marks,color=red, very thick, mark=x,samples=12, domain=0.5:7.5, mark size=4pt]{(0.5*x+0.6+rand};
\addplot[only marks,color=blue,very thick, mark=square,samples=12, domain=1:7,, ]{(0.5*x+5+rand};
\addplot[only marks,color=red, very thick, mark=x,samples=12, domain=1:7, mark size=4pt]{(0.5*x-1.4+rand};
\addplot[color=red, very thick, mark=x,mark size=4pt,draw=none] coordinates {(1.3,1.9)(1.4,3.4)(4.8,5.6)(7.4,5)};
\addplot[color=blue, very thick, mark=square, ] coordinates {(3.4,2.4)};
\addplot[color=black, very thick, smooth] coordinates {(0,1.4)(0.8,1.8)(1,3.8)(1.6,4)(3,2)(3.8,2.2)(4,3)(5,3.6)(5.6,4.8)(4.2,5.4)(4.4,6)(5,6)(6,5.2)(7,5.6)(8,6.4)};
\end{axis}
\end{scope}
\end{tikzpicture}
\end{center}
% !Mode:: "TeX:UTF-8"
% !TEX encoding = UTF-8 Unicode
\input{figures/figure-theme.tex}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
\def\nodewidth{1.8cm}
\def\nodeheight{0.8cm}
\tikzstyle{enode} = [minimum width=\nodewidth,minimum height=\nodeheight,inner sep=1pt,draw=FigureInk!65,line width=.7pt,rounded corners=2pt,fill=FigureMist]
%%% unsupervised pre-training
\begin{scope}
\node [enode,anchor=west,very thick] (unsupervisednode1) at (0,0) {\scriptsize{Unsupervised}};
\node [enode,anchor=west,very thick] (unsupervisednode2) at ([xshift=0.3cm]unsupervisednode1.east) {\footnotesize{Supervised}};
\draw [->,thick] ([xshift=1pt]unsupervisednode1.east) -- ([xshift=-1pt]unsupervisednode2.west);
\node [anchor=south] (node1label) at (unsupervisednode1.north) {\footnotesize{Pre-training}};
\node [anchor=south] (node2label) at (unsupervisednode2.north) {\footnotesize{Training}};
\node [enode,anchor=north,minimum height=3.5*\nodeheight,rounded corners=3pt,fill=red!30,align=left] (unsuperviseddata1) at ([yshift=-0.2em]unsupervisednode1.south) {};
\node [anchor=north] (unsuperviseddata1cap1) at ([yshift=-3pt]unsuperviseddata1.north) {\footnotesize{Unlabeled}};
\node [anchor=north west] (unsuperviseddata1cap2) at ([yshift=2pt]unsuperviseddata1cap1.south west) {\footnotesize{Data}};
\node [enode,anchor=north,minimum height=1.6*\nodeheight,rounded corners=3pt,fill=ugreen!30,align=left] (unsuperviseddata2) at ([yshift=-0.2em]unsupervisednode2.south) {};
\node [anchor=north] (unsuperviseddata2cap1) at ([yshift=-3pt]unsuperviseddata2.north) {\footnotesize{Labeled}};
\node [anchor=north west] (unsuperviseddata2cap2) at ([yshift=2pt]unsuperviseddata2cap1.south west) {\footnotesize{Data}};
\node [anchor=north] (caption) at ([xshift=0.2cm,yshift=-3.2cm]unsupervisednode1.south east) {\footnotesize{(a) Unsupervised Pre-training}};
\end{scope}
%%% supervised pre-training
\begin{scope}[xshift=4.7cm]
\node [enode,anchor=west,very thick] (supervisednode1) at (0,0) {\footnotesize{Supervised}};
\node [enode,anchor=west,very thick] (supervisednode2) at ([xshift=0.3cm]supervisednode1.east) {\footnotesize{Supervised}};
\draw [->,thick] ([xshift=1pt]supervisednode1.east) -- ([xshift=-1pt]supervisednode2.west);
\node [anchor=south] (node1label) at (supervisednode1.north) {\footnotesize{Pre-training}};
\node [anchor=south] (node2label) at (supervisednode2.north) {\footnotesize{Tuning}};
\node [enode,anchor=north,minimum height=2.0*\nodeheight,rounded corners=3pt,fill=blue!30,align=left] (superviseddata1) at ([yshift=-0.2em]supervisednode1.south) {};
\node [anchor=north] (superviseddata1cap1) at ([yshift=-3pt]superviseddata1.north) {\footnotesize{Labeled}};
\node [anchor=north west] (superviseddata1cap2) at ([yshift=2pt]superviseddata1cap1.south west) {\footnotesize{Data}};
\node [anchor=north] (task1) at (superviseddata1.south) {\scriptsize{Task 1}};
\node [enode,anchor=north,minimum height=1.3*\nodeheight,rounded corners=3pt,fill=ugreen!30,align=left] (superviseddata2) at ([yshift=-0.2em]supervisednode2.south) {};
\node [anchor=north] (superviseddata2cap1) at ([yshift=-3pt]superviseddata2.north) {\footnotesize{Labeled}};
\node [anchor=north west] (superviseddata2cap2) at ([yshift=2pt]superviseddata2cap1.south west) {\footnotesize{Data}};
\node [anchor=north] (task2) at (superviseddata2.south) {\scriptsize{Task 2}};
\node [anchor=north] (caption) at ([xshift=0.2cm,yshift=-3.2cm]supervisednode1.south east) {\footnotesize{(b) Supervised Pre-training}};
\end{scope}
%%% self-supervised pre-training
\begin{scope}[xshift=9.4cm]
\node [enode,anchor=west,very thick] (unsupervisednode1) at (0,0) {};
\node [anchor=north west] (tlabel) at (unsupervisednode1.north west) {\footnotesize{Self-}};
\node [anchor=north west] (tlabel2) at ([yshift=4pt]tlabel.south west) {\footnotesize{Supervised}};
\node [enode,anchor=west,very thick] (unsupervisednode2) at ([xshift=0.3cm]unsupervisednode1.east) {\footnotesize{Supervised}};
\node [enode,anchor=south,very thick] (tuningnode) at ([yshift=0.7cm]unsupervisednode2.north) {};
\node [anchor=north west] (tprompt) at (tuningnode.north west) {\footnotesize{Zero/Few}};
\node [anchor=north west] (tprompt2) at ([yshift=4pt]tprompt.south west) {\scriptsize{Shot Learning}};
\draw [->,thick,dashed] ([xshift=1pt]unsupervisednode1.east) -- ([xshift=-1pt]unsupervisednode2.west);
\draw [->,thick,dashed] ([xshift=1pt,yshift=0.5em]unsupervisednode1.east) .. controls +(east:0.3cm) and +(west:0.3cm) .. ([xshift=-1pt]tuningnode.west);
\node [anchor=south] (node1label) at (unsupervisednode1.north) {\footnotesize{Pre-training}};
\node [anchor=south] (node2label) at (unsupervisednode2.north) {\footnotesize{Tuning}};
\node [anchor=south] (tuningnodelabel) at (tuningnode.north) {\footnotesize{Prompting}};
\node [enode,anchor=north,minimum height=3.5*\nodeheight,rounded corners=3pt,fill=red!30,align=left] (unsuperviseddata1) at ([yshift=-0.2em]unsupervisednode1.south) {};
\node [anchor=north] (unsuperviseddata1cap1) at ([yshift=-3pt]unsuperviseddata1.north) {\footnotesize{Unlabeled}};
\node [anchor=north west] (unsuperviseddata1cap2) at ([yshift=2pt]unsuperviseddata1cap1.south west) {\footnotesize{Data}};
\node [enode,anchor=north,minimum height=1.3*\nodeheight,rounded corners=3pt,fill=ugreen!30,align=left] (unsuperviseddata2) at ([yshift=-0.2em]unsupervisednode2.south) {};
\node [anchor=north] (unsuperviseddata2cap1) at ([yshift=-3pt]unsuperviseddata2.north) {\footnotesize{Labeled}};
\node [anchor=north west] (unsuperviseddata2cap2) at ([yshift=2pt]unsuperviseddata2cap1.south west) {\footnotesize{Data}};
\node [anchor=north] (caption) at ([xshift=0.2cm,yshift=-3.2cm]unsupervisednode1.south east) {\footnotesize{(c) Self-supervised Pre-training}};
\end{scope}
\end{tikzpicture}
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
%----------------------------------------------------------------------------------------
% variables
\def\nodesep{2.5em}
\def\noderowsep{2.5em}
\tikzstyle{nnode} = [circle,minimum size=1.8em,draw=FigureInk!70,line width=.7pt,fill=FigureMist]
%----------------------------------------------------------------------------------------
% dropout training
\begin{scope}
% layer 1
\node [anchor=center,nnode,draw=black] (node11) at (0,0) {};
\node [anchor=west,nnode,draw=gray!50] (node12) at ([xshift=\nodesep]node11.east) {};
\node [anchor=west,nnode,draw=black] (node13) at ([xshift=\nodesep]node12.east) {};
% layer 2
\node [anchor=south,nnode,draw=black] (node21) at ([yshift=\noderowsep]node11.north) {};
\node [anchor=west,nnode,draw=black] (node22) at ([xshift=\nodesep]node21.east) {};
\node [anchor=west,nnode,draw=gray!50] (node23) at ([xshift=\nodesep]node22.east) {};
% layer 3
\node [anchor=south,nnode,draw=black] (node31) at ([yshift=\noderowsep]node21.north) {};
\node [anchor=west,nnode,draw=gray!50] (node32) at ([xshift=\nodesep]node31.east) {};
\node [anchor=west,nnode,draw=black] (node33) at ([xshift=\nodesep]node32.east) {};
% connections (layer 1 -> layer 2)
\draw [->,thick] ([yshift=2pt]node11.90) -- ([yshift=-2pt]node21.-90);
\draw [->,thick] ([yshift=2pt]node11.75) -- ([yshift=-2pt]node22.-110);
\draw [->,thick,dotted,gray!50] ([yshift=2pt]node11.60) -- ([yshift=-2pt]node23.-130);
\draw [->,thick,dotted,gray!50] ([yshift=2pt]node12.105) -- ([yshift=-2pt]node21.-75);
\draw [->,thick,dotted,gray!50] ([yshift=2pt]node12.90) -- ([yshift=-2pt]node22.-90);
\draw [->,thick,dotted,gray!50] ([yshift=2pt]node12.75) -- ([yshift=-2pt]node23.-105);
\draw [->,thick] ([yshift=2pt]node13.120) -- ([yshift=-2pt]node21.-50);
\draw [->,very thick,blue] ([yshift=2pt]node13.105) -- ([yshift=-2pt]node22.-70);
\draw [->,thick,dotted,gray!50] ([yshift=2pt]node13.90) -- ([yshift=-2pt]node23.-90);
% connections (layer 2 -> layer 3)
\draw [->,thick] ([yshift=2pt]node21.90) -- ([yshift=-2pt]node31.-90);
\draw [->,thick,dotted,gray!50] ([yshift=2pt]node21.75) -- ([yshift=-2pt]node32.-110);
\draw [->,thick] ([yshift=2pt]node21.60) -- ([yshift=-2pt]node33.-130);
\draw [->,thick] ([yshift=2pt]node22.105) -- ([yshift=-2pt]node31.-75);
\draw [->,thick,dotted,gray!50] ([yshift=2pt]node22.90) -- ([yshift=-2pt]node32.-90);
\draw [->,thick] ([yshift=2pt]node22.75) -- ([yshift=-2pt]node33.-105);
\draw [->,thick,dotted,gray!50] ([yshift=2pt]node23.120) -- ([yshift=-2pt]node31.-50);
\draw [->,thick,dotted,gray!50] ([yshift=2pt]node23.105) -- ([yshift=-2pt]node32.-70);
\draw [->,thick,dotted,gray!50] ([yshift=2pt]node23.90) -- ([yshift=-2pt]node33.-90);
% labels
\node [anchor=south west,align=center] (pnodelabel) at ([yshift=1em,xshift=0.0em]node33.north east) {\small{{\color{blue} active neuron}}\\[-3pt]\small{{\color{blue} (present with $\rho$)}}};
\draw [->] ([xshift=2em,yshift=2pt]pnodelabel.south west) .. controls +(south:1.2em) and +(north:1.2em) .. ([xshift=0.3em,yshift=0.3em]node33.center);
\node [anchor=south west,align=center] (dnodelabel) at ([yshift=1em,xshift=0.0em]node23.north east) {\small{{\color{blue} dropped neuron}}};
\draw [->] ([xshift=2em,yshift=2pt]dnodelabel.south west) .. controls +(south:1.2em) and +(north:1.2em) .. ([xshift=0.3em,yshift=0.3em]node23.center);
\node [anchor=south,align=center,fill=white,inner sep =0pt] (wlabel) at ([yshift=0.5em,xshift=1.8em]node13.north) {\small{{\color{blue} weight $W_{32}$}}};
\node [anchor=north] (name) at ([yshift=-0.5em,xshift=2em]node12.south) {\small (a) Training the dropout network};
\end{scope}
%----------------------------------------------------------------------------------------
% dropout test
\begin{scope}[xshift=19em]
% layer 1
\node [anchor=center,nnode,draw=black] (node11) at (0,0) {};
\node [anchor=west,nnode,draw=black] (node12) at ([xshift=\nodesep]node11.east) {};
\node [anchor=west,nnode,draw=black] (node13) at ([xshift=\nodesep]node12.east) {};
% layer 2
\node [anchor=south,nnode,draw=black] (node21) at ([yshift=\noderowsep]node11.north) {};
\node [anchor=west,nnode,draw=black] (node22) at ([xshift=\nodesep]node21.east) {};
\node [anchor=west,nnode,draw=black] (node23) at ([xshift=\nodesep]node22.east) {};
% layer 3
\node [anchor=south,nnode,draw=black] (node31) at ([yshift=\noderowsep]node21.north) {};
\node [anchor=west,nnode,draw=black] (node32) at ([xshift=\nodesep]node31.east) {};
\node [anchor=west,nnode,draw=black] (node33) at ([xshift=\nodesep]node32.east) {};
% connections (layer 1 -> layer 2)
\draw [->,thick] ([yshift=2pt]node11.90) -- ([yshift=-2pt]node21.-90);
\draw [->,thick] ([yshift=2pt]node11.75) -- ([yshift=-2pt]node22.-110);
\draw [->,thick] ([yshift=2pt]node11.60) -- ([yshift=-2pt]node23.-130);
\draw [->,thick] ([yshift=2pt]node12.105) -- ([yshift=-2pt]node21.-75);
\draw [->,thick] ([yshift=2pt]node12.90) -- ([yshift=-2pt]node22.-90);
\draw [->,thick] ([yshift=2pt]node12.75) -- ([yshift=-2pt]node23.-105);
\draw [->,thick] ([yshift=2pt]node13.120) -- ([yshift=-2pt]node21.-50);
\draw [->,very thick,blue] ([yshift=2pt]node13.105) -- ([yshift=-2pt]node22.-70);
\draw [->,thick] ([yshift=2pt]node13.90) -- ([yshift=-2pt]node23.-90);
% connections (layer 2 -> layer 3)
\draw [->,thick] ([yshift=2pt]node21.90) -- ([yshift=-2pt]node31.-90);
\draw [->,thick] ([yshift=2pt]node21.75) -- ([yshift=-2pt]node32.-110);
\draw [->,thick] ([yshift=2pt]node21.60) -- ([yshift=-2pt]node33.-130);
\draw [->,thick] ([yshift=2pt]node22.105) -- ([yshift=-2pt]node31.-75);
\draw [->,thick] ([yshift=2pt]node22.90) -- ([yshift=-2pt]node32.-90);
\draw [->,thick] ([yshift=2pt]node22.75) -- ([yshift=-2pt]node33.-105);
\draw [->,thick] ([yshift=2pt]node23.120) -- ([yshift=-2pt]node31.-50);
\draw [->,thick] ([yshift=2pt]node23.105) -- ([yshift=-2pt]node32.-70);
\draw [->,thick] ([yshift=2pt]node23.90) -- ([yshift=-2pt]node33.-90);
% labels
\node [anchor=south west,align=left] (pnodelabel) at ([yshift=1em,xshift=0.0em]node33.north east) {\small{{\color{blue} all neurons}}\\[-3pt]\small{{\color{blue} are active}}};
\draw [->] ([xshift=2em,yshift=2pt]pnodelabel.south west) .. controls +(south:1.2em) and +(north:1.2em) .. ([xshift=0.3em,yshift=0.3em]node33.center);
\node [anchor=south,align=center,fill=white,inner sep =0pt] (wlabel) at ([yshift=0.5em,xshift=1.8em]node13.north) {\small{{\color{blue} weight $\rho W_{32}$}}};
\node [anchor=north] (name) at ([yshift=-0.5em,xshift=2em]node12.south) {\small (b) Testing with the dropout network};
\end{scope}
\end{tikzpicture}
\end{center}
\input{figures/figure-theme.tex}
\begin{center}
\begin{tikzpicture}[aml figure]
\amlfigurepalette
%----------------------------------------------------------------------------------------
% variables
\def\nodesep{2.5em}
\def\noderowsep{5em}
\tikzstyle{nnode} = [rectangle,minimum size=2em,inner sep=0,draw=FigureInk!70,line width=.7pt,rounded corners=2pt]
\tikzstyle{dnode} = [circle,minimum size=2em,inner sep=0,draw=FigureInk!70,line width=.7pt]
\tikzstyle{lnode} = [circle,minimum size=0.5em,inner sep=1pt,fill=blue]
%----------------------------------------------------------------------------------------
% forward pass
\begin{scope}[every path/.append style={draw=FigureBlue}]
% nodes
\node [anchor=center,nnode,fill=white] (node7) at (0,0) {$\mathbf{x}$};
\node [anchor=west,nnode,fill=white] (node8) at ([xshift=\nodesep]node7.east) {$\mathbf{w}_1$};
\node [anchor=south,nnode,fill=white] (node5) at ([xshift= 0.5*\nodesep,yshift=\noderowsep]node7.east) {$\cdot$};
\node [anchor=west,nnode,fill=white] (node6) at ([xshift=\nodesep]node5.east) {$\mathbf{b}_1$};
\node [anchor=south,nnode,fill=white] (node4) at ([xshift= 0.5*\nodesep,yshift=\noderowsep]node5.east) {$+$};
\node [anchor=south,nnode,fill=white] (node2) at ([yshift=\noderowsep]node4.north) {$\psi(\cdot)$};
\node [anchor=west,nnode,fill=white] (node3) at ([xshift=\nodesep]node2.east) {$\mathbf{w}_2$};
\node [anchor=south,nnode,fill=white] (node1) at ([xshift= 0.5*\nodesep,yshift=\noderowsep]node2.east) {$\cdot$};
\node [anchor=south] (nodeoutput) at ([yshift=0.5*\noderowsep]node1.north) {\footnotesize{$\mathbf{y}$ (Output)}};
\foreach \x in {1,...,8}{
\node [anchor=center,lnode] (label\x) at ([xshift=2pt,yshift=-2pt]node\x.north west) {\tiny{\color{white} $\x$}};
}
\node [anchor=east] (node5math) at (node5.west) {\small{$\mathbf{h}_3 = \mathbf{x} \cdot \mathbf{w}_1$}};
\node [anchor=east] (node4math) at (node4.west) {\small{$\mathbf{h}_2 = \mathbf{h}_3 + \mathbf{b}_1$}};
\node [anchor=east] (node2math) at (node2.west) {\small{$\mathbf{h}_1 = \psi(\mathbf{h}_2)$}};
\node [anchor=east] (node1math) at (node1.west) {\small{$\mathbf{y} = \mathbf{h}_1 \cdot \mathbf{w}_2$}};
% connections
\draw [->,very thick] ([yshift=2pt]node7.north) -- ([yshift=-2pt,xshift=-0.3em]node5.south);
\draw [->,very thick] ([yshift=2pt]node8.north) -- ([yshift=-2pt,xshift=+0.3em]node5.south);
\draw [->,very thick] ([yshift=2pt]node5.north) -- ([yshift=-2pt,xshift=-0.3em]node4.south);
\draw [->,very thick] ([yshift=2pt]node6.north) -- ([yshift=-2pt,xshift=+0.3em]node4.south);
\draw [->,very thick] ([yshift=2pt]node4.north) -- ([yshift=-2pt,xshift=+0.0em]node2.south);
\draw [->,very thick] ([yshift=2pt]node2.north) -- ([yshift=-2pt,xshift=-0.3em]node1.south);
\draw [->,very thick] ([yshift=2pt]node3.north) -- ([yshift=-2pt,xshift=+0.3em]node1.south);
\draw [->,very thick] ([yshift=2pt]node1.north) -- ([yshift=-0pt,xshift=+0.0em]nodeoutput.south);
% label
\node [anchor=east,lnode] (labelsmall1) at ([xshift=-8em]nodeoutput.west) {\tiny{\color{white} $1$}};
\node [anchor=north,lnode] (labelsmall2) at ([yshift=-2.0em]labelsmall1.south) {\tiny{\color{white} $8$}};
\draw [->,thick] ([yshift=1pt]labelsmall2.north) -- ([yshift=-1pt]labelsmall1.south);
\node [anchor=north] (subfigurelabel) at ([yshift=-1em]node8.south) {(a) Forward Pass};
\end{scope}
%----------------------------------------------------------------------------------------
% backward pass
\begin{scope}[xshift=20em,every path/.append style={draw=FigureCoral}]
% nodes
\node [anchor=center,nnode,fill=white] (node7) at (0,0) {$\mathbf{x}$};
\node [anchor=west,nnode,fill=white] (node8) at ([xshift=\nodesep]node7.east) {$\mathbf{w}_1$};
\node [anchor=south,nnode,fill=white] (node5) at ([xshift= 0.5*\nodesep,yshift=\noderowsep]node7.east) {$\cdot$};
\node [anchor=west,nnode,fill=white] (node6) at ([xshift=\nodesep]node5.east) {$\mathbf{b}_1$};
\node [anchor=south,nnode,fill=white] (node4) at ([xshift= 0.5*\nodesep,yshift=\noderowsep]node5.east) {$+$};
\node [anchor=south,nnode,fill=white] (node2) at ([yshift=\noderowsep]node4.north) {$\psi(\cdot)$};
\node [anchor=west,nnode,fill=white] (node3) at ([xshift=\nodesep]node2.east) {$\mathbf{w}_2$};
\node [anchor=south,nnode,fill=white] (node1) at ([xshift= 0.5*\nodesep,yshift=\noderowsep]node2.east) {$\cdot$};
\node [anchor=south] (nodeoutput) at ([yshift=0.5*\noderowsep]node1.north) {\footnotesize{Loss $L$}};
\foreach \x in {1,...,8}{
\node [anchor=center,lnode,fill=red] (label\x) at ([xshift=2pt,yshift=-2pt]node\x.north west) {\tiny{\color{white} $\x$}};
}
\node [anchor=east] (node5math) at (node5.west) {\small{$\mathbf{h}_3 = \mathbf{x} \cdot \mathbf{w}_1$}};
\node [anchor=east] (node4math) at (node4.west) {\small{$\mathbf{h}_2 = \mathbf{h}_3 + \mathbf{b}_1$}};
\node [anchor=east] (node2math) at (node2.west) {\small{$\mathbf{h}_1 = \psi(\mathbf{h}_2)$}};
\node [anchor=east] (node1math) at (node1.west) {\small{$\mathbf{y} = \mathbf{h}_1 \cdot \mathbf{w}_2$}};
% connections
\draw [<-,very thick] ([yshift=2pt]node7.north) -- ([yshift=-2pt,xshift=-0.3em]node5.south) node [pos=0.5,inner sep=2pt,fill=white,xshift=-2em] (g7) {\scriptsize{{\color{red} $\frac{\partial L}{\partial \mathbf{x}} = \frac{\partial L}{\partial \mathbf{h}_3} \cdot \mathbf{w}_1^{\rm{T}}$}}};
\draw [<-,very thick] ([yshift=2pt]node8.north) -- ([yshift=-2pt,xshift=+0.3em]node5.south) node [pos=0.5,inner sep=2pt,fill=white,xshift=2em] (g8) {\scriptsize{{\color{red} $\frac{\partial L}{\partial \mathbf{w}_1} = \mathbf{x}^{\rm{T}} \cdot \frac{\partial L}{\partial \mathbf{h}_3}$}}};
\draw [<-,very thick] ([yshift=2pt]node5.north) -- ([yshift=-2pt,xshift=-0.3em]node4.south) node [pos=0.5,inner sep=2pt,fill=white,xshift=-1em] (g5) {\scriptsize{{\color{red} $\frac{\partial L}{\partial \mathbf{h}_3} = \frac{\partial L}{\partial \mathbf{h}_2}$}}};
\draw [<-,very thick] ([yshift=2pt]node6.north) -- ([yshift=-2pt,xshift=+0.3em]node4.south) node [pos=0.5,inner sep=2pt,fill=white,xshift=1em] (g6) {\scriptsize{{\color{red} $\frac{\partial L}{\partial \mathbf{b}_1} = \frac{\partial L}{\partial \mathbf{h}_2}$}}};
\draw [<-,very thick] ([yshift=2pt]node4.north) -- ([yshift=-2pt,xshift=+0.0em]node2.south) node [pos=0.5,inner sep=2pt,fill=white,xshift=0em] (g4) {\scriptsize{{\color{red} $\frac{\partial L}{\partial \mathbf{h}_2} = \frac{\partial L}{\partial \mathbf{h}_1} \odot \psi'(\mathbf{h}_2)$}}};
\draw [<-,very thick] ([yshift=2pt]node2.north) -- ([yshift=-2pt,xshift=-0.3em]node1.south) node [pos=0.5,inner sep=2pt,fill=white,xshift=-2em] (g2) {\scriptsize{{\color{red} $\frac{\partial L}{\partial \mathbf{h}_1} = \frac{\partial L}{\partial \mathbf{y}} \cdot \mathbf{w}_2^{\rm{T}}$}}};
\draw [<-,very thick] ([yshift=2pt]node3.north) -- ([yshift=-2pt,xshift=+0.3em]node1.south) node [pos=0.5,inner sep=2pt,fill=white,xshift=2em] (g3) {\scriptsize{{\color{red} $\frac{\partial L}{\partial \mathbf{w}_2} = \mathbf{h}_1^{\rm{T}} \cdot \frac{\partial L}{\partial \mathbf{y}}$}}};
\draw [<-,very thick] ([yshift=2pt]node1.north) -- ([yshift=-0pt,xshift=+0.0em]nodeoutput.south) node [pos=0.5,inner sep=2pt,fill=white] (g5) {\scriptsize{{\color{red} $\frac{\partial L}{\partial \mathbf{y}}$}}};
% label
\node [anchor=east,lnode,fill=red] (labelsmall1) at ([xshift=-8em]nodeoutput.west) {\tiny{\color{white} $1$}};
\node [anchor=north,lnode,fill=red] (labelsmall2) at ([yshift=-2.0em]labelsmall1.south) {\tiny{\color{white} $8$}};
\draw [<-,thick] ([yshift=1pt]labelsmall2.north) -- ([yshift=-1pt]labelsmall1.south);
\node [anchor=north] (subfigurelabel) at ([yshift=-1em]node8.south) {(b) Backward Pass};
\end{scope}
\end{tikzpicture}
\end{center}
% Core textbooks and source material
@book{BoydVandenberghe2004Convex,
author = {Stephen Boyd and Lieven Vandenberghe},
title = {Convex Optimization},
publisher = {Cambridge University Press},
year = {2004},
isbn = {978-0-521-83378-3},
url = {https://web.stanford.edu/~boyd/cvxbook/}
}
@book{GoodfellowEtAl2016DeepLearning,
author = {Ian J. Goodfellow and Yoshua Bengio and Aaron C. Courville},
title = {Deep Learning},
series = {Adaptive Computation and Machine Learning},
publisher = {MIT Press},
year = {2016},
isbn = {978-0-262-03561-3},
url = {https://www.deeplearningbook.org/}
}
@book{ManningEtAl2008IR,
author = {Christopher D. Manning and Prabhakar Raghavan and Hinrich Sch{"u}tze},
title = {Introduction to Information Retrieval},
publisher = {Cambridge University Press},
year = {2008},
isbn = {978-0-521-86571-5},
url = {https://nlp.stanford.edu/IR-book/}
}
@book{Jolliffe2002PCA,
author = {Ian T. Jolliffe},
title = {Principal Component Analysis},
edition = {2},
series = {Springer Series in Statistics},
publisher = {Springer},
address = {New York},
year = {2002},
doi = {10.1007/b98835},
isbn = {978-0-387-95442-4}
}
@book{SuttonBarto2018RL,
author = {Richard S. Sutton and Andrew G. Barto},
title = {Reinforcement Learning: An Introduction},
edition = {2},
publisher = {MIT Press},
year = {2018},
isbn = {978-0-262-03924-6},
url = {http://incompleteideas.net/book/the-book-2nd.html}
}
% Neural networks, optimization, and regularization
@article{Rosenblatt1958Perceptron,
author = {Frank Rosenblatt},
title = {The Perceptron: A Probabilistic Model for Information Storage and Organization in the Brain},
journal = {Psychological Review},
volume = {65},
number = {6},
pages = {386--408},
year = {1958},
doi = {10.1037/h0042519}
}
@article{RumelhartEtAl1986Backprop,
author = {David E. Rumelhart and Geoffrey E. Hinton and Ronald J. Williams},
title = {Learning Representations by Back-Propagating Errors},
journal = {Nature},
volume = {323},
number = {6088},
pages = {533--536},
year = {1986},
doi = {10.1038/323533a0}
}
@inproceedings{SutskeverEtAl2013Momentum,
author = {Ilya Sutskever and James Martens and George E. Dahl and Geoffrey E. Hinton},
title = {On the Importance of Initialization and Momentum in Deep Learning},
booktitle = {Proceedings of the 30th International Conference on Machine Learning},
series = {Proceedings of Machine Learning Research},
volume = {28},
pages = {1139--1147},
publisher = {PMLR},
year = {2013},
url = {https://proceedings.mlr.press/v28/sutskever13.html}
}
@inproceedings{KingmaBa2015Adam,
author = {Diederik P. Kingma and Jimmy Ba},
title = {Adam: A Method for Stochastic Optimization},
booktitle = {Proceedings of the 3rd International Conference on Learning Representations},
year = {2015},
url = {https://arxiv.org/abs/1412.6980}
}
@article{SrivastavaEtAl2014Dropout,
author = {Nitish Srivastava and Geoffrey E. Hinton and Alex Krizhevsky and Ilya Sutskever and Ruslan Salakhutdinov},
title = {Dropout: A Simple Way to Prevent Neural Networks from Overfitting},
journal = {Journal of Machine Learning Research},
volume = {15},
number = {1},
pages = {1929--1958},
year = {2014},
url = {https://jmlr.org/papers/v15/srivastava14a.html}
}
@inproceedings{SzegedyEtAl2016LabelSmoothing,
author = {Christian Szegedy and Vincent Vanhoucke and Sergey Ioffe and Jonathon Shlens and Zbigniew Wojna},
title = {Rethinking the Inception Architecture for Computer Vision},
booktitle = {Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
pages = {2818--2826},
year = {2016},
doi = {10.1109/CVPR.2016.308}
}
@inproceedings{HeEtAl2016ResNet,
author = {Kaiming He and Xiangyu Zhang and Shaoqing Ren and Jian Sun},
title = {Deep Residual Learning for Image Recognition},
booktitle = {Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
pages = {770--778},
year = {2016},
doi = {10.1109/CVPR.2016.90}
}
@article{BaEtAl2016LayerNorm,
author = {Lei Jimmy Ba and Jamie Ryan Kiros and Geoffrey E. Hinton},
title = {Layer Normalization},
journal = {arXiv preprint arXiv:1607.06450},
year = {2016},
eprint = {1607.06450},
archiveprefix = {arXiv},
url = {https://arxiv.org/abs/1607.06450}
}
% Evaluation and representation learning
@inproceedings{DrorEtAl2018Significance,
author = {Rotem Dror and Gili Baumer and Segev Shlomov and Roi Reichart},
title = {The Hitchhiker's Guide to Testing Statistical Significance in Natural Language Processing},
booktitle = {Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics},
pages = {1383--1392},
publisher = {Association for Computational Linguistics},
year = {2018},
doi = {10.18653/v1/P18-1128}
}
@article{BengioEtAl2013Representation,
author = {Yoshua Bengio and Aaron C. Courville and Pascal Vincent},
title = {Representation Learning: A Review and New Perspectives},
journal = {IEEE Transactions on Pattern Analysis and Machine Intelligence},
volume = {35},
number = {8},
pages = {1798--1828},
year = {2013},
doi = {10.1109/TPAMI.2013.50}
}
@article{BengioEtAl2003NeuralLM,
author = {Yoshua Bengio and R{\'e}jean Ducharme and Pascal Vincent and Christian Janvin},
title = {A Neural Probabilistic Language Model},
journal = {Journal of Machine Learning Research},
volume = {3},
pages = {1137--1155},
year = {2003},
url = {https://jmlr.org/papers/v3/bengio03a.html}
}
@article{OordEtAl2018CPC,
author = {A{"a}ron van den Oord and Yazhe Li and Oriol Vinyals},
title = {Representation Learning with Contrastive Predictive Coding},
journal = {arXiv preprint arXiv:1807.03748},
year = {2018},
eprint = {1807.03748},
archiveprefix = {arXiv},
url = {https://arxiv.org/abs/1807.03748}
}
@inproceedings{ChenEtAl2020SimCLR,
author = {Ting Chen and Simon Kornblith and Mohammad Norouzi and Geoffrey E. Hinton},
title = {A Simple Framework for Contrastive Learning of Visual Representations},
booktitle = {Proceedings of the 37th International Conference on Machine Learning},
series = {Proceedings of Machine Learning Research},
volume = {119},
pages = {1597--1607},
publisher = {PMLR},
year = {2020},
url = {https://proceedings.mlr.press/v119/chen20j.html}
}
@inproceedings{MikolovEtAl2013Word2Vec,
author = {Tom{\'a}s Mikolov and Kai Chen and Greg Corrado and Jeffrey Dean},
title = {Efficient Estimation of Word Representations in Vector Space},
booktitle = {Proceedings of the 1st International Conference on Learning Representations, Workshop Track},
year = {2013},
url = {https://arxiv.org/abs/1301.3781}
}
@inproceedings{PenningtonEtAl2014GloVe,
author = {Jeffrey Pennington and Richard Socher and Christopher D. Manning},
title = {{GloVe}: Global Vectors for Word Representation},
booktitle = {Proceedings of the 2014 Conference on Empirical Methods in Natural Language Processing},
pages = {1532--1543},
publisher = {Association for Computational Linguistics},
year = {2014},
doi = {10.3115/v1/D14-1162}
}
@inproceedings{PapineniEtAl2002BLEU,
author = {Kishore Papineni and Salim Roukos and Todd Ward and Wei{-}Jing Zhu},
title = {{BLEU}: A Method for Automatic Evaluation of Machine Translation},
booktitle = {Proceedings of the 40th Annual Meeting of the Association for Computational Linguistics},
pages = {311--318},
publisher = {Association for Computational Linguistics},
year = {2002},
doi = {10.3115/1073083.1073135}
}
@article{PanYang2010Transfer,
author = {Sinno Jialin Pan and Qiang Yang},
title = {A Survey on Transfer Learning},
journal = {IEEE Transactions on Knowledge and Data Engineering},
volume = {22},
number = {10},
pages = {1345--1359},
year = {2010},
doi = {10.1109/TKDE.2009.191}
}
% Sequence models and Transformers
@inproceedings{SutskeverEtAl2014Seq2Seq,
author = {Ilya Sutskever and Oriol Vinyals and Quoc V. Le},
title = {Sequence to Sequence Learning with Neural Networks},
booktitle = {Advances in Neural Information Processing Systems 27},
pages = {3104--3112},
year = {2014},
url = {https://proceedings.neurips.cc/paper/2014/hash/a14ac55a4f27472c5d894ec1c3c743d2-Abstract.html}
}
@inproceedings{BahdanauEtAl2015Attention,
author = {Dzmitry Bahdanau and Kyunghyun Cho and Yoshua Bengio},
title = {Neural Machine Translation by Jointly Learning to Align and Translate},
booktitle = {Proceedings of the 3rd International Conference on Learning Representations},
year = {2015},
url = {https://arxiv.org/abs/1409.0473}
}
@inproceedings{VaswaniEtAl2017Transformer,
author = {Ashish Vaswani and Noam Shazeer and Niki Parmar and Jakob Uszkoreit and Llion Jones and Aidan N. Gomez and Lukasz Kaiser and Illia Polosukhin},
title = {Attention Is All You Need},
booktitle = {Advances in Neural Information Processing Systems 30},
pages = {5998--6008},
year = {2017},
url = {https://proceedings.neurips.cc/paper/2017/hash/3f5ee243547dee91fbd053c1c4a845aa-Abstract.html}
}
% Pre-training and transfer across modalities
@inproceedings{HeEtAl2022MAE,
author = {Kaiming He and Xinlei Chen and Saining Xie and Yanghao Li and Piotr Doll{\'a}r and Ross B. Girshick},
title = {Masked Autoencoders Are Scalable Vision Learners},
booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
pages = {15979--15988},
year = {2022},
doi = {10.1109/CVPR52688.2022.01553}
}
@inproceedings{BaevskiEtAl2020Wav2vec,
author = {Alexei Baevski and Yuhao Zhou and Abdelrahman Mohamed and Michael Auli},
title = {wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations},
booktitle = {Advances in Neural Information Processing Systems 33},
year = {2020},
url = {https://proceedings.neurips.cc/paper/2020/hash/92d1e1eb1cd6f9fba3227870bb6d7f07-Abstract.html}
}
@inproceedings{DevlinEtAl2019BERT,
author = {Jacob Devlin and Ming{-}Wei Chang and Kenton Lee and Kristina Toutanova},
title = {{BERT}: Pre-training of Deep Bidirectional Transformers for Language Understanding},
booktitle = {Proceedings of the 2019 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies},
pages = {4171--4186},
publisher = {Association for Computational Linguistics},
year = {2019},
doi = {10.18653/v1/N19-1423}
}
@inproceedings{LewisEtAl2020BART,
author = {Mike Lewis and Yinhan Liu and Naman Goyal and Marjan Ghazvininejad and Abdelrahman Mohamed and Omer Levy and Veselin Stoyanov and Luke Zettlemoyer},
title = {{BART}: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension},
booktitle = {Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics},
pages = {7871--7880},
publisher = {Association for Computational Linguistics},
year = {2020},
doi = {10.18653/v1/2020.acl-main.703}
}
@article{RaffelEtAl2020T5,
author = {Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu},
title = {Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer},
journal = {Journal of Machine Learning Research},
volume = {21},
number = {140},
pages = {1--67},
year = {2020},
url = {https://jmlr.org/papers/v21/20-074.html}
}
@inproceedings{BrownEtAl2020GPT3,
author = {Tom B. Brown and Benjamin Mann and Nick Ryder and Melanie Subbiah and Jared Kaplan and Prafulla Dhariwal and Arvind Neelakantan and Pranav Shyam and Girish Sastry and Amanda Askell and Sandhini Agarwal and Ariel Herbert{-}Voss and Gretchen Krueger and Tom Henighan and Rewon Child and Aditya Ramesh and Daniel M. Ziegler and Jeffrey Wu and Clemens Winter and Christopher Hesse and Mark Chen and Eric Sigler and Mateusz Litwin and Scott Gray and Benjamin Chess and Jack Clark and Christopher Berner and Sam McCandlish and Alec Radford and Ilya Sutskever and Dario Amodei},
title = {Language Models Are Few-Shot Learners},
booktitle = {Advances in Neural Information Processing Systems 33},
pages = {1877--1901},
year = {2020},
url = {https://proceedings.neurips.cc/paper/2020/hash/1457c0d6bfcb4967418bfb8ac142f64a-Abstract.html}
}
% Entries transferred from the reference textbook for overlapping material
@article{Chen-and-Goodman:1999,
title = {An Empirical Study of Smoothing Techniques for Language Modeling},
author = {Stanley F. Chen and Joshua Goodman},
journal = {Computer Speech and Language},
volume = {13},
pages = {359--394},
year = {1999}
}
@article{Cortes-and-Vapnik:1995,
title = {Support-Vector Networks},
author = {Corinna Cortes and Vladimir Vapnik},
journal = {Machine Learning},
volume = {20},
pages = {273--297},
year = {1995}
}
@book{Mitchell:1997,
title = {Machine Learning},
author = {Tom M. Mitchell},
publisher = {McGraw-Hill Education},
year = {1997}
}
@inproceedings{Collobert-and-Weston:2008AUA,
title = {A Unified Architecture for Natural Language Processing: Deep Neural Networks with Multitask Learning},
author = {Ronan Collobert and Jason Weston},
booktitle = {Proceedings of the 25th International Conference on Machine Learning},
pages = {160--167},
year = {2008}
}
@book{Hastie-etal:2009,
title = {The Elements of Statistical Learning},
author = {Trevor Hastie and Robert Tibshirani and Jerome Friedman},
publisher = {Springer},
year = {2009}
}
@book{McClave-and-Sincich-2006,
title = {Statistics},
edition = {10},
author = {James T. McClave and Terry Sincich},
publisher = {Prentice Hall},
year = {2006}
}
@book{Freedman-et-al-2007,
title = {Statistics},
edition = {4},
author = {David Freedman and Robert Pisani and Roger Purves},
publisher = {W. W. Norton \& Company},
year = {2007}
}
@book{Freedman:2009,
title = {Statistical Models: Theory and Practice},
edition = {2},
author = {David A. Freedman},
publisher = {Cambridge University Press},
year = {2009}
}
@book{Dror-et-al:2020,
title = {Neural Network Methods for Natural Language Processing},
author = {Rotem Dror and Lotem Peled-Cohen and Segev Shlomov},
publisher = {Morgan \& Claypool Publishers},
year = {2020}
}
@article{hinton-etal:2012improving,
title = {Improving Neural Networks by Preventing Co-adaptation of Feature Detectors},
author = {Geoffrey E. Hinton and Nitish Srivastava and Alex Krizhevsky and Ilya Sutskever and Ruslan R. Salakhutdinov},
journal = {arXiv preprint arXiv:1207.0580},
year = {2012}
}
@incollection{prechelt1998early,
title = {Early Stopping---But When?},
author = {Lutz Prechelt},
booktitle = {Neural Networks: Tricks of the Trade},
pages = {55--69},
publisher = {Springer},
year = {1998}
}
@techreport{plaut-etal:1986experiments,
title = {Experiments on Learning by Back Propagation},
author = {David C. Plaut and Steven J. Nowlan and Geoffrey E. Hinton},
institution = {Carnegie-Mellon University},
year = {1986}
}
@article{Holmstrom-Koistinen:1992noise,
title = {Using Additive Noise in Back-Propagation Training},
author = {Lasse Holmstr{\"o}m and Petri Koistinen},
journal = {IEEE Transactions on Neural Networks},
volume = {3},
number = {1},
pages = {24--38},
year = {1992}
}
@article{bishop:1995training,
title = {Training with Noise Is Equivalent to {Tikhonov} Regularization},
author = {Christopher M. Bishop},
journal = {Neural Computation},
volume = {7},
number = {1},
pages = {108--116},
year = {1995}
}
@inproceedings{graves-etal:2013speech,
title = {Speech Recognition with Deep Recurrent Neural Networks},
author = {Alex Graves and Abdel-rahman Mohamed and Geoffrey E. Hinton},
booktitle = {2013 IEEE International Conference on Acoustics, Speech and Signal Processing},
pages = {6645--6649},
year = {2013}
}
@article{neelakantan-etal:2015adding,
title = {Adding Gradient Noise Improves Learning for Very Deep Networks},
author = {Arvind Neelakantan and Luke Vilnis and Quoc V. Le and Ilya Sutskever and Lukasz Kaiser and Karol Kurach and James Martens},
journal = {arXiv preprint arXiv:1511.06807},
year = {2015}
}
@inproceedings{sennrich-etal:2016improving,
title = {Improving Neural Machine Translation Models with Monolingual Data},
author = {Rico Sennrich and Barry Haddow and Alexandra Birch},
booktitle = {Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics},
pages = {86--96},
year = {2016}
}
@inproceedings{feng-etal:2021survey,
title = {A Survey of Data Augmentation Approaches for {NLP}},
author = {Steven Y. Feng and Varun Gangal and Jason Wei and Sarath Chandar and Soroush Vosoughi and Teruko Mitamura and Eduard Hovy},
booktitle = {Findings of the Association for Computational Linguistics: ACL-IJCNLP 2021},
pages = {968--988},
year = {2021}
}
@article{shorten-Khoshgoftaar:2019survey,
title = {A Survey on Image Data Augmentation for Deep Learning},
author = {Connor Shorten and Taghi M. Khoshgoftaar},
journal = {Journal of Big Data},
volume = {6},
number = {1},
pages = {1--48},
year = {2019}
}
@inproceedings{Szegedy-etal:2014neural,
title = {Intriguing Properties of Neural Networks},
author = {Christian Szegedy and Wojciech Zaremba and Ilya Sutskever and Joan Bruna and Dumitru Erhan and Ian Goodfellow and Rob Fergus},
booktitle = {Proceedings of the 2nd International Conference on Learning Representations},
year = {2014}
}
@inproceedings{Goodfellow-etal:2015Adversarial,
title = {Explaining and Harnessing Adversarial Examples},
author = {Ian Goodfellow and Jonathon Shlens and Christian Szegedy},
booktitle = {Proceedings of the 3rd International Conference on Learning Representations},
year = {2015}
}
@article{duchi-etal:2011adaptive,
title = {Adaptive Subgradient Methods for Online Learning and Stochastic Optimization},
author = {John Duchi and Elad Hazan and Yoram Singer},
journal = {Journal of Machine Learning Research},
volume = {12},
pages = {2121--2159},
year = {2011}
}
@misc{Hinton:2018RMSProp,
author = {Geoffrey E. Hinton},
title = {Neural Networks for Machine Learning, Lecture 6},
url = {http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf},
year = {2018}
}
@article{bengio-etal:2000neural,
title = {A Neural Probabilistic Language Model},
author = {Yoshua Bengio and R{\'e}jean Ducharme and Pascal Vincent},
journal = {Advances in Neural Information Processing Systems},
volume = {13},
year = {2000}
}
@book{Kelly-Stone:1975senses,
title = {Computer Recognition of English Word Senses},
author = {Edward F. Kelly and Philip J. Stone},
publisher = {American Elsevier Pub},
year = {1975}
}
@article{harris:1954distributional,
title = {Distributional Structure},
author = {Zellig S. Harris},
journal = {Word},
volume = {10},
number = {2--3},
pages = {146--162},
year = {1954}
}
@article{firth:1957synopsis,
title = {A Synopsis of Linguistic Theory, 1930--1955},
author = {John R. Firth},
journal = {Studies in Linguistic Analysis},
year = {1957}
}
@inproceedings{cho-etal:2014learning,
title = {Learning Phrase Representations Using {RNN} Encoder--Decoder for Statistical Machine Translation},
author = {Kyunghyun Cho and Bart van Merri{\"e}nboer and Caglar Gulcehre and Dzmitry Bahdanau and Fethi Bougares and Holger Schwenk and Yoshua Bengio},
booktitle = {Proceedings of the 2014 Conference on Empirical Methods in Natural Language Processing},
pages = {1724--1734},
year = {2014}
}
@article{graves-etal:2014neural,
title = {Neural {Turing} Machines},
author = {Alex Graves and Greg Wayne and Ivo Danihelka},
journal = {arXiv preprint arXiv:1410.5401},
year = {2014}
}
@inproceedings{luong-etal:2015effective,
title = {Effective Approaches to Attention-Based Neural Machine Translation},
author = {Minh-Thang Luong and Hieu Pham and Christopher D. Manning},
booktitle = {Proceedings of the 2015 Conference on Empirical Methods in Natural Language Processing},
pages = {1412--1421},
year = {2015}
}
@article{chaudhari-etal:2021attentive,
title = {An Attentive Survey of Attention Models},
author = {Sneha Chaudhari and Varun Mithal and Gungor Polatkan and Rohan Ramanath},
journal = {ACM Transactions on Intelligent Systems and Technology},
volume = {12},
number = {5},
pages = {1--32},
year = {2021}
}
@article{radford-etal:2018improving,
title = {Improving Language Understanding by Generative Pre-Training},
author = {Alec Radford and Karthik Narasimhan and Tim Salimans and Ilya Sutskever},
journal = {OpenAI Technical Report},
publisher = {OpenAI},
year = {2018}
}
@article{bengio-etal:2006greedy,
title = {Greedy Layer-Wise Training of Deep Networks},
author = {Yoshua Bengio and Pascal Lamblin and Dan Popovici and Hugo Larochelle},
journal = {Advances in Neural Information Processing Systems},
volume = {19},
year = {2006}
}
@inproceedings{erhan-etal:2010does,
title = {Why Does Unsupervised Pre-Training Help Deep Learning?},
author = {Dumitru Erhan and Aaron Courville and Yoshua Bengio and Pascal Vincent},
booktitle = {Proceedings of the Thirteenth International Conference on Artificial Intelligence and Statistics},
pages = {201--208},
year = {2010}
}
# 中文课程讲义 LaTeX 模板
主文件是 `aml_notes.tex`,建议使用 XeLaTeX 编译:
```powershell
xelatex aml_notes.tex
xelatex aml_notes.tex
```
第二次编译用于生成完整目录和交叉引用。图片可以放在 `figures/``images/` 目录中,然后用 `\includegraphics` 插入。
常用修改位置:
- 课程信息:修改 `\coursename``\semester``\teacher``\school`
- 新增章节:使用 `\chapter{章节标题}`
- 新增小节:使用 `\section{小节标题}``\subsection{小节标题}`
- 插入表格:参考模板中的 `table``longtable` 示例。
- 插入图片:把图片放入 `figures/`,参考模板中的 `figure` 示例。
如果使用 Overleaf,也请选择 XeLaTeX 作为编译器。
% !TeX program = xelatex
% 中文课程讲义 LaTeX 模板
% 编译建议:XeLaTeX 或 LuaLaTeX。Windows / macOS / Linux 均可使用 ctex 自动处理中文字体。
\documentclass[UTF8,a4paper,zihao=-4,oneside,openany]{ctexbook}
% 页面与基础排版
\usepackage[
top=2.6cm,
bottom=2.6cm,
left=2.7cm,
right=2.7cm,
headheight=15pt
]{geometry}
\usepackage{setspace}
\setstretch{1.25}
\setlength{\parindent}{2em}
\setlength{\parskip}{0.2em}
% 数学、表格、图片
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{array}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{caption}
\usepackage{subcaption}
\graphicspath{{figures/}{images/}}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,calc}
% 颜色与强调框
\usepackage[most]{tcolorbox}
\usepackage{xcolor}
\definecolor{CourseInk}{HTML}{1F2933}
\definecolor{CourseBlue}{HTML}{24577A}
\definecolor{CourseGreen}{HTML}{2D6A4F}
\definecolor{CourseGold}{HTML}{A16207}
\definecolor{CourseGray}{HTML}{F4F6F8}
\definecolor{CourseLine}{HTML}{D9E2EC}
\tcbset{
enhanced,
boxrule=0.6pt,
arc=2mm,
left=3mm,
right=3mm,
top=2mm,
bottom=2mm,
breakable
}
\newtcolorbox{learninggoals}{
colback=CourseGray,
colframe=CourseBlue,
title=学习目标,
fonttitle=\bfseries
}
\newtcolorbox{importantnote}{
colback=white,
colframe=CourseGreen,
title=重点提示,
fonttitle=\bfseries
}
\newtcolorbox{examplebox}[1][]{
colback=white,
colframe=CourseGold,
title=例题 #1,
fonttitle=\bfseries
}
\newtcolorbox{exercisebox}{
colback=CourseGray,
colframe=CourseLine,
title=课堂练习,
fonttitle=\bfseries\color{CourseInk}
}
% 页眉页脚与链接
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\small 中文课程讲义}
\fancyhead[R]{\small \leftmark}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0pt}
\usepackage[
colorlinks=true,
linkcolor=CourseBlue,
urlcolor=CourseGreen,
citecolor=CourseGold
]{hyperref}
% 章节样式
\ctexset{
chapter = {
name = {第,讲},
number = \chinese{chapter},
format = \huge\bfseries\color{CourseBlue},
beforeskip = 0pt,
afterskip = 22pt
},
section = {
format = \Large\bfseries\color{CourseInk}
},
subsection = {
format = \large\bfseries\color{CourseInk}
}
}
% 常用命令
\newcommand{\coursename}{中文课程名称}
\newcommand{\semester}{2026 春季}
\newcommand{\teacher}{授课教师}
\newcommand{\school}{学校 / 机构名称}
\newcommand{\lessondate}{\today}
\newcommand{\blankline}{\par\noindent\rule{\linewidth}{0.4pt}\par}
\newcommand{\keyword}[1]{\textbf{\color{CourseBlue}#1}}
\begin{document}
% 封面
\begin{titlepage}
\centering
\vspace*{1.5cm}
{\zihao{1}\bfseries\color{CourseBlue}\coursename\par}
\vspace{0.8cm}
{\zihao{3}\bfseries 课程讲义模板\par}
\vspace{1.2cm}
\IfFileExists{figures/cover.jpg}{
\includegraphics[width=0.78\linewidth,height=0.32\textheight,keepaspectratio]{figures/cover.jpg}
}{
\fbox{
\begin{minipage}[c][0.24\textheight][c]{0.78\linewidth}
\centering
将封面图命名为 \texttt{cover.jpg} 并放入 \texttt{figures/} 目录后,会自动显示在这里。
\end{minipage}
}
}
\vfill
\begin{tabular}{rl}
\textbf{教师:} & \teacher \\
\textbf{学期:} & \semester \\
\textbf{单位:} & \school \\
\textbf{日期:} & \lessondate \\
\end{tabular}
\vspace*{1.2cm}
\end{titlepage}
% 目录
\frontmatter
\pagestyle{plain}
\tableofcontents
\cleardoublepage
% 正文
\mainmatter
\pagestyle{fancy}
\chapter{第一部分}
\chapter{强化学习基础}
许多智能任务并不是对一个静态输入给出一次答案,而是连续作出选择。机器人当前的转向会改变下一时刻的位置,调度系统当前分配的资源会影响后续任务的等待时间,推荐系统展示的内容也可能改变用户之后的行为。此类任务中的动作既影响即时结果,也改变未来能够看到的状态和可以采取的动作。强化学习研究的正是这种\keyword{智能体通过与环境交互,依据反馈学习长期决策策略}的问题。
强化学习并非随着大模型才出现。它吸收了行为心理学中的试错学习、动态规划与最优控制等思想,早期主要用于控制、机器人、资源调度和博弈等序列决策问题。与监督学习依赖“输入—标签”样本不同,强化学习通常只得到奖励或代价:智能体必须探索不同动作,判断延迟出现的结果应当归因于此前哪些决策,并在利用已有经验与尝试未知行为之间作出权衡。价值函数、时序差分学习和策略优化等方法,都是围绕这些困难逐步发展起来的。
深度学习的发展进一步扩大了强化学习能够处理的状态规模。深度神经网络可以直接从图像等高维输入中学习表示,DQN 在一组 Atari 游戏中展示了端到端学习决策策略的能力;随后,深度强化学习与搜索、自我对弈结合,在围棋等复杂博弈中取得重要进展。强化学习的应用也由相对低维的控制问题,扩展到高维感知、复杂策略和大规模函数近似场景。
大模型出现后,强化学习获得了新的角色。预训练通过预测海量语料中的下一个词元学习通用知识和语言能力,但“更像训练语料”并不自动等于更符合用户意图。大模型后训练可以把模型看作策略,把生成过程看作一串动作,再利用人类偏好、奖励模型、规则检查器或可自动验证的任务结果提供反馈。基于人类反馈的强化学习(Reinforcement Learning from Human Feedback, RLHF)已被用于改善指令遵循和人类偏好对齐;在答案能够可靠验证的数学、编程等任务中,强化学习还可用于强化多步推理行为。
因此,在当下的大模型技术体系中,强化学习是连接“已有模型能力”与“目标行为”的重要后训练手段,也是训练可交互智能体和自主决策系统的基础工具。它并不替代预训练、监督微调、搜索或工具调用,也不是所有任务都必须采用的方案:只有当目标能够转化为可信反馈、决策会影响后续过程,并且训练数据与探索成本可以控制时,强化学习的优势才更容易发挥。奖励若不能代表真实目标,策略反而可能更有效地利用奖励漏洞;反馈昂贵或不能在线试错时,也需要离线强化学习、模仿学习或其他方法共同解决。
本章沿着三个问题展开:怎样把持续交互的任务写成一个明确的数学模型;怎样利用有限的交互数据判断动作的长期价值并改进策略;当算法进入真实任务时,怎样检查状态、动作、奖励和评估方案是否设计正确。为使这些抽象问题始终落到同一任务上,下面引入五子棋作为贯穿案例。
设想一个刚开始训练的五子棋程序。它在一局棋中落下几十枚棋子,直到对局结束才得到胜负结果。如果程序输了,究竟是哪一步出了问题?最后一次漏掉防守当然可疑,但更早的一次落子也可能已经让局面无法挽回。反过来,一步当时没有得分的落子,也可能为十几步后的胜利创造条件。程序的每次选择都会改变后续局面,训练数据也会随着策略改变而变化,因而不能只用一次静态的“输入—标签”预测描述这一过程。
五子棋在本章中的作用是说明方法,而不是把某一种算法包装成完整的现成解法。除非另有说明,后文采用同一套设定:棋盘大小为 $15\times15$,智能体执黑棋,对手及棋盘规则属于环境。智能体观察完整棋盘,在每个空位中选择一次落子;环境完成合法性检查、更新棋盘并执行对手回应,然后返回智能体下一次决策时看到的局面。任一方形成五子连线或棋盘填满时,对局自然终止。胜、负、平的核心奖励分别为 $1$$-1$$0$。本章中的数值计算只用于解释算法更新,不代表一套经过训练验证的五子棋系统。
\begin{learninggoals}
\begin{itemize}
\item 能够把一个序列决策任务表示为马尔可夫决策过程,并区分环境状态、智能体观测、即时奖励和长期回报
\item 能够解释 Q-learning、策略梯度、Actor-Critic 和 PPO 的更新依据,以及这些方法各自试图解决的问题
\item 能够根据任务目标设计状态、动作、奖励、约束和评估方案,并识别离线数据、训练稳定性、泛化和奖励偏差带来的风险
\end{itemize}
\end{learninggoals}
\section{强化学习建模方法}
一个强化学习任务首先要说清楚两件事:谁在作决定,以及一次决定会怎样改变后续处境。如图\ref{fig:framework}所示,作决定的部分称为智能体;其余能够影响决策结果、但不由智能体直接控制的部分统称为环境。智能体接收信息、选择动作,环境执行动作并返回新的信息和奖励。下一次决策建立在这次交互产生的结果之上。
\begin{figure}[htbp]
\centering
\IfFileExists{figures/framework.tex}{
\resizebox{0.78\linewidth}{!}{
\input{figures/framework.tex}
}
}{
\fbox{
\begin{minipage}[c][5cm][c]{0.78\linewidth}
\centering
强化学习的基本建模框架 \texttt{figures/f\_framework.tex}
\end{minipage}
}
}
\caption{强化学习的基本建模框架}
\label{fig:framework}
\end{figure}
图中的边界并非总是唯一的。训练五子棋程序时,可以把对手看作环境的一部分,也可以把双方都看作独立智能体。两种划分会产生不同的数据和训练方法。因此,建模不是给既定对象贴上几个名称,而是决定模型能够观察什么、控制什么,以及环境负责完成什么。
\subsection{马尔可夫决策过程}
马尔可夫决策过程(Markov Decision Process, MDP)是描述上述交互的一种标准模型。它的关键要求不是“任务没有历史”,而是当前状态已经概括了预测未来所需的历史信息。给定当前状态和动作后,更早的状态、动作不应再改变下一状态的条件分布:
\begin{equation}
P(s_{t+1} \mid s_t, a_t, s_{t-1}, a_{t-1}, \ldots, s_0, a_0)
=
P(s_{t+1} \mid s_t, a_t)
\end{equation}
其中,\(s_t\) 是环境在时刻 \(t\) 的状态,\(a_t\) 是随后执行的动作。这个等式是对状态表示的要求,而不是可以无条件接受的事实。如果机器人只输入一张静止图像,它可能无法判断前方物体正在靠近还是远离;此时单帧图像就不是充分状态。可以加入连续观测、速度信息或模型的记忆状态,重新构造更充分的决策信息。
一个 MDP 记为五元组:
\begin{equation}
\mathcal{M} = (\mathcal{S}, \mathcal{A}, P, R, \gamma)
\end{equation}
其中,\(\mathcal{S}\) 是状态空间,\(\mathcal{A}\) 是动作空间,\(P\) 是状态转移分布,\(R\) 是奖励函数,\(\gamma\in[0,1]\) 是折扣因子。转移分布可写为
\begin{equation}
P(s'\mid s,a)=\Pr(S_{t+1}=s'\mid S_t=s,A_t=a)
\end{equation}
奖励函数则描述一次转移产生的即时反馈:
\begin{equation}
R(s,a,s')=\mathbb{E}[R_t\mid S_t=s,A_t=a,S_{t+1}=s']
\end{equation}
正文后续用小写 \(r_t\) 表示一次实际交互得到的奖励样本。奖励函数可以采用其他等价定义,例如只依赖 \((s,a)\);关键是全章使用同一时间约定:智能体在 \(s_t\) 执行 \(a_t\),随后收到 \(r_t\) 并进入 \(s_{t+1}\)
一次从初始状态到终止状态的完整交互称为轨迹:
\begin{equation}
\tau=(s_0,a_0,r_0,s_1,a_1,r_1,\ldots,a_{T-1},r_{T-1},s_T)
\end{equation}
这里 \(s_T\) 是终止状态,最后一次动作和奖励分别是 \(a_{T-1}\)\(r_{T-1}\)。固定这一约定可以避免后续回报和 TD 目标出现一位下标偏差。
\begin{importantnote}
马尔可夫性取决于状态如何构造。任务具有历史依赖,并不意味着它一定无法使用 MDP;真正的问题是当前状态是否已经保存了影响未来的历史信息。
\end{importantnote}
\subsection{关键元素介绍}
\ref{tab:rl-elements} 将 MDP 中的主要对象与五子棋任务对应起来。这里把对手划入环境,并规定智能体每次只控制自己的落子。
\begin{table}[htbp]
\centering
\caption{强化学习基本元素及其在五子棋中的对应关系}
\label{tab:rl-elements}
\begin{tabularx}{\linewidth}{>{\bfseries}p{2.2cm}p{3.2cm}X}
\toprule
对象 & 数学记号 & 五子棋中的含义 \\
\midrule
智能体 & --- & 需要学习落子策略的棋手 \\
环境 & --- & 棋盘规则、胜负判断以及对手行为 \\
环境状态 & $s_t$ & 完整棋盘、当前行动方及规则所需信息 \\
智能体观测 & $o_t$ & 实际提供给策略模型的信息;完全可观测时可与 $s_t$ 相同 \\
动作 & $a_t\in\mathcal A$ & 在一个合法空位落子 \\
即时奖励 & $r_t$ & 本次落子及随后转移产生的反馈 \\
策略 & $\pi(a\mid s)$ & 给定局面时各合法落子的选择规则或概率 \\
轨迹 & $\tau$ & 从开局到胜、负或平局的完整交互序列 \\
\bottomrule
\end{tabularx}
\end{table}
状态与观测的区别值得单独保留。环境状态是对真实决策过程的描述,观测则是智能体实际拿到的输入。五子棋棋盘通常完全可见,因此二者可以相同;机器人被遮挡的视野、推荐系统中未被记录的用户意图则使二者产生差异。后文为了简化公式,在完全可观测的讨论中使用 \(s_t\) 作为策略输入;涉及信息缺失时再显式使用 \(o_t\)
策略可以是确定性的,也可以是随机的。确定性策略为每个状态指定一个动作;随机策略写成条件分布
\begin{equation}
\pi(a\mid s)=\Pr(A_t=a\mid S_t=s)
\end{equation}
训练的目标不是让策略复现某一条固定轨迹,而是让它在可能遇到的状态中作出长期效果更好的选择。
\subsection{优化目标}
一步动作的即时奖励不能完整说明它是否合理。从时刻 \(t\) 开始,智能体关心的是随后整段交互的折扣回报
\begin{equation}
G_t=\sum_{k=t}^{T-1}\gamma^{k-t}r_k
\end{equation}
折扣因子 \(\gamma\) 决定未来奖励在当前评价中的权重。较小的 \(\gamma\) 强调近期结果;接近 1 的 \(\gamma\) 保留更多长期影响。在有限回合任务中可以取 \(\gamma=1\);对于可能无限持续的任务,则需要保证回报及其期望有良好定义。
整条轨迹从初始时刻获得的回报记为 \(R(\tau)=G_0\)。参数化策略 \(\pi_\theta\) 会诱导出轨迹分布 \(p_\theta(\tau)\):策略改变后,智能体访问的状态和收集到的数据也随之改变。策略的性能定义为轨迹回报的期望:
\begin{equation}
J(\theta)
=
\mathbb{E}_{\tau\sim p_\theta(\tau)}[R(\tau)]
\end{equation}
训练希望找到使该期望尽可能大的参数:
\begin{equation}
\theta^{*} = \arg\max_{\theta} J(\theta)
\end{equation}
这个目标有两层难点。第一,不能枚举所有轨迹,只能用采样数据估计策略表现;第二,策略一旦改变,数据分布也会改变。后续算法的差别,主要在于它们怎样估计动作的长期影响,以及怎样利用这种估计更新策略。
\section{强化学习的基本优化算法}
在前一节中,强化学习问题被形式化为长期累积奖励最大化问题,其核心目标是寻找能够使性能函数 $J(\theta)$ 最大化的最优策略。然而,在实际任务中,环境的状态转移规律通常难以完全获得,所有可能的交互轨迹也无法被逐一枚举,因此很难直接计算性能函数并求解最优策略。强化学习算法需要利用智能体与环境交互产生的有限样本,对不同状态和动作所带来的长期收益进行估计,并依据估计结果不断改进决策策略。
根据策略改进方式的不同,强化学习的基本优化算法可以分为基于价值函数的方法和基于策略优化的方法。基于价值函数的方法首先估计某一状态或状态与动作组合能够获得的长期回报,再根据价值估计结果选择动作,将策略优化问题转化为价值函数的迭代更新问题。基于策略优化的方法则直接构建参数化策略,通过调整策略参数,提高高回报动作被选择的概率,从而实现对长期累积奖励的优化。两类方法在优化对象、动作选择方式和适用场景等方面具有不同特点。
在此基础上,Actor-Critic 框架将价值估计与策略优化结合起来。其中,Actor 负责根据当前状态选择动作并更新策略,Critic 负责评估当前策略或动作的长期收益,并为 Actor 的更新提供指导。该框架既保留了策略优化方法处理随机策略和连续动作空间的能力,也利用价值函数降低策略更新过程中的估计波动,成为许多现代强化学习算法的基础。
\subsection{基于价值函数的方法}
五子棋中的多数落子不会立即产生胜负奖励,却会改变后续局面。价值方法要解决的问题正是:当即时奖励不足以评价动作时,怎样估计它对未来回报的影响?
在策略 $\pi$ 下,状态价值函数定义为
\begin{equation}
V^{\pi}(s_t)
=
\mathbb{E}_{\pi}\!\left[G_t\mid S_t=s_t\right]
\end{equation}
它评价的是“处于这个状态,并继续按照 $\pi$ 行动”的期望回报。动作价值函数进一步固定当前动作:
\begin{equation}
Q^{\pi}(s_t,a_t)
=
\mathbb{E}_{\pi}\!\left[G_t\mid S_t=s_t,A_t=a_t\right]
\end{equation}
因此,$V^\pi$ 比较局面,$Q^\pi$ 比较同一局面中的候选动作。两者都依赖后续策略:同一个棋盘局面由强策略继续下和由随机策略继续下,其价值一般不同。
图~\ref{fig:gobang_value} 给出了某一棋盘局面下不同候选落子位置的动作价值,以及智能体依据动作价值进行落子选择的过程。
\begin{figure}[htbp]
\centering
\IfFileExists{figures/gobang_value.tex}{
\resizebox{0.92\linewidth}{!}{
\input{figures/gobang_value.tex}
}
}{
\fbox{
\begin{minipage}[c][6cm][c]{0.92\linewidth}
\centering
五子棋某一局面下的候选落子位置及动作价值示意\\
\texttt{figures/gobang\_value.tex}
\end{minipage}
}
}
\caption{基于动作价值的五子棋落子选择}
\label{fig:gobang_value}
\end{figure}
图中位置 $(8,8)$ 的估计价值最高。若这些估计足够准确,贪心策略会在合法动作集合 $\mathcal A(s_t)$ 中选择
\begin{equation}
a_t
=
\underset{a\in\mathcal{A}(s_t)}{\arg\max}
Q(s_t,a)
\end{equation}
这里最难的部分不是取最大值,而是得到可信的 $Q$ 值。回报满足
\begin{equation}
G_t=r_t+\gamma G_{t+1}
\end{equation}
所以长期问题可以拆成“本次奖励”和“下一状态之后的回报”。对最优动作价值函数,这一递推成为贝尔曼最优方程:
\begin{equation}
Q^{*}(s_t,a_t)
=
\mathbb{E}\!\left[
r_t+\gamma\max_{a'\in\mathcal{A}(s_{t+1})}
Q^{*}(s_{t+1},a')
\mid s_t,a_t\right]
\end{equation}
期望来自环境转移的不确定性;最大值表示下一状态采用当前认为最优的动作。若 $s_{t+1}$ 是自然终止状态,后续回报为零,最大值项也应置零。
Q-learning 不要求提前知道转移概率。每观察到一次转移 $(s_t,a_t,r_t,s_{t+1})$,它就用单步目标
\begin{equation}
y_t=r_t+\gamma\max_{a'\in\mathcal A(s_{t+1})}Q(s_{t+1},a')
\end{equation}
修正旧估计:
\begin{equation}
Q(s_t,a_t)
\leftarrow
Q(s_t,a_t)
+\alpha\left[y_t-Q(s_t,a_t)\right]
\end{equation}
其中 $\alpha$ 是学习率,差值 $y_t-Q(s_t,a_t)$ 是这次样本带来的时序差分误差。更新目标使用下一状态的最大动作价值,而采集样本时不必始终执行贪心动作;这正是 Q-learning 的离策略特征。
以图中的位置 $(8,8)$ 为例。设旧估计为 $2.0$,落子后的即时奖励为 $0$,下一决策状态的最大动作价值为 $6.0$,并取 $\gamma=0.9$$\alpha=0.5$。图~\ref{fig:q_learning_update} 展示了这次更新。
\begin{figure}[htbp]
\centering
\IfFileExists{figures/q_learning_update.tex}{
\resizebox{0.94\linewidth}{!}{
\input{figures/q_learning_update.tex}
}
}{
\fbox{
\begin{minipage}[c][5.5cm][c]{0.94\linewidth}
\centering
Q-learning 动作价值更新过程\\
\texttt{figures/q\_learning\_update.tex}
\end{minipage}
}
}
\caption{Q-learning 动作价值更新过程示意}
\label{fig:q_learning_update}
\end{figure}
单步目标为
\begin{equation}
y_t=0+0.9\times6.0=5.4
\end{equation}
因此
\begin{equation}
Q(s_t,(8,8))
\leftarrow
2.0+0.5\times(5.4-2.0)
=3.7
\end{equation}
目标值高于原估计,说明这次观察到的后续局面比原先预期更好。一次更新只把估计向 $5.4$ 推近,并不会立刻把它改成目标值。反复采样让终局奖励逐步传回更早的状态,但传递速度和估计质量仍取决于数据覆盖、学习率与函数近似方式。
如果始终选择当前估计最高的动作,智能体可能永远收集不到其他动作的数据。常用的 $\varepsilon$-greedy 行为策略是
\begin{equation}
a_t
=
\begin{cases}
\text{$\mathcal{A}(s_t)$ 中随机选择动作}, & \text{概率为 }\varepsilon,\\
\displaystyle
\underset{a\in\mathcal{A}(s_t)}{\arg\max}Q(s_t,a),
& \text{概率为 }1-\varepsilon.
\end{cases}
\end{equation}
$\varepsilon=0.1$ 时,约 $10\%$ 的决策在合法动作中随机探索。训练后期常逐渐减小 $\varepsilon$,但不能只按固定日程机械衰减:如果重要状态仍未被覆盖,过早停止探索只会把当前误差固化下来。
\begin{importantnote}
Q-learning 的示例说明了价值如何从下一状态传回当前动作,并不意味着表格型 Q-learning 能够直接解决完整五子棋。即使不考虑棋子顺序,$15\times15$ 棋盘的合法局面数量也极其庞大,绝大多数状态不可能被重复访问到足够多次。
\end{importantnote}
小规模任务可以用表格保存每个状态—动作对的估计;大规模任务则要借助神经网络等函数近似器,让相似状态共享统计信息。深度 Q 网络属于后一类方法,但它还需要经验回放、目标网络等稳定化设计,本章不在这里展开。价值方法也更自然地适用于能够枚举或有效比较候选动作的场景。下一节换一个角度:不先为每个动作学习一个价值,再取最大值,而是直接调整动作的选择概率。
\subsection{基于策略优化的方法}
价值方法先回答“每个动作值多少”,再选择价值最高的动作。策略方法跳过这个中间决策规则,直接学习“在当前状态下,各动作应当以多大概率被选择”。这使它能够自然表示随机策略,也不必在连续动作空间中枚举所有候选动作。
设参数化策略为:
\begin{equation}
\pi_{\theta}(a_t\mid s_t)
\end{equation}
其中 $\theta$ 是策略参数。离散动作策略输出概率质量函数;连续动作策略通常输出高斯分布等概率分布的参数。两种情况下,训练期间都可以从分布中采样动作。
在五子棋中,策略网络接收棋盘状态,输出各位置的 logits。合法动作掩码排除已经落子的格点,归一化后得到空位上的概率分布。图~\ref{fig:gobang_policy_distribution} 给出一个简化示例。
% \begin{figure}[htbp]
% \centering
% \IfFileExists{figures/gobang_policy.tex}{
% \resizebox{0.94\linewidth}{!}{
% \input{figures/gobang_policy.tex}
% }
% }{
% \fbox{
% \begin{minipage}[c][6cm][c]{0.94\linewidth}
% \centering
% 策略模型输出的五子棋落子概率分布\\
% \texttt{figures/gobang\_policy.tex}
% \end{minipage}
% }
% }
% \caption{策略模型输出的五子棋落子概率分布}
% \label{fig:gobang_policy_distribution}
% \end{figure}
图中四个候选位置的概率分别为 $0.15$$0.40$$0.25$$0.20$。位置 $B=(8,8)$ 最可能被选中,但并非必然被选中:
\begin{equation}
a_t
\sim
\pi_{\theta}(\cdot\mid s_t)
\end{equation}
这种随机性保留了探索机会。评估时是否继续采样要由评估目标决定;如果只取最大概率动作,测得的是确定性部署规则的表现,而不是训练时随机策略的完整表现。
策略应怎样利用对弈结果改变这些概率?设 $\pi_\theta$ 生成一条轨迹
\begin{equation}
\tau
=
(s_0,a_0,r_0,s_1,\ldots,a_{T-1},r_{T-1},s_T)
\end{equation}
其目标仍是最大化期望轨迹回报:
\begin{equation}
J(\theta)
=
\mathbb{E}_{\tau\sim p_{\theta}(\tau)}
\left[
R(\tau)
\right]
\end{equation}
利用对数导数技巧,可以把无法直接求和的轨迹期望转化为可用采样估计的策略梯度:
\begin{equation}
\nabla_{\theta}J(\theta)
=
\mathbb{E}_{\tau\sim p_{\theta}(\tau)}
\left[
\sum_{t=0}^{T-1}
G_t
\nabla_{\theta}
\log\pi_{\theta}(a_t\mid s_t)
\right]
\end{equation}
其中
\begin{equation}
G_t
=
\sum_{k=t}^{T-1}
\gamma^{k-t}r_k
\end{equation}
梯度中的 $\nabla_\theta\log\pi_\theta(a_t\mid s_t)$ 指向提高已采样动作对数概率的方向,$G_t$ 决定这个方向的权重和符号。这里不能简单说“回报较低就降低概率”:只要回报仍为正,原始 REINFORCE 更新仍可能提高该动作概率。要判断一次结果是好于还是差于当前局面的正常水平,需要引入基线。
直接以蒙特卡洛回报估计上述梯度的经典算法称为 REINFORCE。用当前策略采样 $N$ 条轨迹后,可最小化
\begin{equation}
\mathcal{L}_{\mathrm{PG}}(\theta)
=
-
\frac{1}{N}
\sum_{i=1}^{N}
\sum_{t=0}^{T_i-1}
G_t^{(i)}
\log
\pi_{\theta}
\left(
a_t^{(i)}\mid s_t^{(i)}
\right)
\end{equation}
负号把回报最大化写成损失最小化。REINFORCE 的优点是梯度估计在常见条件下无偏,代价是必须等到后续奖励出现才能计算回报,而且不同轨迹的结果可能相差很大。
仅依靠完整对局的最终结果更新策略,也会带来一些问题。五子棋的一局对弈通常包含多个动作,最终胜负是双方多次落子共同作用的结果。智能体可能在前期连续作出合理决策,却因为后期的一次失误输掉整局。如果将负回报直接作用于此前所有动作,一些原本合理的落子也会受到错误抑制。类似地,获胜对局中也可能包含效果较差的动作,但这些动作可能因为最终获胜而获得正向更新。
这就是时间上的信用分配问题。同一局面还可能因为对手行动和后续采样不同而得到相反结果,所以单条轨迹的 $G_t$ 是噪声很大的估计。增加样本能缓解波动,却不能让模型更早获得反馈。
为了减小回报波动对策略更新的影响,可以引入基线 $b(s_t)$,用动作回报与基线之间的差值代替原始回报:
\begin{equation}
\mathcal{L}_{\mathrm{PG}}(\theta)
=
-
\mathbb{E}
\left[
\sum_{t=0}^{T-1}
\left(
G_t-b(s_t)
\right)
\log\pi_{\theta}(a_t\mid s_t)
\right]
\end{equation}
只要基线不依赖当前采样动作,从策略梯度中减去它不会改变梯度的期望,却能降低方差。状态相关基线可以理解为当前局面的正常回报水平:更新关注的从此不再是“这次回报是否为正”,而是“这次结果是否好于在该局面下的通常表现”。
较为常用的状态相关基线是状态价值函数:
\begin{equation}
b(s_t)=V^{\pi}(s_t)
\end{equation}
此时,回报与基线之间的差值可以写为:
\begin{equation}
\widehat{A}_t
=
G_t-V^{\pi}(s_t)
\end{equation}
这给出优势函数 $A^\pi(s,a)=Q^\pi(s,a)-V^\pi(s)$ 的蒙特卡洛估计。正优势提高已采样动作的概率,负优势降低它;这种相对比较比回报的绝对正负更有意义。
仍以前文的五子棋局面为例。假设智能体在状态 $s_t$ 下选择了位置 $B=(8,8)$,当前选择概率为 $\pi_{\theta}(B\mid s_t)=0.40$。图~\ref{fig:policy_advantage_update} 展示了该动作在两种不同回报情况下的更新结果。
% \begin{figure}[htbp]
% \centering
% \IfFileExists{figures/policy_advantage_update.tex}{
% \resizebox{0.96\linewidth}{!}{
% \input{figures/policy_advantage_update.tex}
% }
% }{
% \fbox{
% \begin{minipage}[c][6.2cm][c]{0.96\linewidth}
% \centering
% 回报与基线对落子概率更新的影响\\
% \texttt{figures/policy\_advantage\_update.tex}
% \end{minipage}
% }
% }
% \caption{回报与基线对落子概率更新的影响}
% \label{fig:policy_advantage_update}
% \end{figure}
在图~\ref{fig:policy_advantage_update} 的第一种情况下,动作 $B$ 获得的实际回报为 $8$,而当前状态的基线为 $6$,因此优势估计为:
\begin{equation}
\widehat{A}_t
=
8-6
=
2>0
\end{equation}
这一结果表明,位置 $B$ 的实际表现高于当前局面的平均水平,策略会提高该动作的选择概率。
在第二种情况下,动作 $B$ 获得的实际回报为 $3$,低于基线 $6$,因此有:
\begin{equation}
\widehat{A}_t
=
3-6
=
-3<0
\end{equation}
此时,策略会降低位置 $B$ 的选择概率。可见,优势估计关注的是动作表现与当前状态一般水平之间的差异,而不是回报的绝对大小。
\begin{importantnote}
原始 REINFORCE 用采样回报给动作加权,并不是简单执行“正奖励鼓励、低奖励惩罚”。引入状态价值基线后,$G_t-V^\pi(s_t)$ 才显式比较这次动作与当前状态正常水平之间的差异。基线降低方差,但不会自动解决长序列中的信用分配问题。
\end{importantnote}
如果不想等到整局结束,可以用一个学习得到的价值模型近似后续回报。这样会引入估计偏差,却能更及时地更新策略。Actor-Critic 正是沿着这一取舍建立的。
\subsection{Actor-Critic 框架}
REINFORCE 用实际采样到的后续回报评价动作。这个评价不依赖一个可能出错的价值模型,却来得晚、波动大。Actor-Critic 用学习得到的价值估计替代一部分尚未发生的回报,在更早更新和更小方差之间换取一定偏差。
框架包含两个可训练部分:Actor 是参数为 $\theta$ 的策略 $\pi_\theta(a\mid s)$,负责行动;Critic 是参数为 $\phi$ 的价值近似器 $V_\phi(s)$,负责预测当前策略下的期望回报。二者可以使用独立网络,也可以共享特征提取层,不能仅凭“两个角色”推断实现中一定存在两张完全独立的网络。
在五子棋任务中,一条样本的产生和使用过程如下。Actor 接收 $s_t$ 并输出
\begin{equation}
\pi_{\theta}(a_t\mid s_t)
\end{equation}
并采样 $a_t$。环境执行动作和约定范围内的对手回应,返回 $r_t$$s_{t+1}$ 与终止标记。Critic 随后用相邻状态的价值构造训练目标。图~\ref{fig:actor_critic_framework} 展示了这条数据流。
% \begin{figure}[htbp]
% \centering
% \IfFileExists{figures/actor_critic_framework.tex}{
% \resizebox{0.96\linewidth}{!}{
% \input{figures/actor_critic_framework.tex}
% }
% }{
% \fbox{
% \begin{minipage}[c][6.2cm][c]{0.96\linewidth}
% \centering
% Actor-Critic 框架中的策略选择与价值评价过程\\
% \texttt{figures/actor\_critic\_framework.tex}
% \end{minipage}
% }
% }
% \caption{Actor-Critic 框架中的策略选择与价值评价过程}
% \label{fig:actor_critic_framework}
% \end{figure}
Actor 回答“下一步按什么分布行动”,Critic 回答“从当前状态继续执行该策略,预计能得到多少回报”。动作相对于当前状态平均水平的好坏由优势函数表示:
\begin{equation}
A^{\pi}(s_t,a_t)
=
Q^{\pi}(s_t,a_t)
-
V^{\pi}(s_t)
\end{equation}
正优势表示该动作优于当前策略在这个状态下的平均选择,负优势表示它更差。Actor 分别提高或降低相应动作的对数概率。
真实的 $Q^\pi(s_t,a_t)$ 未知时,可以用一步奖励加下一状态价值构造 TD 目标:
\begin{equation}
y_t=r_t+\gamma(1-d_t)V_{\bar\phi}(s_{t+1})
\end{equation}
其中 $d_t=1$ 表示任务在本次转移后自然终止,否则为 0。记号 $\bar\phi$ 强调计算目标时不通过下一状态价值反向传播梯度;实现中可以对这一项停止梯度,也可以使用单独维护的目标网络。TD 误差为
\begin{equation}
\delta_t
=
r_t+\gamma(1-d_t)V_{\bar\phi}(s_{t+1})-V_{\phi}(s_t)
\end{equation}
在最简单的单步 Actor-Critic 中,可用 $\delta_t$ 近似优势:
\begin{equation}
\widehat{A}_t
=
\delta_t
\end{equation}
仍以前文的棋盘为例。Actor 以 $0.40$ 的概率选择位置 $B=(8,8)$。该动作没有自然终止对局,所以 $d_t=0$,并设
\begin{equation}
r_t=0
\end{equation}
\begin{align}
V_{\phi}(s_t)&=4.0\\
V_{\phi}(s_{t+1})&=6.0
\end{align}
$\gamma=0.9$ 时,
\begin{equation}
\delta_t
=
0+0.9\times6.0-4.0
=
1.4
\end{equation}
图~\ref{fig:actor_critic_td_update} 给出了该评价信号对 Actor 和 Critic 的作用。
\begin{figure}[htbp]
\centering
\IfFileExists{figures/actor_critic_td_update.tex}{
\resizebox{0.96\linewidth}{!}{
\input{figures/actor_critic_td_update.tex}
}
}{
\fbox{
\begin{minipage}[c][6.5cm][c]{0.96\linewidth}
\centering
时序差分误差对 Actor 与 Critic 更新的作用\\
\texttt{figures/actor\_critic\_td\_update.tex}
\end{minipage}
}
}
\caption{时序差分误差对 Actor 与 Critic 更新的作用}
\label{fig:actor_critic_td_update}
\end{figure}
这个样本给出的 TD 目标是 $5.4$,高于 Critic 对 $s_t$ 的旧估计 $4.0$,所以 $\delta_t=1.4$。Actor 把它当作正优势,提高动作 $B$ 的对数概率;Critic 则把 $V_\phi(s_t)$$5.4$ 调整。需要注意,这只是当前 Critic 提供的学习信号,并不能证明动作 $B$ 客观上一定是好棋。
Actor 使用优势估计更新策略,其损失函数可以写为:
\begin{equation}
\mathcal{L}_{\mathrm{actor}}(\theta)
=
-
\mathbb{E}
\left[
\widehat{A}_t
\log
\pi_{\theta}(a_t\mid s_t)
\right]
\end{equation}
计算 Actor 梯度时,$\widehat A_t$ 应被视为固定目标,不让梯度通过优势估计回流到 Critic。正负优势决定更新方向,绝对值影响单个样本的权重。
Critic 的任务是使当前状态价值接近 TD 目标:
\begin{equation}
y_t=r_t+\gamma(1-d_t)V_{\bar\phi}(s_{t+1})
\end{equation}
在上述例子中:
\begin{equation}
y_t
=
0+0.9\times6.0
=
5.4
\end{equation}
Critic 原先对状态 $s_t$ 的估计为 $4.0$,而新的目标值为 $5.4$,因此需要适当提高 $V_{\phi}(s_t)$。Critic 可以使用平方误差进行训练:
\begin{equation}
\mathcal{L}_{\mathrm{critic}}(\phi)
=
\frac{1}{2}
\mathbb{E}
\left[
\left(
y_t-V_{\phi}(s_t)
\right)^2
\right]
\end{equation}
若下一状态的估计价值只有 $2.0$,则
\begin{equation}
V_{\phi}(s_t)=4.0,\qquad
V_{\phi}(s_{t+1})=2.0
\end{equation}
在即时奖励仍为 $0$、折扣因子为 $0.9$ 时,有:
\begin{equation}
\delta_t
=
0+0.9\times2.0-4.0
=
-2.2
\end{equation}
Actor 会降低该动作的概率,Critic 会把当前状态估计向 $1.8$ 调整。若落子直接结束对局,$d_t=1$,目标只剩最终奖励。时间上限造成的截断不一定等于自然终止:如果任务在时间上限之后本可继续,是否保留 bootstrap 项要根据环境语义处理。
Actor-Critic 框架中,Actor 负责输出动作概率并更新策略,Critic 负责估计状态价值。时序差分误差
\[
\delta_t=r_t+\gamma(1-d_t)V_{\bar\phi}(s_{t+1})-V_{\phi}(s_t)
\]
可以作为单步优势估计,也给出 Critic 当前预测与 TD 目标的差异。它是带有函数近似误差的训练信号,不是动作质量的真实标签。
一条基本更新链可以概括为:Actor 采样动作,环境产生转移,Critic 构造 TD 目标并学习价值,Actor 使用停止梯度的优势信号更新策略。它比纯蒙特卡洛更新更及时、方差通常更小,代价是 Actor 会继承 Critic 的偏差。
二者的学习速度失衡时,这个问题尤其明显。Critic 学得太慢,优势长期带有系统误差;Actor 变化太快,Critic 刚拟合的数据分布已经过时;Critic 在有限样本上过拟合,也会向 Actor 提供过度自信的方向。后续的 PPO 仍采用 Actor-Critic 结构,它重点限制的是策略一次更新的幅度,而不是消除 Critic 的估计误差。
\section{强化学习的进阶优化算法}
Actor-Critic 给出了策略与价值函数协同更新的框架,实际训练还要面对两个具体限制。第一,策略一旦变化太快,旧策略采集的优势估计便不能可靠地描述新策略;第二,有些任务根本不允许当前策略不断进入环境试错。PPO 处理前一种更新尺度问题,离线强化学习处理后一种固定数据问题。二者解决的困难不同,不能因为都“减少训练风险”而混为一谈。
\subsection{近端策略优化方法}
Actor-Critic 有了评价动作的方向,却没有回答一次应当走多远。设旧策略在某个棋盘状态下以 $0.40$ 的概率选择位置 $B$。正优势说明这个概率应当提高,但从 $0.40$ 调到 $0.46$ 和一步调到 $0.80$ 完全不是同一件事。后者会显著改变后续访问的局面,而用于计算优势的样本仍来自概率为 $0.40$ 的旧策略。
单纯减小学习率不能直接解决这个问题。学习率约束参数移动的距离,同样大小的参数变化却可能在不同状态下造成完全不同的概率变化。近端策略优化(Proximal Policy Optimization, PPO)因此直接比较新旧策略对已采样动作给出的概率,并让过于激进的变化不再从代理目标中继续获益。
PPO 仍采用 Actor-Critic 结构。Critic 提供优势估计,主要变化发生在 Actor 的目标函数。“近端”描述的是希望新策略留在采样策略附近,并不是一个由裁剪严格保证的硬约束。
在每一轮 PPO 训练开始时,先固定当前策略,并将其记为旧策略:
\begin{equation}
\pi_{\theta_{\mathrm{old}}}(a_t\mid s_t)
\end{equation}
使用旧策略与环境交互后,可以得到一批状态、动作和奖励数据。更新 Actor 时,新策略与旧策略可能对同一动作给出不同的选择概率。为衡量这一变化,定义概率比:
\begin{equation}
\rho_t(\theta)
=
\frac{
\pi_{\theta}(a_t\mid s_t)
}{
\pi_{\theta_{\mathrm{old}}}(a_t\mid s_t)
}
\end{equation}
概率比反映新旧策略对已采样动作 $a_t$ 的偏好变化。$\rho_t(\theta)>1$ 表示概率提高,$\rho_t(\theta)<1$ 表示概率降低。它只比较样本中实际执行的动作,并不直接约束该状态下整个动作分布。
例如,旧策略选择位置 $B$ 的概率为:
\[
\pi_{\theta_{\mathrm{old}}}(B\mid s_t)=0.40
\]
如果新策略将该概率提高到 $0.48$,则有:
\begin{equation}
\rho_t(\theta)
=
\frac{0.48}{0.40}
=
1.20
\end{equation}
这表示新策略选择位置 $B$ 的概率是旧策略的 $1.20$ 倍。概率比比参数距离更直接地反映这条样本上的行为变化。
若暂时不限制策略变化,可以利用概率比与优势估计构造代理目标:
\begin{equation}
L^{\mathrm{PG}}(\theta)
=
\mathbb{E}
\left[
\rho_t(\theta)\widehat{A}_t
\right]
\end{equation}
其中,$\widehat{A}_t$ 表示 Critic 给出的优势估计。当 $\widehat{A}_t>0$ 时,动作 $a_t$ 的表现高于当前策略的一般水平,提高该动作的概率会增大代理目标;当 $\widehat{A}_t<0$ 时,该动作的表现低于一般水平,降低其选择概率会增大代理目标。
如果对同一批样本反复优化,Actor 可能持续增大正优势动作的概率,或持续减小负优势动作的概率。PPO 为概率比设置裁剪范围:
\[
[1-\epsilon,1+\epsilon]
\]
其中,$\epsilon$ 表示裁剪阈值。PPO 的裁剪代理目标写为:
\begin{equation}
L^{\mathrm{CLIP}}(\theta)
=
\mathbb{E}
\left[
\min
\left(
\rho_t(\theta)\widehat{A}_t,\,
\operatorname{clip}
\left(
\rho_t(\theta),
1-\epsilon,
1+\epsilon
\right)
\widehat{A}_t
\right)
\right]
\end{equation}
式中的 $\min$ 不能忽略:PPO 取原始项与裁剪项中更保守的一个。裁剪只在变化会继续改善代理目标的方向上截断收益,并不是把所有概率比强制投影到区间内。$\epsilon$ 是需要验证的超参数,不能脱离批量大小、更新轮数和学习率单独判断。
裁剪机制对正优势动作和负优势动作的作用可以通过五子棋中的概率变化进行说明。图~\ref{fig:ppo_clipping_mechanism} 给出了旧策略概率为 $0.40$、裁剪阈值为 $\epsilon=0.2$ 时的两种情况。
% \begin{figure}[htbp]
% \centering
% \IfFileExists{figures/ppo_clipping_mechanism.tex}{
% \resizebox{0.96\linewidth}{!}{
% \input{figures/ppo_clipping_mechanism.tex}
% }
% }{
% \fbox{
% \begin{minipage}[c][6.8cm][c]{0.96\linewidth}
% \centering
% PPO 裁剪机制对策略概率更新的限制\\
% \texttt{figures/ppo\_clipping\_mechanism.tex}
% \end{minipage}
% }
% }
% \caption{PPO 裁剪机制对策略概率更新的限制}
% \label{fig:ppo_clipping_mechanism}
% \end{figure}
在图~\ref{fig:ppo_clipping_mechanism} 中,裁剪区间为:
\[
[1-\epsilon,1+\epsilon]=[0.8,1.2]
\]
当位置 $B$ 的优势估计为正时,PPO 允许适当提高该动作的选择概率。若新策略将概率从 $0.40$ 提高到 $0.48$,概率比值为:
\[
\rho_t(\theta)=\frac{0.48}{0.40}=1.2
\]
该比值位于裁剪上界,概率增加仍然能够反映在代理目标中。若新策略进一步将概率提高到 $0.60$,概率比值变为:
\[
\rho_t(\theta)=\frac{0.60}{0.40}=1.5
\]
此时概率比值已经超过上界。对于正优势动作,超过上界的部分不会继续增加裁剪目标,因而优化过程不再强烈推动该动作概率继续增大。
当某一动作的优势估计为负时,PPO 会降低该动作的选择概率。如果新策略将概率从 $0.40$ 降低到 $0.32$,概率比值为:
\[
\rho_t(\theta)=\frac{0.32}{0.40}=0.8
\]
该比值位于裁剪下界。如果概率继续降低到 $0.20$,概率比值变为:
\[
\rho_t(\theta)=\frac{0.20}{0.40}=0.5
\]
对于负优势动作,低于裁剪下界的变化不会继续带来更多优化收益,策略因而缺少进一步大幅降低该动作概率的动力。
裁剪机制并不会将新策略的实际概率强制固定在某个区间内。它限制的是过大概率变化能够从代理目标中获得的收益。策略仍然可以发生变化,但当变化超过一定范围后,继续扩大变化不再受到目标函数的鼓励。
这种做法与信任域思想相近。信任域可以理解为旧策略附近的一定范围,策略更新尽量在这一范围内完成。PPO 不需要直接求解带有严格约束的复杂优化问题,而是通过裁剪代理目标近似控制更新幅度。在实际训练中,还可以监测新旧策略之间的 KL 散度。当二者差异超过设定范围时,可以提前结束当前批次的策略更新。
除了限制策略变化,PPO 还会在有限范围内重复使用同一批交互数据。它先用旧策略采集一批样本,再将数据划分为小批次,对 Actor 和 Critic 进行多轮训练。裁剪机制减弱了策略在这些更新中偏离采样策略过远的动机,但并不保证偏离一定不会发生。
图~\ref{fig:ppo_training_process} 展示了 PPO 一次训练迭代的主要过程。
% \begin{figure}[htbp]
% \centering
% \resizebox{0.94\linewidth}{!}{
% \input{figures/ppo_training_process.tex}
% }
% \caption{PPO 的一次采样与更新过程}
% \label{fig:ppo_training_process}
% \end{figure}
实际实现通常还把策略目标、价值误差和熵正则合并训练。价值损失用于拟合回报或优势计算所需的基线,熵项用于延缓策略过早变得确定。三个分量的尺度不同,因此应分别记录,而不能只观察相加后的总损失。若价值误差持续增大,单靠缩小 PPO 的裁剪范围通常不能解决问题;若近似 KL 很大或大量样本触发裁剪,则说明同一批数据上的更新可能过多。
\begin{importantnote}
PPO 的裁剪目标限制的是过大策略变化继续获得的优化收益,并不把新策略严格约束在某个概率区间,也不保证每次更新后性能都提高。
\end{importantnote}
\subsection{离线强化学习方法}
在线强化学习可以用当前策略继续采样,离线强化学习则只使用预先收集并固定的数据集
\begin{equation}
\mathcal D=\{(s_i,a_i,r_i,s_i',d_i)\}_{i=1}^{N}
\end{equation}
其中 $d_i$ 记录转移是否因自然终止而结束。数据可能来自人工操作、旧策略或多个不同版本的系统。训练期间不能让目标策略进入环境补充它最缺少的样本,这是离线学习与经验回放之间的关键区别。
\begin{figure}[htbp]
\centering
\resizebox{0.92\linewidth}{!}{
\input{figures/online_offline_rl.tex}
}
\caption{在线强化学习与离线强化学习的数据来源}
\label{fig:online_offline_rl}
\end{figure}
困难不只在于数据有限,还在于学习得到的策略可能主动选择数据中罕见的动作。价值网络必须在这些位置外推,而最大化操作又偏好估计偏高的动作。一个偶然的高估会使策略更常选择该动作,但固定数据无法提供新的后果来纠正它,于是形成“分布偏移—价值高估—策略进一步偏移”的循环。
\begin{figure}[htbp]
\centering
\resizebox{0.92\linewidth}{!}{
\input{figures/offline_rl_distribution_shift.tex}
}
\caption{离线强化学习中的分布偏移与价值高估}
\label{fig:offline_rl_distribution_shift}
\end{figure}
不同离线算法以不同方式限制这种外推。BCQ 和 BEAR 约束目标策略不要轻易离开行为数据覆盖的动作范围;CQL 对缺少数据支持却被估计得很高的动作施加保守惩罚;IQL 主要在数据中的动作上学习价值关系,再用优势加权方式拟合策略。这些方法共同承认一个事实:没有记录的动作不一定差,但现有数据不足以证明它好。约束越强,外推风险通常越小,同时也越难发现数据之外的改进。
方法选择以前,先要弄清数据由哪些策略产生、关键状态和动作是否出现、奖励规则是否一致,以及终止与截断是否被正确记录。同一条轨迹不应拆散到训练集和验证集两边,否则相邻转移会造成泄漏。行为克隆提供了重要基线:如果复杂离线算法甚至不能稳定超过对历史动作的模仿,就很难把性能提升归因于长期价值学习。
离线指标也有边界。保留数据上的 Bellman 误差较低,只说明模型能拟合选定的自举目标,不能直接证明新策略有效。离线策略评估、模拟器测试和重要性采样各自依赖额外假设;目标策略离行为数据越远,结论通常越不可靠。高风险任务仍需经过受控测试和可回退的小范围部署。
\begin{importantnote}
离线强化学习的核心问题不是样本总数,而是目标策略的选择是否有数据支持。重复很多次相似轨迹,并不能补上从未出现过的关键状态和动作。
\end{importantnote}
\section{强化学习的应用设计方法}
选定算法以前,任务定义已经决定模型能看到什么、能改变什么,以及什么结果会被当成成功。下面继续使用本章开头的五子棋设定,把这些决定落实为可以执行和评估的系统。
\subsection{任务边界与转移定义}
本章把智能体定义为执黑的一方,对手和规则属于环境。一次转移从黑方决策前开始:策略选择一个空位,环境检查合法性、落下黑子;若对局尚未结束,环境再执行白方回应,随后返回新的黑方决策局面。这样,数据中的 $(s_t,a_t,r_t,s_{t+1})$ 与算法使用的时间步保持一致。
如果改成黑白双方各自都是智能体,或者让同一个策略在每一个落子回合都被调用,状态中的行动方、奖励归属和轨迹结构都要随之改变。这并非实现细节,而是另一个任务定义。比较算法时应冻结环境版本、对手来源、开局分布和终止规则,否则性能差异无法只归因于算法。
最小规模实验很适合暴露边界错误。随机策略若能落到已占位置,问题在动作执行或掩码;相同局面得到互相矛盾的输入,通常意味着行动方或坐标编码不一致;训练回报上升而独立胜率不变,则应先核对奖励和评估目标。只有任务定义能够稳定地产生正确轨迹,扩大训练规模才有意义。
\subsection{状态表示与动作空间}
环境状态不一定等于策略能够观察到的信息。本章的棋盘完全可见,且策略只在黑方回合调用,因此可以令 $o_t=B_t$。若同一模型控制双方,还需加入当前行动方 $p_t$,否则同一棋盘在黑方和白方回合会得到相同输入。
棋盘可以用单个整数矩阵表示
\begin{equation}
B_t\in\{-1,0,1\}^{15\times15}
\end{equation}
也可以使用两个二值通道分别记录双方棋子
\begin{equation}
B_t\in\{0,1\}^{2\times15\times15}
\end{equation}
前者紧凑,适合作为最小基线;后者把类别含义分开,更便于卷积网络处理。表示方式不应包含对局结束后才能得到的信息,归一化统计量也只能由训练数据计算。
动作空间对应棋盘的 $225$ 个位置。策略网络可以为每个位置输出一个得分 $z_\theta(s_t,a)$,再用合法动作集合 $\mathcal A(s_t)$ 构造掩码后的分布
\begin{equation}
\pi_\theta(a\mid s_t)
=
\frac{\exp z_\theta(s_t,a)}
{\sum_{a'\in\mathcal A(s_t)}\exp z_\theta(s_t,a')}
\end{equation}
已经有棋子的格点由规则确定为非法,可以直接屏蔽;一手棋即使很差,只要符合规则就仍是合法动作。把棋力启发式写入掩码,会使策略失去发现其他解法的机会。
动作编码还要与环境坐标严格一致。把二维位置展平时,可以固定使用 $a=15i+j$,并用边角位置和往返转换做单元测试。训练阶段若允许随机策略直接产生未掩码动作,环境也应给出明确错误,而不是静默地改成另一个位置。
\subsection{奖励与约束}
在固定设定中,胜、负、平分别得到 $1$$-1$$0$,非终止步骤奖励为 $0$。这种稀疏奖励与真实目标接近,却让早期落子的评价依赖整局结果。加入棋型分数等过程反馈能够降低学习难度,但也改变了算法实际优化的目标。
密集反馈缩短了动作与奖励之间的距离,也把设计者的棋型偏好写进了优化目标。若“形成活三”可重复计分,策略可能维持局部棋型来刷取奖励,而不是尽快获胜。过程奖励因此不是免费的训练加速器。
将任务奖励和多个辅助奖励组合时,可以写为:
\begin{equation}
r_t
=
r_t^{\mathrm{task}}
+
\sum_{i=1}^{K}
\lambda_i r_t^{(i)}
\end{equation}
其中 $r_t^{\mathrm{task}}$ 是核心奖励,$r_t^{(i)}$ 是辅助项。判断 $\lambda_i$ 不能只比较单步数值,还要估算每项在一个回合中的累计上限和出现频率。
奖励权重的大小还会影响不同目标之间的取舍。例如,机器人控制任务可能同时希望提高移动速度、降低能量消耗并保持动作平稳。总奖励可以由多个部分组成:
\begin{equation}
r_t
=
\lambda_1 r_t^{\mathrm{progress}}
-
\lambda_2 r_t^{\mathrm{energy}}
-
\lambda_3 r_t^{\mathrm{instability}}
\end{equation}
权重会改变策略的取舍:能耗惩罚过大可能让机器人静止,每步惩罚过大可能让处于劣势的棋手主动寻求快速失败。奖励尺度还影响价值目标和梯度数值。加入任何新项后,都要分别记录各项回报以及独立任务指标;总回报上升无法说明究竟是哪一项被优化。
约束是否进入奖励,取决于它能否被权衡。机械极限、非法落子和不可突破的安全边界应用掩码、动作范围或安全控制器保证;能耗、平稳性和延迟等允许折中的要求才适合写成软成本:
\begin{equation}
r_t'
=
r_t
-
\lambda c_t
\end{equation}
其中 $c_t$ 是成本,$\lambda$ 表示任务愿意为降低成本牺牲多少核心回报。若需求方无法接受任何违反,就不存在这个权衡,也不应仅靠把 $\lambda$ 调得很大来模拟硬约束。
巨大负奖励不能替代硬约束。随机策略仍可能先执行危险动作再收到惩罚,而真实系统可能没有承受这次探索的余地。
奖励信号还可能被智能体以设计者没有预料到的方式利用。智能体只会优化实际给出的奖励,而不会自动理解设计者的真实意图。当奖励函数存在漏洞时,策略可能获得很高回报,却没有完成预期任务。这种现象通常称为奖励投机或奖励漏洞。
例如,如果只根据连续棋子数量奖励五子棋智能体,策略可能偏好不断扩展已有棋型,而忽略对手已经形成的直接威胁。如果机器人任务只奖励向目标方向的瞬时速度,机器人可能通过剧烈摆动获得较高速度信号,却没有稳定到达目标位置。
奖励漏洞要靠行为审计和独立指标发现。五子棋至少应检查真实胜率、先后手表现、不同对手下的结果及典型失败棋局。如果辅助奖励上升而胜率下降,应回滚该奖励版本,而不是继续调高训练步数。
新奖励项加入后,应单独记录并做消融。除了比较总回报,还要检查辅助项在整回合尺度上是否压过核心奖励,以及高回报轨迹的行为是否符合预期。
奖励版本确定后,还要建立彼此隔离的训练、验证和测试流程。
\subsection{训练与评估过程设计}
训练、验证和测试承担不同职责,混用会让结果失去解释性。
\begin{table}[htbp]
\centering
\caption{训练、验证与最终测试的职责}
\label{tab:train-validation-test}
\begin{tabularx}{\linewidth}{>{\bfseries}p{2.3cm}p{4.3cm}X}
\toprule
阶段 & 可以做什么 & 不能做什么 \\
\midrule
训练 & 采集或读取训练数据、探索、更新参数、监控优化指标 & 用训练回报宣称最终任务性能 \\
验证 & 比较检查点、选择超参数、触发早停、诊断失败 & 把反复调参后的验证结果当成无偏测试结果 \\
最终测试 & 在冻结方案上报告预先定义的任务指标与波动 & 继续选模型、改奖励、调对手或回流测试数据 \\
\bottomrule
\end{tabularx}
\end{table}
采样量决定梯度方差和数据新鲜度。只用一局五子棋更新,胜负偶然性可能支配梯度;等待过多对局,策略又长期使用旧数据。PPO 对同一批样本更新过多轮还会使当前策略偏离采样策略。批量大小、更新轮数和学习率必须联合报告,并结合 KL、裁剪比例、价值误差和策略熵诊断。
探索规则属于实验配置。始终取最大概率动作容易固化早期偏好,随机性过强又会掩盖已经学到的能力。训练可以采样并调节熵,验证和测试则要预先规定使用采样策略还是确定性策略;两种评估回答的问题不同,不能在看到结果后择优报告。
五子棋训练还需要设计对手来源。如果智能体始终与一个固定的弱对手进行比赛,它可能很快取得较高胜率,但这种胜率并不代表策略具有普遍能力。智能体可能只是发现了该对手的某些固定漏洞。
自我对弈是一种常见的数据生成方式。当前策略同时控制对弈双方,使对手能力随着训练同步提高。这样可以不断产生与当前策略水平接近的比赛,减少固定对手过强或过弱的问题。
单纯使用最新策略进行自我对弈也存在局限。如果新策略暂时形成某一种棋路,双方可能反复围绕这一棋路训练,逐渐遗忘对旧策略有效的行为。为增加对手多样性,可以定期保存历史策略,并从不同版本中随机选择对手。还可以加入规则策略、随机策略和外部棋谱策略,使训练数据覆盖不同水平和不同风格的对局。
对手池中的角色应预先定义:随机策略检查基本能力,规则策略提供稳定锚点,历史策略检查遗忘,当前策略提供相近难度。训练对手可以参与数据生成,测试对手不得因为测试结果而被加入当前模型的训练循环。
五子棋验证应使用独立种子、开局和对手,并交换先后手。只报合并胜率会掩盖先手优势,因此还应分别报告先手、后手及各对手上的结果。
设评估总局数为 $N_{\mathrm{eval}}$,获胜局数为 $N_{\mathrm{win}}$,则评估胜率可以写为:
\begin{equation}
\widehat{p}_{\mathrm{win}}
=
\frac{
N_{\mathrm{win}}
}{
N_{\mathrm{eval}}
}
\end{equation}
训练指标用于解释优化过程:策略损失、价值误差、熵、KL、回合长度和各奖励分项都属于诊断信息。任务指标用于评价结果:五子棋的胜率、平局率和对手分项才回答棋力是否提高。两类曲线相关时也不能互相替代。
如果使用动作掩码,还应检查非法动作在掩码前是否获得过高得分。如果模型长期为大量非法位置分配较高原始得分,只是依靠掩码将其删除,说明策略网络对棋盘状态的理解可能仍然不足。掩码保证了动作合法,但不能代替模型学习合理的动作偏好。
网络初始化、环境随机性、动作采样和小批次顺序都会改变训练轨迹。应使用多个训练随机种子,在固定测试协议下报告均值与离散程度,不能从多次实验中只挑最好的一次。
检查点应包含策略、Critic、优化器、训练步数、归一化统计量和环境版本。验证集负责选择检查点和触发早停;最终测试只对冻结的候选方案运行。短期胜率波动不应触发即时早停,平滑窗口和耐心周期应在实验前设定。
在分析训练失败原因时,应区分任务设计问题和优化问题。如果策略损失剧烈变化、价值估计不断发散,问题可能来自学习率、更新频率或算法实现;如果训练过程稳定但策略始终选择无效行为,问题可能来自状态、动作或奖励设计;如果训练表现很好而独立评估较差,则需要检查训练场景是否过于单一。
训练集用于更新参数,验证集用于选择模型和调整设计,最终测试用于报告冻结方案。测试结果一旦反过来影响奖励、超参数、对手或检查点选择,这部分数据就已经参与了开发过程,需要重新建立未使用过的测试协议。
即使流程隔离正确,规模、训练波动和环境变化仍会带来新的困难。下一节从失败现象出发讨论这些问题。
% \section{强化学习的前沿方向}
% \subsection{多智能体强化学习}
% \subsection{面向样本重放的强化学习方法}
% \subsection{强化学习方法的稳定性改进}
\section{强化学习方法的挑战}
一条最终上升的训练曲线可能掩盖不同问题。有些重要局面从未进入训练数据,有些结果换一个随机种子便无法复现,还有些策略只适用于训练对手或当前奖励。下面从这些现象追查原因。
\subsection{面向大规模决策的挑战}
“任务规模大”可能指完全不同的瓶颈。高维状态首先造成覆盖困难,模型不得不对未见局面作出外推;动作候选太多时,计算和探索会分散在大量选择上;回合过长则拉开动作与结果的距离,使信用分配更加困难。扩大网络主要增加函数近似能力,并不会自动解决动作搜索或长时域问题。
继续以五子棋任务为例。一个 $15\times15$ 棋盘包含 $225$ 个格点。若暂时不考虑棋局规则,每个格点可能为空、黑子或白子,棋盘状态数量的上界为:
\begin{equation}
|\mathcal{S}|
\leq
3^{225}
\end{equation}
实际合法棋局的数量远小于这一上界,但仍然十分庞大。智能体不可能遍历全部棋盘状态,也无法为每个状态单独保存一个精确价值。训练只能利用有限对局学习不同棋型之间的共同规律,并将已学到的判断推广到没有出现过的局面。
这说明,表格形式的价值函数只适合状态数量较少的任务。在大规模问题中,通常需要使用神经网络近似价值函数或策略函数:
\begin{equation}
V_{\phi}(s)
\approx
V^{\pi}(s)
\end{equation}
\begin{equation}
\pi_{\theta}(a\mid s)
\approx
\pi(a\mid s)
\end{equation}
神经网络能够利用相似状态之间的共享结构。例如,五子棋中的连续三子、两端开放和对手威胁等局部棋型,可能出现在棋盘的不同位置。卷积结构可以提取这些局部特征,使模型不必分别记忆每一个具体棋盘。
不过,函数近似并没有消除状态空间过大的问题。模型只能根据训练数据学习状态之间的相似性。如果某类棋型在训练中很少出现,网络对这些局面的判断仍可能不准确。随着状态维度增加,覆盖具有代表性的环境情况需要更多数据,模型训练和存储成本也会随之上升。
除了状态空间,动作空间也会带来明显挑战。五子棋在空棋盘状态下最多有 $225$ 个候选落子位置,策略模型需要为这些位置分别给出动作得分。随着棋盘逐渐被填满,合法动作数量会减少,但训练早期仍需要在大量候选位置之间进行比较。
在推荐、调度和组合优化任务中,动作规模可能远大于五子棋。例如,推荐系统可能需要从大量候选物品中选择一个或多个结果,生产调度需要同时决定任务顺序、执行设备和开始时间。若将所有可能组合直接视为独立动作,动作数量会迅速超过模型能够逐一评价的范围。
假设每一步平均有 $b$ 个候选动作,一段决策过程包含 $H$ 步,则可能形成的动作序列数量约为:
\begin{equation}
N_{\mathrm{seq}}
\approx
b^H
\end{equation}
即使每一步的动作数量并不特别大,随着决策长度增加,完整动作序列仍会呈指数增长。智能体无法逐一尝试所有序列,只能根据有限经验判断哪些早期动作更可能带来较好的长期结果。
较大的动作空间还会增加探索难度。训练初期,策略对不同动作的判断较为接近,有限的交互次数会被分散到大量候选动作上。许多动作只能获得少量样本,价值估计因而难以稳定。
在五子棋中,真正值得重点考虑的落子位置通常集中在已有棋子附近、己方棋型延伸位置和对手威胁位置。如果策略在训练初期对所有空白格点进行近似均匀探索,大量对局会消耗在与当前棋局关系较小的位置上。
处理大规模动作空间的一种方法是先缩小候选范围,再由强化学习策略进行精细选择。候选集合可以由任务规则、启发式方法或单独训练的候选生成模型产生。设原始动作空间为 $\mathcal{A}$,候选生成过程得到状态相关集合:
\begin{equation}
\mathcal{A}_{\mathrm{cand}}(s_t)
\subseteq
\mathcal{A}
\end{equation}
策略只需要在候选集合中计算动作概率:
\begin{equation}
\pi_{\theta}
\left(
a\mid s_t,
a\in\mathcal{A}_{\mathrm{cand}}(s_t)
\right)
\end{equation}
这种方式可以显著降低单次决策的计算量,但候选生成过程本身也可能引入偏差。如果真正的最优动作被提前排除,后续策略无论如何训练都无法选择它。因此,候选集合既要足够小,又需要保留较高的有效动作覆盖率。
除了候选筛选,还可以将复杂动作拆分为多个较小决策。例如,在调度任务中,可以先选择待处理任务,再选择执行设备,最后确定开始时间。原本需要一次完成的组合动作被分解为多个阶段,每个阶段只处理其中一部分选择。
分解动作能够降低单次输出规模,但也会延长决策过程,并使后续选择依赖前面已经作出的决定。如果早期阶段选错了任务,后续阶段即使选择合理,也难以得到好的最终结果。因此,动作分解需要在单步决策难度和整体序列长度之间进行权衡。
分层强化学习采用相近思路,将长任务划分为不同层次。高层策略负责选择阶段目标,低层策略负责完成具体操作。例如,机器人导航可以先由高层策略选择需要到达的区域,再由低层策略控制移动方向和速度。这样可以减少高层策略需要处理的决策步数,也使低层策略能够重复用于不同目标。
长决策序列本身也是大规模任务的重要难点。在五子棋中,一步失误可能要经过多轮对弈后才表现为最终失败。随着回合长度增加,早期动作和最终奖励之间的距离越来越远,智能体更难判断每一步对结果的影响。
如果回合长度为 $T$,时刻 $t$ 的动作需要考虑之后的全部奖励:
\begin{equation}
G_t
=
\sum_{k=t}^{T-1}
\gamma^{k-t}r_k
\end{equation}
$T$ 较大时,回报会受到许多后续随机事件影响。同一个早期动作在不同轨迹中可能得到差异较大的回报,使价值估计和策略梯度具有较高方差。
折扣因子 $\gamma$ 可以降低远期奖励对当前动作的影响,但过小的折扣因子会使智能体过度关注短期收益。在五子棋中,如果策略只关注少数几步后的局面,可能会偏好立即形成局部棋型,却忽略更长时间后的防守风险。因此,长序列任务不能简单依靠减小折扣因子解决,还需要更准确的价值估计、合理的过程反馈或分层任务结构。
大规模问题还会提高环境交互成本。状态和动作覆盖不足时,通常需要采集更多轨迹;模型规模增加后,每次前向计算和参数更新也会消耗更多资源。在仿真环境中,可以通过并行运行多个环境提高数据采集速度:
\begin{equation}
\mathcal{D}
=
\bigcup_{i=1}^{M}
\mathcal{D}_i
\end{equation}
其中,$M$ 表示并行环境数量,$\mathcal{D}_i$ 表示第 $i$ 个环境采集的数据。
并行采样能够缩短收集相同数量数据所需的时间,但不能减少任务本身的样本需求。在真实机器人、工业设备和人工交互任务中,同时运行大量环境往往并不现实。此时可以利用历史数据、模拟环境或已有策略减少真实交互次数。
模拟环境能够低成本产生大量数据,但模拟过程与真实环境之间可能存在差异。若策略过度依赖模拟环境中特有的状态特征或动态规律,部署到真实系统后可能无法保持原有性能。因此,模拟训练通常还需要配合环境随机化、少量真实数据调整和独立测试。
在大规模任务中,课程学习也可以降低初始训练难度。课程学习先让智能体处理较简单的状态和任务,再逐渐增加环境复杂度。例如,五子棋训练可以先在较小棋盘或预设中期局面中学习基本进攻和防守,再逐步过渡到完整棋盘和完整对局。
课程设置必须保持前后任务之间的连续性。如果早期任务与最终任务差异过大,智能体在简单环境中学到的行为可能无法迁移到复杂环境。课程学习的作用是调整学习顺序,而不是替代对最终任务的训练。
这些方法都在改变困难的位置,而不是消灭困难。函数近似把表格存储变成分布外预测;候选筛选用较小计算量换取漏掉关键动作的风险;动作分解降低单步输出规模,却增加阶段依赖;模拟器扩大数据量,也引入模拟—现实偏差;课程学习降低早期难度,但课程断层会阻止能力迁移。
选择扩展方法时,要先找到实际瓶颈。候选动作方法需要报告候选召回率,函数近似需要检查分布外状态,动作分解则要评估早期错误能否恢复。并行采样能够缩短训练时间,却没有改变任务所需的样本数量。
规模问题解决后,仍需区分“同样条件能否复现”和“条件变化后是否有效”。
\subsection{强化学习训练的不稳定性和泛化性问题}
训练数据由策略产生,Critic 又用这些数据评价策略,于是形成反馈环:策略改变访问分布,访问分布改变价值估计,价值估计再改变策略。早期一次偶然胜局因此可能被后续自我对弈反复放大。
本节区分三个容易混用的概念。
\begin{table}[htbp]
\centering
\caption{稳定性、泛化性与鲁棒性的区别}
\label{tab:stability-generalization-robustness}
\begin{tabularx}{\linewidth}{>{\bfseries}p{2.5cm}p{4.2cm}X}
\toprule
性质 & 问题 & 检查方式 \\
\midrule
训练稳定性 & 相同配置重复训练,结果是否接近?训练中是否突然退化? & 多随机种子、学习曲线、梯度和价值诊断 \\
泛化性 & 规则不变但状态、初始条件或对手未见时,性能是否保持? & 冻结策略,在预先划分的未见场景测试 \\
鲁棒性 & 观测噪声、参数扰动或有限对抗变化下,性能是否平稳退化? & 分级扰动和压力测试 \\
\bottomrule
\end{tabularx}
\end{table}
训练曲线平滑只提供第一类问题的部分证据,不能证明后两类性质。
图中的波动可以从四个位置诊断:样本是否过少而放大偶然轨迹;Actor 是否移动过快;Critic 是否在变化的数据上产生系统偏差;探索是否过早消失或长期过强。它们可能同时发生,不能仅凭“回报下降”判断超参数方向。
在 Actor-Critic 框架中,时序差分误差为:
\begin{equation}
\delta_t
=
r_t
+
\gamma V_{\phi}(s_{t+1})
-
V_{\phi}(s_t)
\end{equation}
TD 目标中的当前价值与下一状态价值都来自正在变化的 Critic,下一状态误差会通过 bootstrap 向前传播。Actor 过快会让 Critic 追逐不断移动的数据分布,Critic 在小批次上过拟合又会向 Actor 提供过度自信的优势。PPO 裁剪只缓解策略目标的一部分,不能修正错误价值目标。
诊断时应成组观察采样规模、近似 KL、裁剪比例、策略熵、解释方差、价值误差和梯度范数。单独把学习率调小可能只是让错误方向走得更慢。梯度裁剪可以拦住个别异常更新:
\begin{equation}
g
\leftarrow
g
\cdot
\min
\left(
1,
\frac{c}{\|g\|}
\right)
\end{equation}
其中,$g$ 表示当前梯度,$c$ 表示允许的最大梯度范数。梯度裁剪可以避免个别异常样本造成过大的参数更新,但不能解决价值目标错误或奖励设计不合理等根本问题。
多次重复实验也是判断训练稳定性的重要手段。设使用 $M$ 个随机种子得到的评估结果分别为 $J_1,J_2,\ldots,J_M$,可以计算平均性能:
\begin{equation}
\overline{J}
=
\frac{1}{M}
\sum_{i=1}^{M}
J_i
\end{equation}
同时还应报告不同实验之间的波动。如果一种方法只有少数实验能够取得较高性能,而多数实验表现较差,仅报告最好结果会高估方法的可靠性。
多随机种子均值与离散程度用于回答训练能否复现,仍不能回答未见场景是否有效。
在五子棋任务中,如果智能体始终与同一个规则对手进行训练,它可能学会专门利用该对手的固定弱点。当评估对手改变棋路时,策略的胜率可能迅速下降。此时,训练过程本身可能十分稳定,训练胜率也持续提高,但模型学习到的是针对单一对手的行为,而不是普遍适用的棋局判断能力。
泛化问题常与训练数据覆盖范围有关。如果训练过程中只出现少量初始状态,策略便可能记住这些状态下的有效动作,而没有学会更一般的决策规律。五子棋若始终从空棋盘开始,并且对手开局方式十分固定,模型可能只熟悉少数常见开局。
为了检查这种情况,可以在评估时使用不同的开局状态,例如随机放置少量合法棋子后开始比赛,或从历史棋谱的中间局面继续对弈。若策略只能在标准空棋盘开局中取得较好结果,说明其局面适应能力仍然有限。
环境中的无关特征也可能被策略错误利用。假设训练环境中某类棋盘显示方式总是与某个对手绑定,模型可能根据显示差异识别对手,而不是根据棋盘局面判断动作。当显示方式改变后,性能便会下降。
在机器人和自动驾驶任务中,类似问题可能来自光照、背景、传感器噪声或模拟环境中的固定纹理。策略可能依赖这些与任务目标无关的特征,导致环境外观稍有变化时无法正常决策。
提高泛化能力的一种方法是增加训练环境的多样性。五子棋可以使用不同水平、不同棋风和不同历史版本的策略组成对手池,使智能体不能只依赖某个对手的固定行为。训练中还可以随机交换先后手、改变初始棋盘状态,并加入不同类型的规则策略。
对于连续控制任务,可以在合理范围内随机改变物体质量、摩擦系数、传感器误差和环境布局。策略只有在多种条件下都取得较好回报,才更可能学到稳定的控制规律。这种做法通常称为环境随机化或域随机化。
状态数据增强也可以利用任务中的对称结构。五子棋棋盘经过旋转或翻转后,棋局的基本规则保持不变。训练时可以对同一棋盘进行旋转和镜像变换,并对动作位置作出相应调整,使模型接触更多等价局面。
设状态变换为 $T_s$,对应的动作变换为 $T_a$。如果变换不改变任务含义,则策略应当满足近似一致性:
\begin{equation}
\pi_{\theta}
\left(
T_a(a)\mid T_s(s)
\right)
\approx
\pi_{\theta}(a\mid s)
\end{equation}
这种一致性要求可以通过数据增强自然学习,也可以通过额外损失进行约束。它能够减少模型对棋盘绝对方向的依赖,提高对等价局面的利用效率。
模型结构也会影响泛化能力。与任务结构相匹配的网络通常更容易学习可复用特征。五子棋使用卷积网络能够在不同棋盘位置共享局部棋型检测参数,比为每个格点独立设置大量参数更有利于位置泛化。
正则化方法可以减少模型对训练数据的过度拟合,例如限制网络规模、使用权重衰减或在状态输入中加入适量噪声。不过,强化学习中的泛化问题不能只通过监督学习中的正则化解决,因为训练数据分布还取决于策略本身。更重要的是扩大训练场景和评估场景的覆盖范围。
评估变化需要分级,否则无法解释性能下降。第一层保持训练分布,检查任务是否学会;第二层保持规则不变,只更换合法开局、随机种子和未见对手,检查泛化;第三层加入观测噪声或合理参数扰动,检查鲁棒性。规则和目标都发生改变时,测到的是任务迁移,不应继续称为同一任务的泛化。
五子棋应分别测试训练对手、冻结的未见对手、合法中间局面、先后手和旋转翻转等价局面。失败类型决定修正方向:未见对手下降指向对手覆盖,中间局面下降指向状态覆盖,对称变换下降则说明模型没有学到规则本身具有的对称性。
稳定与泛化并不总是同步。单一训练分布可能让策略平滑地收敛到一个对手特化解;训练波动较大时,不同检查点又可能表现出不同的泛化结果。因此,多种子重复训练回答“能否复现”,未见场景测试回答“能否迁移到同一任务的新条件”,扰动测试回答“性能怎样退化”。一条平滑曲线不能代替后两种实验。
最后一种失败更隐蔽:训练可复现、未见场景也表现稳定,但策略优化的根本不是设计者真正关心的目标。
\subsection{强化学习方法的奖励构建问题}
前文讨论了怎样设置奖励,这里进一步看一种更棘手的情况:奖励已经可以计算,训练也能收敛,结果仍然可能不符合预期。训练回报上升而真实指标不变,往往说明可计算奖励只是一个不完整的代理目标;终局结果明确而早期动作更新混乱,问题来自反馈延迟;策略反复利用某条规则刷取高分,则属于奖励投机。任务同时追求速度、安全和能耗时,困难又不只是计算奖励,而是这些目标之间本来就需要人为取舍。
若可计算奖励为 $R$,算法求解的是
\begin{equation}
\pi_R^*
=
\underset{\pi}{\arg\max}
\;
\mathbb{E}_{\pi}
\left[
\sum_{t=0}^{T-1}
\gamma^t R(s_t,a_t,s_{t+1})
\right]
\end{equation}
这只定义了奖励最优策略 $\pi_R^*$。设计者真正关心的任务效用若记为 $U$,一般没有理由保证最大化 $R$ 同时最大化 $U$。五子棋胜率接近真实目标,但活三数量只是代理指标;暂时放弃局部棋型可能反而是长期好棋。代理指标越容易反复获取,错位越容易被优化放大。
当奖励只在任务结束时给出时,还会出现明显的信用分配问题。信用分配关注的是最终结果应当归因于此前哪些动作。五子棋智能体在一局对弈结束后得到了失败奖励,但这次失败可能来自最后一步没有阻挡,也可能来自更早之前破坏了自身棋型,还可能是连续多步选择共同造成的结果。
设一条轨迹包含动作序列:
\begin{equation}
a_0,a_1,\ldots,a_{T-1}
\end{equation}
如果只有终止时刻得到奖励 $r_{T-1}$,那么较早动作与最终奖励之间相隔较远。强化学习需要依靠回报和价值估计,将最终结果逐步传递到此前状态:
\begin{equation}
G_t
=
\sum_{k=t}^{T-1}
\gamma^{k-t}r_k
\end{equation}
当决策序列较长时,早期动作的回报会受到大量后续动作和环境变化影响。同一个动作在不同轨迹中可能获得完全不同的结果,使模型难以准确判断它对最终目标的贡献。
过程奖励缩短信号延迟,也把人工判断写进目标。为“形成活三”加分无法表达“此刻必须先防守”的局面依赖。更详细的规则未必更准确,只可能让策略更快学会设计者已经写出的启发式偏好。加入塑形奖励后必须重新检查最优行为是否发生变化。
奖励投机比一般的目标近似误差更具体:策略找到了一条规则允许、但设计者没有预料到的高回报路径。同一棋型被重复计分、机器人靠摆动刷取瞬时速度、推荐系统靠诱导性内容刷点击都属于这种情况。测试不能只验证“预期好行为能否得高分”,还要反向搜索“还有哪些行为能得到同样高分”。
复杂任务通常还包含多个目标。例如,一个控制系统既需要完成任务,又需要降低能耗、减少时间并保证安全。常见方法是将多个奖励项进行加权组合:
\begin{equation}
r_t
=
\sum_{i=1}^{K}
\lambda_i r_t^{(i)}
\end{equation}
其中 $\lambda_i$ 不是纯技术超参数,而是对速度、能耗、安全等目标作出的偏好声明。完成更快但能耗更高,与更慢但更安全的策略之间可能不存在唯一最优解。调权重不能替代需求方作出取舍,而且相同权重也不代表相同影响:各项的尺度和出现频率不同。多智能体协作还会叠加个体贡献分配问题,本章不在此展开。
难以手写奖励时,可以借助专家示范、逆强化学习或成对偏好。它们改变的是监督来源,不会消除目标错位:示范只覆盖专家到达的状态,评价者可能存在分歧,学习到的奖励模型仍会在训练分布外外推。策略持续优化这个模型时,同样可能发现模型漏洞。
无论奖励来自规则还是学习模型,都要保留与它不同源的评估信号。五子棋用独立对局胜率和失败棋局审计;控制任务可以使用任务完成率、安全事件和资源消耗。如果训练回报提高而这些指标不变,继续增加训练时间只会更充分地优化错误目标。
奖励审查应以轨迹为单位。分别抽取高回报、低回报、指标冲突和约束边界附近的轨迹,检查策略怎样获得每一项分数;再做奖励消融和权重敏感性实验,观察行为是否发生预期变化。只看奖励公式和均值曲线,无法发现策略实际利用的路径。
高回报只说明策略善于优化当前奖励。代理指标是否错位、反馈是否过度延迟、规则是否存在可利用路径,还需要通过独立指标、轨迹审计和消融实验判断。
几类挑战会相互放大:覆盖不足使奖励模型在数据外失真,训练波动改变策略利用奖励的方式,单一环境又让这些问题在训练指标中隐藏起来。因此,算法诊断、奖励审计和独立评估不能彼此替代。
\section{本章小结}
强化学习关注的是带有反馈和时间关联的序列决策:当前动作不仅产生即时结果,还会改变后续状态、数据分布和可选动作。从早期的控制、调度和博弈,到深度强化学习处理高维状态,再到大模型利用人类偏好或可验证结果进行后训练,应用对象不断变化,但核心问题始终是怎样用有限反馈改进长期行为。强化学习在大模型时代是连接已有能力与目标行为的重要工具,却不能替代预训练、监督微调和可靠评估。
五子棋把这一问题具体化:程序在终局才知道输赢,它怎样评价几十步以前的落子?回报把当前动作与未来结果联系起来,价值函数把这种联系写成可以递推估计的形式,策略梯度则直接改变动作概率。Actor-Critic 用正在学习的价值模型提供更及时的评价,因此减小了蒙特卡洛回报的部分波动,也把 Critic 的偏差带进了策略更新。PPO 试图让这种更新不要一次走得太远,但裁剪不是稳定性的保证。
当训练只能依靠历史数据时,问题从“怎样探索”变成“哪些判断有数据支持”。离线强化学习可以在行为数据之上改进策略,却无法现场验证陌生动作。进入具体任务后,算法只是整个系统的一部分:观测是否充分、动作能否执行、奖励是否代表真实目标,以及测试数据是否独立,都会改变最终结论。
因此,训练曲线上升之后仍要回到任务本身:策略究竟依据了哪些信息,数据怎样随策略改变,最后报告的指标是否真的对应任务目标。只说明实验采用了哪一种算法,还不足以回答这些问题。
训练回报高只说明当前奖励被有效优化;PPO 裁剪不保证训练稳定;离线数据量大不等于关键动作得到覆盖;输入更多信息也可能造成泄漏;密集奖励未必更接近真实目标;最后一个检查点未必优于验证集选出的检查点。这些反例指向同一个原则:算法输出只能在任务定义、数据来源和评估协议给定的范围内解释。
\section*{练习}
\begin{exercisebox}
\begin{enumerate}
\item \textbf{基础·价值递推} 设智能体从时刻 $t$ 开始获得的折扣回报为
\[
G_t=\sum_{k=t}^{T-1}\gamma^{k-t}r_k
\]
请先证明
\[
G_t=r_t+\gamma G_{t+1}
\]
再根据这一递推关系,写出策略 $\pi$ 下动作价值函数 $Q^\pi(s_t,a_t)$ 的贝尔曼期望方程,并说明最优动作价值函数中为什么会出现
$\max_{a'}Q^*(s_{t+1},a')$
\item \textbf{基础·Actor-Critic} 在某一五子棋状态 $s_t$ 下,Actor 选择位置 $B$ 落子,且该转移没有自然终止。已知即时奖励为
$r_t=0$,折扣因子为 $\gamma=0.9$,Critic 对落子前后状态的价值估计分别为
\[
V_\phi(s_t)=3.5,\qquad V_\phi(s_{t+1})=5.0
\]
计算时序差分目标 $y_t$ 和时序差分误差 $\delta_t$。根据计算结果,说明 Actor 应提高还是降低位置 $B$ 的选择概率,并说明 Critic 应如何调整对 $V_\phi(s_t)$ 的估计。
\item \textbf{进阶·PPO 裁剪} 在一次 PPO 更新中,旧策略选择动作 $a_t$ 的概率为 $0.40$,裁剪阈值为
$\epsilon=0.2$。分别考虑以下两种情况:
\[
\text{情况一:}\quad \widehat{A}_t=2,\qquad \pi_\theta(a_t\mid s_t)=0.56
\]
\[
\text{情况二:}\quad \widehat{A}_t=-3,\qquad \pi_\theta(a_t\mid s_t)=0.20
\]
对每种情况计算概率比 $\rho_t(\theta)$、裁剪后的概率比,以及 PPO 裁剪目标中的两个候选项。判断最终采用哪一项,并解释裁剪机制如何限制策略发生过大变化。
\item \textbf{进阶·离线强化学习} 判断下列说法是否正确,并简要说明理由:
“离线强化学习使用固定历史数据进行训练,因此只要数据量足够大,就可以像在线强化学习一样放心地选择数据集中从未出现过的动作。”
回答时应说明行为策略与目标策略的区别,并结合分布偏移、外推误差和保守价值估计解释离线强化学习为什么需要对未知动作保持谨慎。
\item \textbf{设计·任务建模} 现需要设计一个仓库移动机器人强化学习任务。机器人需要从起点到达指定货架,同时尽量缩短时间、降低能耗,并避免与障碍物发生碰撞。请完成以下设计:
\begin{enumerate}
\item 给出一种状态表示和动作空间
\item 区分任务中的核心奖励、辅助奖励和惩罚项
\item 判断“不能穿过障碍物”和“尽量减少能耗”分别应采用硬约束还是软约束
\item 说明至少两个用于独立评估策略性能的指标
\end{enumerate}
并简要分析奖励权重设置不合理时,机器人可能出现的非预期行为。
\item \textbf{设计·实验诊断} 某五子棋智能体在训练对手上的胜率达到 $90\%$,但更换为未参与训练的规则策略后,胜率下降到 $45\%$。同时,使用不同随机种子重复训练时,最终胜率波动较大。请分别从训练不稳定性和泛化性两个方面分析可能原因,并提出改进方案。
回答中至少应涉及以下内容中的四项:数据采样规模、Actor 与 Critic 的更新速度、策略熵、对手池、随机开局、棋盘旋转与翻转增强、多随机种子评估、独立测试环境。
\end{enumerate}
\end{exercisebox}
\end{document}
% !TeX root = ../main.tex
% 本文件由 section2/aml_notes.tex 抽取正文生成,保留章节目录独立性。
\chapter{强化学习基础}
\chapterinfo{朱梓铭、徐欣怡、王成龙}
许多智能任务并不是对一个静态输入给出一次答案,而是连续作出选择。机器人当前的转向会改变下一时刻的位置,调度系统当前分配的资源会影响后续任务的等待时间,推荐系统展示的内容也可能改变用户之后的行为。此类任务中的动作既影响即时结果,也改变未来能够看到的状态和可以采取的动作。强化学习研究的正是这种\keyword{智能体通过与环境交互,依据反馈学习长期决策策略}的问题。
强化学习并非随着大模型才出现。它吸收了行为心理学中的试错学习、动态规划与最优控制等思想,早期主要用于控制、机器人、资源调度和博弈等序列决策问题。与监督学习依赖“输入—标签”样本不同,强化学习通常只得到奖励或代价:智能体必须探索不同动作,判断延迟出现的结果应当归因于此前哪些决策,并在利用已有经验与尝试未知行为之间作出权衡。价值函数、时序差分学习和策略优化等方法,都是围绕这些困难逐步发展起来的。
深度学习的发展进一步扩大了强化学习能够处理的状态规模。深度神经网络可以直接从图像等高维输入中学习表示,DQN 在一组 Atari 游戏中展示了端到端学习决策策略的能力;随后,深度强化学习与搜索、自我对弈结合,在围棋等复杂博弈中取得重要进展。强化学习的应用也由相对低维的控制问题,扩展到高维感知、复杂策略和大规模函数近似场景。
大模型出现后,强化学习获得了新的角色。预训练通过预测海量语料中的下一个词元学习通用知识和语言能力,但“更像训练语料”并不自动等于更符合用户意图。大模型后训练可以把模型看作策略,把生成过程看作一串动作,再利用人类偏好、奖励模型、规则检查器或可自动验证的任务结果提供反馈。基于人类反馈的强化学习(Reinforcement Learning from Human Feedback, RLHF)已被用于改善指令遵循和人类偏好对齐;在答案能够可靠验证的数学、编程等任务中,强化学习还可用于强化多步推理行为。
因此,在当下的大模型技术体系中,强化学习是连接“已有模型能力”与“目标行为”的重要后训练手段,也是训练可交互智能体和自主决策系统的基础工具。它并不替代预训练、监督微调、搜索或工具调用,也不是所有任务都必须采用的方案:只有当目标能够转化为可信反馈、决策会影响后续过程,并且训练数据与探索成本可以控制时,强化学习的优势才更容易发挥。奖励若不能代表真实目标,策略反而可能更有效地利用奖励漏洞;反馈昂贵或不能在线试错时,也需要离线强化学习、模仿学习或其他方法共同解决。
本章沿着三个问题展开:怎样把持续交互的任务写成一个明确的数学模型;怎样利用有限的交互数据判断动作的长期价值并改进策略;当算法进入真实任务时,怎样检查状态、动作、奖励和评估方案是否设计正确。为使这些抽象问题始终落到同一任务上,下面引入五子棋作为贯穿案例。
设想一个刚开始训练的五子棋程序。它在一局棋中落下几十枚棋子,直到对局结束才得到胜负结果。如果程序输了,究竟是哪一步出了问题?最后一次漏掉防守当然可疑,但更早的一次落子也可能已经让局面无法挽回。反过来,一步当时没有得分的落子,也可能为十几步后的胜利创造条件。程序的每次选择都会改变后续局面,训练数据也会随着策略改变而变化,因而不能只用一次静态的“输入—标签”预测描述这一过程。
五子棋在本章中的作用是说明方法,而不是把某一种算法包装成完整的现成解法。除非另有说明,后文采用同一套设定:棋盘大小为 $15\times15$,智能体执黑棋,对手及棋盘规则属于环境。智能体观察完整棋盘,在每个空位中选择一次落子;环境完成合法性检查、更新棋盘并执行对手回应,然后返回智能体下一次决策时看到的局面。任一方形成五子连线或棋盘填满时,对局自然终止。胜、负、平的核心奖励分别为 $1$$-1$$0$。本章中的数值计算只用于解释算法更新,不代表一套经过训练验证的五子棋系统。
\begin{learninggoals}
\begin{itemize}
\item 能够把一个序列决策任务表示为马尔可夫决策过程,并区分环境状态、智能体观测、即时奖励和长期回报
\item 能够解释 Q-learning、策略梯度、Actor-Critic 和 PPO 的更新依据,以及这些方法各自试图解决的问题
\item 能够根据任务目标设计状态、动作、奖励、约束和评估方案,并识别离线数据、训练稳定性、泛化和奖励偏差带来的风险
\end{itemize}
\end{learninggoals}
\section{强化学习建模方法}
一个强化学习任务首先要说清楚两件事:谁在作决定,以及一次决定会怎样改变后续处境。如图\ref{fig:framework}所示,作决定的部分称为智能体;其余能够影响决策结果、但不由智能体直接控制的部分统称为环境。智能体接收信息、选择动作,环境执行动作并返回新的信息和奖励。下一次决策建立在这次交互产生的结果之上。
\begin{figure}[htbp]
\centering
\IfFileExists{figures/framework.tex}{
\resizebox{0.78\linewidth}{!}{
\input{figures/framework.tex}
}
}{
\fbox{
\begin{minipage}[c][5cm][c]{0.78\linewidth}
\centering
强化学习的基本建模框架 \texttt{figures/f\_framework.tex}
\end{minipage}
}
}
\caption{强化学习的基本建模框架}
\label{fig:framework}
\end{figure}
图中的边界并非总是唯一的。训练五子棋程序时,可以把对手看作环境的一部分,也可以把双方都看作独立智能体。两种划分会产生不同的数据和训练方法。因此,建模不是给既定对象贴上几个名称,而是决定模型能够观察什么、控制什么,以及环境负责完成什么。
\subsection{马尔可夫决策过程}
马尔可夫决策过程(Markov Decision Process, MDP)是描述上述交互的一种标准模型。它的关键要求不是“任务没有历史”,而是当前状态已经概括了预测未来所需的历史信息。给定当前状态和动作后,更早的状态、动作不应再改变下一状态的条件分布:
\begin{equation}
P(s_{t+1} \mid s_t, a_t, s_{t-1}, a_{t-1}, \ldots, s_0, a_0)
=
P(s_{t+1} \mid s_t, a_t)
\end{equation}
其中,\(s_t\) 是环境在时刻 \(t\) 的状态,\(a_t\) 是随后执行的动作。这个等式是对状态表示的要求,而不是可以无条件接受的事实。如果机器人只输入一张静止图像,它可能无法判断前方物体正在靠近还是远离;此时单帧图像就不是充分状态。可以加入连续观测、速度信息或模型的记忆状态,重新构造更充分的决策信息。
一个 MDP 记为五元组:
\begin{equation}
\mathcal{M} = (\mathcal{S}, \mathcal{A}, P, R, \gamma)
\end{equation}
其中,\(\mathcal{S}\) 是状态空间,\(\mathcal{A}\) 是动作空间,\(P\) 是状态转移分布,\(R\) 是奖励函数,\(\gamma\in[0,1]\) 是折扣因子。转移分布可写为
\begin{equation}
P(s'\mid s,a)=\Pr(S_{t+1}=s'\mid S_t=s,A_t=a)
\end{equation}
奖励函数则描述一次转移产生的即时反馈:
\begin{equation}
R(s,a,s')=\mathbb{E}[R_t\mid S_t=s,A_t=a,S_{t+1}=s']
\end{equation}
正文后续用小写 \(r_t\) 表示一次实际交互得到的奖励样本。奖励函数可以采用其他等价定义,例如只依赖 \((s,a)\);关键是全章使用同一时间约定:智能体在 \(s_t\) 执行 \(a_t\),随后收到 \(r_t\) 并进入 \(s_{t+1}\)
一次从初始状态到终止状态的完整交互称为轨迹:
\begin{equation}
\tau=(s_0,a_0,r_0,s_1,a_1,r_1,\ldots,a_{T-1},r_{T-1},s_T)
\end{equation}
这里 \(s_T\) 是终止状态,最后一次动作和奖励分别是 \(a_{T-1}\)\(r_{T-1}\)。固定这一约定可以避免后续回报和 TD 目标出现一位下标偏差。
\begin{importantnote}
马尔可夫性取决于状态如何构造。任务具有历史依赖,并不意味着它一定无法使用 MDP;真正的问题是当前状态是否已经保存了影响未来的历史信息。
\end{importantnote}
\subsection{关键元素介绍}
\ref{tab:rl-elements} 将 MDP 中的主要对象与五子棋任务对应起来。这里把对手划入环境,并规定智能体每次只控制自己的落子。
\begin{table}[htbp]
\centering
\caption{强化学习基本元素及其在五子棋中的对应关系}
\label{tab:rl-elements}
\begin{tabularx}{\linewidth}{>{\bfseries}p{2.2cm}p{3.2cm}X}
\toprule
对象 & 数学记号 & 五子棋中的含义 \\
\midrule
智能体 & --- & 需要学习落子策略的棋手 \\
环境 & --- & 棋盘规则、胜负判断以及对手行为 \\
环境状态 & $s_t$ & 完整棋盘、当前行动方及规则所需信息 \\
智能体观测 & $o_t$ & 实际提供给策略模型的信息;完全可观测时可与 $s_t$ 相同 \\
动作 & $a_t\in\mathcal A$ & 在一个合法空位落子 \\
即时奖励 & $r_t$ & 本次落子及随后转移产生的反馈 \\
策略 & $\pi(a\mid s)$ & 给定局面时各合法落子的选择规则或概率 \\
轨迹 & $\tau$ & 从开局到胜、负或平局的完整交互序列 \\
\bottomrule
\end{tabularx}
\end{table}
状态与观测的区别值得单独保留。环境状态是对真实决策过程的描述,观测则是智能体实际拿到的输入。五子棋棋盘通常完全可见,因此二者可以相同;机器人被遮挡的视野、推荐系统中未被记录的用户意图则使二者产生差异。后文为了简化公式,在完全可观测的讨论中使用 \(s_t\) 作为策略输入;涉及信息缺失时再显式使用 \(o_t\)
策略可以是确定性的,也可以是随机的。确定性策略为每个状态指定一个动作;随机策略写成条件分布
\begin{equation}
\pi(a\mid s)=\Pr(A_t=a\mid S_t=s)
\end{equation}
训练的目标不是让策略复现某一条固定轨迹,而是让它在可能遇到的状态中作出长期效果更好的选择。
\subsection{优化目标}
一步动作的即时奖励不能完整说明它是否合理。从时刻 \(t\) 开始,智能体关心的是随后整段交互的折扣回报
\begin{equation}
G_t=\sum_{k=t}^{T-1}\gamma^{k-t}r_k
\end{equation}
折扣因子 \(\gamma\) 决定未来奖励在当前评价中的权重。较小的 \(\gamma\) 强调近期结果;接近 1 的 \(\gamma\) 保留更多长期影响。在有限回合任务中可以取 \(\gamma=1\);对于可能无限持续的任务,则需要保证回报及其期望有良好定义。
整条轨迹从初始时刻获得的回报记为 \(R(\tau)=G_0\)。参数化策略 \(\pi_\theta\) 会诱导出轨迹分布 \(p_\theta(\tau)\):策略改变后,智能体访问的状态和收集到的数据也随之改变。策略的性能定义为轨迹回报的期望:
\begin{equation}
J(\theta)
=
\mathbb{E}_{\tau\sim p_\theta(\tau)}[R(\tau)]
\end{equation}
训练希望找到使该期望尽可能大的参数:
\begin{equation}
\theta^{*} = \arg\max_{\theta} J(\theta)
\end{equation}
这个目标有两层难点。第一,不能枚举所有轨迹,只能用采样数据估计策略表现;第二,策略一旦改变,数据分布也会改变。后续算法的差别,主要在于它们怎样估计动作的长期影响,以及怎样利用这种估计更新策略。
\section{强化学习的基本优化算法}
在前一节中,强化学习问题被形式化为长期累积奖励最大化问题,其核心目标是寻找能够使性能函数 $J(\theta)$ 最大化的最优策略。然而,在实际任务中,环境的状态转移规律通常难以完全获得,所有可能的交互轨迹也无法被逐一枚举,因此很难直接计算性能函数并求解最优策略。强化学习算法需要利用智能体与环境交互产生的有限样本,对不同状态和动作所带来的长期收益进行估计,并依据估计结果不断改进决策策略。
根据策略改进方式的不同,强化学习的基本优化算法可以分为基于价值函数的方法和基于策略优化的方法。基于价值函数的方法首先估计某一状态或状态与动作组合能够获得的长期回报,再根据价值估计结果选择动作,将策略优化问题转化为价值函数的迭代更新问题。基于策略优化的方法则直接构建参数化策略,通过调整策略参数,提高高回报动作被选择的概率,从而实现对长期累积奖励的优化。两类方法在优化对象、动作选择方式和适用场景等方面具有不同特点。
在此基础上,Actor-Critic 框架将价值估计与策略优化结合起来。其中,Actor 负责根据当前状态选择动作并更新策略,Critic 负责评估当前策略或动作的长期收益,并为 Actor 的更新提供指导。该框架既保留了策略优化方法处理随机策略和连续动作空间的能力,也利用价值函数降低策略更新过程中的估计波动,成为许多现代强化学习算法的基础。
\subsection{基于价值函数的方法}
五子棋中的多数落子不会立即产生胜负奖励,却会改变后续局面。价值方法要解决的问题正是:当即时奖励不足以评价动作时,怎样估计它对未来回报的影响?
在策略 $\pi$ 下,状态价值函数定义为
\begin{equation}
V^{\pi}(s_t)
=
\mathbb{E}_{\pi}\!\left[G_t\mid S_t=s_t\right]
\end{equation}
它评价的是“处于这个状态,并继续按照 $\pi$ 行动”的期望回报。动作价值函数进一步固定当前动作:
\begin{equation}
Q^{\pi}(s_t,a_t)
=
\mathbb{E}_{\pi}\!\left[G_t\mid S_t=s_t,A_t=a_t\right]
\end{equation}
因此,$V^\pi$ 比较局面,$Q^\pi$ 比较同一局面中的候选动作。两者都依赖后续策略:同一个棋盘局面由强策略继续下和由随机策略继续下,其价值一般不同。
图~\ref{fig:gobang_value} 给出了某一棋盘局面下不同候选落子位置的动作价值,以及智能体依据动作价值进行落子选择的过程。
\begin{figure}[htbp]
\centering
\IfFileExists{figures/gobang_value.tex}{
\resizebox{0.92\linewidth}{!}{
\input{figures/gobang_value.tex}
}
}{
\fbox{
\begin{minipage}[c][6cm][c]{0.92\linewidth}
\centering
五子棋某一局面下的候选落子位置及动作价值示意\\
\texttt{figures/gobang\_value.tex}
\end{minipage}
}
}
\caption{基于动作价值的五子棋落子选择}
\label{fig:gobang_value}
\end{figure}
图中位置 $(8,8)$ 的估计价值最高。若这些估计足够准确,贪心策略会在合法动作集合 $\mathcal A(s_t)$ 中选择
\begin{equation}
a_t
=
\underset{a\in\mathcal{A}(s_t)}{\arg\max}
Q(s_t,a)
\end{equation}
这里最难的部分不是取最大值,而是得到可信的 $Q$ 值。回报满足
\begin{equation}
G_t=r_t+\gamma G_{t+1}
\end{equation}
所以长期问题可以拆成“本次奖励”和“下一状态之后的回报”。对最优动作价值函数,这一递推成为贝尔曼最优方程:
\begin{equation}
Q^{*}(s_t,a_t)
=
\mathbb{E}\!\left[
r_t+\gamma\max_{a'\in\mathcal{A}(s_{t+1})}
Q^{*}(s_{t+1},a')
\mid s_t,a_t\right]
\end{equation}
期望来自环境转移的不确定性;最大值表示下一状态采用当前认为最优的动作。若 $s_{t+1}$ 是自然终止状态,后续回报为零,最大值项也应置零。
Q-learning 不要求提前知道转移概率。每观察到一次转移 $(s_t,a_t,r_t,s_{t+1})$,它就用单步目标
\begin{equation}
y_t=r_t+\gamma\max_{a'\in\mathcal A(s_{t+1})}Q(s_{t+1},a')
\end{equation}
修正旧估计:
\begin{equation}
Q(s_t,a_t)
\leftarrow
Q(s_t,a_t)
+\alpha\left[y_t-Q(s_t,a_t)\right]
\end{equation}
其中 $\alpha$ 是学习率,差值 $y_t-Q(s_t,a_t)$ 是这次样本带来的时序差分误差。更新目标使用下一状态的最大动作价值,而采集样本时不必始终执行贪心动作;这正是 Q-learning 的离策略特征。
以图中的位置 $(8,8)$ 为例。设旧估计为 $2.0$,落子后的即时奖励为 $0$,下一决策状态的最大动作价值为 $6.0$,并取 $\gamma=0.9$$\alpha=0.5$。图~\ref{fig:q_learning_update} 展示了这次更新。
\begin{figure}[htbp]
\centering
\IfFileExists{figures/q_learning_update.tex}{
\resizebox{0.94\linewidth}{!}{
\input{figures/q_learning_update.tex}
}
}{
\fbox{
\begin{minipage}[c][5.5cm][c]{0.94\linewidth}
\centering
Q-learning 动作价值更新过程\\
\texttt{figures/q\_learning\_update.tex}
\end{minipage}
}
}
\caption{Q-learning 动作价值更新过程示意}
\label{fig:q_learning_update}
\end{figure}
单步目标为
\begin{equation}
y_t=0+0.9\times6.0=5.4
\end{equation}
因此
\begin{equation}
Q(s_t,(8,8))
\leftarrow
2.0+0.5\times(5.4-2.0)
=3.7
\end{equation}
目标值高于原估计,说明这次观察到的后续局面比原先预期更好。一次更新只把估计向 $5.4$ 推近,并不会立刻把它改成目标值。反复采样让终局奖励逐步传回更早的状态,但传递速度和估计质量仍取决于数据覆盖、学习率与函数近似方式。
如果始终选择当前估计最高的动作,智能体可能永远收集不到其他动作的数据。常用的 $\varepsilon$-greedy 行为策略是
\begin{equation}
a_t
=
\begin{cases}
\text{$\mathcal{A}(s_t)$ 中随机选择动作}, & \text{概率为 }\varepsilon,\\
\displaystyle
\underset{a\in\mathcal{A}(s_t)}{\arg\max}Q(s_t,a),
& \text{概率为 }1-\varepsilon.
\end{cases}
\end{equation}
$\varepsilon=0.1$ 时,约 $10\%$ 的决策在合法动作中随机探索。训练后期常逐渐减小 $\varepsilon$,但不能只按固定日程机械衰减:如果重要状态仍未被覆盖,过早停止探索只会把当前误差固化下来。
\begin{importantnote}
Q-learning 的示例说明了价值如何从下一状态传回当前动作,并不意味着表格型 Q-learning 能够直接解决完整五子棋。即使不考虑棋子顺序,$15\times15$ 棋盘的合法局面数量也极其庞大,绝大多数状态不可能被重复访问到足够多次。
\end{importantnote}
小规模任务可以用表格保存每个状态—动作对的估计;大规模任务则要借助神经网络等函数近似器,让相似状态共享统计信息。深度 Q 网络属于后一类方法,但它还需要经验回放、目标网络等稳定化设计,本章不在这里展开。价值方法也更自然地适用于能够枚举或有效比较候选动作的场景。下一节换一个角度:不先为每个动作学习一个价值,再取最大值,而是直接调整动作的选择概率。
\subsection{基于策略优化的方法}
价值方法先回答“每个动作值多少”,再选择价值最高的动作。策略方法跳过这个中间决策规则,直接学习“在当前状态下,各动作应当以多大概率被选择”。这使它能够自然表示随机策略,也不必在连续动作空间中枚举所有候选动作。
设参数化策略为:
\begin{equation}
\pi_{\theta}(a_t\mid s_t)
\end{equation}
其中 $\theta$ 是策略参数。离散动作策略输出概率质量函数;连续动作策略通常输出高斯分布等概率分布的参数。两种情况下,训练期间都可以从分布中采样动作。
在五子棋中,策略网络接收棋盘状态,输出各位置的 logits。合法动作掩码排除已经落子的格点,归一化后得到空位上的概率分布。图~\ref{fig:gobang_policy_distribution} 给出一个简化示例。
\begin{figure}[htbp]
\centering
\IfFileExists{figures/gobang_policy.tex}{
\resizebox{0.94\linewidth}{!}{
\input{figures/gobang_policy.tex}
}
}{
\fbox{
\begin{minipage}[c][6cm][c]{0.94\linewidth}
\centering
策略模型输出的五子棋落子概率分布\\
\texttt{figures/gobang\_policy.tex}
\end{minipage}
}
}
\caption{策略模型输出的五子棋落子概率分布}
\label{fig:gobang_policy_distribution}
\end{figure}
图中四个候选位置的概率分别为 $0.15$$0.40$$0.25$$0.20$。位置 $B=(8,8)$ 最可能被选中,但并非必然被选中:
\begin{equation}
a_t
\sim
\pi_{\theta}(\cdot\mid s_t)
\end{equation}
这种随机性保留了探索机会。评估时是否继续采样要由评估目标决定;如果只取最大概率动作,测得的是确定性部署规则的表现,而不是训练时随机策略的完整表现。
策略应怎样利用对弈结果改变这些概率?设 $\pi_\theta$ 生成一条轨迹
\begin{equation}
\tau
=
(s_0,a_0,r_0,s_1,\ldots,a_{T-1},r_{T-1},s_T)
\end{equation}
其目标仍是最大化期望轨迹回报:
\begin{equation}
J(\theta)
=
\mathbb{E}_{\tau\sim p_{\theta}(\tau)}
\left[
R(\tau)
\right]
\end{equation}
利用对数导数技巧,可以把无法直接求和的轨迹期望转化为可用采样估计的策略梯度:
\begin{equation}
\nabla_{\theta}J(\theta)
=
\mathbb{E}_{\tau\sim p_{\theta}(\tau)}
\left[
\sum_{t=0}^{T-1}
G_t
\nabla_{\theta}
\log\pi_{\theta}(a_t\mid s_t)
\right]
\end{equation}
其中
\begin{equation}
G_t
=
\sum_{k=t}^{T-1}
\gamma^{k-t}r_k
\end{equation}
梯度中的 $\nabla_\theta\log\pi_\theta(a_t\mid s_t)$ 指向提高已采样动作对数概率的方向,$G_t$ 决定这个方向的权重和符号。这里不能简单说“回报较低就降低概率”:只要回报仍为正,原始 REINFORCE 更新仍可能提高该动作概率。要判断一次结果是好于还是差于当前局面的正常水平,需要引入基线。
直接以蒙特卡洛回报估计上述梯度的经典算法称为 REINFORCE。用当前策略采样 $N$ 条轨迹后,可最小化
\begin{equation}
\mathcal{L}_{\mathrm{PG}}(\theta)
=
-
\frac{1}{N}
\sum_{i=1}^{N}
\sum_{t=0}^{T_i-1}
G_t^{(i)}
\log
\pi_{\theta}
\left(
a_t^{(i)}\mid s_t^{(i)}
\right)
\end{equation}
负号把回报最大化写成损失最小化。REINFORCE 的优点是梯度估计在常见条件下无偏,代价是必须等到后续奖励出现才能计算回报,而且不同轨迹的结果可能相差很大。
仅依靠完整对局的最终结果更新策略,也会带来一些问题。五子棋的一局对弈通常包含多个动作,最终胜负是双方多次落子共同作用的结果。智能体可能在前期连续作出合理决策,却因为后期的一次失误输掉整局。如果将负回报直接作用于此前所有动作,一些原本合理的落子也会受到错误抑制。类似地,获胜对局中也可能包含效果较差的动作,但这些动作可能因为最终获胜而获得正向更新。
这就是时间上的信用分配问题。同一局面还可能因为对手行动和后续采样不同而得到相反结果,所以单条轨迹的 $G_t$ 是噪声很大的估计。增加样本能缓解波动,却不能让模型更早获得反馈。
为了减小回报波动对策略更新的影响,可以引入基线 $b(s_t)$,用动作回报与基线之间的差值代替原始回报:
\begin{equation}
\mathcal{L}_{\mathrm{PG}}(\theta)
=
-
\mathbb{E}
\left[
\sum_{t=0}^{T-1}
\left(
G_t-b(s_t)
\right)
\log\pi_{\theta}(a_t\mid s_t)
\right]
\end{equation}
只要基线不依赖当前采样动作,从策略梯度中减去它不会改变梯度的期望,却能降低方差。状态相关基线可以理解为当前局面的正常回报水平:更新关注的从此不再是“这次回报是否为正”,而是“这次结果是否好于在该局面下的通常表现”。
较为常用的状态相关基线是状态价值函数:
\begin{equation}
b(s_t)=V^{\pi}(s_t)
\end{equation}
此时,回报与基线之间的差值可以写为:
\begin{equation}
\widehat{A}_t
=
G_t-V^{\pi}(s_t)
\end{equation}
这给出优势函数 $A^\pi(s,a)=Q^\pi(s,a)-V^\pi(s)$ 的蒙特卡洛估计。正优势提高已采样动作的概率,负优势降低它;这种相对比较比回报的绝对正负更有意义。
仍以前文的五子棋局面为例。假设智能体在状态 $s_t$ 下选择了位置 $B=(8,8)$,当前选择概率为 $\pi_{\theta}(B\mid s_t)=0.40$。图~\ref{fig:policy_advantage_update} 展示了该动作在两种不同回报情况下的更新结果。
\begin{figure}[htbp]
\centering
\IfFileExists{figures/policy_advantage_update.tex}{
\resizebox{0.96\linewidth}{!}{
\input{figures/policy_advantage_update.tex}
}
}{
\fbox{
\begin{minipage}[c][6.2cm][c]{0.96\linewidth}
\centering
回报与基线对落子概率更新的影响\\
\texttt{figures/policy\_advantage\_update.tex}
\end{minipage}
}
}
\caption{回报与基线对落子概率更新的影响}
\label{fig:policy_advantage_update}
\end{figure}
在图~\ref{fig:policy_advantage_update} 的第一种情况下,动作 $B$ 获得的实际回报为 $8$,而当前状态的基线为 $6$,因此优势估计为:
\begin{equation}
\widehat{A}_t
=
8-6
=
2>0
\end{equation}
这一结果表明,位置 $B$ 的实际表现高于当前局面的平均水平,策略会提高该动作的选择概率。
在第二种情况下,动作 $B$ 获得的实际回报为 $3$,低于基线 $6$,因此有:
\begin{equation}
\widehat{A}_t
=
3-6
=
-3<0
\end{equation}
此时,策略会降低位置 $B$ 的选择概率。可见,优势估计关注的是动作表现与当前状态一般水平之间的差异,而不是回报的绝对大小。
\begin{importantnote}
原始 REINFORCE 用采样回报给动作加权,并不是简单执行“正奖励鼓励、低奖励惩罚”。引入状态价值基线后,$G_t-V^\pi(s_t)$ 才显式比较这次动作与当前状态正常水平之间的差异。基线降低方差,但不会自动解决长序列中的信用分配问题。
\end{importantnote}
如果不想等到整局结束,可以用一个学习得到的价值模型近似后续回报。这样会引入估计偏差,却能更及时地更新策略。Actor-Critic 正是沿着这一取舍建立的。
\subsection{Actor-Critic 框架}
REINFORCE 用实际采样到的后续回报评价动作。这个评价不依赖一个可能出错的价值模型,却来得晚、波动大。Actor-Critic 用学习得到的价值估计替代一部分尚未发生的回报,在更早更新和更小方差之间换取一定偏差。
框架包含两个可训练部分:Actor 是参数为 $\theta$ 的策略 $\pi_\theta(a\mid s)$,负责行动;Critic 是参数为 $\phi$ 的价值近似器 $V_\phi(s)$,负责预测当前策略下的期望回报。二者可以使用独立网络,也可以共享特征提取层,不能仅凭“两个角色”推断实现中一定存在两张完全独立的网络。
在五子棋任务中,一条样本的产生和使用过程如下。Actor 接收 $s_t$ 并输出
\begin{equation}
\pi_{\theta}(a_t\mid s_t)
\end{equation}
并采样 $a_t$。环境执行动作和约定范围内的对手回应,返回 $r_t$$s_{t+1}$ 与终止标记。Critic 随后用相邻状态的价值构造训练目标。图~\ref{fig:actor_critic_framework} 展示了这条数据流。
\begin{figure}[htbp]
\centering
\IfFileExists{figures/actor_critic_framework.tex}{
\resizebox{0.96\linewidth}{!}{
\input{figures/actor_critic_framework.tex}
}
}{
\fbox{
\begin{minipage}[c][6.2cm][c]{0.96\linewidth}
\centering
Actor-Critic 框架中的策略选择与价值评价过程\\
\texttt{figures/actor\_critic\_framework.tex}
\end{minipage}
}
}
\caption{Actor-Critic 框架中的策略选择与价值评价过程}
\label{fig:actor_critic_framework}
\end{figure}
Actor 回答“下一步按什么分布行动”,Critic 回答“从当前状态继续执行该策略,预计能得到多少回报”。动作相对于当前状态平均水平的好坏由优势函数表示:
\begin{equation}
A^{\pi}(s_t,a_t)
=
Q^{\pi}(s_t,a_t)
-
V^{\pi}(s_t)
\end{equation}
正优势表示该动作优于当前策略在这个状态下的平均选择,负优势表示它更差。Actor 分别提高或降低相应动作的对数概率。
真实的 $Q^\pi(s_t,a_t)$ 未知时,可以用一步奖励加下一状态价值构造 TD 目标:
\begin{equation}
y_t=r_t+\gamma(1-d_t)V_{\bar\phi}(s_{t+1})
\end{equation}
其中 $d_t=1$ 表示任务在本次转移后自然终止,否则为 0。记号 $\bar\phi$ 强调计算目标时不通过下一状态价值反向传播梯度;实现中可以对这一项停止梯度,也可以使用单独维护的目标网络。TD 误差为
\begin{equation}
\delta_t
=
r_t+\gamma(1-d_t)V_{\bar\phi}(s_{t+1})-V_{\phi}(s_t)
\end{equation}
在最简单的单步 Actor-Critic 中,可用 $\delta_t$ 近似优势:
\begin{equation}
\widehat{A}_t
=
\delta_t
\end{equation}
仍以前文的棋盘为例。Actor 以 $0.40$ 的概率选择位置 $B=(8,8)$。该动作没有自然终止对局,所以 $d_t=0$,并设
\begin{equation}
r_t=0
\end{equation}
\begin{align}
V_{\phi}(s_t)&=4.0\\
V_{\phi}(s_{t+1})&=6.0
\end{align}
$\gamma=0.9$ 时,
\begin{equation}
\delta_t
=
0+0.9\times6.0-4.0
=
1.4
\end{equation}
图~\ref{fig:actor_critic_td_update} 给出了该评价信号对 Actor 和 Critic 的作用。
\begin{figure}[htbp]
\centering
\IfFileExists{figures/actor_critic_td_update.tex}{
\resizebox{0.96\linewidth}{!}{
\input{figures/actor_critic_td_update.tex}
}
}{
\fbox{
\begin{minipage}[c][6.5cm][c]{0.96\linewidth}
\centering
时序差分误差对 Actor 与 Critic 更新的作用\\
\texttt{figures/actor\_critic\_td\_update.tex}
\end{minipage}
}
}
\caption{时序差分误差对 Actor 与 Critic 更新的作用}
\label{fig:actor_critic_td_update}
\end{figure}
这个样本给出的 TD 目标是 $5.4$,高于 Critic 对 $s_t$ 的旧估计 $4.0$,所以 $\delta_t=1.4$。Actor 把它当作正优势,提高动作 $B$ 的对数概率;Critic 则把 $V_\phi(s_t)$$5.4$ 调整。需要注意,这只是当前 Critic 提供的学习信号,并不能证明动作 $B$ 客观上一定是好棋。
Actor 使用优势估计更新策略,其损失函数可以写为:
\begin{equation}
\mathcal{L}_{\mathrm{actor}}(\theta)
=
-
\mathbb{E}
\left[
\widehat{A}_t
\log
\pi_{\theta}(a_t\mid s_t)
\right]
\end{equation}
计算 Actor 梯度时,$\widehat A_t$ 应被视为固定目标,不让梯度通过优势估计回流到 Critic。正负优势决定更新方向,绝对值影响单个样本的权重。
Critic 的任务是使当前状态价值接近 TD 目标:
\begin{equation}
y_t=r_t+\gamma(1-d_t)V_{\bar\phi}(s_{t+1})
\end{equation}
在上述例子中:
\begin{equation}
y_t
=
0+0.9\times6.0
=
5.4
\end{equation}
Critic 原先对状态 $s_t$ 的估计为 $4.0$,而新的目标值为 $5.4$,因此需要适当提高 $V_{\phi}(s_t)$。Critic 可以使用平方误差进行训练:
\begin{equation}
\mathcal{L}_{\mathrm{critic}}(\phi)
=
\frac{1}{2}
\mathbb{E}
\left[
\left(
y_t-V_{\phi}(s_t)
\right)^2
\right]
\end{equation}
若下一状态的估计价值只有 $2.0$,则
\begin{equation}
V_{\phi}(s_t)=4.0,\qquad
V_{\phi}(s_{t+1})=2.0
\end{equation}
在即时奖励仍为 $0$、折扣因子为 $0.9$ 时,有:
\begin{equation}
\delta_t
=
0+0.9\times2.0-4.0
=
-2.2
\end{equation}
Actor 会降低该动作的概率,Critic 会把当前状态估计向 $1.8$ 调整。若落子直接结束对局,$d_t=1$,目标只剩最终奖励。时间上限造成的截断不一定等于自然终止:如果任务在时间上限之后本可继续,是否保留 bootstrap 项要根据环境语义处理。
Actor-Critic 框架中,Actor 负责输出动作概率并更新策略,Critic 负责估计状态价值。时序差分误差
\[
\delta_t=r_t+\gamma(1-d_t)V_{\bar\phi}(s_{t+1})-V_{\phi}(s_t)
\]
可以作为单步优势估计,也给出 Critic 当前预测与 TD 目标的差异。它是带有函数近似误差的训练信号,不是动作质量的真实标签。
一条基本更新链可以概括为:Actor 采样动作,环境产生转移,Critic 构造 TD 目标并学习价值,Actor 使用停止梯度的优势信号更新策略。它比纯蒙特卡洛更新更及时、方差通常更小,代价是 Actor 会继承 Critic 的偏差。
二者的学习速度失衡时,这个问题尤其明显。Critic 学得太慢,优势长期带有系统误差;Actor 变化太快,Critic 刚拟合的数据分布已经过时;Critic 在有限样本上过拟合,也会向 Actor 提供过度自信的方向。后续的 PPO 仍采用 Actor-Critic 结构,它重点限制的是策略一次更新的幅度,而不是消除 Critic 的估计误差。
\section{强化学习的进阶优化算法}
Actor-Critic 给出了策略与价值函数协同更新的框架,实际训练还要面对两个具体限制。第一,策略一旦变化太快,旧策略采集的优势估计便不能可靠地描述新策略;第二,有些任务根本不允许当前策略不断进入环境试错。PPO 处理前一种更新尺度问题,离线强化学习处理后一种固定数据问题。二者解决的困难不同,不能因为都“减少训练风险”而混为一谈。
\subsection{近端策略优化方法}
Actor-Critic 有了评价动作的方向,却没有回答一次应当走多远。设旧策略在某个棋盘状态下以 $0.40$ 的概率选择位置 $B$。正优势说明这个概率应当提高,但从 $0.40$ 调到 $0.46$ 和一步调到 $0.80$ 完全不是同一件事。后者会显著改变后续访问的局面,而用于计算优势的样本仍来自概率为 $0.40$ 的旧策略。
单纯减小学习率不能直接解决这个问题。学习率约束参数移动的距离,同样大小的参数变化却可能在不同状态下造成完全不同的概率变化。近端策略优化(Proximal Policy Optimization, PPO)因此直接比较新旧策略对已采样动作给出的概率,并让过于激进的变化不再从代理目标中继续获益。
PPO 仍采用 Actor-Critic 结构。Critic 提供优势估计,主要变化发生在 Actor 的目标函数。“近端”描述的是希望新策略留在采样策略附近,并不是一个由裁剪严格保证的硬约束。
在每一轮 PPO 训练开始时,先固定当前策略,并将其记为旧策略:
\begin{equation}
\pi_{\theta_{\mathrm{old}}}(a_t\mid s_t)
\end{equation}
使用旧策略与环境交互后,可以得到一批状态、动作和奖励数据。更新 Actor 时,新策略与旧策略可能对同一动作给出不同的选择概率。为衡量这一变化,定义概率比:
\begin{equation}
\rho_t(\theta)
=
\frac{
\pi_{\theta}(a_t\mid s_t)
}{
\pi_{\theta_{\mathrm{old}}}(a_t\mid s_t)
}
\end{equation}
概率比反映新旧策略对已采样动作 $a_t$ 的偏好变化。$\rho_t(\theta)>1$ 表示概率提高,$\rho_t(\theta)<1$ 表示概率降低。它只比较样本中实际执行的动作,并不直接约束该状态下整个动作分布。
例如,旧策略选择位置 $B$ 的概率为:
\[
\pi_{\theta_{\mathrm{old}}}(B\mid s_t)=0.40
\]
如果新策略将该概率提高到 $0.48$,则有:
\begin{equation}
\rho_t(\theta)
=
\frac{0.48}{0.40}
=
1.20
\end{equation}
这表示新策略选择位置 $B$ 的概率是旧策略的 $1.20$ 倍。概率比比参数距离更直接地反映这条样本上的行为变化。
若暂时不限制策略变化,可以利用概率比与优势估计构造代理目标:
\begin{equation}
L^{\mathrm{PG}}(\theta)
=
\mathbb{E}
\left[
\rho_t(\theta)\widehat{A}_t
\right]
\end{equation}
其中,$\widehat{A}_t$ 表示 Critic 给出的优势估计。当 $\widehat{A}_t>0$ 时,动作 $a_t$ 的表现高于当前策略的一般水平,提高该动作的概率会增大代理目标;当 $\widehat{A}_t<0$ 时,该动作的表现低于一般水平,降低其选择概率会增大代理目标。
如果对同一批样本反复优化,Actor 可能持续增大正优势动作的概率,或持续减小负优势动作的概率。PPO 为概率比设置裁剪范围:
\[
[1-\epsilon,1+\epsilon]
\]
其中,$\epsilon$ 表示裁剪阈值。PPO 的裁剪代理目标写为:
\begin{equation}
L^{\mathrm{CLIP}}(\theta)
=
\mathbb{E}
\left[
\min
\left(
\rho_t(\theta)\widehat{A}_t,\,
\operatorname{clip}
\left(
\rho_t(\theta),
1-\epsilon,
1+\epsilon
\right)
\widehat{A}_t
\right)
\right]
\end{equation}
式中的 $\min$ 不能忽略:PPO 取原始项与裁剪项中更保守的一个。裁剪只在变化会继续改善代理目标的方向上截断收益,并不是把所有概率比强制投影到区间内。$\epsilon$ 是需要验证的超参数,不能脱离批量大小、更新轮数和学习率单独判断。
裁剪机制对正优势动作和负优势动作的作用可以通过五子棋中的概率变化进行说明。图~\ref{fig:ppo_clipping_mechanism} 给出了旧策略概率为 $0.40$、裁剪阈值为 $\epsilon=0.2$ 时的两种情况。
\begin{figure}[htbp]
\centering
\IfFileExists{figures/ppo_clipping_mechanism.tex}{
\resizebox{0.96\linewidth}{!}{
\input{figures/ppo_clipping_mechanism.tex}
}
}{
\fbox{
\begin{minipage}[c][6.8cm][c]{0.96\linewidth}
\centering
PPO 裁剪机制对策略概率更新的限制\\
\texttt{figures/ppo\_clipping\_mechanism.tex}
\end{minipage}
}
}
\caption{PPO 裁剪机制对策略概率更新的限制}
\label{fig:ppo_clipping_mechanism}
\end{figure}
在图~\ref{fig:ppo_clipping_mechanism} 中,裁剪区间为:
\[
[1-\epsilon,1+\epsilon]=[0.8,1.2]
\]
当位置 $B$ 的优势估计为正时,PPO 允许适当提高该动作的选择概率。若新策略将概率从 $0.40$ 提高到 $0.48$,概率比值为:
\[
\rho_t(\theta)=\frac{0.48}{0.40}=1.2
\]
该比值位于裁剪上界,概率增加仍然能够反映在代理目标中。若新策略进一步将概率提高到 $0.60$,概率比值变为:
\[
\rho_t(\theta)=\frac{0.60}{0.40}=1.5
\]
此时概率比值已经超过上界。对于正优势动作,超过上界的部分不会继续增加裁剪目标,因而优化过程不再强烈推动该动作概率继续增大。
当某一动作的优势估计为负时,PPO 会降低该动作的选择概率。如果新策略将概率从 $0.40$ 降低到 $0.32$,概率比值为:
\[
\rho_t(\theta)=\frac{0.32}{0.40}=0.8
\]
该比值位于裁剪下界。如果概率继续降低到 $0.20$,概率比值变为:
\[
\rho_t(\theta)=\frac{0.20}{0.40}=0.5
\]
对于负优势动作,低于裁剪下界的变化不会继续带来更多优化收益,策略因而缺少进一步大幅降低该动作概率的动力。
裁剪机制并不会将新策略的实际概率强制固定在某个区间内。它限制的是过大概率变化能够从代理目标中获得的收益。策略仍然可以发生变化,但当变化超过一定范围后,继续扩大变化不再受到目标函数的鼓励。
这种做法与信任域思想相近。信任域可以理解为旧策略附近的一定范围,策略更新尽量在这一范围内完成。PPO 不需要直接求解带有严格约束的复杂优化问题,而是通过裁剪代理目标近似控制更新幅度。在实际训练中,还可以监测新旧策略之间的 KL 散度。当二者差异超过设定范围时,可以提前结束当前批次的策略更新。
除了限制策略变化,PPO 还会在有限范围内重复使用同一批交互数据。它先用旧策略采集一批样本,再将数据划分为小批次,对 Actor 和 Critic 进行多轮训练。裁剪机制减弱了策略在这些更新中偏离采样策略过远的动机,但并不保证偏离一定不会发生。
图~\ref{fig:ppo_training_process} 展示了 PPO 一次训练迭代的主要过程。
\begin{figure}[htbp]
\centering
\resizebox{0.94\linewidth}{!}{
\input{figures/ppo_training_process.tex}
}
\caption{PPO 的一次采样与更新过程}
\label{fig:ppo_training_process}
\end{figure}
实际实现通常还把策略目标、价值误差和熵正则合并训练。价值损失用于拟合回报或优势计算所需的基线,熵项用于延缓策略过早变得确定。三个分量的尺度不同,因此应分别记录,而不能只观察相加后的总损失。若价值误差持续增大,单靠缩小 PPO 的裁剪范围通常不能解决问题;若近似 KL 很大或大量样本触发裁剪,则说明同一批数据上的更新可能过多。
\begin{importantnote}
PPO 的裁剪目标限制的是过大策略变化继续获得的优化收益,并不把新策略严格约束在某个概率区间,也不保证每次更新后性能都提高。
\end{importantnote}
\subsection{离线强化学习方法}
在线强化学习可以用当前策略继续采样,离线强化学习则只使用预先收集并固定的数据集
\begin{equation}
\mathcal D=\{(s_i,a_i,r_i,s_i',d_i)\}_{i=1}^{N}
\end{equation}
其中 $d_i$ 记录转移是否因自然终止而结束。数据可能来自人工操作、旧策略或多个不同版本的系统。训练期间不能让目标策略进入环境补充它最缺少的样本,这是离线学习与经验回放之间的关键区别。
\begin{figure}[htbp]
\centering
\resizebox{0.92\linewidth}{!}{
\input{figures/online_offline_rl.tex}
}
\caption{在线强化学习与离线强化学习的数据来源}
\label{fig:online_offline_rl}
\end{figure}
困难不只在于数据有限,还在于学习得到的策略可能主动选择数据中罕见的动作。价值网络必须在这些位置外推,而最大化操作又偏好估计偏高的动作。一个偶然的高估会使策略更常选择该动作,但固定数据无法提供新的后果来纠正它,于是形成“分布偏移—价值高估—策略进一步偏移”的循环。
\begin{figure}[htbp]
\centering
\resizebox{0.92\linewidth}{!}{
\input{figures/offline_rl_distribution_shift.tex}
}
\caption{离线强化学习中的分布偏移与价值高估}
\label{fig:offline_rl_distribution_shift}
\end{figure}
不同离线算法以不同方式限制这种外推。BCQ 和 BEAR 约束目标策略不要轻易离开行为数据覆盖的动作范围;CQL 对缺少数据支持却被估计得很高的动作施加保守惩罚;IQL 主要在数据中的动作上学习价值关系,再用优势加权方式拟合策略。这些方法共同承认一个事实:没有记录的动作不一定差,但现有数据不足以证明它好。约束越强,外推风险通常越小,同时也越难发现数据之外的改进。
方法选择以前,先要弄清数据由哪些策略产生、关键状态和动作是否出现、奖励规则是否一致,以及终止与截断是否被正确记录。同一条轨迹不应拆散到训练集和验证集两边,否则相邻转移会造成泄漏。行为克隆提供了重要基线:如果复杂离线算法甚至不能稳定超过对历史动作的模仿,就很难把性能提升归因于长期价值学习。
离线指标也有边界。保留数据上的 Bellman 误差较低,只说明模型能拟合选定的自举目标,不能直接证明新策略有效。离线策略评估、模拟器测试和重要性采样各自依赖额外假设;目标策略离行为数据越远,结论通常越不可靠。高风险任务仍需经过受控测试和可回退的小范围部署。
\begin{importantnote}
离线强化学习的核心问题不是样本总数,而是目标策略的选择是否有数据支持。重复很多次相似轨迹,并不能补上从未出现过的关键状态和动作。
\end{importantnote}
\section{强化学习的应用设计方法}
选定算法以前,任务定义已经决定模型能看到什么、能改变什么,以及什么结果会被当成成功。下面继续使用本章开头的五子棋设定,把这些决定落实为可以执行和评估的系统。
\subsection{任务边界与转移定义}
本章把智能体定义为执黑的一方,对手和规则属于环境。一次转移从黑方决策前开始:策略选择一个空位,环境检查合法性、落下黑子;若对局尚未结束,环境再执行白方回应,随后返回新的黑方决策局面。这样,数据中的 $(s_t,a_t,r_t,s_{t+1})$ 与算法使用的时间步保持一致。
如果改成黑白双方各自都是智能体,或者让同一个策略在每一个落子回合都被调用,状态中的行动方、奖励归属和轨迹结构都要随之改变。这并非实现细节,而是另一个任务定义。比较算法时应冻结环境版本、对手来源、开局分布和终止规则,否则性能差异无法只归因于算法。
最小规模实验很适合暴露边界错误。随机策略若能落到已占位置,问题在动作执行或掩码;相同局面得到互相矛盾的输入,通常意味着行动方或坐标编码不一致;训练回报上升而独立胜率不变,则应先核对奖励和评估目标。只有任务定义能够稳定地产生正确轨迹,扩大训练规模才有意义。
\subsection{状态表示与动作空间}
环境状态不一定等于策略能够观察到的信息。本章的棋盘完全可见,且策略只在黑方回合调用,因此可以令 $o_t=B_t$。若同一模型控制双方,还需加入当前行动方 $p_t$,否则同一棋盘在黑方和白方回合会得到相同输入。
棋盘可以用单个整数矩阵表示
\begin{equation}
B_t\in\{-1,0,1\}^{15\times15}
\end{equation}
也可以使用两个二值通道分别记录双方棋子
\begin{equation}
B_t\in\{0,1\}^{2\times15\times15}
\end{equation}
前者紧凑,适合作为最小基线;后者把类别含义分开,更便于卷积网络处理。表示方式不应包含对局结束后才能得到的信息,归一化统计量也只能由训练数据计算。
动作空间对应棋盘的 $225$ 个位置。策略网络可以为每个位置输出一个得分 $z_\theta(s_t,a)$,再用合法动作集合 $\mathcal A(s_t)$ 构造掩码后的分布
\begin{equation}
\pi_\theta(a\mid s_t)
=
\frac{\exp z_\theta(s_t,a)}
{\sum_{a'\in\mathcal A(s_t)}\exp z_\theta(s_t,a')}
\end{equation}
已经有棋子的格点由规则确定为非法,可以直接屏蔽;一手棋即使很差,只要符合规则就仍是合法动作。把棋力启发式写入掩码,会使策略失去发现其他解法的机会。
动作编码还要与环境坐标严格一致。把二维位置展平时,可以固定使用 $a=15i+j$,并用边角位置和往返转换做单元测试。训练阶段若允许随机策略直接产生未掩码动作,环境也应给出明确错误,而不是静默地改成另一个位置。
\subsection{奖励与约束}
在固定设定中,胜、负、平分别得到 $1$$-1$$0$,非终止步骤奖励为 $0$。这种稀疏奖励与真实目标接近,却让早期落子的评价依赖整局结果。加入棋型分数等过程反馈能够降低学习难度,但也改变了算法实际优化的目标。
密集反馈缩短了动作与奖励之间的距离,也把设计者的棋型偏好写进了优化目标。若“形成活三”可重复计分,策略可能维持局部棋型来刷取奖励,而不是尽快获胜。过程奖励因此不是免费的训练加速器。
将任务奖励和多个辅助奖励组合时,可以写为:
\begin{equation}
r_t
=
r_t^{\mathrm{task}}
+
\sum_{i=1}^{K}
\lambda_i r_t^{(i)}
\end{equation}
其中 $r_t^{\mathrm{task}}$ 是核心奖励,$r_t^{(i)}$ 是辅助项。判断 $\lambda_i$ 不能只比较单步数值,还要估算每项在一个回合中的累计上限和出现频率。
奖励权重的大小还会影响不同目标之间的取舍。例如,机器人控制任务可能同时希望提高移动速度、降低能量消耗并保持动作平稳。总奖励可以由多个部分组成:
\begin{equation}
r_t
=
\lambda_1 r_t^{\mathrm{progress}}
-
\lambda_2 r_t^{\mathrm{energy}}
-
\lambda_3 r_t^{\mathrm{instability}}
\end{equation}
权重会改变策略的取舍:能耗惩罚过大可能让机器人静止,每步惩罚过大可能让处于劣势的棋手主动寻求快速失败。奖励尺度还影响价值目标和梯度数值。加入任何新项后,都要分别记录各项回报以及独立任务指标;总回报上升无法说明究竟是哪一项被优化。
约束是否进入奖励,取决于它能否被权衡。机械极限、非法落子和不可突破的安全边界应用掩码、动作范围或安全控制器保证;能耗、平稳性和延迟等允许折中的要求才适合写成软成本:
\begin{equation}
r_t'
=
r_t
-
\lambda c_t
\end{equation}
其中 $c_t$ 是成本,$\lambda$ 表示任务愿意为降低成本牺牲多少核心回报。若需求方无法接受任何违反,就不存在这个权衡,也不应仅靠把 $\lambda$ 调得很大来模拟硬约束。
巨大负奖励不能替代硬约束。随机策略仍可能先执行危险动作再收到惩罚,而真实系统可能没有承受这次探索的余地。
奖励信号还可能被智能体以设计者没有预料到的方式利用。智能体只会优化实际给出的奖励,而不会自动理解设计者的真实意图。当奖励函数存在漏洞时,策略可能获得很高回报,却没有完成预期任务。这种现象通常称为奖励投机或奖励漏洞。
例如,如果只根据连续棋子数量奖励五子棋智能体,策略可能偏好不断扩展已有棋型,而忽略对手已经形成的直接威胁。如果机器人任务只奖励向目标方向的瞬时速度,机器人可能通过剧烈摆动获得较高速度信号,却没有稳定到达目标位置。
奖励漏洞要靠行为审计和独立指标发现。五子棋至少应检查真实胜率、先后手表现、不同对手下的结果及典型失败棋局。如果辅助奖励上升而胜率下降,应回滚该奖励版本,而不是继续调高训练步数。
新奖励项加入后,应单独记录并做消融。除了比较总回报,还要检查辅助项在整回合尺度上是否压过核心奖励,以及高回报轨迹的行为是否符合预期。
奖励版本确定后,还要建立彼此隔离的训练、验证和测试流程。
\subsection{训练与评估过程设计}
训练、验证和测试承担不同职责,混用会让结果失去解释性。
\begin{table}[htbp]
\centering
\caption{训练、验证与最终测试的职责}
\label{tab:train-validation-test}
\begin{tabularx}{\linewidth}{>{\bfseries}p{2.3cm}p{4.3cm}X}
\toprule
阶段 & 可以做什么 & 不能做什么 \\
\midrule
训练 & 采集或读取训练数据、探索、更新参数、监控优化指标 & 用训练回报宣称最终任务性能 \\
验证 & 比较检查点、选择超参数、触发早停、诊断失败 & 把反复调参后的验证结果当成无偏测试结果 \\
最终测试 & 在冻结方案上报告预先定义的任务指标与波动 & 继续选模型、改奖励、调对手或回流测试数据 \\
\bottomrule
\end{tabularx}
\end{table}
采样量决定梯度方差和数据新鲜度。只用一局五子棋更新,胜负偶然性可能支配梯度;等待过多对局,策略又长期使用旧数据。PPO 对同一批样本更新过多轮还会使当前策略偏离采样策略。批量大小、更新轮数和学习率必须联合报告,并结合 KL、裁剪比例、价值误差和策略熵诊断。
探索规则属于实验配置。始终取最大概率动作容易固化早期偏好,随机性过强又会掩盖已经学到的能力。训练可以采样并调节熵,验证和测试则要预先规定使用采样策略还是确定性策略;两种评估回答的问题不同,不能在看到结果后择优报告。
五子棋训练还需要设计对手来源。如果智能体始终与一个固定的弱对手进行比赛,它可能很快取得较高胜率,但这种胜率并不代表策略具有普遍能力。智能体可能只是发现了该对手的某些固定漏洞。
自我对弈是一种常见的数据生成方式。当前策略同时控制对弈双方,使对手能力随着训练同步提高。这样可以不断产生与当前策略水平接近的比赛,减少固定对手过强或过弱的问题。
单纯使用最新策略进行自我对弈也存在局限。如果新策略暂时形成某一种棋路,双方可能反复围绕这一棋路训练,逐渐遗忘对旧策略有效的行为。为增加对手多样性,可以定期保存历史策略,并从不同版本中随机选择对手。还可以加入规则策略、随机策略和外部棋谱策略,使训练数据覆盖不同水平和不同风格的对局。
对手池中的角色应预先定义:随机策略检查基本能力,规则策略提供稳定锚点,历史策略检查遗忘,当前策略提供相近难度。训练对手可以参与数据生成,测试对手不得因为测试结果而被加入当前模型的训练循环。
五子棋验证应使用独立种子、开局和对手,并交换先后手。只报合并胜率会掩盖先手优势,因此还应分别报告先手、后手及各对手上的结果。
设评估总局数为 $N_{\mathrm{eval}}$,获胜局数为 $N_{\mathrm{win}}$,则评估胜率可以写为:
\begin{equation}
\widehat{p}_{\mathrm{win}}
=
\frac{
N_{\mathrm{win}}
}{
N_{\mathrm{eval}}
}
\end{equation}
训练指标用于解释优化过程:策略损失、价值误差、熵、KL、回合长度和各奖励分项都属于诊断信息。任务指标用于评价结果:五子棋的胜率、平局率和对手分项才回答棋力是否提高。两类曲线相关时也不能互相替代。
如果使用动作掩码,还应检查非法动作在掩码前是否获得过高得分。如果模型长期为大量非法位置分配较高原始得分,只是依靠掩码将其删除,说明策略网络对棋盘状态的理解可能仍然不足。掩码保证了动作合法,但不能代替模型学习合理的动作偏好。
网络初始化、环境随机性、动作采样和小批次顺序都会改变训练轨迹。应使用多个训练随机种子,在固定测试协议下报告均值与离散程度,不能从多次实验中只挑最好的一次。
检查点应包含策略、Critic、优化器、训练步数、归一化统计量和环境版本。验证集负责选择检查点和触发早停;最终测试只对冻结的候选方案运行。短期胜率波动不应触发即时早停,平滑窗口和耐心周期应在实验前设定。
在分析训练失败原因时,应区分任务设计问题和优化问题。如果策略损失剧烈变化、价值估计不断发散,问题可能来自学习率、更新频率或算法实现;如果训练过程稳定但策略始终选择无效行为,问题可能来自状态、动作或奖励设计;如果训练表现很好而独立评估较差,则需要检查训练场景是否过于单一。
训练集用于更新参数,验证集用于选择模型和调整设计,最终测试用于报告冻结方案。测试结果一旦反过来影响奖励、超参数、对手或检查点选择,这部分数据就已经参与了开发过程,需要重新建立未使用过的测试协议。
即使流程隔离正确,规模、训练波动和环境变化仍会带来新的困难。下一节从失败现象出发讨论这些问题。
% \section{强化学习的前沿方向}
% \subsection{多智能体强化学习}
% \subsection{面向样本重放的强化学习方法}
% \subsection{强化学习方法的稳定性改进}
\section{强化学习方法的挑战}
一条最终上升的训练曲线可能掩盖不同问题。有些重要局面从未进入训练数据,有些结果换一个随机种子便无法复现,还有些策略只适用于训练对手或当前奖励。下面从这些现象追查原因。
\subsection{面向大规模决策的挑战}
“任务规模大”可能指完全不同的瓶颈。高维状态首先造成覆盖困难,模型不得不对未见局面作出外推;动作候选太多时,计算和探索会分散在大量选择上;回合过长则拉开动作与结果的距离,使信用分配更加困难。扩大网络主要增加函数近似能力,并不会自动解决动作搜索或长时域问题。
继续以五子棋任务为例。一个 $15\times15$ 棋盘包含 $225$ 个格点。若暂时不考虑棋局规则,每个格点可能为空、黑子或白子,棋盘状态数量的上界为:
\begin{equation}
|\mathcal{S}|
\leq
3^{225}
\end{equation}
实际合法棋局的数量远小于这一上界,但仍然十分庞大。智能体不可能遍历全部棋盘状态,也无法为每个状态单独保存一个精确价值。训练只能利用有限对局学习不同棋型之间的共同规律,并将已学到的判断推广到没有出现过的局面。
这说明,表格形式的价值函数只适合状态数量较少的任务。在大规模问题中,通常需要使用神经网络近似价值函数或策略函数:
\begin{equation}
V_{\phi}(s)
\approx
V^{\pi}(s)
\end{equation}
\begin{equation}
\pi_{\theta}(a\mid s)
\approx
\pi(a\mid s)
\end{equation}
神经网络能够利用相似状态之间的共享结构。例如,五子棋中的连续三子、两端开放和对手威胁等局部棋型,可能出现在棋盘的不同位置。卷积结构可以提取这些局部特征,使模型不必分别记忆每一个具体棋盘。
不过,函数近似并没有消除状态空间过大的问题。模型只能根据训练数据学习状态之间的相似性。如果某类棋型在训练中很少出现,网络对这些局面的判断仍可能不准确。随着状态维度增加,覆盖具有代表性的环境情况需要更多数据,模型训练和存储成本也会随之上升。
除了状态空间,动作空间也会带来明显挑战。五子棋在空棋盘状态下最多有 $225$ 个候选落子位置,策略模型需要为这些位置分别给出动作得分。随着棋盘逐渐被填满,合法动作数量会减少,但训练早期仍需要在大量候选位置之间进行比较。
在推荐、调度和组合优化任务中,动作规模可能远大于五子棋。例如,推荐系统可能需要从大量候选物品中选择一个或多个结果,生产调度需要同时决定任务顺序、执行设备和开始时间。若将所有可能组合直接视为独立动作,动作数量会迅速超过模型能够逐一评价的范围。
假设每一步平均有 $b$ 个候选动作,一段决策过程包含 $H$ 步,则可能形成的动作序列数量约为:
\begin{equation}
N_{\mathrm{seq}}
\approx
b^H
\end{equation}
即使每一步的动作数量并不特别大,随着决策长度增加,完整动作序列仍会呈指数增长。智能体无法逐一尝试所有序列,只能根据有限经验判断哪些早期动作更可能带来较好的长期结果。
较大的动作空间还会增加探索难度。训练初期,策略对不同动作的判断较为接近,有限的交互次数会被分散到大量候选动作上。许多动作只能获得少量样本,价值估计因而难以稳定。
在五子棋中,真正值得重点考虑的落子位置通常集中在已有棋子附近、己方棋型延伸位置和对手威胁位置。如果策略在训练初期对所有空白格点进行近似均匀探索,大量对局会消耗在与当前棋局关系较小的位置上。
处理大规模动作空间的一种方法是先缩小候选范围,再由强化学习策略进行精细选择。候选集合可以由任务规则、启发式方法或单独训练的候选生成模型产生。设原始动作空间为 $\mathcal{A}$,候选生成过程得到状态相关集合:
\begin{equation}
\mathcal{A}_{\mathrm{cand}}(s_t)
\subseteq
\mathcal{A}
\end{equation}
策略只需要在候选集合中计算动作概率:
\begin{equation}
\pi_{\theta}
\left(
a\mid s_t,
a\in\mathcal{A}_{\mathrm{cand}}(s_t)
\right)
\end{equation}
这种方式可以显著降低单次决策的计算量,但候选生成过程本身也可能引入偏差。如果真正的最优动作被提前排除,后续策略无论如何训练都无法选择它。因此,候选集合既要足够小,又需要保留较高的有效动作覆盖率。
除了候选筛选,还可以将复杂动作拆分为多个较小决策。例如,在调度任务中,可以先选择待处理任务,再选择执行设备,最后确定开始时间。原本需要一次完成的组合动作被分解为多个阶段,每个阶段只处理其中一部分选择。
分解动作能够降低单次输出规模,但也会延长决策过程,并使后续选择依赖前面已经作出的决定。如果早期阶段选错了任务,后续阶段即使选择合理,也难以得到好的最终结果。因此,动作分解需要在单步决策难度和整体序列长度之间进行权衡。
分层强化学习采用相近思路,将长任务划分为不同层次。高层策略负责选择阶段目标,低层策略负责完成具体操作。例如,机器人导航可以先由高层策略选择需要到达的区域,再由低层策略控制移动方向和速度。这样可以减少高层策略需要处理的决策步数,也使低层策略能够重复用于不同目标。
长决策序列本身也是大规模任务的重要难点。在五子棋中,一步失误可能要经过多轮对弈后才表现为最终失败。随着回合长度增加,早期动作和最终奖励之间的距离越来越远,智能体更难判断每一步对结果的影响。
如果回合长度为 $T$,时刻 $t$ 的动作需要考虑之后的全部奖励:
\begin{equation}
G_t
=
\sum_{k=t}^{T-1}
\gamma^{k-t}r_k
\end{equation}
$T$ 较大时,回报会受到许多后续随机事件影响。同一个早期动作在不同轨迹中可能得到差异较大的回报,使价值估计和策略梯度具有较高方差。
折扣因子 $\gamma$ 可以降低远期奖励对当前动作的影响,但过小的折扣因子会使智能体过度关注短期收益。在五子棋中,如果策略只关注少数几步后的局面,可能会偏好立即形成局部棋型,却忽略更长时间后的防守风险。因此,长序列任务不能简单依靠减小折扣因子解决,还需要更准确的价值估计、合理的过程反馈或分层任务结构。
大规模问题还会提高环境交互成本。状态和动作覆盖不足时,通常需要采集更多轨迹;模型规模增加后,每次前向计算和参数更新也会消耗更多资源。在仿真环境中,可以通过并行运行多个环境提高数据采集速度:
\begin{equation}
\mathcal{D}
=
\bigcup_{i=1}^{M}
\mathcal{D}_i
\end{equation}
其中,$M$ 表示并行环境数量,$\mathcal{D}_i$ 表示第 $i$ 个环境采集的数据。
并行采样能够缩短收集相同数量数据所需的时间,但不能减少任务本身的样本需求。在真实机器人、工业设备和人工交互任务中,同时运行大量环境往往并不现实。此时可以利用历史数据、模拟环境或已有策略减少真实交互次数。
模拟环境能够低成本产生大量数据,但模拟过程与真实环境之间可能存在差异。若策略过度依赖模拟环境中特有的状态特征或动态规律,部署到真实系统后可能无法保持原有性能。因此,模拟训练通常还需要配合环境随机化、少量真实数据调整和独立测试。
在大规模任务中,课程学习也可以降低初始训练难度。课程学习先让智能体处理较简单的状态和任务,再逐渐增加环境复杂度。例如,五子棋训练可以先在较小棋盘或预设中期局面中学习基本进攻和防守,再逐步过渡到完整棋盘和完整对局。
课程设置必须保持前后任务之间的连续性。如果早期任务与最终任务差异过大,智能体在简单环境中学到的行为可能无法迁移到复杂环境。课程学习的作用是调整学习顺序,而不是替代对最终任务的训练。
这些方法都在改变困难的位置,而不是消灭困难。函数近似把表格存储变成分布外预测;候选筛选用较小计算量换取漏掉关键动作的风险;动作分解降低单步输出规模,却增加阶段依赖;模拟器扩大数据量,也引入模拟—现实偏差;课程学习降低早期难度,但课程断层会阻止能力迁移。
选择扩展方法时,要先找到实际瓶颈。候选动作方法需要报告候选召回率,函数近似需要检查分布外状态,动作分解则要评估早期错误能否恢复。并行采样能够缩短训练时间,却没有改变任务所需的样本数量。
规模问题解决后,仍需区分“同样条件能否复现”和“条件变化后是否有效”。
\subsection{强化学习训练的不稳定性和泛化性问题}
训练数据由策略产生,Critic 又用这些数据评价策略,于是形成反馈环:策略改变访问分布,访问分布改变价值估计,价值估计再改变策略。早期一次偶然胜局因此可能被后续自我对弈反复放大。
本节区分三个容易混用的概念。
\begin{table}[htbp]
\centering
\caption{稳定性、泛化性与鲁棒性的区别}
\label{tab:stability-generalization-robustness}
\begin{tabularx}{\linewidth}{>{\bfseries}p{2.5cm}p{4.2cm}X}
\toprule
性质 & 问题 & 检查方式 \\
\midrule
训练稳定性 & 相同配置重复训练,结果是否接近?训练中是否突然退化? & 多随机种子、学习曲线、梯度和价值诊断 \\
泛化性 & 规则不变但状态、初始条件或对手未见时,性能是否保持? & 冻结策略,在预先划分的未见场景测试 \\
鲁棒性 & 观测噪声、参数扰动或有限对抗变化下,性能是否平稳退化? & 分级扰动和压力测试 \\
\bottomrule
\end{tabularx}
\end{table}
训练曲线平滑只提供第一类问题的部分证据,不能证明后两类性质。
图中的波动可以从四个位置诊断:样本是否过少而放大偶然轨迹;Actor 是否移动过快;Critic 是否在变化的数据上产生系统偏差;探索是否过早消失或长期过强。它们可能同时发生,不能仅凭“回报下降”判断超参数方向。
在 Actor-Critic 框架中,时序差分误差为:
\begin{equation}
\delta_t
=
r_t
+
\gamma V_{\phi}(s_{t+1})
-
V_{\phi}(s_t)
\end{equation}
TD 目标中的当前价值与下一状态价值都来自正在变化的 Critic,下一状态误差会通过 bootstrap 向前传播。Actor 过快会让 Critic 追逐不断移动的数据分布,Critic 在小批次上过拟合又会向 Actor 提供过度自信的优势。PPO 裁剪只缓解策略目标的一部分,不能修正错误价值目标。
诊断时应成组观察采样规模、近似 KL、裁剪比例、策略熵、解释方差、价值误差和梯度范数。单独把学习率调小可能只是让错误方向走得更慢。梯度裁剪可以拦住个别异常更新:
\begin{equation}
g
\leftarrow
g
\cdot
\min
\left(
1,
\frac{c}{\|g\|}
\right)
\end{equation}
其中,$g$ 表示当前梯度,$c$ 表示允许的最大梯度范数。梯度裁剪可以避免个别异常样本造成过大的参数更新,但不能解决价值目标错误或奖励设计不合理等根本问题。
多次重复实验也是判断训练稳定性的重要手段。设使用 $M$ 个随机种子得到的评估结果分别为 $J_1,J_2,\ldots,J_M$,可以计算平均性能:
\begin{equation}
\overline{J}
=
\frac{1}{M}
\sum_{i=1}^{M}
J_i
\end{equation}
同时还应报告不同实验之间的波动。如果一种方法只有少数实验能够取得较高性能,而多数实验表现较差,仅报告最好结果会高估方法的可靠性。
多随机种子均值与离散程度用于回答训练能否复现,仍不能回答未见场景是否有效。
在五子棋任务中,如果智能体始终与同一个规则对手进行训练,它可能学会专门利用该对手的固定弱点。当评估对手改变棋路时,策略的胜率可能迅速下降。此时,训练过程本身可能十分稳定,训练胜率也持续提高,但模型学习到的是针对单一对手的行为,而不是普遍适用的棋局判断能力。
泛化问题常与训练数据覆盖范围有关。如果训练过程中只出现少量初始状态,策略便可能记住这些状态下的有效动作,而没有学会更一般的决策规律。五子棋若始终从空棋盘开始,并且对手开局方式十分固定,模型可能只熟悉少数常见开局。
为了检查这种情况,可以在评估时使用不同的开局状态,例如随机放置少量合法棋子后开始比赛,或从历史棋谱的中间局面继续对弈。若策略只能在标准空棋盘开局中取得较好结果,说明其局面适应能力仍然有限。
环境中的无关特征也可能被策略错误利用。假设训练环境中某类棋盘显示方式总是与某个对手绑定,模型可能根据显示差异识别对手,而不是根据棋盘局面判断动作。当显示方式改变后,性能便会下降。
在机器人和自动驾驶任务中,类似问题可能来自光照、背景、传感器噪声或模拟环境中的固定纹理。策略可能依赖这些与任务目标无关的特征,导致环境外观稍有变化时无法正常决策。
提高泛化能力的一种方法是增加训练环境的多样性。五子棋可以使用不同水平、不同棋风和不同历史版本的策略组成对手池,使智能体不能只依赖某个对手的固定行为。训练中还可以随机交换先后手、改变初始棋盘状态,并加入不同类型的规则策略。
对于连续控制任务,可以在合理范围内随机改变物体质量、摩擦系数、传感器误差和环境布局。策略只有在多种条件下都取得较好回报,才更可能学到稳定的控制规律。这种做法通常称为环境随机化或域随机化。
状态数据增强也可以利用任务中的对称结构。五子棋棋盘经过旋转或翻转后,棋局的基本规则保持不变。训练时可以对同一棋盘进行旋转和镜像变换,并对动作位置作出相应调整,使模型接触更多等价局面。
设状态变换为 $T_s$,对应的动作变换为 $T_a$。如果变换不改变任务含义,则策略应当满足近似一致性:
\begin{equation}
\pi_{\theta}
\left(
T_a(a)\mid T_s(s)
\right)
\approx
\pi_{\theta}(a\mid s)
\end{equation}
这种一致性要求可以通过数据增强自然学习,也可以通过额外损失进行约束。它能够减少模型对棋盘绝对方向的依赖,提高对等价局面的利用效率。
模型结构也会影响泛化能力。与任务结构相匹配的网络通常更容易学习可复用特征。五子棋使用卷积网络能够在不同棋盘位置共享局部棋型检测参数,比为每个格点独立设置大量参数更有利于位置泛化。
正则化方法可以减少模型对训练数据的过度拟合,例如限制网络规模、使用权重衰减或在状态输入中加入适量噪声。不过,强化学习中的泛化问题不能只通过监督学习中的正则化解决,因为训练数据分布还取决于策略本身。更重要的是扩大训练场景和评估场景的覆盖范围。
评估变化需要分级,否则无法解释性能下降。第一层保持训练分布,检查任务是否学会;第二层保持规则不变,只更换合法开局、随机种子和未见对手,检查泛化;第三层加入观测噪声或合理参数扰动,检查鲁棒性。规则和目标都发生改变时,测到的是任务迁移,不应继续称为同一任务的泛化。
五子棋应分别测试训练对手、冻结的未见对手、合法中间局面、先后手和旋转翻转等价局面。失败类型决定修正方向:未见对手下降指向对手覆盖,中间局面下降指向状态覆盖,对称变换下降则说明模型没有学到规则本身具有的对称性。
稳定与泛化并不总是同步。单一训练分布可能让策略平滑地收敛到一个对手特化解;训练波动较大时,不同检查点又可能表现出不同的泛化结果。因此,多种子重复训练回答“能否复现”,未见场景测试回答“能否迁移到同一任务的新条件”,扰动测试回答“性能怎样退化”。一条平滑曲线不能代替后两种实验。
最后一种失败更隐蔽:训练可复现、未见场景也表现稳定,但策略优化的根本不是设计者真正关心的目标。
\subsection{强化学习方法的奖励构建问题}
前文讨论了怎样设置奖励,这里进一步看一种更棘手的情况:奖励已经可以计算,训练也能收敛,结果仍然可能不符合预期。训练回报上升而真实指标不变,往往说明可计算奖励只是一个不完整的代理目标;终局结果明确而早期动作更新混乱,问题来自反馈延迟;策略反复利用某条规则刷取高分,则属于奖励投机。任务同时追求速度、安全和能耗时,困难又不只是计算奖励,而是这些目标之间本来就需要人为取舍。
若可计算奖励为 $R$,算法求解的是
\begin{equation}
\pi_R^*
=
\underset{\pi}{\arg\max}
\;
\mathbb{E}_{\pi}
\left[
\sum_{t=0}^{T-1}
\gamma^t R(s_t,a_t,s_{t+1})
\right]
\end{equation}
这只定义了奖励最优策略 $\pi_R^*$。设计者真正关心的任务效用若记为 $U$,一般没有理由保证最大化 $R$ 同时最大化 $U$。五子棋胜率接近真实目标,但活三数量只是代理指标;暂时放弃局部棋型可能反而是长期好棋。代理指标越容易反复获取,错位越容易被优化放大。
当奖励只在任务结束时给出时,还会出现明显的信用分配问题。信用分配关注的是最终结果应当归因于此前哪些动作。五子棋智能体在一局对弈结束后得到了失败奖励,但这次失败可能来自最后一步没有阻挡,也可能来自更早之前破坏了自身棋型,还可能是连续多步选择共同造成的结果。
设一条轨迹包含动作序列:
\begin{equation}
a_0,a_1,\ldots,a_{T-1}
\end{equation}
如果只有终止时刻得到奖励 $r_{T-1}$,那么较早动作与最终奖励之间相隔较远。强化学习需要依靠回报和价值估计,将最终结果逐步传递到此前状态:
\begin{equation}
G_t
=
\sum_{k=t}^{T-1}
\gamma^{k-t}r_k
\end{equation}
当决策序列较长时,早期动作的回报会受到大量后续动作和环境变化影响。同一个动作在不同轨迹中可能获得完全不同的结果,使模型难以准确判断它对最终目标的贡献。
过程奖励缩短信号延迟,也把人工判断写进目标。为“形成活三”加分无法表达“此刻必须先防守”的局面依赖。更详细的规则未必更准确,只可能让策略更快学会设计者已经写出的启发式偏好。加入塑形奖励后必须重新检查最优行为是否发生变化。
奖励投机比一般的目标近似误差更具体:策略找到了一条规则允许、但设计者没有预料到的高回报路径。同一棋型被重复计分、机器人靠摆动刷取瞬时速度、推荐系统靠诱导性内容刷点击都属于这种情况。测试不能只验证“预期好行为能否得高分”,还要反向搜索“还有哪些行为能得到同样高分”。
复杂任务通常还包含多个目标。例如,一个控制系统既需要完成任务,又需要降低能耗、减少时间并保证安全。常见方法是将多个奖励项进行加权组合:
\begin{equation}
r_t
=
\sum_{i=1}^{K}
\lambda_i r_t^{(i)}
\end{equation}
其中 $\lambda_i$ 不是纯技术超参数,而是对速度、能耗、安全等目标作出的偏好声明。完成更快但能耗更高,与更慢但更安全的策略之间可能不存在唯一最优解。调权重不能替代需求方作出取舍,而且相同权重也不代表相同影响:各项的尺度和出现频率不同。多智能体协作还会叠加个体贡献分配问题,本章不在此展开。
难以手写奖励时,可以借助专家示范、逆强化学习或成对偏好。它们改变的是监督来源,不会消除目标错位:示范只覆盖专家到达的状态,评价者可能存在分歧,学习到的奖励模型仍会在训练分布外外推。策略持续优化这个模型时,同样可能发现模型漏洞。
无论奖励来自规则还是学习模型,都要保留与它不同源的评估信号。五子棋用独立对局胜率和失败棋局审计;控制任务可以使用任务完成率、安全事件和资源消耗。如果训练回报提高而这些指标不变,继续增加训练时间只会更充分地优化错误目标。
奖励审查应以轨迹为单位。分别抽取高回报、低回报、指标冲突和约束边界附近的轨迹,检查策略怎样获得每一项分数;再做奖励消融和权重敏感性实验,观察行为是否发生预期变化。只看奖励公式和均值曲线,无法发现策略实际利用的路径。
高回报只说明策略善于优化当前奖励。代理指标是否错位、反馈是否过度延迟、规则是否存在可利用路径,还需要通过独立指标、轨迹审计和消融实验判断。
几类挑战会相互放大:覆盖不足使奖励模型在数据外失真,训练波动改变策略利用奖励的方式,单一环境又让这些问题在训练指标中隐藏起来。因此,算法诊断、奖励审计和独立评估不能彼此替代。
\section{本章小结}
强化学习关注的是带有反馈和时间关联的序列决策:当前动作不仅产生即时结果,还会改变后续状态、数据分布和可选动作。从早期的控制、调度和博弈,到深度强化学习处理高维状态,再到大模型利用人类偏好或可验证结果进行后训练,应用对象不断变化,但核心问题始终是怎样用有限反馈改进长期行为。强化学习在大模型时代是连接已有能力与目标行为的重要工具,却不能替代预训练、监督微调和可靠评估。
五子棋把这一问题具体化:程序在终局才知道输赢,它怎样评价几十步以前的落子?回报把当前动作与未来结果联系起来,价值函数把这种联系写成可以递推估计的形式,策略梯度则直接改变动作概率。Actor-Critic 用正在学习的价值模型提供更及时的评价,因此减小了蒙特卡洛回报的部分波动,也把 Critic 的偏差带进了策略更新。PPO 试图让这种更新不要一次走得太远,但裁剪不是稳定性的保证。
当训练只能依靠历史数据时,问题从“怎样探索”变成“哪些判断有数据支持”。离线强化学习可以在行为数据之上改进策略,却无法现场验证陌生动作。进入具体任务后,算法只是整个系统的一部分:观测是否充分、动作能否执行、奖励是否代表真实目标,以及测试数据是否独立,都会改变最终结论。
因此,训练曲线上升之后仍要回到任务本身:策略究竟依据了哪些信息,数据怎样随策略改变,最后报告的指标是否真的对应任务目标。只说明实验采用了哪一种算法,还不足以回答这些问题。
训练回报高只说明当前奖励被有效优化;PPO 裁剪不保证训练稳定;离线数据量大不等于关键动作得到覆盖;输入更多信息也可能造成泄漏;密集奖励未必更接近真实目标;最后一个检查点未必优于验证集选出的检查点。这些反例指向同一个原则:算法输出只能在任务定义、数据来源和评估协议给定的范围内解释。
\section*{练习}
\begin{exercisebox}
\begin{enumerate}
\item \textbf{基础·价值递推} 设智能体从时刻 $t$ 开始获得的折扣回报为
\[
G_t=\sum_{k=t}^{T-1}\gamma^{k-t}r_k
\]
请先证明
\[
G_t=r_t+\gamma G_{t+1}
\]
再根据这一递推关系,写出策略 $\pi$ 下动作价值函数 $Q^\pi(s_t,a_t)$ 的贝尔曼期望方程,并说明最优动作价值函数中为什么会出现
$\max_{a'}Q^*(s_{t+1},a')$
\item \textbf{基础·Actor-Critic} 在某一五子棋状态 $s_t$ 下,Actor 选择位置 $B$ 落子,且该转移没有自然终止。已知即时奖励为
$r_t=0$,折扣因子为 $\gamma=0.9$,Critic 对落子前后状态的价值估计分别为
\[
V_\phi(s_t)=3.5,\qquad V_\phi(s_{t+1})=5.0
\]
计算时序差分目标 $y_t$ 和时序差分误差 $\delta_t$。根据计算结果,说明 Actor 应提高还是降低位置 $B$ 的选择概率,并说明 Critic 应如何调整对 $V_\phi(s_t)$ 的估计。
\item \textbf{进阶·PPO 裁剪} 在一次 PPO 更新中,旧策略选择动作 $a_t$ 的概率为 $0.40$,裁剪阈值为
$\epsilon=0.2$。分别考虑以下两种情况:
\[
\text{情况一:}\quad \widehat{A}_t=2,\qquad \pi_\theta(a_t\mid s_t)=0.56
\]
\[
\text{情况二:}\quad \widehat{A}_t=-3,\qquad \pi_\theta(a_t\mid s_t)=0.20
\]
对每种情况计算概率比 $\rho_t(\theta)$、裁剪后的概率比,以及 PPO 裁剪目标中的两个候选项。判断最终采用哪一项,并解释裁剪机制如何限制策略发生过大变化。
\item \textbf{进阶·离线强化学习} 判断下列说法是否正确,并简要说明理由:
“离线强化学习使用固定历史数据进行训练,因此只要数据量足够大,就可以像在线强化学习一样放心地选择数据集中从未出现过的动作。”
回答时应说明行为策略与目标策略的区别,并结合分布偏移、外推误差和保守价值估计解释离线强化学习为什么需要对未知动作保持谨慎。
\item \textbf{设计·任务建模} 现需要设计一个仓库移动机器人强化学习任务。机器人需要从起点到达指定货架,同时尽量缩短时间、降低能耗,并避免与障碍物发生碰撞。请完成以下设计:
\begin{enumerate}
\item 给出一种状态表示和动作空间
\item 区分任务中的核心奖励、辅助奖励和惩罚项
\item 判断“不能穿过障碍物”和“尽量减少能耗”分别应采用硬约束还是软约束
\item 说明至少两个用于独立评估策略性能的指标
\end{enumerate}
并简要分析奖励权重设置不合理时,机器人可能出现的非预期行为。
\item \textbf{设计·实验诊断} 某五子棋智能体在训练对手上的胜率达到 $90\%$,但更换为未参与训练的规则策略后,胜率下降到 $45\%$。同时,使用不同随机种子重复训练时,最终胜率波动较大。请分别从训练不稳定性和泛化性两个方面分析可能原因,并提出改进方案。
回答中至少应涉及以下内容中的四项:数据采样规模、Actor 与 Critic 的更新速度、策略熵、对手池、随机开局、棋盘旋转与翻转增强、多随机种子评估、独立测试环境。
\end{enumerate}
\end{exercisebox}
把课程讲义中需要使用的图片放在这个目录中。
示例:
- `cover.jpg`:封面图片,模板会自动读取。
- `example.png`:正文图片示例,模板会自动读取。
\begin{tikzpicture}[
font=\small,
box/.style={
draw=black,
rounded corners=4pt,
minimum width=2.8cm,
minimum height=1.15cm,
align=center,
line width=0.8pt
},
statebox/.style={
box,
fill=yellow!12,
minimum width=2.6cm
},
actorbox/.style={
box,
fill=blue!10,
minimum width=3.5cm,
minimum height=1.55cm
},
criticbox/.style={
box,
fill=green!12,
minimum width=3.5cm,
minimum height=1.55cm
},
envbox/.style={
box,
fill=orange!12,
minimum width=3.0cm,
minimum height=1.2cm
},
tdbox/.style={
box,
fill=gray!10,
minimum width=6.8cm,
minimum height=1.35cm
},
note/.style={
draw=black,
rounded corners=4pt,
fill=gray!5,
minimum width=5.0cm,
minimum height=0.95cm,
align=center,
line width=0.8pt
},
flow/.style={
draw=black,
-Latex,
line width=0.8pt
},
dashedflow/.style={
draw=black!75,
-Latex,
dashed,
line width=0.75pt
}
]
% =========================
% Nodes
% =========================
\node[statebox] (state) at (0,1.8) {当前棋盘状态\\$s_t$};
\node[actorbox] (actor) at (4.2,3.1) {Actor\\策略网络\\$\pi_{\theta}(a_t\mid s_t)$};
\node[criticbox] (critic) at (10.8,3.1) {Critic\\价值网络\\$V_{\phi}(s)$};
\node[envbox] (env) at (7.5,0.9) {五子棋环境};
\node[tdbox] (td) at (7.5,-2.0) {时序差分误差\\[0.08cm]
$\delta_t=r_t+\gamma V_{\phi}(s_{t+1})-V_{\phi}(s_t)$};
\node[note] (note1) at (4.2,5.1) {Actor 负责根据状态选择动作};
\node[note] (note2) at (10.8,5.1) {Critic 负责估计状态价值并评价动作};
% =========================
% Arrows: state input
% =========================
\draw[flow] (state.east) -- (actor.west);
\node at (2.0,2.15) {$s_t$};
\draw[flow]
(state.east)
.. controls (2.2,1.2) and (7.9,1.3)
.. (critic.west);
\node at (5.4,1.25) {$s_t$};
% =========================
% Action and environment
% =========================
\draw[flow] (actor.south east) -- (env.north west);
\node at (5.55,2.05) {动作 $a_t$};
\draw[flow] (env.north east) -- (critic.south west);
\node[align=center] at (9.45,2.0) {奖励 $r_t$\\下一状态 $s_{t+1}$};
% =========================
% TD error construction
% =========================
\draw[flow] (env.south) -- (td.north);
\node at (7.0,-0.45) {$r_t$};
\draw[flow] (critic.south) -- (td.north east);
\node[align=center] at (10.9,-0.2) {$V_{\phi}(s_t)$\\$V_{\phi}(s_{t+1})$};
% =========================
% Updates from TD error
% =========================
\draw[dashedflow]
(td.north west)
.. controls (5.4,-0.8) and (4.0,0.9)
.. (actor.south);
\node[align=center] at (4.3,-0.6) {策略更新\\Actor};
\draw[dashedflow]
(td.north east)
.. controls (9.8,-0.8) and (11.0,0.9)
.. (critic.south);
\node[align=center] at (10.8,-0.7) {价值更新\\Critic};
% =========================
% Optional environment loop
% =========================
\draw[dashedflow]
(env.west)
.. controls (5.2,0.1) and (1.8,0.1)
.. (state.south);
\node[align=center] at (3.2,0.35) {\footnotesize 下一状态用于下一轮决策};
\end{tikzpicture}
\ No newline at end of file
\begin{tikzpicture}[
font=\small,
box/.style={
draw=black,
rounded corners=4pt,
minimum width=2.8cm,
minimum height=1.0cm,
align=center,
line width=0.8pt
},
statebox/.style={
box,
fill=yellow!12,
minimum width=3.2cm,
minimum height=2.2cm
},
actorbox/.style={
box,
fill=blue!10,
minimum width=4.0cm,
minimum height=2.1cm
},
criticbox/.style={
box,
fill=green!12,
minimum width=4.2cm,
minimum height=2.1cm
},
tdbox/.style={
box,
fill=gray!10,
minimum width=6.2cm,
minimum height=1.8cm
},
note/.style={
draw=black,
rounded corners=4pt,
fill=gray!5,
minimum width=3.8cm,
minimum height=0.95cm,
align=center,
line width=0.8pt
},
flow/.style={
draw=black,
-Latex,
line width=0.8pt
},
dashedflow/.style={
draw=black!80,
-Latex,
dashed,
line width=0.75pt
}
]
% =========================
% Top: two states
% =========================
\node[statebox] (s0) at (2.2,4.8) {
落子前状态 $s_t$\\[0.15cm]
Critic 估计\\[0.08cm]
$V_{\phi}(s_t)=4.0$
};
\node[statebox] (s1) at (10.8,4.8) {
落子后状态 $s_{t+1}$\\[0.15cm]
Critic 估计\\[0.08cm]
$V_{\phi}(s_{t+1})=6.0$
};
% action arrow
\draw[flow] (s0.east) -- (s1.west);
\node[align=center] at (6.5,5.35) {Actor 选择动作\\$a_t=B=(8,8)$};
\node at (6.5,4.3) {即时奖励 $r_t=0$};
% =========================
% Middle: TD error
% =========================
\node[tdbox] (td) at (6.5,2.0) {
时序差分误差\\[0.10cm]
$\delta_t=r_t+\gamma V_{\phi}(s_{t+1})-V_{\phi}(s_t)$\\[0.10cm]
$=0+0.9\times 6.0-4.0=1.4>0$
};
\draw[flow] (s0.south) -- (td.north west);
\draw[flow] (s1.south) -- (td.north east);
% =========================
% Bottom left: Actor update
% =========================
\node[actorbox] (actor) at (3.0,-1.3) {
Actor 更新\\[0.15cm]
$\delta_t>0$,说明动作 $B$ 的表现\\
好于原来的预期\\[0.18cm]
提高动作 $B$ 的选择概率\\[0.08cm]
$0.40 \rightarrow 0.48$
};
% =========================
% Bottom right: Critic update
% =========================
\node[criticbox] (critic) at (10.0,-1.3) {
Critic 更新\\[0.15cm]
价值目标:\\[0.08cm]
$y_t=r_t+\gamma V_{\phi}(s_{t+1})$\\[0.08cm]
$=0+0.9\times 6.0=5.4$\\[0.18cm]
$V_{\phi}(s_t)$$4.0$\\
$5.4$ 调整
};
% \draw[dashedflow] (td.south west) .. controls (4.6,0.7) and (3.6,-0.1) .. (actor.north);
% \draw[dashedflow] (td.south east) .. controls (8.5,0.7) and (9.4,-0.1) .. (critic.north);
\draw[flow] (td.south west) -- (actor.north);
\draw[flow] (td.south east) -- (critic.north);
% =========================
% Bottom notes
% =========================
% \node[note] at (3.0,-3.3) {Actor 根据评价信号调整策略};
% \node[note] at (10.0,-3.3) {Critic 根据 TD 误差修正价值估计};
\end{tikzpicture}
\ No newline at end of file
\begin{tikzpicture}[
font=\small,
box/.style={
draw=black,
rounded corners=4pt,
minimum width=3.0cm,
minimum height=0.75cm,
align=center
},
envbox/.style={
box,
fill=orange!10
},
agentbox/.style={
box,
fill=green!10
},
policybox/.style={
box,
fill=blue!3,
minimum width=2.4cm,
minimum height=0.9cm
},
flow/.style={
draw=black,
-Latex,
line width=0.8pt
},
smallflow/.style={
draw=black!80,
-Latex,
line width=0.7pt
}
]
% =========================
% Nodes
% =========================
\node[envbox] (env) at (0,1.45) {环境};
\node[agentbox] (agent) at (0,-1.55) {智能体};
\node[policybox] (policy) at (0,0.0) {策略\\$\pi(a_t|s_t)$};
% =========================
% Outer interaction loop
% =========================
% action: agent -> environment, right side
\draw[flow]
(agent.east)
-- ++(2.2,0)
-- ++(0,3.0)
-- (env.east);
\node at (3.0,0.05) {动作$(a)$};
% reward: environment -> agent, left side outer
\draw[flow]
(env.west)
-- ++(-2.5,0)
-- ++(0,-3.0)
-- (agent.west);
\node at (-4.75,0.05) {奖励$(r)$};
% state: environment -> agent, left inner curved arrow
\draw[flow]
(env.west)
.. controls (-2.55,0.65) and (-2.55,-0.65)
.. (agent.west);
\node at (-3.05,0.05) {状态$(s)$};
% =========================
% Policy-Agent relation
% =========================
\draw[smallflow, dashed]
(policy.west)
.. controls (-1.85,-0.1) and (-1.85,-1)
.. (agent.north west);
\draw[smallflow, dashed]
(agent.north east)
.. controls (1.85,-1.05) and (1.85,-0.25)
.. (policy.east);
\end{tikzpicture}
\begin{tikzpicture}[
font=\small,
flow/.style={
draw=black,
-Latex,
line width=0.8pt
},
dashedflow/.style={
draw=black!75,
-Latex,
dashed,
line width=0.75pt
},
panel/.style={
draw=black,
rounded corners=4pt,
line width=0.8pt
},
titlebox/.style={
draw=black,
rounded corners=4pt,
fill=blue!5,
minimum height=0.75cm,
align=center
},
infobox/.style={
draw=black,
rounded corners=4pt,
fill=gray!5,
align=center,
line width=0.8pt
},
blackstone/.style={
circle,
draw=black,
fill=black,
minimum size=6.5mm,
inner sep=0pt
},
whitestone/.style={
circle,
draw=black,
fill=white,
minimum size=6.5mm,
inner sep=0pt
},
cand/.style={
circle,
draw=orange!80!black,
fill=orange!20,
line width=0.9pt,
minimum size=7mm,
inner sep=0pt,
font=\bfseries
},
bestcand/.style={
circle,
draw=green!50!black,
fill=green!25,
line width=1.1pt,
minimum size=7.4mm,
inner sep=0pt,
font=\bfseries
},
rowbox/.style={
draw=black!30,
rounded corners=2pt,
minimum height=0.62cm,
align=left
}
]
% =========================
% Left panel: board
% =========================
\draw[panel, fill=orange!8] (0.3,5.2) rectangle (5.9,11.0);
\node[titlebox, minimum width=4.3cm] at (3.1,11.55) {当前棋盘状态 $s_t$};
% board grid
\draw[line width=0.8pt] (1,6) rectangle (5,10);
\foreach \x in {1,2,3,4,5}{
\draw[line width=0.5pt] (\x,6) -- (\x,10);
}
\foreach \y in {6,7,8,9,10}{
\draw[line width=0.5pt] (1,\y) -- (5,\y);
}
% axis labels
\node at (1,5.65) {6};
\node at (2,5.65) {7};
\node at (3,5.65) {8};
\node at (4,5.65) {9};
\node at (5,5.65) {10};
\node at (0.55,6) {6};
\node at (0.55,7) {7};
\node at (0.55,8) {8};
\node at (0.55,9) {9};
\node at (0.55,10) {10};
\node at (3.0,10.55) {\footnotesize 局部棋盘示意};
% existing stones
\node[blackstone] at (2,7) {};
\node[blackstone] at (3,7) {};
\node[blackstone] at (4,8) {};
\node[whitestone] at (2,9) {};
\node[whitestone] at (4,7) {};
\node[whitestone] at (5,8) {};
% candidate positions
\node[cand] at (2,8) {A};
\node[bestcand] at (3,8) {B};
\node[cand] at (3,9) {C};
\node[cand] at (4,9) {D};
% legend
\node[blackstone] at (1.2,5.1) {};
\node[anchor=west] at (1.45,5.1) {\footnotesize 黑棋};
\node[whitestone] at (2.4,5.1) {};
\node[anchor=west] at (2.65,5.1) {\footnotesize 白棋};
\node[cand, minimum size=5.8mm] at (3.7,5.1) {};
\node[anchor=west] at (3.95,5.1) {\footnotesize 候选位置};
\node[bestcand, minimum size=6.2mm] at (5.05,5.1) {};
\node[anchor=west] at (5.3,5.1) {\footnotesize 采样动作};
% =========================
% Middle panel: policy model
% =========================
\draw[flow] (6.15,8.2) -- (7.3,8.2);
\node at (6.72,8.55) {\footnotesize 状态输入};
\draw[panel, fill=blue!4] (7.5,7.1) rectangle (10.8,9.3);
\node[titlebox, minimum width=2.8cm] at (9.15,9.75) {策略模型};
\node at (9.15,8.55) {$\pi_{\theta}(a\mid s_t)$};
\node[align=center] at (9.15,7.75) {\footnotesize 输出合法动作的\\\footnotesize 概率分布};
\draw[flow] (10.95,8.2) -- (12.1,8.2);
\node at (11.52,8.55) {\footnotesize 概率输出};
% =========================
% Right panel: probability distribution
% =========================
\draw[panel, fill=green!5] (12.3,5.2) rectangle (18.5,11.0);
\node[titlebox, minimum width=5.0cm] at (15.4,11.55) {落子概率分布};
\draw[rowbox, fill=white] (12.8,10.1) rectangle (18.0,10.7);
\node[anchor=west] at (13.05,10.4) {$A:(7,8)\qquad \pi_{\theta}(A\mid s_t)=0.15$};
\draw[rowbox, fill=green!15] (12.8,9.15) rectangle (18.0,9.75);
\node[anchor=west] at (13.05,9.45) {$B:(8,8)\qquad \pi_{\theta}(B\mid s_t)=0.40$};
\node[anchor=west, text=green!40!black] at (16.95,9.45) {\footnotesize 概率最高};
\draw[rowbox, fill=white] (12.8,8.2) rectangle (18.0,8.8);
\node[anchor=west] at (13.05,8.5) {$C:(8,9)\qquad \pi_{\theta}(C\mid s_t)=0.25$};
\draw[rowbox, fill=white] (12.8,7.25) rectangle (18.0,7.85);
\node[anchor=west] at (13.05,7.55) {$D:(9,9)\qquad \pi_{\theta}(D\mid s_t)=0.20$};
% bottom formula / note
\draw[infobox] (12.8,5.85) rectangle (18.0,6.85);
\node[align=center] at (15.4,6.48) {$a_t\sim\pi_{\theta}(\cdot\mid s_t)$};
\node[align=center, text=green!40!black] at (15.4,6.1) {\footnotesize 按概率分布采样动作};
% sampled action arrow from probability row B back to board B
\draw[dashedflow]
(12.8,9.45)
.. controls (10.8,10.9) and (6.7,10.8)
.. (3,8.35);
\node[text=green!40!black] at (8.0,10.45) {\footnotesize 本次采样选择位置 $B=(8,8)$};
\end{tikzpicture}
\ No newline at end of file
\begin{tikzpicture}[
font=\small,
flow/.style={
draw=black,
-Latex,
line width=0.8pt
},
panel/.style={
draw=black,
rounded corners=4pt,
line width=0.8pt
},
titlebox/.style={
draw=black,
rounded corners=4pt,
fill=blue!5,
minimum height=0.75cm,
minimum width=4.6cm,
align=center
},
info/.style={
draw=black!65,
rounded corners=3pt,
line width=0.6pt,
fill=gray!5
},
blackstone/.style={
circle,
draw=black,
fill=black,
minimum size=6.5mm,
inner sep=0pt
},
whitestone/.style={
circle,
draw=black,
fill=white,
minimum size=6.5mm,
inner sep=0pt
},
cand/.style={
circle,
draw=orange!80!black,
fill=orange!20,
line width=0.9pt,
minimum size=7mm,
inner sep=0pt,
font=\bfseries
},
bestcand/.style={
circle,
draw=green!50!black,
fill=green!25,
line width=1.1pt,
minimum size=7.4mm,
inner sep=0pt,
font=\bfseries
},
legendblack/.style={
circle,
draw=black,
fill=black,
minimum size=4.8mm,
inner sep=0pt
},
legendwhite/.style={
circle,
draw=black,
fill=white,
minimum size=4.8mm,
inner sep=0pt
},
legendcand/.style={
circle,
draw=orange!80!black,
fill=orange!20,
line width=0.7pt,
minimum size=5.0mm,
inner sep=0pt
},
legendbest/.style={
circle,
draw=green!50!black,
fill=green!25,
line width=0.8pt,
minimum size=5.2mm,
inner sep=0pt
},
rowbox/.style={
draw=black!30,
rounded corners=2pt,
line width=0.5pt
},
resultbox/.style={
draw=green!50!black,
rounded corners=2pt,
line width=0.65pt,
fill=green!10
}
]
% ==================================================
% 左侧面板:当前棋盘局面
% ==================================================
\draw[panel, fill=orange!8]
(4.9,3.65) rectangle (11.1,11.0);
\node[titlebox]
at (8.0,11.65)
{当前棋盘局面 $s_t$};
\node at (8.0,10.55)
{\footnotesize 局部棋盘示意};
% 棋盘网格
\draw[line width=0.8pt]
(6,6) rectangle (10,10);
\foreach \x in {6,7,8,9,10}{
\draw[line width=0.5pt]
(\x,6) -- (\x,10);
}
\foreach \y in {6,7,8,9,10}{
\draw[line width=0.5pt]
(6,\y) -- (10,\y);
}
% 横坐标
\foreach \x in {6,7,8,9,10}{
\node[anchor=north]
at (\x,5.72)
{\x};
}
% 纵坐标:右对齐
\foreach \y in {6,7,8,9,10}{
\node[anchor=east]
at (5.72,\y)
{\y};
}
% 已有棋子
\node[blackstone] at (7,7) {};
\node[blackstone] at (8,7) {};
\node[blackstone] at (9,8) {};
\node[whitestone] at (7,9) {};
\node[whitestone] at (9,7) {};
\node[whitestone] at (10,8) {};
% 候选位置
\node[cand] at (7,8) {A};
\node[bestcand] at (8,8) {B};
\node[cand] at (8,9) {C};
\node[cand] at (9,9) {D};
% 图例:第一行
\node[legendblack] at (6.0,4.95) {};
\node[
anchor=west,
font=\footnotesize
] at (6.30,4.95) {黑棋};
\node[legendwhite] at (8.45,4.95) {};
\node[
anchor=west,
font=\footnotesize
] at (8.75,4.95) {白棋};
% 图例:第二行
\node[legendcand] at (6.0,4.25) {};
\node[
anchor=west,
font=\footnotesize
] at (6.30,4.25) {候选位置};
\node[legendbest] at (8.45,4.25) {};
\node[
anchor=west,
font=\footnotesize
] at (8.75,4.25) {最优候选};
% ==================================================
% 中间连接箭头
% ==================================================
\draw[flow]
(11.45,8.90) -- (13.15,8.90);
\node at (12.30,9.23)
{\footnotesize 价值评估};
% ==================================================
% 右侧面板:动作价值评估
% ==================================================
\draw[panel, fill=green!5]
(13.5,3.65) rectangle (19.7,11.0);
\node[titlebox]
at (16.6,11.65)
{动作价值评估};
% 动作 A
\draw[rowbox, fill=white]
(13.8,9.95) rectangle (19.3,10.60);
\node[anchor=west]
at (13.98,10.275)
{$A:(7,8)$};
\node[anchor=west]
at (16.15,10.275)
{$Q(s_t,A)=1.5$};
% 动作 B
\draw[rowbox, fill=green!15]
(13.8,9.05) rectangle (19.3,9.70);
\node[anchor=west]
at (13.98,9.375)
{$B:(8,8)$};
\node[anchor=west]
at (16.15,9.375)
{$Q(s_t,B)=6.2$};
% 动作 C
\draw[rowbox, fill=white]
(13.8,8.15) rectangle (19.3,8.80);
\node[anchor=west]
at (13.98,8.475)
{$C:(8,9)$};
\node[anchor=west]
at (16.15,8.475)
{$Q(s_t,C)=3.7$};
% 动作 D
\draw[rowbox, fill=white]
(13.8,7.25) rectangle (19.3,7.90);
\node[anchor=west]
at (13.98,7.575)
{$D:(9,9)$};
\node[anchor=west]
at (16.15,7.575)
{$Q(s_t,D)=-1.1$};
% 动作选择公式
\draw[info]
(13.8,5.75) rectangle (19.3,6.75);
\node[align=center]
at (16.55,6.25)
{$
a_t=
\displaystyle
\arg\max_{a\in\mathcal{A}(s_t)}
Q(s_t,a)
$};
% 最终选择结果
\draw[resultbox]
(13.8,4.50) rectangle (19.3,5.25);
\node[
align=center,
text=green!40!black,
font=\footnotesize\bfseries
] at (16.55,4.875)
{因此选择位置 $(8,8)$ 落子};
\end{tikzpicture}
\begin{tikzpicture}[
font=\small,
flow/.style={
draw=black,
-Latex,
line width=0.8pt
},
dashedflow/.style={
draw=black!70,
-Latex,
dashed,
line width=0.75pt
},
panel/.style={
draw=black,
rounded corners=5pt,
line width=0.8pt
},
titlebox/.style={
draw=black,
rounded corners=4pt,
fill=blue!6,
minimum height=0.72cm,
align=center,
line width=0.8pt
},
statebox/.style={
draw=black,
rounded corners=4pt,
fill=gray!6,
minimum width=4.8cm,
minimum height=0.9cm,
align=center,
line width=0.8pt
},
actionbox/.style={
draw=black,
rounded corners=3pt,
fill=green!7,
minimum width=2.1cm,
minimum height=0.95cm,
align=center,
line width=0.75pt
},
rarebox/.style={
draw=red!70!black,
rounded corners=3pt,
fill=red!7,
minimum width=2.1cm,
minimum height=0.95cm,
align=center,
line width=0.9pt
},
valuebox/.style={
draw=black,
rounded corners=3pt,
fill=blue!6,
minimum width=2.1cm,
minimum height=0.95cm,
align=center,
line width=0.75pt
},
highvaluebox/.style={
draw=red!70!black,
rounded corners=3pt,
fill=red!9,
minimum width=2.1cm,
minimum height=0.95cm,
align=center,
line width=0.9pt
},
policybox/.style={
draw=black,
rounded corners=4pt,
fill=yellow!12,
minimum width=4.8cm,
minimum height=1.05cm,
align=center,
line width=0.8pt
},
warningbox/.style={
draw=red!70!black,
rounded corners=4pt,
fill=red!7,
minimum width=4.8cm,
minimum height=1.15cm,
align=center,
line width=0.9pt
},
notebox/.style={
draw=black,
rounded corners=4pt,
fill=gray!6,
minimum height=1.05cm,
align=center,
line width=0.8pt
}
]
% ==================================================
% Overall title
% ==================================================
% \node[titlebox, minimum width=8.2cm] at (9.4,10.8)
% {离线强化学习中的分布偏移与价值高估};
% ==================================================
% Left panel: data coverage
% ==================================================
\draw[panel, fill=green!2] (0.2,2.0) rectangle (6.0,10.1);
\node[titlebox, minimum width=4.5cm] at (3.1,9.55)
{\textbf{历史数据覆盖}};
\node[statebox] at (3.1,8.25)
{
五子棋状态 $s_t$\\
历史棋谱中的候选落子
};
\node[actionbox] (a1) at (1.65,6.75)
{
位置 $A$\\
样本数:$120$
};
\node[actionbox] (a2) at (4.55,6.75)
{
位置 $B$\\
样本数:$85$
};
\node[actionbox] (a3) at (1.65,5.25)
{
位置 $C$\\
样本数:$46$
};
\node[rarebox] (a4) at (4.55,5.25)
{
位置 $D$\\
样本数:$2$
};
\node[notebox, minimum width=4.9cm] at (3.1,3.25)
{
位置 $A$$B$$C$ 有较多数据支持\\
位置 $D$ 在历史棋谱中很少出现
};
% ==================================================
% Middle panel: value estimation
% ==================================================
\draw[panel, fill=blue!2] (6.5,2.0) rectangle (12.3,10.1);
\node[titlebox, minimum width=4.5cm] at (9.4,9.55)
{\textbf{价值模型估计}};
\node[statebox] at (9.4,8.25)
{
价值网络计算\\
$Q(s_t,a)$
};
\node[valuebox] (q1) at (7.95,6.75)
{
$Q(s_t,A)$\\
$5.2$
};
\node[valuebox] (q2) at (10.85,6.75)
{
$Q(s_t,B)$\\
$5.8$
};
\node[valuebox] (q3) at (7.95,5.25)
{
$Q(s_t,C)$\\
$4.6$
};
\node[highvaluebox] (q4) at (10.85,5.25)
{
$Q(s_t,D)$\\
$8.7$
};
\node[notebox, minimum width=4.9cm] at (9.4,3.25)
{
位置 $D$ 缺少数据支持\\
但被价值模型赋予了最高估计
};
% ==================================================
% Right panel: target policy shift
% ==================================================
\draw[panel, fill=orange!3] (12.8,2.0) rectangle (18.6,10.1);
\node[titlebox, minimum width=4.5cm] at (15.7,9.55)
{\textbf{目标策略偏移}};
\node[policybox] (policy) at (15.7,8.05)
{
目标策略选择价值最大的动作\\
$a_t=\arg\max_a Q(s_t,a)$
};
\node[policybox, fill=red!8] (selectd) at (15.7,6.25)
{
选择位置 $D$\\
$\pi_{\theta}(D\mid s_t)$ 增大
};
\node[warningbox] (newstate) at (15.7,4.35)
{
进入历史数据很少覆盖的状态\\
价值估计可能不再可靠
};
\node[notebox, minimum width=4.9cm] at (15.7,2.75)
{
离线训练无法通过新的环境交互\\
及时验证并修正这一判断
};
% ==================================================
% Arrows between panels
% ==================================================
% \draw[flow]
% (6.05,6.15) --
% node[above, align=center] {\footnotesize 训练价值模型}
% (6.45,6.15);
% \draw[flow]
% (12.35,6.15) --
% node[above, align=center] {\footnotesize 根据估计更新策略}
% (12.75,6.15);
% Right internal arrows
\draw[flow] (policy.south) -- (selectd.north);
\draw[flow] (selectd.south) -- (newstate.north);
% Highlight rare action correspondence
% \draw[dashedflow]
% (a4.east)
% .. controls (5.9,4.65) and (7.0,4.65)
% .. (q4.west);
% ==================================================
% Bottom summary
% ==================================================
\node[
notebox,
fill=yellow!10,
minimum width=17.2cm,
minimum height=1.05cm
] at (9.4,1.15)
{
数据覆盖不足
$\longrightarrow$
未知动作价值被高估
$\longrightarrow$
目标策略偏离行为数据分布
};
\end{tikzpicture}
\ No newline at end of file
\begin{tikzpicture}[
font=\small,
flow/.style={
draw=black,
-Latex,
line width=0.8pt
},
dashedflow/.style={
draw=black!70,
-Latex,
dashed,
line width=0.75pt
},
panel/.style={
draw=black,
rounded corners=5pt,
line width=0.8pt
},
titlebox/.style={
draw=black,
rounded corners=4pt,
fill=blue!6,
minimum height=0.72cm,
align=center,
line width=0.8pt
},
agentbox/.style={
draw=black,
rounded corners=4pt,
fill=blue!9,
minimum width=3.8cm,
minimum height=1.15cm,
align=center,
line width=0.8pt
},
envbox/.style={
draw=black,
rounded corners=4pt,
fill=orange!10,
minimum width=3.8cm,
minimum height=1.15cm,
align=center,
line width=0.8pt
},
databox/.style={
draw=black,
rounded corners=4pt,
fill=green!8,
minimum width=4.3cm,
minimum height=1.25cm,
align=center,
line width=0.8pt
},
trainbox/.style={
draw=black,
rounded corners=4pt,
fill=purple!7,
minimum width=4.3cm,
minimum height=1.2cm,
align=center,
line width=0.8pt
},
resultbox/.style={
draw=black,
rounded corners=4pt,
fill=yellow!12,
minimum width=4.3cm,
minimum height=1.15cm,
align=center,
line width=0.8pt
},
notebox/.style={
draw=black,
rounded corners=4pt,
fill=gray!6,
minimum width=6.7cm,
minimum height=1.15cm,
align=center,
line width=0.8pt
}
]
% ==================================================
% Overall title
% ==================================================
% \node[titlebox, minimum width=8.0cm] at (9.3,10.9)
% {在线强化学习与离线强化学习的数据使用方式};
% ==================================================
% Left panel: online RL
% ==================================================
\draw[panel, fill=blue!2] (0.3,1.1) rectangle (8.8,10.2);
\node[titlebox, minimum width=4.8cm] at (4.55,9.65)
{\textbf{在线强化学习}};
\node[agentbox] (online-agent) at (4.55,7.9)
{
当前策略 $\pi_{\theta}$\\[0.05cm]
根据棋盘状态选择落子
};
\node[envbox] (online-env) at (4.55,5.55)
{
环境交互\\[0.05cm]
完成新的五子棋对弈
};
\node[databox] (online-data) at (4.55,3.2)
{
新交互数据\\[0.05cm]
$(s_t,a_t,r_t,s_{t+1})$
};
% Main online loop
\draw[flow] (online-agent.south) --
node[right, align=left] {\footnotesize 执行动作 $a_t$}
(online-env.north);
\draw[flow] (online-env.south) --
node[right, align=left] {\footnotesize 返回奖励和新状态}
(online-data.north);
\draw[flow]
(online-data.west)
.. controls (1.05,3.2) and (1.05,7.9)
.. node[left, align=center] {\footnotesize 使用新数据\\[-0.05cm]\footnotesize 更新策略}
(online-agent.west);
\node[notebox, minimum width=6.6cm] at (4.55,1.75)
{
策略更新后重新进入环境\\
训练数据会随当前策略不断变化
};
% ==================================================
% Right panel: offline RL
% ==================================================
\draw[panel, fill=green!2] (9.3,1.1) rectangle (18.3,10.2);
\node[titlebox, minimum width=4.8cm] at (13.8,9.65)
{\textbf{离线强化学习}};
\node[databox, minimum width=5.1cm] (offline-data) at (13.8,8.0)
{
固定历史数据集 $\mathcal{D}$\\[0.05cm]
棋盘状态、落子、奖励和后续状态
};
\node[trainbox, minimum width=5.1cm] (offline-train) at (13.8,5.7)
{
离线训练\\[0.05cm]
学习价值函数与目标策略
};
\node[resultbox, minimum width=5.1cm] (offline-policy) at (13.8,3.4)
{
目标策略 $\pi_{\theta}$\\[0.05cm]
部署后根据状态选择动作
};
\draw[flow] (offline-data.south) --
node[right, align=left] {\footnotesize 从固定数据集中采样}
(offline-train.north);
\draw[flow] (offline-train.south) --
node[right, align=left] {\footnotesize 参数优化}
(offline-policy.north);
\node[notebox, minimum width=7.0cm] at (13.8,1.75)
{
训练期间不再进入环境采集新数据\\
能够学习的内容受历史数据覆盖范围限制
};
% ==================================================
% Contrast annotation in the middle
% ==================================================
\draw[dashedflow]
(8.85,7.95) --
node[above, align=center] {\footnotesize 数据来源不同}
(9.25,7.95);
\end{tikzpicture}
\ No newline at end of file
\begin{tikzpicture}[
font=\small,
flow/.style={
draw=black!80,
-Latex,
line width=0.8pt,
shorten <=1.5pt,
shorten >=1.5pt
},
inputbox/.style={
draw=black!75,
rounded corners=4pt,
fill=orange!8,
text width=4.0cm,
minimum height=4.9cm,
align=center,
line width=0.8pt,
inner xsep=7pt,
inner ysep=7pt
},
branchbox/.style={
draw=black!75,
rounded corners=4pt,
text width=5.2cm,
minimum height=2.85cm,
align=center,
line width=0.8pt,
inner xsep=7pt,
inner ysep=6pt
},
positivebox/.style={
branchbox,
fill=green!9
},
negativebox/.style={
branchbox,
fill=red!6
},
resultbox/.style={
draw=black!75,
rounded corners=4pt,
text width=4.2cm,
minimum height=2.35cm,
align=center,
line width=0.8pt,
inner xsep=7pt,
inner ysep=6pt
},
positiveresult/.style={
resultbox,
fill=green!6
},
negativeresult/.style={
resultbox,
fill=red!4
},
principlebox/.style={
draw=black!65,
rounded corners=4pt,
fill=gray!6,
text width=13.3cm,
minimum height=1.2cm,
align=center,
line width=0.7pt,
inner xsep=8pt,
inner ysep=6pt
},
arrowlabel/.style={
font=\footnotesize,
text=black!80,
fill=white,
inner xsep=2pt,
inner ysep=1pt
},
branchlabel/.style={
font=\footnotesize,
text=black!80,
fill=white,
inner xsep=2pt,
inner ysep=1pt
}
]
% 设置安全边界,避免导出时裁切
\path[use as bounding box]
(-0.2,-0.35) rectangle (18.2,8.2);
% ==================================================
% 左侧:当前状态、策略与动作采样
% ==================================================
\node[inputbox] (input) at (2.5,4.75) {
{\bfseries 当前策略与动作}\\[6pt]
{\bfseries 当前棋盘状态 $s_t$}\\[4pt]
候选位置:$B=(8,8)$\\[4pt]
当前策略概率:\\[2pt]
$\displaystyle
\pi_{\theta}(B\mid s_t)=0.40
$\\[9pt]
{\footnotesize 按当前策略采样}\\[-1pt]
$\Downarrow$\\[3pt]
$\displaystyle
a_t=B=(8,8)
$\\[7pt]
{\footnotesize
随后观察该动作产生的实际回报}
};
% ==================================================
% 中间:正优势分支
% ==================================================
\node[positivebox] (positive) at (9.0,6.40) {
{\bfseries 正优势($+$):回报高于基线}\\[6pt]
实际回报:$G_t=8$\\[3pt]
状态基线:$b(s_t)=6$\\[5pt]
优势估计:$\widehat{A}_t=G_t-b(s_t)$\\[2pt]
$\displaystyle
=8-6=2>0
$\\[5pt]
{\footnotesize
该动作表现优于当前局面的平均水平}
};
% ==================================================
% 中间:负优势分支
% ==================================================
\node[negativebox] (negative) at (9.0,3.10) {
{\bfseries 负优势($-$):回报低于基线}\\[6pt]
实际回报:$G_t=3$\\[3pt]
状态基线:$b(s_t)=6$\\[5pt]
优势估计:$\widehat{A}_t=G_t-b(s_t)$\\[2pt]
$\displaystyle
=3-6=-3<0
$\\[5pt]
{\footnotesize
该动作表现低于当前局面的平均水平}
};
% ==================================================
% 右侧:正优势对应的策略更新结果
% ==================================================
\node[positiveresult] (positive-result) at (15.6,6.40) {
{\bfseries 动作概率提高($\uparrow$}\\[7pt]
更新前:\\[-1pt]
$\displaystyle
\pi_{\theta}(B\mid s_t)=0.40
$\\[5pt]
更新后:\\[-1pt]
$\displaystyle
\pi_{\theta}(B\mid s_t)=0.48
$\\[6pt]
{\footnotesize
后续更容易选择动作 $B$}
};
% ==================================================
% 右侧:负优势对应的策略更新结果
% ==================================================
\node[negativeresult] (negative-result) at (15.6,3.10) {
{\bfseries 动作概率降低($\downarrow$}\\[7pt]
更新前:\\[-1pt]
$\displaystyle
\pi_{\theta}(B\mid s_t)=0.40
$\\[5pt]
更新后:\\[-1pt]
$\displaystyle
\pi_{\theta}(B\mid s_t)=0.32
$\\[6pt]
{\footnotesize
后续选择动作 $B$ 的概率下降}
};
% ==================================================
% 左侧动作节点到正负分支的公共分叉
% ==================================================
\coordinate (split) at (5.35,4.75);
\coordinate (positive-join) at (5.35,6.40);
\coordinate (negative-join) at (5.35,3.10);
% 从左侧动作模块引出主干
\draw[flow]
(input.east)
--
(split);
% 公共分叉点
\fill[black!80]
(split) circle (1.4pt);
% 正优势分支
\draw[flow]
(split)
--
(positive-join)
--
node[midway, above=3pt, branchlabel]
{$G_t>b(s_t)$}
(positive.west);
% 负优势分支
\draw[flow]
(split)
--
(negative-join)
--
node[midway, below=3pt, branchlabel]
{$G_t<b(s_t)$}
(negative.west);
% ==================================================
% 优势分支到概率更新结果
% ==================================================
\draw[flow]
(positive.east)
--
node[midway, above=4pt, arrowlabel]
{提高选择概率}
(positive-result.west);
\draw[flow]
(negative.east)
--
node[midway, above=4pt, arrowlabel]
{降低选择概率}
(negative-result.west);
% ==================================================
% 底部:统一的基线与策略更新原则
% ==================================================
\node[principlebox] (principle) at (9.0,0.45) {
{\bfseries 基线与策略更新原则}\\[5pt]
常用状态基线为
$\displaystyle b(s_t)=V^{\pi}(s_t)$\\[3pt]
策略更新关注的不是回报的绝对大小,
而是动作结果相对于当前平均水平的好坏。
};
\end{tikzpicture}
\ No newline at end of file
\begin{tikzpicture}[
font=\small,
flow/.style={
draw=black,
-Latex,
line width=0.8pt
},
dashedflow/.style={
draw=black!80,
-Latex,
dashed,
line width=0.75pt
},
panel/.style={
draw=black,
rounded corners=4pt,
line width=0.8pt
},
titlebox/.style={
draw=black,
rounded corners=4pt,
fill=blue!5,
minimum height=0.72cm,
align=center
},
infobox/.style={
draw=black,
rounded corners=4pt,
fill=gray!6,
minimum width=4.2cm,
minimum height=1.05cm,
align=center,
line width=0.8pt
},
rowbox/.style={
draw=black,
rounded corners=4pt,
minimum width=5.2cm,
minimum height=1.95cm,
align=left,
line width=0.8pt
},
pospanel/.style={
draw=black,
rounded corners=4pt,
fill=green!6,
line width=0.8pt
},
negpanel/.style={
draw=black,
rounded corners=4pt,
fill=red!5,
line width=0.8pt
}
]
% =========================
% Left panel
% =========================
\draw[panel, fill=orange!8] (0.2,3.8) rectangle (5.5,10.4);
\node[titlebox, minimum width=4.2cm] at (2.85,10.9) {旧策略与裁剪区间};
\node[infobox] at (2.85,9.55) {
旧策略\\[0.06cm]
$\pi_{\theta_{\mathrm{old}}}(B\mid s_t)=0.40$
};
\node[infobox] at (2.85,7.8) {
裁剪阈值\\[0.06cm]
$\epsilon=0.2$
};
\node[infobox] at (2.85,6.05) {
概率比值允许范围\\[0.06cm]
$[1-\epsilon,1+\epsilon]=[0.8,1.2]$
};
\node[infobox, minimum width=4.4cm, minimum height=1.35cm] at (2.85,4.5) {
裁剪限制的是\\[0.05cm]
过大概率变化带来的\\[0.05cm]
目标函数收益
};
% arrows to two cases
\draw[flow] (5.65,8.6) -- (6.8,8.9);
\draw[flow] (5.65,5.6) -- (6.8,5.3);
% =========================
% Top right panel: positive advantage
% =========================
\draw[pospanel] (6.9,7.25) rectangle (18.1,10.4);
\node[titlebox, minimum width=4.8cm] at (12.5,10.9) {正优势动作 $\widehat{A}_t>0$};
\draw[rowbox, fill=white] (7.3,8.05) rectangle (12.2,9.8);
\node[anchor=west, align=left] at (7.55,9.45) {
新策略概率:$0.48$\\
概率比值:$r_t(\theta)=0.48/0.40=1.20$\\
位于上界\\
允许提高动作概率
};
\draw[rowbox, fill=green!12] (12.8,8.05) rectangle (17.7,9.8);
\node[anchor=west, align=left] at (13.05,9.45) {
新策略概率:$0.60$\\
概率比值:$r_t(\theta)=0.60/0.40=1.50$\\
超出上界 $1.20$\\
裁剪后按 $1.20$ 计算收益
};
\node[text=green!40!black] at (9.75,7.55) {\footnotesize 适度提高概率};
\node[text=green!40!black] at (15.25,7.55) {\footnotesize 过大变化不再获得额外鼓励};
% =========================
% Bottom right panel: negative advantage
% =========================
\draw[negpanel] (6.9,3.8) rectangle (18.1,6.95);
\node[titlebox, minimum width=4.8cm] at (12.5,7.45) {负优势动作 $\widehat{A}_t<0$};
\draw[rowbox, fill=white] (7.3,4.6) rectangle (12.2,6.35);
\node[anchor=west, align=left] at (7.55,6.0) {
新策略概率:$0.32$\\
概率比值:$r_t(\theta)=0.32/0.40=0.80$\\
位于下界\\
允许降低动作概率
};
\draw[rowbox, fill=red!10] (12.8,4.6) rectangle (17.7,6.35);
\node[anchor=west, align=left] at (13.05,6.0) {
新策略概率:$0.20$\\
概率比值:$r_t(\theta)=0.20/0.40=0.50$\\
低于下界 $0.80$\\
裁剪后按 $0.80$ 计算收益
};
\node[text=red!60!black] at (9.75,4.1) {\footnotesize 适度降低概率};
\node[text=red!60!black] at (15.25,4.1) {\footnotesize 过大变化不再获得额外鼓励};
% =========================
% Bottom note
% =========================
\draw[panel, fill=gray!6] (6.9,2.2) rectangle (18.1,3.3);
\node[align=center] at (12.5,2.75) {
PPO 并不直接禁止新策略继续变化\\
而是限制超出裁剪范围后的额外优化收益
};
\end{tikzpicture}
\ No newline at end of file
\begin{tikzpicture}[
font=\small,
flow/.style={
draw=black,
-Latex,
line width=0.8pt
},
dashedflow/.style={
draw=black!75,
-Latex,
dashed,
line width=0.75pt
},
panel/.style={
draw=black,
rounded corners=4pt,
line width=0.8pt
},
titlebox/.style={
draw=black,
rounded corners=4pt,
fill=blue!5,
minimum height=0.72cm,
align=center
},
stepbox/.style={
draw=black,
rounded corners=4pt,
fill=gray!5,
minimum width=3.8cm,
minimum height=1.1cm,
align=center,
line width=0.8pt
},
actorbox/.style={
draw=black,
rounded corners=4pt,
fill=blue!8,
minimum width=4.1cm,
minimum height=1.05cm,
align=center,
line width=0.8pt
},
criticbox/.style={
draw=black,
rounded corners=4pt,
fill=green!8,
minimum width=4.1cm,
minimum height=1.05cm,
align=center,
line width=0.8pt
},
envbox/.style={
draw=black,
rounded corners=4pt,
fill=orange!10,
minimum width=4.1cm,
minimum height=1.05cm,
align=center,
line width=0.8pt
},
notebox/.style={
draw=black,
rounded corners=4pt,
fill=yellow!10,
minimum width=5.2cm,
minimum height=1.0cm,
align=center,
line width=0.8pt
},
comparebox/.style={
draw=black,
rounded corners=4pt,
fill=gray!8,
minimum width=5.4cm,
minimum height=1.8cm,
align=left,
line width=0.8pt
}
]
% =========================
% Outer panel
% =========================
\draw[panel, fill=white] (0.2,0.3) rectangle (18.7,11.2);
\node[titlebox, minimum width=5.4cm] at (9.45,11.7) {PPO 的策略更新与样本重复利用过程};
% =========================
% Main steps
% =========================
\node[actorbox] (s1) at (3.0,9.7) {
第 1 步\\
固定当前策略\\
$\theta \rightarrow \theta_{\mathrm{old}}$
};
\node[envbox] (s2) at (9.45,9.7) {
第 2 步\\
使用旧策略与环境交互\\
采集状态、动作、奖励数据
};
\node[criticbox] (s3) at (15.8,9.7) {
第 3 步\\
Critic 计算价值估计\\
得到回报和优势 $\widehat{A}_t$
};
\node[stepbox] (s4) at (3.0,6.9) {
第 4 步\\
将采集到的数据\\
划分为多个小批次
};
\node[stepbox] (s5) at (9.45,6.9) {
第 5 步\\
基于裁剪目标\\
更新 Actor
};
\node[criticbox] (s6) at (15.8,6.9) {
第 6 步\\
更新 Critic\\
提高价值估计精度
};
\node[notebox] (s7) at (9.45,4.4) {
对同一批数据重复训练 $K$\\
每一轮都使用裁剪目标限制策略变化
};
\node[actorbox] (s8) at (9.45,1.9) {
第 7 步\\
完成本轮更新后\\
用新策略重新采样数据
};
% =========================
% Main arrows
% =========================
\draw[flow] (s1.east) -- (s2.west);
\draw[flow] (s2.east) -- (s3.west);
\draw[flow] (s3.south west) .. controls (14.0,8.5) and (4.8,8.2) .. (s4.north east);
\draw[flow] (s4.east) -- (s5.west);
\draw[flow] (s5.east) -- (s6.west);
\draw[flow] (s6.south west) .. controls (14.0,5.6) and (11.7,5.0) .. (s7.north east);
\draw[flow] (s7.south) -- (s8.north);
% =========================
% Reuse loop
% =========================
\draw[dashedflow]
(s7.west)
.. controls (4.8,4.4) and (4.8,6.0)
.. node[midway, above] {\footnotesize 重复 $K$} (s4.south);
% =========================
% Comparison note
% =========================
\node[comparebox] at (3.5,2.1) {
\textbf{与 A2C 的区别:}\\
A2C:采集一批数据后通常只更新一次\\
PPO:采集一批数据后可划分小批次并重复更新多轮
};
% =========================
% Small note
% =========================
\node[comparebox, minimum width=5.0cm, minimum height=1.6cm] at (15.1,2.1) {
\textbf{核心原因:}\\
裁剪目标限制单次策略更新幅度\\
因此同一批样本可以在一定范围内重复利用
};
\end{tikzpicture}
\ No newline at end of file
\begin{tikzpicture}[
font=\small,
topbox/.style={
draw=black!75,
rounded corners=4pt,
minimum height=2.55cm,
align=center,
line width=0.75pt,
inner xsep=6pt,
inner ysep=5pt,
font=\footnotesize
},
estimatebox/.style={
topbox,
fill=blue!6,
text width=3.25cm
},
samplebox/.style={
topbox,
fill=orange!10,
text width=3.85cm
},
targetbox/.style={
topbox,
fill=green!8,
text width=3.65cm
},
updatebox/.style={
topbox,
fill=green!15,
text width=3.15cm
},
formulabox/.style={
draw=black!75,
rounded corners=4pt,
fill=gray!5,
minimum width=16.4cm,
minimum height=1.90cm,
align=center,
line width=0.8pt,
inner xsep=8pt,
inner ysep=7pt
},
calculationbox/.style={
draw=green!45!black,
rounded corners=4pt,
fill=green!7,
minimum width=12.5cm,
minimum height=1.30cm,
align=center,
line width=0.8pt,
inner xsep=8pt,
inner ysep=6pt
},
flow/.style={
draw=black!80,
-Latex,
line width=0.75pt,
shorten <=2pt,
shorten >=2pt
},
inputflow/.style={
draw=black!65,
dashed,
-Latex,
line width=0.7pt,
shorten <=2pt,
shorten >=2pt
},
flowlabel/.style={
font=\scriptsize,
text=black!80,
fill=white,
inner xsep=1.5pt,
inner ysep=0.5pt
},
inputlabel/.style={
font=\scriptsize,
text=black!70,
fill=white,
inner xsep=1.5pt,
inner ysep=0.5pt
},
explanation/.style={
font=\footnotesize,
text=black!75,
align=center
}
]
% 保留足够的上下左右边距,避免导出时内容被裁切
\path[use as bounding box]
(-2.2,-8.15) rectangle (18.0,1.75);
% ==================================================
% 第一层:Q-learning 的四个步骤
% ==================================================
\node[estimatebox] (estimate) at (0,0) {
{\small\bfseries 当前价值估计}\\[5pt]
\mbox{智能体在位置 $(8,8)$ 落子}\\[6pt]
$\displaystyle Q(s_t,(8,8))=2.0$
};
\node[samplebox] (sample) at (5.2,0) {
{\small\bfseries 观察交互结果}\\[5pt]
即时奖励:$r_t=0$\\[5pt]
\mbox{下一状态最大动作价值}\\[3pt]
$\displaystyle
\max_{a'}Q(s_{t+1},a')=6.0
$
};
\node[targetbox] (target) at (10.8,0) {
{\small\bfseries 计算价值目标}\\[5pt]
$\displaystyle
y_t=
r_t+\gamma\max_{a'}Q(s_{t+1},a')
$\\[6pt]
$\displaystyle
y_t=0+0.9\times6.0=5.4
$
};
\node[updatebox] (update) at (15.8,0) {
{\small\bfseries 更新动作价值}\\[5pt]
学习率:$\alpha=0.5$\\[7pt]
$\displaystyle Q_{\mathrm{new}}=3.7$
};
% ==================================================
% 顶部流程箭头
% ==================================================
\draw[flow]
(estimate.east)
--
node[midway, above=4pt, flowlabel]
{执行动作}
(sample.west);
\draw[flow]
(sample.east)
--
node[midway, above=4pt, flowlabel]
{计算目标}
(target.west);
\draw[flow]
(target.east)
--
node[midway, above=4pt, flowlabel]
{更新估计}
(update.west);
% ==================================================
% 第二层:Q-learning 更新公式
% ==================================================
\node[formulabox] (formula) at (7.9,-3.75) {
{\small\bfseries Q-learning 更新公式}\\[7pt]
$\displaystyle
\begin{aligned}
y_t
&=
r_t+
\gamma\max_{a'}Q(s_{t+1},a'),
\\[3pt]
Q(s_t,a_t)
&\leftarrow
Q(s_t,a_t)
+
\alpha
\bigl[
y_t-Q(s_t,a_t)
\bigr].
\end{aligned}
$
};
% 在公式框上边缘设置两个输入位置
\coordinate (formula-current-input)
at ([xshift=-6.7cm]formula.north);
\coordinate (formula-target-input)
at ([xshift=1.7cm]formula.north);
% 当前估计值输入
\draw[inputflow]
(estimate.south)
to[out=-90,in=90]
node[pos=0.50, left=3pt, inputlabel]
{当前估计值}
(formula-current-input);
% 目标值输入
\draw[inputflow]
(target.south)
to[out=-90,in=90]
node[pos=0.50, right=3pt, inputlabel]
{目标值}
(formula-target-input);
% ==================================================
% 第三层:代入数值并得到更新结果
% ==================================================
\node[calculationbox] (calculation) at (7.9,-6.35) {
$\displaystyle
Q(s_t,(8,8))
\leftarrow
2.0
+
0.5\times(5.4-2.0)
=
\textcolor{green!40!black}{\mathbf{3.7}}
$
};
\draw[flow]
(formula.south)
--
node[midway, right=5pt, flowlabel]
{代入数值}
(calculation.north);
% ==================================================
% 底部结果说明
% ==================================================
\node[
explanation,
text width=12.5cm
] at (7.9,-7.65) {
新观测得到的目标价值高于原有估计,\\[3pt]
因此位置 $(8,8)$ 的动作价值由 $2.0$ 提高至 $3.7$
};
\end{tikzpicture}
\ No newline at end of file
# 中文课程讲义 LaTeX 模板
主文件是 `aml_notes.tex`,建议使用 XeLaTeX 编译:
```powershell
xelatex aml_notes.tex
xelatex aml_notes.tex
```
第二次编译用于生成完整目录和交叉引用。图片可以放在 `figures/``images/` 目录中,然后用 `\includegraphics` 插入。
常用修改位置:
- 课程信息:修改 `\coursename``\semester``\teacher``\school`
- 新增章节:使用 `\chapter{章节标题}`
- 新增小节:使用 `\section{小节标题}``\subsection{小节标题}`
- 插入表格:参考模板中的 `table``longtable` 示例。
- 插入图片:把图片放入 `figures/`,参考模板中的 `figure` 示例。
如果使用 Overleaf,也请选择 XeLaTeX 作为编译器。
% !TeX program = xelatex
% 中文课程讲义 LaTeX 模板
% 编译建议:XeLaTeX 或 LuaLaTeX。Windows / macOS / Linux 均可使用 ctex 自动处理中文字体。
\documentclass[UTF8,a4paper,zihao=-4,oneside,openany]{ctexbook}
% 页面与基础排版
\usepackage[
top=2.6cm,
bottom=2.6cm,
left=2.7cm,
right=2.7cm,
headheight=15pt
]{geometry}
\usepackage{setspace}
\setstretch{1.25}
\setlength{\parindent}{2em}
\setlength{\parskip}{0.2em}
\setlength{\emergencystretch}{3em}
\sloppy
% 数学、表格、图片
\usepackage{amsmath,amssymb}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{array}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{graphicx}
\usepackage{hyperref}
\usetikzlibrary{arrows.meta,backgrounds,decorations.pathreplacing,fit}
\usepackage[numbers,sort&compress]{natbib}
\pgfplotsset{compat=1.18}
\bibliographystyle{plainnat}
\usepackage{ulem} % 提供 \uline 下划线命令(或使用 \underline)
\graphicspath{{figures/}{images/}}
% 颜色与强调框
\usepackage[most]{tcolorbox}
\usepackage{xcolor}
\definecolor{CourseInk}{HTML}{1F2933}
\definecolor{CourseBlue}{HTML}{24577A}
\definecolor{CourseGreen}{HTML}{2D6A4F}
\definecolor{CourseGold}{HTML}{A16207}
\definecolor{CourseGray}{HTML}{F4F6F8}
\definecolor{CourseLine}{HTML}{D9E2EC}
\definecolor{ugreen}{rgb}{0,0.5,0}
\definecolor{lolgreen}{RGB}{103,213,181}
\definecolor{lolorange}{RGB}{246,179,82}
\definecolor{lolpurple}{RGB}{200,158,196}
\tcbset{
enhanced,
boxrule=0.6pt,
arc=2mm,
left=3mm,
right=3mm,
top=2mm,
bottom=2mm,
breakable
}
\newtcolorbox{learninggoals}{
colback=CourseGray,
colframe=CourseBlue,
title=学习目标,
fonttitle=\bfseries
}
\newtcolorbox{importantnote}{
colback=white,
colframe=CourseGreen,
title=重点提示,
fonttitle=\bfseries
}
\newtcolorbox{examplebox}[1][]{
colback=white,
colframe=CourseGold,
title=例题 #1,
fonttitle=\bfseries
}
\newtcolorbox{exercisebox}{
colback=CourseGray,
colframe=CourseLine,
title=课堂练习,
fonttitle=\bfseries\color{CourseInk}
}
% 页眉页脚与链接
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\small 中文课程讲义}
\fancyhead[R]{\small \leftmark}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0pt}
\usepackage[
colorlinks=true,
linkcolor=CourseBlue,
urlcolor=CourseGreen,
citecolor=CourseGold
]{hyperref}
% 章节样式
\ctexset{
chapter = {
name = {第,讲},
number = \chinese{chapter},
format = \huge\bfseries\color{CourseBlue},
beforeskip = 0pt,
afterskip = 22pt
},
section = {
format = \Large\bfseries\color{CourseInk}
},
subsection = {
format = \large\bfseries\color{CourseInk}
}
}
% 常用命令
\newcommand{\coursename}{中文课程名称}
\newcommand{\semester}{2026 春季}
\newcommand{\teacher}{授课教师}
\newcommand{\school}{学校 / 机构名称}
\newcommand{\lessondate}{\today}
\newcommand{\blankline}{\par\noindent\rule{\linewidth}{0.4pt}\par}
\newcommand{\keyword}[1]{\textbf{\color{CourseBlue}#1}}
% 统一图片占位符:所有待补图片统一使用该占位框。
\newcommand{\imageplaceholder}[2][5cm]{%
\fbox{%
\begin{minipage}[c][#1][c]{0.78\linewidth}
\centering
\textbf{图片占位}\\[0.45em]
{\small #2}
\end{minipage}%
}%
}
% 原讲义中多处使用 \mindex 标注术语,这里给出安全定义,避免未定义命令。
\newcommand{\mindex}[1]{\textit{#1}}
\begin{document}
% % 封面
% \begin{titlepage}
% \centering
% \vspace*{1.5cm}
% {\zihao{1}\bfseries\color{CourseBlue}\coursename\par}
% \vspace{0.8cm}
% {\zihao{3}\bfseries 课程讲义模板\par}
% \vspace{1.2cm}
% \imageplaceholder[0.24\textheight]{封面图:将图片命名为 \texttt{cover.jpg} 并放入 \texttt{figures/} 目录。}
% \vfill
% \begin{tabular}{rl}
% \textbf{教师:} & \teacher \\
% \textbf{学期:} & \semester \\
% \textbf{单位:} & \school \\
% \textbf{日期:} & \lessondate \\
% \end{tabular}
% \vspace*{1.2cm}
% \end{titlepage}
% 目录
\frontmatter
\pagestyle{plain}
\tableofcontents
\cleardoublepage
% 正文
\mainmatter
\pagestyle{fancy}
% \chapter{课程导论}
% \begin{learninggoals}
% \begin{itemize}
% \item 了解本课程的学习目标、评价方式与课堂要求。
% \item 熟悉讲义中的例题、练习、阅读材料与作业模块。
% \item 掌握如何在模板中插入章节、表格、图片和重点提示。
% \end{itemize}
% \end{learninggoals}
% \section{课程说明}
% 这里填写本讲的正文内容。中文可直接输入,不需要额外转码。若要突出关键词,可使用
% \keyword{关键词强调},也可以使用普通的 \textbf{加粗}、\emph{斜体} 等格式。
% \begin{importantnote}
% 这里可以写课堂重点、易错点、考试提示,或者需要学生特别记住的概念。
% \end{importantnote}
% \section{表格示例}
% 表格建议使用 \texttt{booktabs} 提供的 \verb|\toprule|、\verb|\midrule|、\verb|\bottomrule|,
% 视觉会比普通竖线表格更清爽。
% \begin{table}[htbp]
% \centering
% \caption{课程安排示例}
% \label{tab:schedule}
% \begin{tabularx}{0.92\linewidth}{>{\centering\arraybackslash}p{2.3cm}X>{\centering\arraybackslash}p{2.8cm}}
% \toprule
% 周次 & 主题 & 任务 \\
% \midrule
% 第 1 周 & 课程导论与学习方法 & 阅读讲义 \\
% 第 2 周 & 核心概念一 & 完成练习 \\
% 第 3 周 & 核心概念二 & 小组讨论 \\
% \bottomrule
% \end{tabularx}
% \end{table}
% \section{图片示例}
% 将图片放入 \texttt{figures/} 目录,然后用 \verb|\includegraphics| 插入。下面的示例会在
% \texttt{figures/example.png} 存在时显示图片,否则显示一个占位框。
% \begin{figure}[htbp]
% \centering
% \imageplaceholder{示例图片:将图片保存为 \texttt{figures/example.png}。}
% \caption{图片插入示例}
% \label{fig:example}
% \end{figure}
% \section{例题与练习}
% \begin{examplebox}[1]
% 这里放置例题内容。可以包含公式:
% \[
% a^2 + b^2 = c^2
% \]
% 也可以写解题步骤或分析。
% \end{examplebox}
% \begin{exercisebox}
% \begin{enumerate}
% \item 请用自己的话概括本讲的三个重点。
% \item 根据表~\ref{tab:schedule},说明第 2 周需要完成什么任务。
% \item 观察图~\ref{fig:example},写出你能得到的信息。
% \end{enumerate}
% \end{exercisebox}
% \chapter{第二讲标题}
% \section{知识点一}
% 从这里开始写第二讲内容。新增章节时,复制下面结构即可:
% \begin{verbatim}
% \chapter{新的一讲}
% \section{小节标题}
% 正文内容……
% \end{verbatim}
% \subsection{更小的层级}
% 如果一讲内部内容较多,可以继续使用 \verb|\subsection| 或 \verb|\subsubsection| 组织层次。
% \section{长表格示例}
% 如果表格跨页,可以使用 \texttt{longtable}。
% \begin{longtable}{p{0.18\linewidth}p{0.34\linewidth}p{0.34\linewidth}}
% \caption{长表格结构示例}
% \label{tab:longtable}\\
% \toprule
% 模块 & 内容 & 备注 \\
% \midrule
% \endfirsthead
% \toprule
% 模块 & 内容 & 备注 \\
% \midrule
% \endhead
% 课前 & 预习阅读材料 & 可附链接或二维码图片 \\
% 课中 & 讲授、讨论、练习 & 建议加入例题与即时反馈 \\
% 课后 & 作业、复盘、扩展阅读 & 可列出提交要求 \\
% \bottomrule
% \end{longtable}
\chapter{大模型预训练与微调方法}
\input{section/3.0-introduction}
\input{section/3.1-pretraining}
\input{section/3.2-prompt}
\input{section/3.3-finetune}
\input{section/3.4-alignment}
\input{section/3.5-reasoning}
\input{section/3.6-summary}
\appendix
\chapter{附录}
\section{常用 LaTeX 片段}
\begin{verbatim}
% 插入普通图片
\begin{figure}[htbp]
\centering
\includegraphics[width=0.8\linewidth]{figures/your-image.png}
\caption{图片标题}
\end{figure}
% 插入三线表
\begin{table}[htbp]
\centering
\caption{表格标题}
\begin{tabular}{lll}
\toprule
A & B & C \\
\midrule
内容 & 内容 & 内容 \\
\bottomrule
\end{tabular}
\end{table}
\end{verbatim}
\backmatter
\chapter{参考资料}
这里可以列出教材、论文、网站或其他阅读材料。
% 使用外部 BibTeX 数据库。请将 bibliography.bib 与本 .tex 文件放在同一目录。
% 如果你的 bib 文件名不同,例如 refs.bib,请改成 \bibliography{refs}。
\bibliography{bibliography}
\end{document}
This source diff could not be displayed because it is too large. You can view the blob instead.
% \usepackage[french]{babel}
\usepackage{titlesec}
\usepackage{graphicx}
\graphicspath{{Figures/}}
\usepackage{multirow}
\usepackage{rotating}
\usepackage{enumitem}
\setlist{nolistsep}
\usepackage{amsmath,bm}
\usepackage{pifont}
\usepackage{bbm}
\usepackage{enumitem}
\usepackage[normalem]{ulem}
\usepackage{framed}
\usepackage{tcolorbox}
\usepackage{ulem}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usepackage{tikz-3dplot}
\usepgflibrary{patterns}
\usetikzlibrary{patterns}
\usetikzlibrary{patterns.meta}
\usepackage{array}
\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
\newcolumntype{C}[1]{>{\PreserveBackslash\centering}p{#1}}
\newcolumntype{R}[1]{>{\PreserveBackslash\raggedleft}p{#1}}
\newcolumntype{L}[1]{>{\PreserveBackslash\raggedright}p{#1}}
\usetikzlibrary {arrows.meta,bending,positioning}
\usetikzlibrary{angles,quotes}
\usetikzlibrary{fit}
\usetikzlibrary{backgrounds}
\usepackage{pgfplots}
\usepackage{soul}
\usepackage{xcolor}
\definecolor{ocrebase}{RGB}{243,102,25}
\definecolor{ocre}{RGB}{0,0,0}
\definecolor{amber}{rgb}{1.0, 0.75, 0.0}
\definecolor{ublue}{rgb}{0.152,0.250,0.545}
\definecolor{ugreen}{rgb}{0,0.5,0}
\definecolor{lgreen}{rgb}{0.9,1,0.8}
\definecolor{lightgreen}{rgb}{0.56, 0.93, 0.56}
\definecolor{kellygreen}{rgb}{0.3, 0.73, 0.09}
\definecolor{xtgreen}{rgb}{0.914,0.945,0.902}
\definecolor{lightgray}{gray}{0.85}
\definecolor{darkblue}{rgb}{0, 0, 0.5}
\definecolor{shadecolor}{rgb}{0.96,0.96,0.93}
\definecolor{lightcyan}{RGB}{194,232,247}
\definecolor{lightorange}{RGB}{255,226,187}
\definecolor{lightpink}{RGB}{252,224,225}
\definecolor{lightgreen}{RGB}{204,231,207}
\definecolor{lolgreen}{RGB}{103,213,181}
\definecolor{lolred}{RGB}{238,119,133}
\definecolor{lolpurple}{RGB}{200,158,196}
\definecolor{lolblue}{RGB}{132,177,237}
\definecolor{lolorange}{RGB}{246,179,82}
\definecolor{lightsalmon}{RGB}{255,160,122}
\definecolor{lightskyblue}{RGB}{135,206,250}
\usetikzlibrary{shadows}
\usepackage{multirow}
\usepackage{appendix}
\usepackage{longtable}
\usepackage{makecell}
\usepackage{array}
\newcommand{\ctext}[3][RGB]{
\begingroup
\definecolor{hlcolor}{#1}{#2}\sethlcolor{hlcolor}
\hl{#3}
\endgroup
}
\usepackage{geometry}
\geometry{
papersize={185mm,260mm},
top=2.2cm,
bottom=1.7cm,
left=2.2cm,
right=2.0cm,
headheight=10pt,
footskip=1.4cm,
headsep=10pt,
}
\usepackage{times}
\usepackage{mathptmx}
\usepackage{microtype}
% \usepackage[utf8]{inputenc}
% \usepackage[T1]{fontenc}
\makeatletter
\renewcommand*\env@matrix[1][\arraystretch]{
\edef\arraystretch{#1}
\hskip -\arraycolsep
\let\@ifnextchar\new@ifnextchar
\array{*\c@MaxMatrixCols c}}
\makeatother
\usepackage{natbib}
\setlength{\bibsep}{5pt}
\renewcommand{\bibfont}{\fontsize{10pt}{12pt}\selectfont}
\setcitestyle{square,aysep={,},yysep={;}}
\renewcommand\cite{\citep}
\newcommand\shortcite{\citeyearpar}
\newcommand\newcite{\citet}
\usepackage{calc}
\usepackage{makeidx}
\makeindex
\newcommand{\upcite}[1]{\textsuperscript{\textsuperscript{\cite{#1}}}}
\newcommand{\mindex}[1]{\textbf{#1}\index{#1}}
\usepackage{titletoc}
\contentsmargin{0cm}
\titlecontents{part}
[0cm]
{\addvspace{20pt}\bfseries}
{}
{}
{}
\titlecontents{chapter}
[1.25cm]
{\addvspace{12pt}\large\sffamily\bfseries}
{\color{black}\contentslabel[\Large\thecontentslabel]{1.25cm}\color{black}}
{\color{black}}
{\color{black}\normalsize\;\titlerule*[.5pc]{.}\;\thecontentspage}
\titlecontents{section}
[1.25cm]
{\addvspace{3pt}\sffamily\bfseries}
{\contentslabel[\thecontentslabel]{1.25cm}}
{}
{\titlerule*[.5pc]{.}\;\thecontentspage}
\titlecontents{subsection}
[1.25cm]
{\addvspace{1pt}\sffamily\small}
{\contentslabel[\thecontentslabel]{1.25cm}}
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage}
\titlecontents{figure}
[1.25cm]
{\addvspace{1pt}\sffamily\small}
{\thecontentslabel\hspace*{1em}}
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage}
\titlecontents{table}
[1.25cm]
{\addvspace{1pt}\sffamily\small}
{\thecontentslabel\hspace*{1em}}
{}
{\ \titlerule*[.5pc]{.}\;\thecontentspage}
\titlecontents{lchapter}
[0em]
{\addvspace{15pt}\large\sffamily\bfseries}
{\color{black}\contentslabel[\Large\thecontentslabel]{1.25cm}\color{black}}
{}
{\color{black}\normalsize\sffamily\bfseries\;\titlerule*[.5pc]{.}\;\thecontentspage}
\titlecontents{lsection}
[0em]
{\sffamily\small}
{\contentslabel[\thecontentslabel]{1.25cm}}
{}
{}
\titlecontents{lsubsection}
[.5em]
{\sffamily\footnotesize}
{\contentslabel[\thecontentslabel]{1.25cm}}
{}
{}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{\sffamily\normalsize\chaptername\ \thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\sffamily\normalsize\thesection\hspace{5pt}#1}{}}
\fancyhf{}
\fancyhead[LE,RO]{\sffamily\normalsize\thepage}
\fancyhead[LO]{\rightmark}
\fancyhead[RE]{\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\fancypagestyle{plain}{
\fancyhead{}\renewcommand{\headrulewidth}{0pt}
}
\makeatletter
\renewcommand{\cleardoublepage}{
\clearpage\ifodd\c@page\else
\hbox{}
\vspace*{\fill}
\thispagestyle{empty}
\newpage
\fi}
\newcommand{\newlinebold}{\vskip6pt\goodbreak\hrule height 1pt\vskip6pt}
\newcommand{\newlinelight}{\vskip6pt\goodbreak\hrule\vskip6pt}
\let\bblxv\verbatim
\let\bblexv\endverbatim
\def\verbatim{\begin{shaded*}\bblxv\vskip-\baselineskip\vskip2.5\parsep}
\def\endverbatim{\bblexv\vskip-2\baselineskip\end{shaded*}}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareSymbolFont{EulerExtension}{U}{euex}{m}{n}
\DeclareMathSymbol{\euintop}{\mathop} {EulerExtension}{"52}
\let\intop\euintop
\newcommand{\intoo}[2]{\mathopen{]}#1\,;#2\mathclose{[}}
\newcommand{\ud}{\mathop{\mathrm{{}d}}\mathopen{}}
\newcommand{\intff}[2]{\mathopen{[}#1\,;#2\mathclose{]}}
\renewcommand{\qedsymbol}{$\blacksquare$}
\newtheorem{notation}{Notation}[chapter]
\newtheoremstyle{ocrenumbox}
{0pt}
{0pt}
{\normalfont}
{}
{\small\bf\sffamily\color{ocre}}
{\;}
{0.25em}
{\small\sffamily\color{ocre}\thmname{#1}\nobreakspace\thmnumber{\@ifnotempty{#1}{}\@upn{#2}}
\thmnote{\nobreakspace\the\thm@notefont\sffamily\bfseries\color{black}---\nobreakspace#3.}}
\newtheoremstyle{blacknumex}
{5pt}
{5pt}
{\normalfont}
{}
{\small\bf\sffamily}
{\;}
{0.25em}
{\small\sffamily{\tiny\ensuremath{}}\nobreakspace\thmname{#1}\nobreakspace\thmnumber{\@ifnotempty{#1}{}\@upn{#2}}
\thmnote{\nobreakspace\the\thm@notefont\sffamily\bfseries---\nobreakspace#3.}}
\newtheoremstyle{blacknumbox}
{0pt}
{0pt}
{\normalfont}
{}
{\small\bf\sffamily}
{\;}
{0.25em}
{\small\sffamily\thmname{#1}\nobreakspace\thmnumber{\@ifnotempty{#1}{}\@upn{#2}}
\thmnote{\nobreakspace\the\thm@notefont\sffamily\bfseries---\nobreakspace#3.}}
\newtheoremstyle{ocrenum}
{5pt}
{5pt}
{\normalfont}
{}
{\small\bf\sffamily\color{ocre}}
{\;}
{0.25em}
{\small\sffamily\color{ocre}\thmname{#1}\nobreakspace\thmnumber{\@ifnotempty{#1}{}\@upn{#2}}
\thmnote{\nobreakspace\the\thm@notefont\sffamily\bfseries\color{black}---\nobreakspace#3.}}
\makeatother
\newcounter{dummy}
\numberwithin{dummy}{section}
\theoremstyle{ocrenumbox}
\newtheorem{theoremeT}[dummy]{Theorem}
\newtheorem{problem}{Problem}[chapter]
\newtheorem{exerciseT}{Example}[chapter]
\theoremstyle{blacknumex}
\newtheorem{exampleT}{Example}[chapter]
\theoremstyle{blacknumbox}
\newtheorem{vocabulary}{Vocabulary}[chapter]
\newtheorem{definitionT}{Definition}[section]
\newtheorem{corollaryT}[dummy]{Corollary}
\theoremstyle{ocrenum}
\newtheorem{proposition}[dummy]{Proposition}
\RequirePackage[framemethod=default]{mdframed}
\newmdenv[skipabove=7pt,
skipbelow=7pt,
rightline=false,
leftline=true,
topline=false,
bottomline=false,
linecolor=ublue,
innerleftmargin=5pt,
innerrightmargin=10pt,
innertopmargin=0pt,
leftmargin=0cm,
rightmargin=0cm,
linewidth=4pt,
backgroundcolor=black!5,
innerbottommargin=0pt]{AlgorithmBox}
\newmdenv[skipabove=7pt,
skipbelow=7pt,
backgroundcolor=black!5,
linecolor=ocre,
innerleftmargin=5pt,
innerrightmargin=5pt,
innertopmargin=5pt,
leftmargin=0cm,
rightmargin=0cm,
innerbottommargin=5pt]{tBox}
\newmdenv[skipabove=7pt,
skipbelow=7pt,
rightline=false,
leftline=true,
topline=false,
bottomline=false,
backgroundcolor=ocre!10,
linecolor=ocre,
innerleftmargin=5pt,
innerrightmargin=5pt,
innertopmargin=5pt,
innerbottommargin=5pt,
leftmargin=0cm,
rightmargin=0cm,
linewidth=4pt]{eBox}
\newmdenv[skipabove=7pt,
skipbelow=7pt,
rightline=false,
leftline=true,
topline=false,
bottomline=false,
linecolor=ublue,
innerleftmargin=5pt,
innerrightmargin=5pt,
innertopmargin=0pt,
leftmargin=0cm,
rightmargin=0cm,
linewidth=4pt,
innerbottommargin=0pt]{dBox}
\newmdenv[skipabove=7pt,
skipbelow=7pt,
rightline=false,
leftline=true,
topline=false,
bottomline=false,
linecolor=gray,
backgroundcolor=black!5,
innerleftmargin=5pt,
innerrightmargin=5pt,
innertopmargin=5pt,
leftmargin=0cm,
rightmargin=0cm,
linewidth=4pt,
innerbottommargin=5pt]{cBox}
\newenvironment{theorem}{\begin{tBox}\begin{theoremeT}}{\end{theoremeT}\end{tBox}}
\newenvironment{exercise}{\begin{eBox}\begin{exerciseT}}{\hfill{\color{ocre}\tiny\ensuremath{\blacksquare}}\end{exerciseT}\end{eBox}}
\newenvironment{definition}{\begin{dBox}\begin{definitionT}}{\end{definitionT}\end{dBox}}
\newenvironment{example}{\begin{exampleT}}{\end{exampleT}}
\newenvironment{corollary}{\begin{cBox}\begin{corollaryT}}{\end{corollaryT}\end{cBox}}
\newenvironment{remark}{\par\vspace{10pt}\small
\begin{list}{}{
\leftmargin=35pt
\rightmargin=25pt}\item\ignorespaces
\makebox[-2.5pt]{\begin{tikzpicture}[overlay]
\node[draw=ocre!60,line width=1pt,circle,fill=ocre!25,font=\sffamily\bfseries,inner sep=2pt,outer sep=0pt] at (-15pt,0pt){\textcolor{ocre}{R}};\end{tikzpicture}}
\advance\baselineskip 1pt}{\end{list}\vskip5pt}
\makeatletter
\renewcommand{\@seccntformat}[1]{\llap{\textcolor{ocre}{\csname the#1\endcsname}\hspace{1em}}}
\renewcommand{\section}{\@startsection{section}{1}{\z@}
{-4ex \@plus -1ex \@minus -.4ex}
{1ex \@plus.2ex }
{\Large\sffamily\bfseries}}
\renewcommand{\subsection}{\@startsection {subsection}{2}{\z@}
{-3ex \@plus -0.1ex \@minus -.4ex}
{0.5ex \@plus.2ex }
{\normalfont\large\sffamily\bfseries}}
\renewcommand{\subsubsection}{\@startsection {subsubsection}{3}{\z@}
{-3ex \@plus -0.1ex \@minus -.4ex}
{.4ex \@plus.2ex }
{\normalfont\normalsize\sffamily\bfseries}}
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}
{-2ex \@plus-.2ex \@minus .2ex}
{.1ex}
{\normalfont\small\sffamily\bfseries}}
\newcommand{\@mypartnumtocformat}[2]{
\setlength\fboxsep{0pt}
\noindent\colorbox{blue!80}{\strut\parbox[c][.7cm]{\ecart}{\color{white}\Large\sffamily\bfseries\centering#1}}\hskip\esp\colorbox{blue!80}{\strut\parbox[c][.7cm]{\linewidth-\ecart-\esp}{\color{white}\Large\sffamily\centering#2}}
}
\newcommand{\@myparttocformat}[1]{
\setlength\fboxsep{0pt}
\noindent\colorbox{blue!80}{\strut\parbox[c][.7cm]{\linewidth}{\color{white}\Large\sffamily\centering#1}}
}
\newlength\esp
\setlength\esp{4pt}
\newlength\ecart
\setlength\ecart{1.2cm-\esp}
\newcommand{\thepartimage}{}
\newcommand{\partimage}[1]{\renewcommand{\thepartimage}{#1}}
\def\@part[#1]#2{
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}
\addcontentsline{toc}{part}{\texorpdfstring{\protect\@mypartnumtocformat{\thepart}{#1}}{\partname~\thepart\ ---\ #1}}
\else
\addcontentsline{toc}{part}{\texorpdfstring{\protect\@myparttocformat{#1}}{#1}}
\fi
\startcontents
\markboth{}{}
{\thispagestyle{empty}
\begin{tikzpicture}[remember picture,overlay]
\node at (current page.north west){\begin{tikzpicture}[remember picture,overlay]
\fill[white](0cm,0cm) rectangle (\paperwidth,-\paperheight);
\node[anchor=north west] at (2cm,-3.25cm){\color{ublue}\fontsize{80}{60}\sffamily\bfseries\thepart};
\node[anchor=south east] at (\paperwidth-1cm,-\paperheight+1cm){\parbox[t][][t]{9.5cm}{
\printcontents{l}{0}{\setcounter{tocdepth}{1}}
}};
\node[anchor=north east] at (\paperwidth-1.5cm,-3.25cm){\parbox[t][][t]{15cm}{\strut\raggedleft\color{black}\fontsize{30}{30}\sffamily\bfseries#2}};
\end{tikzpicture}};
\end{tikzpicture}}
\@endpart}
\def\@spart#1{
\startcontents
\phantomsection
{\thispagestyle{empty}
\begin{tikzpicture}[remember picture,overlay]
\node at (current page.north west){\begin{tikzpicture}[remember picture,overlay]
\fill[ocre!20](0cm,0cm) rectangle (\paperwidth,-\paperheight);
\node[anchor=north east] at (\paperwidth-1.5cm,-3.25cm){\parbox[t][][t]{15cm}{\strut\raggedleft\color{black}\fontsize{30}{30}\sffamily\bfseries#1}};
\end{tikzpicture}};
\end{tikzpicture}}
\addcontentsline{toc}{part}{\texorpdfstring{
\setlength\fboxsep{0pt}
\noindent\protect\colorbox{ocre!40}{\strut\protect\parbox[c][.7cm]{\linewidth}{\Large\sffamily\protect\centering #1\quad\mbox{}}}}{#1}}
\@endpart}
\def\@endpart{\vfil\newpage
\if@twoside
\if@openright
\null
\thispagestyle{empty}
\newpage
\fi
\fi
\if@tempswa
\twocolumn
\fi}
\titleformat{\chapter}[display]
{\normalfont\sffamily\Huge\bfseries\color{ublue}}
{\chaptertitlename\ \thechapter}{20pt}{\LARGE}
\titleformat{\section}
{\normalfont\sffamily\Large\bfseries\color{ublue}}
{\thesection}{1em}{}
\fancypagestyle{chapterurl}{
\fancyhf{}
\fancyhead[R]{
\small{本书由大语言模型自动翻译生成,非人工翻译}\\[0.5cm]
\small{\url{https://github.com/NiuTrans/NLPBook}}\\[2pt] \small{\url{https://github.com/NiuTrans/NLPBookTranslations}} \\[2pt]
}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}
\newcommand{\sectionnewpage}{}
{\newcommand{\mycfont}{song}}
{\newcommand{\mycfont}{gbsn}}
\AtBeginDocument{
\SetSymbolFont{operators}{normal}{OT1}{cmr} {m}{n}
\SetSymbolFont{letters}{normal}{OML}{cmm} {m}{it}
\SetSymbolFont{symbols}{normal}{OMS}{cmsy}{m}{n}
\SetSymbolFont{largesymbols}{normal}{OMX}{cmex}{m}{n}
\SetSymbolFont{operators}{bold}{OT1}{cmr} {bx}{n}
\SetSymbolFont{letters}{bold}{OML}{cmm} {b}{it}
\SetSymbolFont{symbols}{bold}{OMS}{cmsy}{b}{n}
\SetSymbolFont{largesymbols}{bold}{OMX}{cmex}{m}{n}
\SetMathAlphabet{\mathbf}{normal}{OT1}{cmr}{bx}{n}
\SetMathAlphabet{\mathsf}{normal}{OT1}{cmss}{m}{n}
\SetMathAlphabet{\mathit}{normal}{OT1}{cmr}{m}{it}
\SetMathAlphabet{\mathtt}{normal}{OT1}{cmtt}{m}{n}
\SetMathAlphabet{\mathbf}{bold}{OT1}{cmr}{bx}{n}
\SetMathAlphabet{\mathsf}{bold}{OT1}{cmss}{bx}{n}
\SetMathAlphabet{\mathit}{bold}{OT1}{cmr}{bx}{it}
\SetMathAlphabet{\mathtt}{bold}{OT1}{cmtt}{m}{n}
}
\usepackage{hyperref}
\hypersetup{colorlinks=true,citecolor=blue,linkcolor=blue,urlcolor=blue}
\usepackage{bookmark}
\bookmarksetup{
open,
numbered,
depth=2,
addtohook={
\ifnum\bookmarkget{level}=0
\bookmarksetup{bold}
\fi
\ifnum\bookmarkget{level}=-1
\bookmarksetup{color=ocre,bold}
\fi
}
}
\newcommand{\ChapterNLPFundations}{0}
\newcommand{\ChapterMLFundations}{1}
\newcommand{\ChapterNNFundations}{2}
\newcommand{\ChapterWords}{3}
\newcommand{\ChapterSequences}{4}
\newcommand{\ChapterEncDec}{5}
\newcommand{\ChapterTransformer}{6}
\newcommand{\ChapterPretraining}{7}
\newcommand{\ChapterLLM}{8}
\newcommand{\ChapterLLMPrompting}{9}
\newcommand{\ChapterLLMTuning}{10}
\newcommand{\ChapterLLMInference}{11}
% !TeX root = ../main.tex
% 第三章正文入口。
\chapter{大模型预训练与微调方法}
\chapterinfo[\href{https://arxiv.org/abs/2501.09223}{\textit{Foundations of Large Language Models}}]{吴钰璋、王骏鑫、王成龙}
\input{section/3.0-introduction}
\input{section/3.1-pretraining}
\input{section/3.2-prompt}
\input{section/3.3-finetune}
\input{section/3.4-alignment}
\input{section/3.5-reasoning}
\input{section/3.6-summary}
\begin{tikzpicture}
\def\upline{13.5}
\def\downline{2.5}
\def\upcenpos{17.2}
\begin{scope}
\begin{axis}[
width=11.5cm, height=8cm,
xlabel={Training Dataset Size (Log-scale)},
ylabel={Number of Test Errors (Log-scale)},
xlabel style={align=center,yshift=1.2em,font=\footnotesize},
ylabel style={align=center,yshift=-2.2em,font=\footnotesize},
xticklabel style={fill=white,opacity=0},
yticklabel style={fill=white,opacity=0},
x tick style={fill=white,opacity=0},
y tick style={fill=white,opacity=0},
xtick=\empty,
ytick=\empty,
legend pos=outer north east,
xmin=0,
xmax=100,
ymin=0,
ymax=20]
\addplot [sharp plot] coordinates{(27,1) (27,19)};
\addplot [sharp plot] coordinates{(73,1) (73,19)};
{\footnotesize
\node[anchor=center,align=center,font=\footnotesize] (t1) at (axis cs:13.5,\upcenpos) {Slow Reduction\\ Phase};
\node[anchor=center,align=center,font=\footnotesize] (t2) at (axis cs:50,\upcenpos){Power-law Reduction\\ Phase};
\node[anchor=center, align=center,font=\footnotesize] (t3) at (axis cs:86.5,\upcenpos - 0.7) {Convergence \\ Phase \\ (Irreducible Error)};
\draw[-,line width=2pt,blue] (axis cs:0,\upline-0.1) .. controls ([xshift=0em,yshift=0em]axis cs:10,\upline-0.199) and ([xshift=0em,yshift=0em]axis cs:15,\upline-0.32275) .. (axis cs:22.5,\upline-0.6) .. controls ([xshift=0em,yshift=0em]axis cs:40,12) and ([xshift=0em,yshift=0em]axis cs:60,4) .. (axis cs:77.5,\downline+0.8) .. controls ([xshift=0em,yshift=0em]axis cs:85,\downline+0.32275) and ([xshift=0em,yshift=0em]axis cs:90,\downline+0.199) .. (axis cs:100,\downline);
}
\end{axis}
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\def\vstep{1.3cm}
\def\ssep{1.0cm}
\tikzstyle{enode} = [minimum width=9cm,minimum height=1.4cm,inner sep=2pt,draw,thick];
\begin{scope}
\node [anchor=west] (w0) at (0,0) {\footnotesize{$x_0$}};
\node [anchor=center] (w1) at ([xshift=\ssep]w0.center) {\footnotesize{$x_1$}};
\node [anchor=center] (w2) at ([xshift=\ssep]w1.center) {\footnotesize{$...$}};
\node [anchor=center] (w3) at ([xshift=\ssep]w2.center) {\footnotesize{$x_{m-1}$}};
\node [anchor=center] (e0) at ([yshift=0.7*\ssep]w0.center) {\footnotesize{$\mathbf{e}_0$}};
\node [anchor=center] (e1) at ([yshift=0.7*\ssep]w1.center) {\footnotesize{$\mathbf{e}_1$}};
\node [anchor=center] (e2) at ([yshift=0.7*\ssep]w2.center) {\footnotesize{$...$}};
\node [anchor=center] (e3) at ([yshift=0.7*\ssep]w3.center) {\footnotesize{$\mathbf{e}_{m-1}$}};
\draw [->] (w0.north) -- (e0.south);
\draw [->] (w1.north) -- (e1.south);
\draw [->] (w3.north) -- (e3.south);
\draw [->] (e0.north) -- ([yshift=0.2*\ssep]e0.north);
\draw [->] (e1.north) -- ([yshift=0.2*\ssep]e1.north);
\draw [->] (e3.north) -- ([yshift=0.2*\ssep]e3.north);
\node [anchor=south,minimum width=5*\ssep,minimum height=1.2*\ssep,draw,thick] (lm) at ([yshift=1.0*\ssep,xshift=0.5*\ssep]w1.north) {};
\node [anchor=center] (h0) at ([yshift=2.1*\ssep]w0.center) {\footnotesize{$\mathbf{h}_0^L$}};
\node [anchor=center] (h1) at ([yshift=2.1*\ssep]w1.center) {\footnotesize{$\mathbf{h}_1^L$}};
\node [anchor=center] (h2) at ([yshift=2.1*\ssep]w2.center) {\footnotesize{$...$}};
\node [anchor=center] (h3) at ([yshift=2.1*\ssep]w3.center) {\footnotesize{$\mathbf{h}_{m-1}^L$}};
\node [anchor=south,minimum height=0.8em,minimum width=0.8em,draw,fill=gray!70] (s0) at ([yshift=0.4*\ssep]h0.north) {};
\node [anchor=south,minimum height=0.8em,minimum width=0.8em,draw,fill=gray!70] (s1) at ([yshift=0.4*\ssep]h1.north) {};
\node [anchor=south,minimum height=0.8em,minimum width=0.8em,fill=white] (s2) at ([yshift=0.4*\ssep]h2.north) {...};
\node [anchor=south,minimum height=0.8em,minimum width=0.8em,draw,fill=gray!70] (s3) at ([yshift=0.4*\ssep]h3.north) {};
\draw [->] ([yshift=-3pt]h0.north) -- ([yshift=-2pt]s0.south);
\draw [->] ([yshift=-3pt]h1.north) -- ([yshift=-2pt]s1.south);
\draw [->] ([yshift=-3pt]h3.north) -- ([yshift=-2pt]s3.south);
\node [anchor=south] (o0) at ([yshift=0.4*\ssep,xshift=-0.0cm]s0.north) {\scriptsize{$\Pr({\color{blue} x_1}|x_0)$}};
\node [anchor=south] (o1) at ([yshift=0.8*\ssep,xshift=0.0cm]s1.north) {\scriptsize{$\Pr({\color{blue} x_2}|x_0 x_1)$}};
\node [anchor=south] (o3) at ([yshift=1.2*\ssep,xshift=0.0cm]s3.north) {\scriptsize{$\Pr({\color{blue} x_m}|x_0 x_1 ... x_{m-1})$}};
\draw [->] ([yshift=2pt]s0.north) -- ([yshift=-2pt]o0.south);
\draw [->] ([yshift=2pt]s1.north) -- ([yshift=-2pt]o1.south);
\draw [->] ([yshift=2pt]s3.north) -- ([yshift=-2pt]o3.south);
\node [anchor=south] (p0) at ([yshift=1.3*\ssep]o0.center) {\footnotesize{$x_1$}};
\node [anchor=center] (p1) at ([xshift=\ssep]p0.center) {\footnotesize{$x_2$}};
\node [anchor=center] (p2) at ([xshift=\ssep]p1.center) {\footnotesize{$...$}};
\node [anchor=center] (p3) at ([xshift=\ssep]p2.center) {\footnotesize{$x_{m}$}};
\draw [->] (o0.north) -- (p0.south);
\draw [->] (o1.north) -- (p1.south);
\draw [->] (o3.north) -- (p3.south);
\node [anchor=south] (encodercaption) at ([yshift=0.02cm]lm.south) {Language Model};
\end{scope}
\begin{scope}
\node [anchor=west] (h0) at ([xshift=7.5cm,yshift=0.1cm]w0.center) {\footnotesize{$\mathbf{z}_0$}};
\node [anchor=center] (h1) at ([xshift=\ssep]h0.center) {\footnotesize{$\mathbf{z}_1$}};
\node [anchor=center] (h2) at ([xshift=\ssep]h1.center) {\footnotesize{$...$}};
\node [anchor=center] (h3) at ([xshift=\ssep]h2.center) {\footnotesize{$\mathbf{z}_{m-1}$}};
\node [anchor=south,minimum width=5*\ssep,minimum height=1.2*\ssep,draw,thick] (selfatt) at ([yshift=0.8*\ssep,xshift=0.5*\ssep]h1.north) {};
\draw [arrows = {-Stealth[harpoon]},thick] ([xshift=0.5*\ssep,yshift=-0.8*\ssep]selfatt.south west) -- ([xshift=0.5*\ssep,yshift=-1pt]selfatt.south west);
\draw [arrows = {-Stealth[harpoon,swap]},thick] ([xshift=-0.5*\ssep,yshift=-0.8*\ssep]selfatt.south east) -- ([xshift=-0.5*\ssep,yshift=-1pt]selfatt.south east);
\path [fill=lolgreen!40,fill opacity=0.5] ([xshift=0.5*\ssep+0.5pt,yshift=-0.8*\ssep]selfatt.south west) -- ([xshift=0.5*\ssep+0.5pt,yshift=-1pt]selfatt.south west) -- ([xshift=-0.5*\ssep-0.5pt,yshift=-1pt]selfatt.south east) -- ([xshift=-0.5*\ssep-0.5pt,yshift=-0.8*\ssep]selfatt.south east) -- ([xshift=0.5*\ssep+0.5pt,yshift=-0.8*\ssep]selfatt.south west);
\node [anchor=center,circle,minimum size=4pt,inner sep=0,fill=black] (node00) at ([xshift=-1.8*\ssep,yshift=0.3cm]selfatt.south) {};
\node [anchor=center,circle,minimum size=4pt,inner sep=0,fill=black] (node01) at ([xshift=-0.6*\ssep,yshift=0.3cm]selfatt.south) {};
\node [anchor=center,circle,minimum size=4pt,inner sep=0,fill=black] (node02) at ([xshift=0.6*\ssep,yshift=0.3cm]selfatt.south) {};
\node [anchor=center,circle,minimum size=4pt,inner sep=0,fill=black] (node03) at ([xshift=1.8*\ssep,yshift=0.3cm]selfatt.south) {};
\node [anchor=center,circle,minimum size=4pt,inner sep=0,fill=black] (node10) at ([xshift=-1.8*\ssep,yshift=-0.4cm]selfatt.north) {};
\node [anchor=center,circle,minimum size=4pt,inner sep=0,fill=black] (node11) at ([xshift=-0.6*\ssep,yshift=-0.4cm]selfatt.north) {};
\node [anchor=center,circle,minimum size=4pt,inner sep=0,fill=black] (node12) at ([xshift=0.6*\ssep,yshift=-0.4cm]selfatt.north) {};
\node [anchor=center,circle,minimum size=4pt,inner sep=0,fill=black] (node13) at ([xshift=1.8*\ssep,yshift=-0.4cm]selfatt.north) {};
\draw [-{Stealth[length=1mm]},gray!20] (node00.90) -- (node10.-90);
\draw [-{Stealth[length=1mm]},gray!20] (node00.75) -- (node11.-105);
\draw [-{Stealth[length=1mm]}] (node00.60) -- (node12.-140);
\draw [-{Stealth[length=1mm]},gray!20] (node00.45) -- (node13.-155);
\draw [-{Stealth[length=1mm]},gray!20] (node01.90) -- (node11.-90);
\draw [-{Stealth[length=1mm]}] (node01.75) -- (node12.-105);
\draw [-{Stealth[length=1mm]},gray!20] (node01.60) -- (node13.-130);
\draw [-{Stealth[length=1mm]}] (node02.90) -- (node12.-90);
\draw [-{Stealth[length=1mm]},gray!20] (node02.75) -- (node13.-105);
\draw [-{Stealth[length=1mm]},gray!20] (node03.90) -- (node13.-90);
\node [anchor=south,minimum width=5*\ssep,minimum height=0.8*\ssep,draw,thick] (selfattlnorm) at ([yshift=0.8*\ssep]selfatt.north) {Post-norm or Pre-norm};
\draw [arrows = {-Stealth[harpoon]},thick] ([xshift=0.5*\ssep,yshift=-0.8*\ssep+1pt]selfattlnorm.south west) -- ([xshift=0.5*\ssep,yshift=-1pt]selfattlnorm.south west);
\draw [arrows = {-Stealth[harpoon,swap]},thick] ([xshift=-0.5*\ssep,yshift=-0.8*\ssep+1pt]selfattlnorm.south east) -- ([xshift=-0.5*\ssep,yshift=-1pt]selfattlnorm.south east);
\path [fill=lolgreen!40,fill opacity=0.5] ([xshift=0.5*\ssep+0.5pt,yshift=-0.8*\ssep+1pt]selfattlnorm.south west) -- ([xshift=0.5*\ssep+0.5pt,yshift=-1pt]selfattlnorm.south west) -- ([xshift=-0.5*\ssep-0.5pt,yshift=-1pt]selfattlnorm.south east) -- ([xshift=-0.5*\ssep-0.5pt,yshift=-0.8*\ssep+1pt]selfattlnorm.south east) -- ([xshift=0.5*\ssep+0.5pt,yshift=-0.8*\ssep+1pt]selfattlnorm.south west);
\node [anchor=south,minimum width=5*\ssep,minimum height=1.2*\ssep,draw,thick] (ffn) at ([yshift=1.2*\ssep]selfattlnorm.north) {};
\coordinate (ffnb0) at ([xshift=1.2*\ssep,yshift=0.3*\ssep]ffn.south west);
\coordinate (ffnm0) at ([xshift=0.3*\ssep,yshift=0.6*\ssep]ffn.south west);
\coordinate (ffnh0) at ([xshift=1.2*\ssep,yshift=0.9*\ssep]ffn.south west);
\coordinate (ffnb1) at ([xshift=-1.2*\ssep,yshift=0.3*\ssep]ffn.south east);
\coordinate (ffnm1) at ([xshift=-0.3*\ssep,yshift=0.6*\ssep]ffn.south east);
\coordinate (ffnh1) at ([xshift=-1.2*\ssep,yshift=0.9*\ssep]ffn.south east);
\path [fill=lolorange!30] (ffnb0) -- (ffnm0) -- (ffnm1) -- (ffnb1) -- (ffnb0);
\path [fill=lolpurple!30] (ffnm0) -- (ffnh0) -- (ffnh1) -- (ffnm1) -- (ffnm0);
\draw [arrows = {-latex}] ([xshift=-1pt]ffnb0) -- ([xshift=-1pt,yshift=0]ffnm0);
\draw [arrows = {-latex}] ([xshift=-1pt,yshift=0]ffnm0) -- ([xshift=-1pt]ffnh0);
\draw [arrows = {-latex}] ([xshift=1pt]ffnb1) -- ([xshift=1pt,yshift=0]ffnm1);
\draw [arrows = {-latex}] ([xshift=1pt,yshift=0]ffnm1) -- ([xshift=1pt]ffnh1);
\draw [arrows = {-Stealth[harpoon]},thick] ([xshift=0.5*\ssep,yshift=-1.2*\ssep+1pt]ffn.south west) -- ([xshift=0.5*\ssep,yshift=-1pt]ffn.south west);
\draw [arrows = {-Stealth[harpoon,swap]},thick] ([xshift=-0.5*\ssep,yshift=-1.2*\ssep+1pt]ffn.south east) -- ([xshift=-0.5*\ssep,yshift=-1pt]ffn.south east);
\path [fill=lolgreen!40,fill opacity=0.5] ([xshift=0.5*\ssep+0.5pt,yshift=-1.2*\ssep+1pt]ffn.south west) -- ([xshift=0.5*\ssep+0.5pt,yshift=-1pt]ffn.south west) -- ([xshift=-0.5*\ssep-0.5pt,yshift=-1pt]ffn.south east) -- ([xshift=-0.5*\ssep-0.5pt,yshift=-1.2*\ssep+1pt]ffn.south east) -- ([xshift=0.5*\ssep+0.5pt,yshift=-1.2*\ssep+1pt]ffn.south west);
\node [anchor=south,minimum width=5*\ssep,minimum height=0.8*\ssep,draw,thick] (ffnlnorm) at ([yshift=0.8*\ssep]ffn.north) {Post-norm or Pre-norm};
\draw [arrows = {-Stealth[harpoon]},thick] ([xshift=0.5*\ssep,yshift=-0.8*\ssep+1pt]ffnlnorm.south west) -- ([xshift=0.5*\ssep,yshift=-1pt]ffnlnorm.south west);
\draw [arrows = {-Stealth[harpoon,swap]},thick] ([xshift=-0.5*\ssep,yshift=-0.8*\ssep+1pt]ffnlnorm.south east) -- ([xshift=-0.5*\ssep,yshift=-1pt]ffnlnorm.south east);
\path [fill=lolgreen!40,fill opacity=0.5] ([xshift=0.5*\ssep+0.5pt,yshift=-0.8*\ssep+1pt]ffnlnorm.south west) -- ([xshift=0.5*\ssep+0.5pt,yshift=-1pt]ffnlnorm.south west) -- ([xshift=-0.5*\ssep-0.5pt,yshift=-1pt]ffnlnorm.south east) -- ([xshift=-0.5*\ssep-0.5pt,yshift=-0.8*\ssep+1pt]ffnlnorm.south east) -- ([xshift=0.5*\ssep+0.5pt,yshift=-0.8*\ssep+1pt]ffnlnorm.south west);
\draw [->,thick] ([xshift=0.5*\ssep,yshift=-0.4*\ssep]selfatt.south west) -- ([xshift=-0.5*\ssep,yshift=-0.4*\ssep]selfatt.south west) -- ([xshift=-0.5*\ssep]selfattlnorm.west) -- ([xshift=-1pt]selfattlnorm.west);
\draw [->,thick] ([xshift=0.5*\ssep,yshift=-0.6*\ssep]ffn.south west) -- ([xshift=-0.5*\ssep,yshift=-0.6*\ssep]ffn.south west) -- ([xshift=-0.5*\ssep]ffnlnorm.west) -- ([xshift=-1pt]ffnlnorm.west);
\node [anchor=north west,draw,fill=white,inner sep=0,rounded corners=0.05cm,minimum height=0.4cm,minimum width=2cm] (selfattlabel) at ([xshift=-0.1cm,yshift=0.2cm]selfatt.north west) {\scriptsize{Self-attention}};
\node [anchor=north west,draw,fill=white,inner sep=0,rounded corners=0.05cm,minimum height=0.4cm,minimum width=2cm] (ffnlabel) at ([xshift=-0.1cm,yshift=0.2cm]ffn.north west) {\scriptsize{FFN}};
\draw [decoration={brace,amplitude=5pt},decorate] ([xshift=-2em,yshift=-0.8*\ssep]selfatt.south west) -- ([xshift=-2em,yshift=2pt]ffnlnorm.north west) node [pos=0.5,left,xshift=-1.2em,yshift=2.5em,rotate=90] (timeslayer) {$L$ Blocks};
\draw [->,densely dashed] ([xshift=-0.5em]lm.east) .. controls +(east:1.3cm) and +(west:1.3cm) .. (timeslayer.north);
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\pgfmathsetseed{765}
\begin{scope}
\begin{axis}[
width=7.2cm, height=6cm,
ytick={2.4,3.2,...,5.6},
yticklabels={$2.4$, $3.2$, $4.0$, $4.8$, $5.6$},
xtick={100000,10000000,1000000000},
xlabel={Number of Parameters},
ylabel={Test Loss},
xlabel style={align=center},
ylabel style={yshift=-0.8em},
x tick label style={font=\footnotesize},
y tick label style={font=\footnotesize},
tick align=outside,
tickpos=left,
major tick length=0.05cm,
xmode=log,
ymode=log,
legend style={font=\scriptsize,draw=gray!20,rounded corners=2pt},
xmin=6000,
xmax=3000000000,
ymin=2.2,
ymax=6.4]
\addplot [sharp plot,very thick,gray, domain=10000:1200000000] {(x/(8.8*10^13))^-0.076};
\legend{$\mathcal{L}(N)=(\frac{N}{8.8\cdot10^{13}})^{-0.076}$}
\addplot [sharp plot,blue,mark=*,mark size=1pt, samples=12, domain=10000:1500000000] {((x)/(8.8*10^13))^-0.076 + rand*0.1};
\end{axis}
\end{scope}
\pgfmathsetseed{265}
\begin{scope}[xshift=2.8in]
\begin{axis}[
width=7.2cm, height=6cm,
ytick={2.7,3.0,...,4.2},
xtick={100000000,1000000000},
xlabel={Dataset Size},
ylabel={Test Loss},
xlabel style={align=center},
ylabel style={yshift=-0.8em},
x tick label style={font=\footnotesize},
y tick label style={font=\footnotesize},
log identify minor tick positions=false,
tick align=outside,
tickpos=left,
major tick length=0.05cm,
xmode=log,
legend style={font=\scriptsize,draw=gray!20,rounded corners=2pt},
xmin=10000000,
xmax=2000000000,
ymin=2.6,
ymax=4.3]
\addplot [sharp plot,very thick,gray, domain=15000000:1500000000] {(x/(5.4*10^13))^-0.095};
\legend{$\mathcal{L}(D)=(\frac{D}{5.4\cdot10^{13}})^{-0.095}$}
\addplot [sharp plot,blue,mark=*,mark size=1pt, samples=7, domain=15000000:1500000000] {(x/(5.4*10^13))^-0.095 + rand*0.04};
\end{axis}
\node[anchor=north,fill=white,minimum width=5em,inner sep=0pt,minimum height=0.3em] (mask) at (10em,-0.02em) {};
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\def\ssep{0.3cm}
\def\bsep{2.0cm}
\tikzset{
enode/.style={minimum width=0.8cm, minimum height=0.8cm, inner sep=2pt},
lnode/.style={minimum width=9.2cm, minimum height=1.3cm, inner sep=2pt}
}
\begin{scope}
\node [enode,anchor=center,fill=red!20] (e1) at (0,0) {\footnotesize{$\sigma^{<i}_1$}};
\node [enode,anchor=west,fill=red!20] (e2) at ([xshift=\ssep]e1.east) {\footnotesize{$\sigma^{<i}_2$}};
\node [enode,anchor=west,fill=ugreen!20] (e3) at ([xshift=\ssep]e2.east) {\footnotesize{$\mathbf{e}_1^i$}};
\node [enode,anchor=west,fill=ugreen!20] (e4) at ([xshift=\ssep]e3.east) {\footnotesize{$\mathbf{e}_2^i$}};
\node [enode,anchor=west,fill=ugreen!20] (e5) at ([xshift=\ssep]e4.east) {\footnotesize{$\mathbf{e}_3^i$}};
\node [enode,anchor=west,fill=ugreen!20] (e6) at ([xshift=\ssep]e5.east) {\footnotesize{$\mathbf{e}_4^i$}};
\node [enode,anchor=west,fill=blue!20,inner sep=0pt] (e7) at ([xshift=\ssep]e6.east) {\footnotesize{$\langle \mathbf{e}_1 \rangle$}};
\node [enode,anchor=west,fill=blue!20,inner sep=0pt] (e8) at ([xshift=\ssep]e7.east) {\footnotesize{$\langle \mathbf{e}_2 \rangle$}};
\node [enode,anchor=north] (x3) at ([yshift=-0.3cm]e3.south) {\footnotesize{$z_1^i$}};
\node [enode,anchor=north] (x4) at ([yshift=-0.3cm]e4.south) {\footnotesize{$z_2^i$}};
\node [enode,anchor=north] (x5) at ([yshift=-0.3cm]e5.south) {\footnotesize{$z_3^i$}};
\node [enode,anchor=north] (x6) at ([yshift=-0.3cm]e6.south) {\footnotesize{$z_4^i$}};
\node [enode,anchor=north] (x7) at ([yshift=-0.3cm]e7.south) {\footnotesize{$\langle \mathrm{g}_1 \rangle$}};
\node [enode,anchor=north] (x8) at ([yshift=-0.3cm]e8.south) {\footnotesize{$\langle \mathrm{g}_2 \rangle$}};
\draw [->] ([yshift=-3pt]x3.north) -- ([yshift=-1pt]e3.south);
\draw [->] ([yshift=-3pt]x4.north) -- ([yshift=-1pt]e4.south);
\draw [->] ([yshift=-3pt]x5.north) -- ([yshift=-1pt]e5.south);
\draw [->] ([yshift=-3pt]x6.north) -- ([yshift=-1pt]e6.south);
\draw [->] ([yshift=-3pt]x7.north) -- ([yshift=-1pt]e7.south);
\draw [->] ([yshift=-3pt]x8.north) -- ([yshift=-1pt]e8.south);
\node [lnode,anchor=south west,draw,thick] (transformer) at ([xshift=-0.3cm,yshift=0.4cm]e1.north west) {};
\node [anchor=south] (tcaption) at ([yshift=0.3cm]transformer.south) {{\Large Transformer Layers}};
\node [enode,anchor=south,fill=gray!30] (h1) at ([yshift=2.2cm]e1.north) {\footnotesize{\color{black!30}$\mathbf{h}^{<i}_1$}};
\node [enode,anchor=south,fill=gray!30] (h2) at ([yshift=2.2cm]e2.north) {\footnotesize{\color{black!30}$\mathbf{h}^{<i}_2$}};
\node [enode,anchor=south,fill=gray!30] (h3) at ([yshift=2.2cm]e3.north) {\footnotesize{\color{black!30}$\mathbf{h}_1$}};
\node [enode,anchor=south,fill=gray!30] (h4) at ([yshift=2.2cm]e4.north) {\footnotesize{\color{black!30}$\mathbf{h}_2$}};
\node [enode,anchor=south,fill=gray!30] (h5) at ([yshift=2.2cm]e5.north) {\footnotesize{\color{black!30}$\mathbf{h}_3$}};
\node [enode,anchor=south,fill=gray!30] (h6) at ([yshift=2.2cm]e6.north) {\footnotesize{\color{black!30}$\mathbf{h}_4$}};
\node [enode,anchor=south,fill=red!20,inner sep=0pt] (h7) at ([yshift=2.2cm]e7.north) {\tiny{$\sigma^{<i+1}_1$}};
\node [enode,anchor=south,fill=red!20,inner sep=0pt] (h8) at ([yshift=2.2cm]e8.north) {\tiny{$\sigma^{<i+1}_2$}};
\draw [->] ([yshift=1pt]e1.north) -- ([yshift=0.37cm]e1.north);
\draw [->] ([yshift=1pt]e2.north) -- ([yshift=0.37cm]e2.north);
\draw [->] ([yshift=1pt]e3.north) -- ([yshift=0.37cm]e3.north);
\draw [->] ([yshift=1pt]e4.north) -- ([yshift=0.37cm]e4.north);
\draw [->] ([yshift=1pt]e5.north) -- ([yshift=0.37cm]e5.north);
\draw [->] ([yshift=1pt]e6.north) -- ([yshift=0.37cm]e6.north);
\draw [->] ([yshift=1pt]e7.north) -- ([yshift=0.37cm]e7.north);
\draw [->] ([yshift=1pt]e8.north) -- ([yshift=0.37cm]e8.north);
\draw [<-,gray] ([yshift=-1pt]h1.south) -- ([yshift=-0.4cm]h1.south);
\draw [<-,gray] ([yshift=-1pt]h2.south) -- ([yshift=-0.4cm]h2.south);
\draw [<-,gray] ([yshift=-1pt]h3.south) -- ([yshift=-0.4cm]h3.south);
\draw [<-,gray] ([yshift=-1pt]h4.south) -- ([yshift=-0.4cm]h4.south);
\draw [<-,gray] ([yshift=-1pt]h5.south) -- ([yshift=-0.4cm]h5.south);
\draw [<-,gray] ([yshift=-1pt]h6.south) -- ([yshift=-0.4cm]h6.south);
\draw [<-] ([yshift=-1pt]h7.south) -- ([yshift=-0.4cm]h7.south);
\draw [<-] ([yshift=-1pt]h8.south) -- ([yshift=-0.4cm]h8.south);
\begin{pgfonlayer}{background}
\node [draw,dotted,thick,fit=(e1)(e2)] (softlast) {};
\node [draw,dotted,thick,fit=(h7)(h8)] (softcur) {};
\end{pgfonlayer}
\node [anchor=east] (softlastl) at ([xshift=-0.1cm]softlast.west) {\footnotesize Soft Prompts};
\node [anchor=north west] (softlast2) at ([yshift=0.2cm]softlastl.south west) {\footnotesize at Step $i-1$};
\node [anchor=west] (softcurl) at ([xshift=0.1cm]softcur.east) {\footnotesize Soft Prompts};
\node [anchor=north west] (softcur2) at ([yshift=0.2cm]softcurl.south west) {\footnotesize at the Current Step};
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\def\ssep{0.9cm}
\def\bsep{2.0cm}
\tikzstyle{pnode} = [minimum width=2.5cm,minimum height=0.6cm,inner sep=2pt];
\tikzstyle{tnode} = [minimum width=11cm,minimum height=3.6cm,inner sep=2pt,draw,thick];
\begin{scope}
\node [pnode,anchor=west,minimum width=5.0cm,fill=red!20] (inst) at (0,0) {\footnotesize{Full Context}};
\node [pnode,anchor=west,fill=blue!20] (input) at ([xshift=6cm]inst.west) {\footnotesize{User Input}};
\node [anchor=east] (plus) at ([xshift=-0.2cm]input.west) {$+$};
\node [anchor=west] (y) at ([xshift=2cm]input.east) {$\Pr^t(\mathbf{y}|\mathbf{c},\mathbf{z})$};
\draw [->,thick] ([xshift=0.5cm]input.east) -- ([xshift=-0.5cm]y.west);
\node [anchor=north] (c) at ([yshift=-0.2cm]inst.south) {$\mathbf{c}$};
\node [anchor=north] (z) at ([yshift=-0.2cm]input.south) {$\mathbf{z}$};
\node [anchor=north] (yl) at ([yshift=-0.1cm]y.south) {$\mathbf{y}$};
\node [anchor=south west] (modellabel) at ([yshift=0.2cm]inst.north west) {\small{Teacher Model:}};
\end{scope}
\begin{scope}[yshift=-3.5cm]
\node [pnode,anchor=west,minimum width=4.0cm,fill=ugreen!20] (inst) at (0,0) {\footnotesize{Simplified Context}};
\node [pnode,anchor=west,fill=blue!20] (input) at ([xshift=6cm]inst.west) {\footnotesize{User Input}};
\node [anchor=east] (plus) at ([xshift=-0.2cm]input.west) {$+$};
\node [anchor=west] (y) at ([xshift=2cm]input.east) {$\Pr^s(\mathbf{y}|\mathbf{c}',\mathbf{z})$};
\draw [->,thick] ([xshift=0.5cm]input.east) -- ([xshift=-0.5cm]y.west);
\node [anchor=north] (c) at ([yshift=-0.1cm]inst.south) {$\mathbf{c}'$};
\node [anchor=north] (z) at ([yshift=-0.2cm]input.south) {$\mathbf{z}$};
\node [anchor=north] (yl) at ([yshift=-0.1cm]y.south) {$\mathbf{y}$};
\node [anchor=south west] (modellabel) at ([yshift=0.2cm]inst.north west) {\small{Student Model:}};
\draw [<->,very thick] (y.north) -- ([yshift=2.1cm]y.north) node [pos=0.5,left,xshift=-0.2cm,yshift=0.2cm] {$\mathrm{Loss}$};
\end{scope}
\end{tikzpicture}
% figures/Chapter2/figure-cot-application-examples.tex
% 由主文件通过以下命令读入:
% \input{figures/Chapter2/figure-cot-application-examples}
\begin{figure}[!t]
\centering
\setlength{\tabcolsep}{0pt}
\begin{tabular}{@{}c@{\hspace{0.02\textwidth}}c@{}}
% -------------------- (a) CSQA --------------------
\begin{tcolorbox}[
breakable=false,
width=0.45\textwidth,
height=9cm,
valign=top
]
{\footnotesize
Q: What home entertainment equipment requires cable?
Answer Choices: (a) radio shack (b) substation
(c) television (d) cabinet
\vspace{0.2em}
A: \textcolor[RGB]{154,245,175}{
The answer must require cable. Of the above choices,
only television requires cable.
}
So the answer is (c).
\vspace{0.5em}
Q: What type of person typically contracts illness?
Answer Choices: (a) hospital (b) head (c) sick person
(d) elderly person (e) doctor's office
\vspace{0.2em}
A: \textcolor[RGB]{154,245,175}{\uline{
The answer must be a person susceptible to illness.
Of the choices, ``elderly person'' is correct because
they typically have weaker immune systems.
}}
\uline{So the answer is (d).}
\par}
\end{tcolorbox}
&
% -------------------- (b) StrategyQA --------------------
\begin{tcolorbox}[
breakable=false,
width=0.45\textwidth,
height=9cm,
valign=top
]
{\footnotesize
Q: Do hamsters provide food for any animals?
\vspace{0.2em}
A: \textcolor[RGB]{154,245,175}{
Hamsters are prey animals. Prey are food for predators.
Thus, hamsters provide food for some animals.
}
So the answer is yes.
\vspace{0.5em}
Q: Can you use Microsoft Office without internet?
\vspace{0.2em}
A: \textcolor[RGB]{154,245,175}{\uline{
Yes, you can use Microsoft Office applications,
although features like cloud storage and online
collaboration require internet.
}}
\uline{So the answer is yes.}
\par}
\end{tcolorbox}
\\[-0.2em]
{\small (a) CSQA}
&
{\small (b) StrategyQA}
\\[0.6em]
% -------------------- (c) Dyck languages --------------------
\begin{tcolorbox}[
breakable=false,
width=0.45\textwidth,
height=9cm,
valign=top
]
{\footnotesize
Q: Complete the rest of the sequence, making sure that
the parentheses are closed properly. Input: $[$ $\{$
\vspace{0.2em}
A: \textcolor[RGB]{154,245,175}{
Let's think step by step.
}
\vspace{0.1em}
0: empty stack
\vspace{0.1em}
1: $[$; stack: $[$
\vspace{0.1em}
2: $\{$; stack: $[$ $\{$
\vspace{0.1em}
So the answer is $\}$ $]$.
\vspace{0.5em}
Q: Complete the rest of the sequence, making sure that
the parentheses are closed properly. Input: $<$ $[$ $[$
\vspace{0.2em}
A: \uline{\textcolor[RGB]{154,245,175}{
Let's think step by step.
}}
\vspace{0.1em}
\uline{0: empty stack}
\vspace{0.1em}
\uline{1: $<$; stack: $<$}
\vspace{0.1em}
\uline{2: $[$; stack: $<$ $[$}
\vspace{0.1em}
\uline{3: $[$; stack: $<$ $[$ $[$}
\vspace{0.1em}
\uline{So the answer is $]$ $]$ $>$.}
\par}
\end{tcolorbox}
&
% -------------------- (d) Last Letter Concatenation --------------------
\begin{tcolorbox}[
breakable=false,
width=0.45\textwidth,
height=9cm,
valign=top
]
{\footnotesize
Q: Take the last letters of the words in
``Elon Musk'' and concatenate them.
\vspace{0.2em}
A: \textcolor[RGB]{154,245,175}{
The last letter of ``Elon'' is ``n''.
The last letter of ``Musk'' is ``k''.
Concatenating them is ``nk''.
}
The answer is nk.
\vspace{0.5em}
Q: Take the last letters of each word in
``Renata Mariela Mona Kristin'' and concatenate them.
\vspace{0.2em}
A: \textcolor[RGB]{154,245,175}{\uline{
The last letter of ``Renata'' is ``a''.
The last letter of ``Mariela'' is ``a''.
The last letter of ``Mona'' is ``a''.
The last letter of ``Kristin'' is ``n''.
Concatenating them is ``aaan''.
}}
\uline{The answer is aaan.}
\par}
\end{tcolorbox}
\\[-0.2em]
{\small (c) Dyck languages}
&
{\small (d) Last Letter Concatenation}
\end{tabular}
\caption{四种不同推理任务中的 CoT,包括 CSQA、StrategyQA、戴克语言和末尾字母连接。CoT 部分用绿色突出显示。}
\label{fig:cot-application-examples}
\end{figure}
\ No newline at end of file
\begin{tikzpicture}
\def\ssep{0.50cm}
\tikzstyle{lmnode} = [minimum width=2.5cm,minimum height=0.5cm,inner sep=2pt,draw,thick];
\begin{scope}
\node [lmnode,anchor=south west,draw=blue] (llmbase) at (0,0) {\small{LLM2}};
\node [lmnode,anchor=north west,draw=red] (llm2) at ([yshift=-\ssep]llmbase.south west) {\small{LLM1}};
\node [lmnode,anchor=south west,draw=ugreen] (llm3) at ([yshift=\ssep]llmbase.north west) {\small{LLM2}};
\node [lmnode,anchor=east] (promptbase) at ([xshift=-2*\ssep]llmbase.west) {\small{Prompt}};
\node [lmnode,anchor=west,draw=blue] (predictionbase) at ([xshift=2*\ssep]llmbase.east) {\small{Prediction2}};
\node [lmnode,anchor=north west,draw=red] (prediction2) at ([yshift=-\ssep]predictionbase.south west) {\small{Prediction1}};
\node [lmnode,anchor=south west,draw=ugreen] (prediction3) at ([yshift=\ssep]predictionbase.north west) {\small{Prediction3}};
\draw [decoration={brace,amplitude=4pt},decorate,thick] ([xshift=0.5cm]prediction3.north east) -- ([xshift=0.5cm]prediction2.south east);
\node [anchor=south,fill=gray!30] (combination) at ([xshift=0.5cm,yshift=0.3cm]prediction3.north east) {\small{Combine/Select}};
\node [anchor=west] (final) at ([xshift=0.9cm]predictionbase.east) {\small{Final}};
\node [anchor=north west] (final2) at ([yshift=0.1cm]final.south west) {\small{Prediction}};
\draw [->,thick] ([xshift=2pt]promptbase.east) -- ([xshift=-2pt]llmbase.west);
\draw [->,thick] ([xshift=2pt,yshift=-0.2cm]promptbase.east) -- ([xshift=-2pt]llm2.west);
\draw [->,thick] ([xshift=2pt,yshift=0.2cm]promptbase.east) -- ([xshift=-2pt]llm3.west);
\draw [->,thick] ([xshift=2pt]llmbase.east) -- ([xshift=-2pt]predictionbase.west);
\draw [->,thick] ([xshift=2pt]llm2.east) -- ([xshift=-2pt]prediction2.west);
\draw [->,thick] ([xshift=2pt]llm3.east) -- ([xshift=-2pt]prediction3.west);
\node [anchor=north] (caption) at ([yshift=-1.5cm]llmbase.south east) {\small{(a) Model Ensembling}};
\end{scope}
\begin{scope}[yshift=-5cm]
\node [lmnode,anchor=south west] (llmbase) at (0,0) {\small{LLM}};
\node [lmnode,anchor=east,draw=blue] (promptbase) at ([xshift=-2*\ssep]llmbase.west) {\small{Prompt2}};
\node [lmnode,anchor=north west,draw=red] (prompt2) at ([yshift=-\ssep]promptbase.south west) {\small{Prompt1}};
\node [lmnode,anchor=south west,draw=ugreen] (prompt3) at ([yshift=\ssep]promptbase.north west) {\small{Prompt3}};
\node [lmnode,anchor=west,draw=blue] (predictionbase) at ([xshift=2*\ssep]llmbase.east) {\small{Prediction2}};
\node [lmnode,anchor=north west,draw=red] (prediction2) at ([yshift=-\ssep]predictionbase.south west) {\small{Prediction1}};
\node [lmnode,anchor=south west,draw=ugreen] (prediction3) at ([yshift=\ssep]predictionbase.north west) {\small{Prediction3}};
\draw [decoration={brace,amplitude=4pt},decorate,thick] ([xshift=0.5cm]prediction3.north east) -- ([xshift=0.5cm]prediction2.south east);
\node [anchor=south,fill=gray!30] (combination) at ([xshift=0.5cm,yshift=0.3cm]prediction3.north east) {\small{Combine/Select}};
\node [anchor=west] (final) at ([xshift=0.9cm]predictionbase.east) {\small{Final}};
\node [anchor=north west] (final2) at ([yshift=0.1cm]final.south west) {\small{Prediction}};
\draw [->,thick] ([xshift=2pt]promptbase.east) -- ([xshift=-2pt]llmbase.west);
\draw [->,thick] ([xshift=2pt]prompt2.east) -- ([xshift=-2pt,yshift=-0.2cm]llmbase.west);
\draw [->,thick] ([xshift=2pt]prompt3.east) -- ([xshift=-2pt,yshift=0.2cm]llmbase.west);
\draw [->,thick] ([xshift=2pt]llmbase.east) -- ([xshift=-2pt]predictionbase.west);
\draw [->,thick] ([xshift=2pt,yshift=-0.2cm]llmbase.east) -- ([xshift=-2pt]prediction2.west);
\draw [->,thick] ([xshift=2pt,yshift=0.2cm]llmbase.east) -- ([xshift=-2pt]prediction3.west);
\node [anchor=north] (caption) at ([yshift=-1.5cm]llmbase.south east) {\small{(b) Prompt Ensembling}};
\end{scope}
\begin{scope}[yshift=-10cm]
\node [lmnode,anchor=south west] (llmbase) at (0,0) {\small{LLM}};
\node [lmnode,anchor=east] (promptbase) at ([xshift=-2*\ssep]llmbase.west) {\small{Prompt}};
\node [lmnode,anchor=west,draw=blue] (predictionbase) at ([xshift=2*\ssep]llmbase.east) {\small{Prediction2}};
\node [lmnode,anchor=north west,draw=red] (prediction2) at ([yshift=-\ssep]predictionbase.south west) {\small{Prediction1}};
\node [lmnode,anchor=south west,draw=ugreen] (prediction3) at ([yshift=\ssep]predictionbase.north west) {\small{Prediction3}};
\draw [decoration={brace,amplitude=4pt},decorate,thick] ([xshift=0.5cm]prediction3.north east) -- ([xshift=0.5cm]prediction2.south east);
\node [anchor=south,fill=gray!30] (combination) at ([xshift=0.5cm,yshift=0.3cm]prediction3.north east) {\small{Combine/Select}};
\node [anchor=west] (final) at ([xshift=0.9cm]predictionbase.east) {\small{Final}};
\node [anchor=north west] (final2) at ([yshift=0.1cm]final.south west) {\small{Prediction}};
\draw [->,thick] ([xshift=2pt]promptbase.east) -- ([xshift=-2pt]llmbase.west);
\draw [->,thick] ([xshift=2pt]llmbase.east) -- ([xshift=-2pt]predictionbase.west);
\draw [->,thick] ([xshift=2pt,yshift=-0.2cm]llmbase.east) -- ([xshift=-2pt]prediction2.west);
\draw [->,thick] ([xshift=2pt,yshift=0.2cm]llmbase.east) -- ([xshift=-2pt]prediction3.west) node [pos=0.5,left,rotate=44,yshift=0.3cm,xshift=0.5cm,inner sep=2pt,fill=gray!30] {\scriptsize{Sample}};
\node [anchor=north] (caption) at ([yshift=-1.5cm]llmbase.south east) {\small{(c) Output Ensembling}};
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\def\ssep{0.9cm}
\def\bsep{2.0cm}
\tikzstyle{enode} = [minimum width=0.8cm,minimum height=0.8cm,inner sep=2pt];
\tikzstyle{tnode} = [minimum width=11cm,minimum height=3.6cm,inner sep=2pt,draw,thick];
\tikzstyle{bnode} = [minimum width=4.6cm,minimum height=0.6cm,inner sep=2pt];
\begin{scope}
\node [enode,anchor=center] (x0) at (0,0) {\footnotesize{...}};
\node [enode,anchor=center] (x1) at ([xshift=\ssep]x0.center) {\scriptsize{Translate}};
\node [enode,anchor=center] (x2) at ([xshift=\ssep]x1.center) {\footnotesize{this}};
\node [enode,anchor=center] (x3) at ([xshift=\ssep]x2.center) {\footnotesize{into}};
\node [enode,anchor=center] (x4) at ([xshift=\ssep]x3.center) {\footnotesize{Chinese}};
\node [enode,anchor=center] (x5) at ([xshift=\ssep]x4.center) {\footnotesize{.}};
\node [enode,anchor=center] (x6) at ([xshift=\ssep]x5.center) {\footnotesize{I}};
\node [enode,anchor=center] (x7) at ([xshift=\ssep]x6.center) {\footnotesize{have}};
\node [enode,anchor=center] (x8) at ([xshift=\ssep]x7.center) {\footnotesize{a}};
\node [enode,anchor=center] (x9) at ([xshift=\ssep]x8.center) {\footnotesize{cat}};
\node [enode,anchor=center] (x10) at ([xshift=\ssep]x9.center) {\footnotesize{.}};
\node [enode,anchor=center] (x11) at ([xshift=\ssep]x10.center) {\footnotesize{...}};
\draw [->] ([yshift=0.2cm]x0.center) -- ([yshift=0.2cm+0.6*\ssep]x0.center);
\draw [->] ([yshift=0.2cm]x1.center) -- ([yshift=0.2cm+0.6*\ssep]x1.center);
\draw [->] ([yshift=0.2cm]x2.center) -- ([yshift=0.2cm+0.6*\ssep]x2.center);
\draw [->] ([yshift=0.2cm]x3.center) -- ([yshift=0.2cm+0.6*\ssep]x3.center);
\draw [->] ([yshift=0.2cm]x4.center) -- ([yshift=0.2cm+0.6*\ssep]x4.center);
\draw [->] ([yshift=0.2cm]x5.center) -- ([yshift=0.2cm+0.6*\ssep]x5.center);
\draw [->] ([yshift=0.2cm]x6.center) -- ([yshift=0.2cm+0.6*\ssep]x6.center);
\draw [->] ([yshift=0.2cm]x7.center) -- ([yshift=0.2cm+0.6*\ssep]x7.center);
\draw [->] ([yshift=0.2cm]x8.center) -- ([yshift=0.2cm+0.6*\ssep]x8.center);
\draw [->] ([yshift=0.2cm]x9.center) -- ([yshift=0.2cm+0.6*\ssep]x9.center);
\draw [->] ([yshift=0.2cm]x10.center) -- ([yshift=0.2cm+0.6*\ssep]x10.center);
\draw [->] ([yshift=0.2cm]x11.center) -- ([yshift=0.2cm+0.6*\ssep]x11.center);
\node [tnode,anchor=south west] (llm) at ([xshift=-0.2cm,yshift=0.60*\ssep]x0.north west) {};
\node [anchor=south] (llmlabel) at ([yshift=0.3cm]llm.south) {\LARGE{Transformer}};
\node [enode,anchor=center] (h0) at ([yshift=3cm]x0.center) {\footnotesize{...}};
\node [enode,anchor=center] (h1) at ([xshift=\ssep]h0.center) {\footnotesize{$\mathbf{h}_j$}};
\node [enode,anchor=center] (h2) at ([xshift=\ssep]h1.center) {\footnotesize{$\mathbf{h}_{j+1}$}};
\node [enode,anchor=center] (h3) at ([xshift=\ssep]h2.center) {\footnotesize{$\mathbf{h}_{j+2}$}};
\node [enode,anchor=center] (h4) at ([xshift=\ssep]h3.center) {\footnotesize{$\mathbf{h}_{j+3}$}};
\node [enode,anchor=center] (h5) at ([xshift=\ssep]h4.center) {\footnotesize{$\mathbf{h}_{j+4}$}};
\node [enode,anchor=center] (h6) at ([xshift=\ssep]h5.center) {\footnotesize{$\mathbf{h}_{j+5}$}};
\node [enode,anchor=center] (h7) at ([xshift=\ssep]h6.center) {\footnotesize{$\mathbf{h}_{j+6}$}};
\node [enode,anchor=center] (h8) at ([xshift=\ssep]h7.center) {\footnotesize{$\mathbf{h}_{j+7}$}};
\node [enode,anchor=center] (h9) at ([xshift=\ssep]h8.center) {\footnotesize{$\mathbf{h}_{j+8}$}};
\node [enode,anchor=center] (h10) at ([xshift=\ssep]h9.center) {\footnotesize{$\mathbf{h}_{j+9}$}};
\node [enode,anchor=center] (h11) at ([xshift=\ssep]h10.center) {\footnotesize{...}};
\node [enode,anchor=center] (a0) at ([yshift=1.3*\ssep]h0.center) {\footnotesize{...}};
\node [enode,anchor=center] (a1) at ([yshift=1.3*\ssep]h1.center) {\footnotesize{...}};
\node [enode,anchor=center] (a2) at ([yshift=1.3*\ssep]h2.center) {\footnotesize{...}};
\node [enode,anchor=center] (a3) at ([yshift=1.3*\ssep]h3.center) {\footnotesize{...}};
\node [enode,anchor=center] (a4) at ([yshift=1.3*\ssep]h4.center) {\footnotesize{...}};
\node [enode,anchor=center] (a5) at ([yshift=1.3*\ssep]h5.center) {\footnotesize{...}};
\node [enode,anchor=center] (a6) at ([yshift=1.3*\ssep]h6.center) {\footnotesize{...}};
\node [enode,anchor=center] (a7) at ([yshift=1.3*\ssep]h7.center) {\footnotesize{...}};
\node [enode,anchor=center] (a8) at ([yshift=1.3*\ssep]h8.center) {\footnotesize{...}};
\node [enode,anchor=center] (a9) at ([yshift=1.3*\ssep]h9.center) {\footnotesize{...}};
\node [enode,anchor=center] (a10) at ([yshift=1.3*\ssep]h10.center) {\footnotesize{...}};
\node [enode,anchor=center] (a11) at ([yshift=1.3*\ssep]h11.center) {\footnotesize{...}};
\draw [->] ([yshift=-0.5*\ssep-0.4cm]h0.center) -- ([yshift=-0.4cm]h0.center);
\draw [->] ([yshift=-0.5*\ssep-0.4cm]h1.center) -- ([yshift=-0.4cm]h1.center);
\draw [->] ([yshift=-0.5*\ssep-0.4cm]h2.center) -- ([yshift=-0.4cm]h2.center);
\draw [->] ([yshift=-0.5*\ssep-0.4cm]h3.center) -- ([yshift=-0.4cm]h3.center);
\draw [->] ([yshift=-0.5*\ssep-0.4cm]h4.center) -- ([yshift=-0.4cm]h4.center);
\draw [->] ([yshift=-0.5*\ssep-0.4cm]h5.center) -- ([yshift=-0.4cm]h5.center);
\draw [->] ([yshift=-0.5*\ssep-0.4cm]h6.center) -- ([yshift=-0.4cm]h6.center);
\draw [->] ([yshift=-0.5*\ssep-0.4cm]h7.center) -- ([yshift=-0.4cm]h7.center);
\draw [->] ([yshift=-0.5*\ssep-0.4cm]h8.center) -- ([yshift=-0.4cm]h8.center);
\draw [->] ([yshift=-0.5*\ssep-0.4cm]h9.center) -- ([yshift=-0.4cm]h9.center);
\draw [->] ([yshift=-0.5*\ssep-0.4cm]h10.center) -- ([yshift=-0.4cm]h10.center);
\draw [->] ([yshift=-0.5*\ssep-0.4cm]h11.center) -- ([yshift=-0.4cm]h11.center);
\draw [<-] ([yshift=0.5*\ssep+0.4cm]h0.center) -- ([yshift=0.4cm]h0.center);
\draw [<-] ([yshift=0.5*\ssep+0.4cm]h1.center) -- ([yshift=0.4cm]h1.center);
\draw [<-] ([yshift=0.5*\ssep+0.4cm]h2.center) -- ([yshift=0.4cm]h2.center);
\draw [<-] ([yshift=0.5*\ssep+0.4cm]h3.center) -- ([yshift=0.4cm]h3.center);
\draw [<-] ([yshift=0.5*\ssep+0.4cm]h4.center) -- ([yshift=0.4cm]h4.center);
\draw [<-] ([yshift=0.5*\ssep+0.4cm]h5.center) -- ([yshift=0.4cm]h5.center);
\draw [<-] ([yshift=0.5*\ssep+0.4cm]h6.center) -- ([yshift=0.4cm]h6.center);
\draw [<-] ([yshift=0.5*\ssep+0.4cm]h7.center) -- ([yshift=0.4cm]h7.center);
\draw [<-] ([yshift=0.5*\ssep+0.4cm]h8.center) -- ([yshift=0.4cm]h8.center);
\draw [<-] ([yshift=0.5*\ssep+0.4cm]h9.center) -- ([yshift=0.4cm]h9.center);
\draw [<-] ([yshift=0.5*\ssep+0.4cm]h10.center) -- ([yshift=0.4cm]h10.center);
\draw [<-] ([yshift=0.5*\ssep+0.4cm]h11.center) -- ([yshift=0.4cm]h11.center);
\begin{pgfonlayer}{background}
\node [bnode,anchor=west,fill=blue!20] (hardbox) at (x1.west) {};
\node [bnode,anchor=center,fill=red!20] (softbox) at ([yshift=3cm]hardbox.center) {};
\end{pgfonlayer}
\node [anchor=north] (hardlabel) at ([yshift=-0.1cm]hardbox.south) {\small{Hard Prompt (Instruction)}};
\node [anchor=east] (softlabel) at ([yshift=1.2cm,xshift=-0.4cm]llm.west) {\small{Soft Prompt}};
\draw [->] (softlabel.east) .. controls +(east:0.9cm) and +(west:0.9cm) .. ([yshift=0.2cm,xshift=-2pt]softbox.west);
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\def\ssep{0.35cm}
\def\bsep{1.45cm}
\tikzset{
tnode/.style={minimum width=0.85cm,minimum height=0.32cm,inner sep=2pt,draw},
lnode/.style={minimum width=7.1cm,minimum height=0.95cm,inner sep=2pt,draw,thick,fill=gray!20}
}
\begin{scope}
\node [tnode,anchor=west] (x1) at (0,0) {\small{Look}};
\node [tnode,anchor=west] (x2) at ([xshift=\ssep]x1.east) {\small{out}};
\node [tnode,anchor=west] (x3) at ([xshift=\ssep]x2.east) {\small{!}};
\node [tnode,anchor=west] (x4) at ([xshift=\ssep]x3.east) {\small{小心}};
\node [tnode,anchor=west] (x5) at ([xshift=\ssep]x4.east) {\small{!}};
\node [lnode,anchor=south west] (l1) at ([xshift=-0.2cm,yshift=0.65cm]x1.north west) {};
\node [lnode,anchor=south west] (l2) at ([yshift=\bsep]l1.south west) {};
\node [lnode,anchor=south west] (l3) at ([yshift=\bsep]l2.south west) {};
\node [anchor=center] at (l1.center) {\small{Transformer layer $l-1$}};
\node [anchor=center] at (l2.center) {\small{Transformer layer $l$}};
\node [anchor=center] at (l3.center) {\small{Transformer layer $l+1$}};
\foreach \x in {x1,x2,x3,x4,x5} {
\draw [->,thin] (\x.north) -- ++(0,0.55cm);
}
\node [tnode,fill=red!20,draw=red,anchor=east] (p10) at ([xshift=-0.45cm,yshift=0.25cm]l1.west) {\small{$\mathbf{p}_0^{l-1}$}};
\node [tnode,fill=red!20,draw=red,anchor=east] (p11) at ([xshift=-0.15cm,yshift=-0.25cm]l1.west) {\small{$\mathbf{p}_1^{l-1}$}};
\node [tnode,fill=red!20,draw=red,anchor=east] (p20) at ([xshift=-0.45cm,yshift=0.25cm]l2.west) {\small{$\mathbf{p}_0^{l}$}};
\node [tnode,fill=red!20,draw=red,anchor=east] (p21) at ([xshift=-0.15cm,yshift=-0.25cm]l2.west) {\small{$\mathbf{p}_1^{l}$}};
\node [tnode,fill=red!20,draw=red,anchor=east] (p30) at ([xshift=-0.45cm,yshift=0.25cm]l3.west) {\small{$\mathbf{p}_0^{l+1}$}};
\node [tnode,fill=red!20,draw=red,anchor=east] (p31) at ([xshift=-0.15cm,yshift=-0.25cm]l3.west) {\small{$\mathbf{p}_1^{l+1}$}};
\foreach \p/\l in {p10/l1,p11/l1,p20/l2,p21/l2,p30/l3,p31/l3} {
\draw [->,thin] (\p.east) -- ([xshift=0.05cm]\l.west);
}
\node [anchor=east] (plabel) at ([xshift=-0.9cm]p20.west) {\small{trainable}};
\node [anchor=north east] at ([yshift=0.08cm]plabel.south east) {\small{prefixes}};
\node [tnode,anchor=south,fill=blue!10,draw=blue] (loss1) at ([yshift=0.55cm]l3.north) {\small{Loss}};
\node [tnode,anchor=south,fill=blue!10,draw=blue] (loss2) at ([xshift=1.2cm,yshift=0.55cm]l3.north) {\small{Loss}};
\draw [->,thin] ([xshift=-0.6cm]l3.north) -- (loss1.south);
\draw [->,thin] ([xshift=0.6cm]l3.north) -- (loss2.south);
\draw [decoration={brace,amplitude=4pt},decorate,thick]
([yshift=-0.45cm]x3.east) -- ([yshift=-0.45cm]x1.west)
node [pos=0.5,below,yshift=-0.15cm] {\small{User input}};
\draw [decoration={brace,amplitude=4pt},decorate,thick]
([yshift=-0.45cm]x5.east) -- ([yshift=-0.45cm]x4.west)
node [pos=0.5,below,yshift=-0.15cm] {\small{LLM prediction}};
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\def\ssep{0.15cm}
\def\bsep{2.0cm}
\tikzstyle{tnode} = [minimum width=0.85cm,minimum height=0.3cm,inner sep=2pt];
\tikzstyle{lnode} = [minimum width=3.8cm,minimum height=0.7cm,inner sep=2pt];
\begin{scope}
\node [tnode,anchor=west] (h1) at (0,0) {$\mathbf{p}_0$};
\node [tnode,anchor=west] (h2) at ([xshift=\ssep]h1.east) {$\mathbf{p}_1$};
\node [tnode,anchor=west] (h3) at ([xshift=\ssep]h2.east) {$\cdots$};
\node [tnode,anchor=west] (h4) at ([xshift=\ssep]h3.east) {$\mathbf{p}_n$};
\node [tnode,anchor=west] (h5) at ([xshift=\ssep]h4.east) {$\mathbf{q}_0$};
\node [tnode,anchor=west] (h6) at ([xshift=\ssep]h5.east) {$\mathbf{q}_1$};
\node [tnode,anchor=west] (h7) at ([xshift=\ssep]h6.east) {$\cdots$};
\node [tnode,anchor=west] (h8) at ([xshift=\ssep]h7.east) {$\mathbf{q}_{m'}$};
\node [tnode,anchor=west] (h9) at ([xshift=\ssep]h8.east) {$\mathbf{e}_0$};
\node [tnode,anchor=west] (h10) at ([xshift=\ssep]h9.east) {$\mathbf{e}_1$};
\node [tnode,anchor=west] (h11) at ([xshift=\ssep]h10.east) {$\cdots$};
\node [tnode,anchor=west] (h12) at ([xshift=\ssep]h11.east) {$\mathbf{e}_{m}$};
\draw [<-,thin] ([yshift=-0.25cm+1pt]h5.center) -- ([yshift=-0.30*\bsep-0.25cm]h5.center);
\draw [<-,thin] ([yshift=-0.25cm+1pt]h6.center) -- ([yshift=-0.30*\bsep-0.25cm]h6.center);
\draw [<-,thin] ([yshift=-0.25cm+1pt]h8.center) -- ([yshift=-0.30*\bsep-0.25cm]h8.center);
\draw [<-,thin] ([yshift=-0.25cm+1pt]h9.center) -- ([yshift=-0.30*\bsep-0.25cm]h9.center);
\draw [<-,thin] ([yshift=-0.25cm+1pt]h10.center) -- ([yshift=-0.30*\bsep-0.25cm]h10.center);
\draw [<-,thin] ([yshift=-0.25cm+1pt]h12.center) -- ([yshift=-0.30*\bsep-0.25cm]h12.center);
\node [tnode,anchor=center] (ha5) at ([yshift=-0.55*\bsep]h5.center) {$c_0$};
\node [tnode,anchor=center] (ha6) at ([yshift=-0.55*\bsep]h6.center) {$c_1$};
\node [tnode,anchor=center] (ha7) at ([yshift=-0.55*\bsep]h7.center) {$\cdots$};
\node [tnode,anchor=center] (ha8) at ([yshift=-0.55*\bsep]h8.center) {$c_{m'}$};
\node [tnode,anchor=center] (ha9) at ([yshift=-0.55*\bsep]h9.center) {$z_0$};
\node [tnode,anchor=center] (ha10) at ([yshift=-0.55*\bsep]h10.center) {$z_1$};
\node [tnode,anchor=center] (ha11) at ([yshift=-0.55*\bsep]h11.center) {$\cdots$};
\node [tnode,anchor=center] (ha12) at ([yshift=-0.55*\bsep]h12.center) {$z_m$};
\begin{pgfonlayer}{background}
\node [lnode,anchor=south west,fill=red!20] (soft) at ([xshift=-0.00cm,yshift=-0.1cm]h1.south west) {};
\node [lnode,anchor=south west,fill=ugreen!20] (hard) at ([xshift=-0.00cm,yshift=-0.1cm]h5.south west) {};
\node [lnode,anchor=south west,fill=blue!20] (input) at ([xshift=-0.00cm,yshift=-0.1cm]h9.south west) {};
\end{pgfonlayer}
\node [anchor=south] (softlabel) at (soft.north) {\small{Soft Prompt}};
\node [anchor=south] (hardlabel) at (hard.north) {\small{Hard Prompt}};
\node [anchor=south] (inputlabel) at (input.north) {\small{User Input and Response}};
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\def\ssep{0.3cm}
\def\bsep{2.0cm}
\tikzstyle{tnode} = [minimum width=0.85cm,minimum height=0.3cm,inner sep=2pt];
\tikzstyle{lnode} = [minimum width=8.2cm,minimum height=1.0cm,inner sep=2pt,draw];
\begin{scope}
\node [tnode,anchor=west] (h1) at (0,0) {};
\node [tnode,anchor=west] (h2) at ([xshift=\ssep]h1.east) {};
\node [tnode,anchor=west] (h3) at ([xshift=\ssep]h2.east) {};
\node [tnode,anchor=west] (h4) at ([xshift=\ssep]h3.east) {};
\node [tnode,anchor=west] (h5) at ([xshift=\ssep]h4.east) {};
\node [tnode,anchor=west] (h6) at ([xshift=\ssep]h5.east) {};
\node [tnode,anchor=west] (h7) at ([xshift=\ssep]h6.east) {};
\draw [<-,thin] ([yshift=-1pt]h1.south) -- ([yshift=-0.35*\bsep]h1.south);
\draw [<-,thin] ([yshift=-1pt]h2.south) -- ([yshift=-0.35*\bsep]h2.south);
\draw [<-,thin] ([yshift=-1pt]h3.south) -- ([yshift=-0.35*\bsep]h3.south);
\draw [<-,thin] ([yshift=-1pt]h4.south) -- ([yshift=-0.35*\bsep]h4.south);
\draw [<-,thin] ([yshift=-1pt]h5.south) -- ([yshift=-0.35*\bsep]h5.south);
\draw [<-,thin] ([yshift=-1pt]h6.south) -- ([yshift=-0.35*\bsep]h6.south);
\draw [<-,thin] ([yshift=-1pt]h7.south) -- ([yshift=-0.35*\bsep]h7.south);
\node [anchor=south] (hlabel) at ([yshift=-0.3cm]h4.north) {Layer $l-1$};
\node [tnode,anchor=center] (ha1) at ([yshift=0.6*\bsep]h1.center) {};
\node [tnode,anchor=center] (ha2) at ([yshift=0.6*\bsep]h2.center) {};
\node [tnode,anchor=center] (ha3) at ([yshift=0.6*\bsep]h3.center) {};
\node [tnode,anchor=center] (ha4) at ([yshift=0.6*\bsep]h4.center) {};
\node [tnode,anchor=center] (ha5) at ([yshift=0.6*\bsep]h5.center) {};
\node [tnode,anchor=center] (ha6) at ([yshift=0.6*\bsep]h6.center) {};
\node [tnode,anchor=center] (ha7) at ([yshift=0.6*\bsep]h7.center) {};
\draw [<-,thin] ([yshift=-1pt]ha1.south) -- ([yshift=-0.25*\bsep]ha1.south);
\draw [<-,thin] ([yshift=-1pt]ha2.south) -- ([yshift=-0.25*\bsep]ha2.south);
\draw [<-,thin] ([yshift=-1pt]ha3.south) -- ([yshift=-0.25*\bsep]ha3.south);
\draw [<-,thin] ([yshift=-1pt]ha4.south) -- ([yshift=-0.25*\bsep]ha4.south);
\draw [<-,thin] ([yshift=-1pt]ha5.south) -- ([yshift=-0.25*\bsep]ha5.south);
\draw [<-,thin] ([yshift=-1pt]ha6.south) -- ([yshift=-0.25*\bsep]ha6.south);
\draw [<-,thin] ([yshift=-1pt]ha7.south) -- ([yshift=-0.25*\bsep]ha7.south);
\node [anchor=south] (halabel) at ([yshift=-0.3cm]ha4.north) {Layer $l$};
\node [tnode,anchor=center] (haa1) at ([yshift=0.6*\bsep]ha1.center) {};
\node [tnode,anchor=center] (haa2) at ([yshift=0.6*\bsep]ha2.center) {};
\node [tnode,anchor=center] (haa3) at ([yshift=0.6*\bsep]ha3.center) {};
\node [tnode,anchor=center] (haa4) at ([yshift=0.6*\bsep]ha4.center) {};
\node [tnode,anchor=center] (haa5) at ([yshift=0.6*\bsep]ha5.center) {};
\node [tnode,anchor=center] (haa6) at ([yshift=0.6*\bsep]ha6.center) {};
\node [tnode,anchor=center] (haa7) at ([yshift=0.6*\bsep]ha7.center) {};
\draw [<-,thin] ([yshift=-1pt]haa1.south) -- ([yshift=-0.25*\bsep]haa1.south);
\draw [<-,thin] ([yshift=-1pt]haa2.south) -- ([yshift=-0.25*\bsep]haa2.south);
\draw [<-,thin] ([yshift=-1pt]haa3.south) -- ([yshift=-0.25*\bsep]haa3.south);
\draw [<-,thin] ([yshift=-1pt]haa4.south) -- ([yshift=-0.25*\bsep]haa4.south);
\draw [<-,thin] ([yshift=-1pt]haa5.south) -- ([yshift=-0.25*\bsep]haa5.south);
\draw [<-,thin] ([yshift=-1pt]haa6.south) -- ([yshift=-0.25*\bsep]haa6.south);
\draw [<-,thin] ([yshift=-1pt]haa7.south) -- ([yshift=-0.25*\bsep]haa7.south);
\node [anchor=south] (haalabel) at ([yshift=-0.3cm]haa4.north) {Layer $l+1$};
\node [tnode,anchor=center] (b1) at ([yshift=-0.55*\bsep]h1.center) {\small{$\cdots$}};
\node [tnode,anchor=center] (b2) at ([yshift=-0.55*\bsep]h2.center) {\small{$\cdots$}};
\node [tnode,anchor=center] (b3) at ([yshift=-0.55*\bsep]h3.center) {\small{$\cdots$}};
\node [tnode,anchor=center] (b4) at ([yshift=-0.55*\bsep]h4.center) {\small{$\cdots$}};
\node [tnode,anchor=center] (b5) at ([yshift=-0.55*\bsep]h5.center) {\small{$\cdots$}};
\node [tnode,anchor=center] (b6) at ([yshift=-0.55*\bsep]h6.center) {\small{$\cdots$}};
\node [tnode,anchor=center] (b7) at ([yshift=-0.55*\bsep]h7.center) {\small{$\cdots$}};
\draw [<-,thin] ([yshift=-2pt]b1.south) -- ([yshift=-0.25*\bsep]b1.south);
\draw [<-,thin] ([yshift=-2pt]b2.south) -- ([yshift=-0.25*\bsep]b2.south);
\draw [<-,thin] ([yshift=-2pt]b3.south) -- ([yshift=-0.25*\bsep]b3.south);
\draw [<-,thin] ([yshift=-2pt]b4.south) -- ([yshift=-0.25*\bsep]b4.south);
\draw [<-,thin] ([yshift=-2pt]b5.south) -- ([yshift=-0.25*\bsep]b5.south);
\draw [<-,thin] ([yshift=-2pt]b6.south) -- ([yshift=-0.25*\bsep]b6.south);
\draw [<-,thin] ([yshift=-2pt]b7.south) -- ([yshift=-0.25*\bsep]b7.south);
\node [tnode,anchor=center] (e1) at ([yshift=-0.5*\bsep]b1.center) {\small{\color{red} $\mathbf{p}_0$}};
\node [tnode,anchor=center] (e2) at ([yshift=-0.5*\bsep]b2.center) {\small{\color{red} $\mathbf{p}_1$}};
\node [tnode,anchor=center] (e3) at ([yshift=-0.5*\bsep]b3.center) {\small{$\mathbf{e}_0$}};
\node [tnode,anchor=center] (e4) at ([yshift=-0.5*\bsep]b4.center) {\small{$\mathbf{e}_1$}};
\node [tnode,anchor=center] (e5) at ([yshift=-0.5*\bsep]b5.center) {\small{$\mathbf{e}_2$}};
\node [tnode,anchor=center] (e6) at ([yshift=-0.5*\bsep]b6.center) {\small{$\mathbf{e}_3$}};
\node [tnode,anchor=center] (e7) at ([yshift=-0.5*\bsep]b7.center) {\small{$\mathbf{e}_4$}};
\draw [<-,thin] ([yshift=-2pt]e3.south) -- ([yshift=-0.25*\bsep]e3.south);
\draw [<-,thin] ([yshift=-2pt]e4.south) -- ([yshift=-0.25*\bsep]e4.south);
\draw [<-,thin] ([yshift=-2pt]e5.south) -- ([yshift=-0.25*\bsep]e5.south);
\draw [<-,thin] ([yshift=-2pt]e6.south) -- ([yshift=-0.25*\bsep]e6.south);
\draw [<-,thin] ([yshift=-2pt]e7.south) -- ([yshift=-0.25*\bsep]e7.south);
\node [tnode,anchor=center] (o1) at ([yshift=0.55*\bsep]haa1.center) {\small{$\cdots$}};
\node [tnode,anchor=center] (o2) at ([yshift=0.55*\bsep]haa2.center) {\small{$\cdots$}};
\node [tnode,anchor=center] (o3) at ([yshift=0.55*\bsep]haa3.center) {\small{$\cdots$}};
\node [tnode,anchor=center] (o4) at ([yshift=0.55*\bsep]haa4.center) {\small{$\cdots$}};
\node [tnode,anchor=center] (o5) at ([yshift=0.55*\bsep]haa5.center) {\small{$\cdots$}};
\node [tnode,anchor=center] (o6) at ([yshift=0.55*\bsep]haa6.center) {\small{$\cdots$}};
\node [tnode,anchor=center] (o7) at ([yshift=0.55*\bsep]haa7.center) {\small{$\cdots$}};
\draw [<-,thin] ([yshift=-2pt]o1.south) -- ([yshift=-0.25*\bsep]o1.south);
\draw [<-,thin] ([yshift=-2pt]o2.south) -- ([yshift=-0.25*\bsep]o2.south);
\draw [<-,thin] ([yshift=-2pt]o3.south) -- ([yshift=-0.25*\bsep]o3.south);
\draw [<-,thin] ([yshift=-2pt]o4.south) -- ([yshift=-0.25*\bsep]o4.south);
\draw [<-,thin] ([yshift=-2pt]o5.south) -- ([yshift=-0.25*\bsep]o5.south);
\draw [<-,thin] ([yshift=-2pt]o6.south) -- ([yshift=-0.25*\bsep]o6.south);
\draw [<-,thin] ([yshift=-2pt]o7.south) -- ([yshift=-0.25*\bsep]o7.south);
\node [tnode,anchor=center] (l6) at ([yshift=0.55*\bsep]o6.center) {\small{\color{blue} Loss}};
\node [tnode,anchor=center] (l7) at ([yshift=0.55*\bsep]o7.center) {\small{\color{blue} Loss}};
\draw [<-,thin] ([yshift=-3pt]l6.south) -- ([yshift=-0.27*\bsep]l6.south);
\draw [<-,thin] ([yshift=-3pt]l7.south) -- ([yshift=-0.27*\bsep]l7.south);
\node [tnode,anchor=center] (t3) at ([yshift=-0.50*\bsep]e3.center) {\small{Look}};
\node [tnode,anchor=center] (t4) at ([yshift=-0.50*\bsep]e4.center) {\small{out}};
\node [tnode,anchor=center] (t5) at ([yshift=-0.50*\bsep]e5.center) {\small{!}};
\node [tnode,anchor=center] (t6) at ([yshift=-0.50*\bsep]e6.center) {\small{ 小心 }};
\node [tnode,anchor=center] (t7) at ([yshift=-0.50*\bsep]e7.center) {\small{!}};
\begin{pgfonlayer}{background}
\node [lnode,anchor=south west,fill=gray!20] (layerl) at ([xshift=-0.2cm,yshift=-0.2cm]h1.south west) {};
\node [lnode,anchor=south west,fill=gray!20] (layeral) at ([xshift=-0.2cm,yshift=-0.2cm]ha1.south west) {};
\node [lnode,anchor=south west,fill=gray!20] (layeraal) at ([xshift=-0.2cm,yshift=-0.2cm]haa1.south west) {};
\node [draw,dotted,inner sep=2pt,fill=red!10] [fit = (e1) (e2)] (pbox) {};
\end{pgfonlayer}
\node [anchor=east] (plabel) at ([xshift=-0.5cm]pbox.west) {\small{trainable prompt}};
\node [anchor=north west] (plabel2) at ([yshift=0.1cm]plabel.south west) {\small{embeddings}};
\draw [->] ([xshift=2pt]plabel.east) -- ([xshift=0.5cm]plabel.east);
\draw [decoration={brace,amplitude=4pt},decorate,thick] ([yshift=-0.5cm]t5.east) -- ([yshift=-0.5cm]t3.west) node [pos=0.5,below,yshift=-0.2cm] (inputlabel) {\small{User Input}};
\draw [decoration={brace,amplitude=4pt},decorate,thick] ([yshift=-0.5cm]t7.east) -- ([yshift=-0.5cm]t6.west) node [pos=0.5,below,yshift=-0.2cm] (inputlabel) {\small{LLM Prediction}};
\draw [decoration={brace,amplitude=4pt},decorate,thick] ([yshift=-0.5cm,xshift=-0.4cm]t3.west) -- ([yshift=-0.5cm,xshift=-2.5cm]t3.west) node [pos=0.5,below,yshift=-0.2cm] (inputlabel) {\small{Soft Prompt}};
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\def\ssep{0.35cm}
\def\bsep{2.0cm}
\tikzstyle{enode} = [minimum width=0.35cm,minimum height=0.35cm,inner sep=2pt,draw];
\tikzstyle{tnode} = [minimum width=4.4cm,minimum height=1.3cm,inner sep=2pt,draw,thick,fill=gray!30];
\begin{scope}
\node [enode,anchor=west,draw=white] (e0) at (0,0) {};
\node [enode,anchor=west,draw=white] (e1) at ([xshift=\ssep]e0.east) {};
\node [enode,anchor=west,fill=gray!30] (e2) at ([xshift=\ssep]e1.east) {};
\node [enode,anchor=west,fill=gray!30] (e3) at ([xshift=\ssep]e2.east) {};
\node [enode,anchor=west,fill=gray!30] (e4) at ([xshift=\ssep]e3.east) {};
\node [enode,anchor=west,fill=gray!30] (e5) at ([xshift=\ssep]e4.east) {};
\node [tnode,anchor=south west] (model) at ([yshift=\ssep,xshift=-0.8*\ssep]e0.north west) {};
\node [anchor=east] (modellabel) at ([xshift=0pt]model.west) {\footnotesize{LLM}};
\draw [->,white] ([yshift=1pt]e0.north) -- ([yshift=\ssep-1pt]e0.north);
\draw [->,white] ([yshift=1pt]e1.north) -- ([yshift=\ssep-1pt]e1.north);
\draw [->] ([yshift=1pt]e2.north) -- ([yshift=\ssep-1pt]e2.north);
\draw [->] ([yshift=1pt]e3.north) -- ([yshift=\ssep-1pt]e3.north);
\draw [->] ([yshift=1pt]e4.north) -- ([yshift=\ssep-1pt]e4.north);
\draw [->] ([yshift=1pt]e5.north) -- ([yshift=\ssep-1pt]e5.north);
\node [enode,anchor=south,draw=blue,fill=blue!30] (p0) at ([yshift=1.5*\ssep]e0.north) {};
\node [enode,anchor=south,draw=blue,fill=blue!30] (p1) at ([yshift=1.5*\ssep]e1.north) {};
\node [enode,anchor=south,draw=blue,fill=blue!30] (p01) at ([yshift=3.0*\ssep]e0.north) {};
\node [enode,anchor=south,draw=blue,fill=blue!30] (p11) at ([yshift=3.0*\ssep]e1.north) {};
\node [anchor=north] (caption) at ([yshift=-0.3cm]e2.south) {\small{(a) Soft Prompts as Prefixes}};
\end{scope}
\begin{scope}[xshift=6.5cm]
\node [enode,anchor=west,draw=blue,fill=blue!30] (e0) at (0,0) {};
\node [enode,anchor=west,draw=blue,fill=blue!30] (e1) at ([xshift=\ssep]e0.east) {};
\node [enode,anchor=west,fill=gray!30] (e2) at ([xshift=\ssep]e1.east) {};
\node [enode,anchor=west,fill=gray!30] (e3) at ([xshift=\ssep]e2.east) {};
\node [enode,anchor=west,fill=gray!30] (e4) at ([xshift=\ssep]e3.east) {};
\node [enode,anchor=west,fill=gray!30] (e5) at ([xshift=\ssep]e4.east) {};
\node [tnode,anchor=south west] (model) at ([yshift=\ssep,xshift=-0.8*\ssep]e0.north west) {};
\node [anchor=east] (modellabel) at ([xshift=0pt]model.west) {\footnotesize{LLM}};
\draw [->] ([yshift=1pt]e0.north) -- ([yshift=\ssep-1pt]e0.north);
\draw [->] ([yshift=1pt]e1.north) -- ([yshift=\ssep-1pt]e1.north);
\draw [->] ([yshift=1pt]e2.north) -- ([yshift=\ssep-1pt]e2.north);
\draw [->] ([yshift=1pt]e3.north) -- ([yshift=\ssep-1pt]e3.north);
\draw [->] ([yshift=1pt]e4.north) -- ([yshift=\ssep-1pt]e4.north);
\draw [->] ([yshift=1pt]e5.north) -- ([yshift=\ssep-1pt]e5.north);
\node [anchor=north] (caption) at ([yshift=-0.3cm]e2.south) {\small{(b) Soft Prompts as Inputs (Embeddings)}};
\end{scope}
\begin{scope}[yshift=-4cm]
\node [enode,anchor=west,fill=gray!30] (e0) at (0,0) {};
\node [enode,anchor=west,fill=gray!30] (e1) at ([xshift=\ssep]e0.east) {};
\node [enode,anchor=west,fill=gray!30] (e2) at ([xshift=\ssep]e1.east) {};
\node [enode,anchor=west,fill=gray!30] (e3) at ([xshift=\ssep]e2.east) {};
\node [enode,anchor=west,fill=gray!30] (e4) at ([xshift=\ssep]e3.east) {};
\node [enode,anchor=west,fill=gray!30] (e5) at ([xshift=\ssep]e4.east) {};
\node [tnode,anchor=south west] (model) at ([yshift=\ssep,xshift=-0.8*\ssep]e0.north west) {};
\node [anchor=east] (modellabel) at ([xshift=0pt]model.west) {\footnotesize{LLM}};
\draw [->] ([yshift=1pt]e0.north) -- ([yshift=\ssep-1pt]e0.north);
\draw [->] ([yshift=1pt]e1.north) -- ([yshift=\ssep-1pt]e1.north);
\draw [->] ([yshift=1pt]e2.north) -- ([yshift=\ssep-1pt]e2.north);
\draw [->] ([yshift=1pt]e3.north) -- ([yshift=\ssep-1pt]e3.north);
\draw [->] ([yshift=1pt]e4.north) -- ([yshift=\ssep-1pt]e4.north);
\draw [->] ([yshift=1pt]e5.north) -- ([yshift=\ssep-1pt]e5.north);
\node [anchor=west,minimum width=4.2cm,minimum height=0.3cm,fill=blue!30,draw=blue,inner sep=2pt] (layer) at ([xshift=0.1cm,yshift=0.2cm]model.west) {\scriptsize{Layer}};
\node [anchor=north] (caption) at ([yshift=-0.3cm]e2.south) {\small{(c) Fine-tuning Parts of the Model}};
\end{scope}
\begin{scope}[yshift=-4cm,xshift=6.5cm]
\node [enode,anchor=west,fill=gray!30] (e0) at (0,0) {};
\node [enode,anchor=west,fill=gray!30] (e1) at ([xshift=\ssep]e0.east) {};
\node [enode,anchor=west,fill=gray!30] (e2) at ([xshift=\ssep]e1.east) {};
\node [enode,anchor=west,fill=gray!30] (e3) at ([xshift=\ssep]e2.east) {};
\node [enode,anchor=west,fill=gray!30] (e4) at ([xshift=\ssep]e3.east) {};
\node [enode,anchor=west,fill=gray!30] (e5) at ([xshift=\ssep]e4.east) {};
\node [tnode,anchor=south west] (model) at ([yshift=\ssep,xshift=-0.8*\ssep]e0.north west) {};
\node [anchor=east] (modellabel) at ([xshift=0pt]model.west) {\footnotesize{LLM}};
\draw [->] ([yshift=1pt]e0.north) -- ([yshift=\ssep-1pt]e0.north);
\draw [->] ([yshift=1pt]e1.north) -- ([yshift=\ssep-1pt]e1.north);
\draw [->] ([yshift=1pt]e2.north) -- ([yshift=\ssep-1pt]e2.north);
\draw [->] ([yshift=1pt]e3.north) -- ([yshift=\ssep-1pt]e3.north);
\draw [->] ([yshift=1pt]e4.north) -- ([yshift=\ssep-1pt]e4.north);
\draw [->] ([yshift=1pt]e5.north) -- ([yshift=\ssep-1pt]e5.north);
\node [anchor=west,minimum width=0.5cm,minimum height=0.9cm,fill=blue!30,draw=blue] (adaptor) at ([xshift=0.6cm]model.east) {};
\node [anchor=west,rotate=90] (adaptorlabel) at ([xshift=0.2cm,yshift=-0.5cm]adaptor.east) {\tiny{Adaptor}};
\draw [<-] ([xshift=-1pt,yshift=-0.2cm]adaptor.west) -- ([xshift=1pt,yshift=-0.5cm]model.east);
\draw [->] ([xshift=-1pt,yshift=0.2cm]adaptor.west) -- ([xshift=1pt,yshift=0.5cm]model.east);
\node [anchor=north] (caption) at ([yshift=-0.3cm]e2.south) {\small{(d) Fine-tuning the Adaptor}};
\end{scope}
\end{tikzpicture}
\begin{center}
\begin{tikzpicture}
\def\ssep{1.9cm}
\tikzstyle{snode} = [minimum width=2.5cm,minimum height=1.5cm,inner sep=2pt,draw,thick,fill=white];
\begin{scope}
\node [snode,anchor=west] (step0) at (0,0) {Initialization};
\node [anchor=north west,align=left] (step0text) at ([xshift=0.5cm]step0.north east) {\small{Initialize the task pool with a number of instructions}\\[-0.1cm] \small{and corresponding input-output samples.}};
\node [anchor=north west] (step0sample1) at ([yshift=-0.1cm]step0text.south west) {\scriptsize{Sample 1:}};
\node [anchor=west,fill=gray!30,inner sep=3pt] (step0sample1text) at (step0sample1.east) {\scriptsize{$(\mathrm{Instruction},\mathrm{User}\text{-}\mathrm{input},\mathrm{Output})$}};
\node [anchor=north west] (step0sample2) at ([yshift=-2pt]step0sample1.south west) {\scriptsize{Sample 2:}};
\node [anchor=west,fill=gray!30,inner sep=3pt] (step0sample2text) at (step0sample2.east) {\scriptsize{$(\mathrm{Instruction},\mathrm{User}\text{-}\mathrm{input},\mathrm{Output})$}};
\node [anchor=north] (step0sample3) at ([yshift=-2pt]step0sample2.south) {\scriptsize{$\cdots$}};
\draw [decoration={brace,amplitude=4pt},decorate] ([xshift=0.2cm]step0sample1text.north east) -- ([xshift=0.2cm,yshift=-1.5cm]step0sample1text.north east);
\node [anchor=west,minimum width=1.3cm,minimum height=0.7cm,draw,fill=white,drop shadow] (pool) at ([xshift=0.5cm,yshift=0.1cm]step0sample2text.east) {\footnotesize{Task Pool}};
\node [snode,anchor=north west] (step1) at ([yshift=-\ssep]step0.south west) {Sampling};
\node [anchor=north west,align=left] (step1text) at ([xshift=0.5cm]step1.north east) {\small{Draw a few instructions from the pool}};
\node [anchor=north west,fill=gray!30,inner sep=3pt] (step1instruction1) at ([yshift=-0.1cm,xshift=0.1cm]step1text.south west) {\scriptsize{$\mathrm{Instruction}_a$}};
\node [anchor=north west,fill=gray!30,inner sep=3pt] (step1instruction2) at ([yshift=-3pt]step1instruction1.south west) {\scriptsize{$\mathrm{Instruction}_b$}};
\node [anchor=north west,fill=gray!30,inner sep=3pt] (step1instruction3) at ([yshift=-3pt]step1instruction2.south west) {\scriptsize{$\mathrm{Instruction}_c$}};
\node [anchor=west,minimum width=1.3cm,minimum height=0.7cm,draw,fill=white,drop shadow] (pool) at ([xshift=2.0cm,yshift=0pt]step1instruction2.east) {\footnotesize{Task Pool}};
\draw [->,thick] ([xshift=-3pt]pool.west) -- ([xshift=-1.85cm]pool.west) node [pos=0.5,above] {\scriptsize{sampling}};
\node [snode,anchor=north west,align=left] (step2) at ([yshift=-\ssep]step1.south west) {Instruction\\Generation};
\node [anchor=north west,align=left] (step2text) at ([xshift=0.5cm,yshift=0.2cm]step2.north east) {\small{Prompt the LLM to generate a new instruction based on}\\[-0.1cm] \small{the drawn instructions.}};
\node [anchor=north west,minimum width=5cm,align=left,draw,thin,rounded corners=0.3cm] (prompt1) at ([yshift=-0.1cm,xshift=0.15cm]step2text.south west) {\tiny{\texttt{You are provided several different instructions for performing}}\\ [-0.25cm] \tiny{\texttt{some tasks. Please generate an instruction based on these.}} \\ [-0.2cm] \tiny{\texttt{Task 1:} \ctext[rgb]{0.85, 0.85, 0.85}{$\mathrm{Instruction}_a$}} \\ [-0.2cm] \tiny{\texttt{Task 2:} \ctext[rgb]{0.85, 0.85, 0.85}{$\mathrm{Instruction}_b$}} \\ [-0.2cm] \tiny{\texttt{Task 3:} \ctext[rgb]{0.85, 0.85, 0.85}{$\mathrm{Instruction}_c$}} \\ [-0.2cm] \tiny{\texttt{New Task:} \underline{\ctext[RGB]{255,204,204}{$\mathrm{Instruction}_{\mathrm{new}}$}}}};
\node [snode,anchor=north west,align=left] (step3) at ([yshift=-\ssep-0.2cm]step2.south west) {Sample\\Generation};
\node [anchor=north west,align=left] (step3text) at ([xshift=0.5cm,yshift=0.1cm]step3.north east) {\small{Given the newly-generated instruction and a few}\\[-0.1cm] \small{input-output samples, generate a new sample.}};
\node [anchor=north west,minimum width=8.1cm,align=left,draw,thin,rounded corners=0.3cm] (prompt2) at ([yshift=-0.1cm,xshift=0.15cm]step3text.south west) {\tiny{\texttt{You are provided with a set of input-output samples tasks, }}\\ [-0.25cm] \tiny{\texttt{each composed of an instruction, a user input, and an output.}} \\ [-0.25cm] \tiny{\texttt{Please generate a new sample based on these.}} \\ [-0.2cm] \tiny{\texttt{Sample 1:} \ctext[rgb]{0.85, 0.85, 0.85}{$\mathrm{Sample}_a$}} \\ [-0.2cm] \tiny{\texttt{Sample 2:} \ctext[rgb]{0.85, 0.85, 0.85}{$\mathrm{Sample}_b$}} \\ [-0.2cm] \tiny{\texttt{New Sample:} \ctext[RGB]{255,204,204}{$\mathrm{Instruction}_{\mathrm{new}}$} \ctext[RGB]{153,204,255}{$\mathrm{User\text{-}input}_{\mathrm{new}}$} \ctext[RGB]{153,204,255}{$\mathrm{Output}_{\mathrm{new}}$}}};
\node [snode,anchor=north west] (step4) at ([yshift=-\ssep-0.2cm]step3.south west) {Filtering};
\node [anchor=north west,align=left] (step4text) at ([xshift=0.5cm,yshift=0.1cm]step4.north east) {\small{Filter out invalid and low-quality samples.} \\[-0.1cm] \small{Add the remaining samples into the pool.}};
\draw [->,thick] ([yshift=-2pt]step0.south) -- ([yshift=2pt]step1.north);
\draw [->,thick] ([yshift=-2pt]step1.south) -- ([yshift=2pt]step2.north);
\draw [->,thick] ([yshift=-2pt]step2.south) -- ([yshift=2pt]step3.north);
\draw [->,thick] ([yshift=-2pt]step3.south) -- ([yshift=2pt]step4.north);
\draw [->,thick] ([yshift=-2pt]step4.south) -- ([yshift=-0.5cm]step4.south) -- ([yshift=-0.5cm,xshift=-0.8cm]step4.south west) -- ([xshift=-0.8cm]step1.west) -- ([xshift=-2pt]step1.west);
\end{scope}
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}
\def\ssep{1.0cm}
\tikzstyle{snode} = [circle,minimum size=0.2cm,inner sep=0,draw=white,line width=0.02cm,fill=black];
\begin{scope}
\node [snode,anchor=west] (s01) at (0,0) {};
\node [snode,anchor=west] (s02) at ([xshift=\ssep]s01.east) {};
\node [snode,anchor=west] (s03) at ([xshift=\ssep]s02.east) {};
\node [snode,anchor=west] (s04) at ([xshift=\ssep]s03.east) {};
\node [snode,anchor=west] (s05) at ([xshift=\ssep]s04.east) {};
\node [snode,anchor=south] (s11) at ([yshift=0.8*\ssep]s01.north) {};
\node [snode,anchor=west] (s12) at ([xshift=\ssep]s11.east) {};
\node [snode,anchor=west] (s13) at ([xshift=\ssep]s12.east) {};
\node [snode,anchor=west] (s14) at ([xshift=\ssep]s13.east) {};
\node [snode,anchor=west] (s15) at ([xshift=\ssep]s14.east) {};
\draw [-{Stealth[length=1.3mm]}] (s01.90) -- (s11.-90);
\draw [-{Stealth[length=1.3mm]}] (s01.75) -- (s12.-110);
\draw [-{Stealth[length=1.3mm]}] (s01.60) -- (s13.-130);
\draw [-{Stealth[length=1.3mm]}] (s01.45) -- (s14.-150);
\draw [-{Stealth[length=1.3mm]}] (s01.30) -- (s15.-170);
\draw [-{Stealth[length=1.3mm]}] (s02.90) -- (s12.-90);
\draw [-{Stealth[length=1.3mm]}] (s02.75) -- (s13.-110);
\draw [-{Stealth[length=1.3mm]}] (s02.60) -- (s14.-130);
\draw [-{Stealth[length=1.3mm]}] (s02.45) -- (s15.-150);
\draw [-{Stealth[length=1.3mm]}] (s03.90) -- (s13.-90);
\draw [-{Stealth[length=1.3mm]}] (s03.75) -- (s14.-110);
\draw [-{Stealth[length=1.3mm]}] (s03.60) -- (s15.-130);
\draw [-{Stealth[length=1.3mm]}] (s04.90) -- (s14.-90);
\draw [-{Stealth[length=1.3mm]}] (s04.75) -- (s15.-110);
\draw [-{Stealth[length=1.3mm]}] (s05.90) -- (s15.-90);
\node [anchor=north] (w1) at ([yshift=-0.2cm]s01.south) {\footnotesize{$x_0$}};
\node [anchor=north] (w2) at ([yshift=-0.2cm]s02.south) {\footnotesize{$x_1$}};
\node [anchor=north] (w3) at ([yshift=-0.2cm]s03.south) {\footnotesize{$x_2$}};
\node [anchor=north] (w4) at ([yshift=-0.2cm]s04.south) {\footnotesize{$x_3$}};
\node [anchor=north] (w5) at ([yshift=-0.2cm]s05.south) {\footnotesize{$y_1$}};
\node [anchor=south] (o1) at ([yshift=0.2cm]s11.north) {\footnotesize{$x_1$}};
\node [anchor=south] (o2) at ([yshift=0.2cm]s12.north) {\footnotesize{$x_2$}};
\node [anchor=south] (o3) at ([yshift=0.2cm]s13.north) {\footnotesize{$x_3$}};
\node [anchor=south] (o4) at ([yshift=0.2cm]s14.north) {\footnotesize{$y_1$}};
\node [anchor=south] (o5) at ([yshift=0.2cm]s15.north) {\footnotesize{$y_2$}};
\begin{pgfonlayer}{background}
\node [anchor=west,fill=gray!40,minimum width=3.2cm,minimum height=0.5cm] (xbox) at ([xshift=-0.1cm]o1.west) {};
\node [anchor=west,fill=blue!30,minimum width=2.0cm,minimum height=0.5cm] (ybox) at ([xshift=-0.1cm]o4.west) {};
\end{pgfonlayer}
\node [anchor=south] (xlabel) at (xbox.north) {\small{Input}};
\node [anchor=south] (ylabel) at (ybox.north) {\small{Output}};
\node [anchor=north] (caption) at ([yshift=-0.2cm]w3.south) {\small{(a) Forward Pass}};
\end{scope}
\begin{scope}[xshift=7cm]
\node [snode,anchor=west] (s01) at (0,0) {};
\node [snode,anchor=west] (s02) at ([xshift=\ssep]s01.east) {};
\node [snode,anchor=west] (s03) at ([xshift=\ssep]s02.east) {};
\node [snode,anchor=west] (s04) at ([xshift=\ssep]s03.east) {};
\node [snode,anchor=west] (s05) at ([xshift=\ssep]s04.east) {};
\node [snode,anchor=south,fill=gray!50] (s11) at ([yshift=0.8*\ssep]s01.north) {};
\node [snode,anchor=west,fill=gray!50] (s12) at ([xshift=\ssep]s11.east) {};
\node [snode,anchor=west,fill=gray!50] (s13) at ([xshift=\ssep]s12.east) {};
\node [snode,anchor=west,fill=blue] (s14) at ([xshift=\ssep]s13.east) {};
\node [snode,anchor=west,fill=blue] (s15) at ([xshift=\ssep]s14.east) {};
\draw [{Stealth[length=1.3mm]}-,gray!30] (s01.90) -- (s11.-90);
\draw [{Stealth[length=1.3mm]}-,gray!30] (s01.75) -- (s12.-110);
\draw [{Stealth[length=1.3mm]}-,gray!30] (s01.60) -- (s13.-130);
\draw [{Stealth[length=1.3mm]}-,gray!30] (s02.90) -- (s12.-90);
\draw [{Stealth[length=1.3mm]}-,gray!30] (s02.75) -- (s13.-110);
\draw [{Stealth[length=1.3mm]}-,gray!30] (s03.90) -- (s13.-90);
\draw [{Stealth[length=1.3mm]}-,blue] (s01.45) -- (s14.-150);
\draw [{Stealth[length=1.3mm]}-,blue] (s01.30) -- (s15.-170);
\draw [{Stealth[length=1.3mm]}-,blue] (s02.60) -- (s14.-130);
\draw [{Stealth[length=1.3mm]}-,blue] (s02.45) -- (s15.-150);
\draw [{Stealth[length=1.3mm]}-,blue] (s03.75) -- (s14.-110);
\draw [{Stealth[length=1.3mm]}-,blue] (s03.60) -- (s15.-130);
\draw [{Stealth[length=1.3mm]}-,blue] (s04.90) -- (s14.-90);
\draw [{Stealth[length=1.3mm]}-,blue] (s04.75) -- (s15.-110);
\draw [{Stealth[length=1.3mm]}-,blue] (s05.90) -- (s15.-90);
\node [anchor=north] (w1) at ([yshift=-0.2cm]s01.south) {\footnotesize{$x_0$}};
\node [anchor=north] (w2) at ([yshift=-0.2cm]s02.south) {\footnotesize{$x_1$}};
\node [anchor=north] (w3) at ([yshift=-0.2cm]s03.south) {\footnotesize{$x_2$}};
\node [anchor=north] (w4) at ([yshift=-0.2cm]s04.south) {\footnotesize{$x_3$}};
\node [anchor=north] (w5) at ([yshift=-0.2cm]s05.south) {\footnotesize{$y_1$}};
\node [anchor=south] (o1) at ([yshift=0.2cm]s11.north) {\footnotesize{$x_1$}};
\node [anchor=south] (o2) at ([yshift=0.2cm]s12.north) {\footnotesize{$x_2$}};
\node [anchor=south] (o3) at ([yshift=0.2cm]s13.north) {\footnotesize{$x_3$}};
\node [anchor=south] (o4) at ([yshift=0.2cm]s14.north) {\footnotesize{$y_1$}};
\node [anchor=south] (o5) at ([yshift=0.2cm]s15.north) {\footnotesize{$y_2$}};
\begin{pgfonlayer}{background}
\node [anchor=west,fill=gray!40,minimum width=3.2cm,minimum height=0.5cm] (xbox) at ([xshift=-0.1cm]o1.west) {};
\node [anchor=west,fill=blue!30,minimum width=2.0cm,minimum height=0.5cm] (ybox) at ([xshift=-0.1cm]o4.west) {};
\end{pgfonlayer}
\node [anchor=south] (xlabel) at ([yshift=0.08cm]xbox.north) {\small{$\mathrm{Loss} = 0$}};
\node [anchor=south] (ylabel) at (ybox.north) {\small{$\mathrm{Loss} \ne 0$}};
\node [anchor=north] (caption) at ([yshift=-0.2cm]w3.south) {\small{(b) Backward Pass}};
\end{scope}
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}
\def\ssep{1.0cm}
\tikzstyle{snode} = [minimum width=7.6cm,minimum height=0.7cm,inner sep=0,draw,fill=white];
\begin{scope}
\node [snode,anchor=west] (x1) at (0,0) {};
\node [anchor=west] (x1c) at ([xshift=0.1cm]x1.west) {\footnotesize{{\color{gray} User:} I've been feeling very tired lately.}};
\node [snode,anchor=north west,minimum height=1.15cm] (y1) at ([yshift=-0.5cm,xshift=0.5cm]x1.south) {};
\node [anchor=north west] (y1c) at ([xshift=0.1cm,yshift=-0.1cm]y1.north west) {\footnotesize{{\color{blue} Chatbot:} I'm sorry to hear that. Besides feeling tired,}};
\node [anchor=north west] (y1c2) at ([xshift=1.2cm,yshift=0.1cm]y1c.south west) {\footnotesize{have you noticed any other symptoms?}};
\node [snode,anchor=north west] (x2) at ([yshift=-2.2cm]x1.south west) {};
\node [anchor=west] (x2c) at ([xshift=0.1cm]x2.west) {\footnotesize{{\color{gray} User:} Yes, I'm also experiencing headaches frequently.}};
\node [snode,anchor=north west] (y2) at ([yshift=-1.8cm]y1.south west) {};
\node [anchor=west] (y2c) at ([xshift=0.1cm]y2.west) {\footnotesize{{\color{blue} Chatbot:} How long have these symptoms been going on?}};
\node [anchor=north] (dots) at ([yshift=-0.1cm,xshift=1cm]y2.south west) {$\vdots$};
\node [anchor=south west,minimum width=3cm,minimum height=0.6cm,fill=gray!50] (x1box) at ([yshift=1.5cm]x1.north west) {\small{$\mathbf{x}^1$}};
\node [anchor=west,minimum width=3cm,minimum height=0.6cm,fill=blue!30] (y1box) at ([xshift=1pt]x1box.east) {\small{$\mathbf{y}^1$}};
\node [anchor=west,minimum width=3cm,minimum height=0.6cm,fill=gray!50] (x2box) at ([xshift=1pt]y1box.east) {\small{$\mathbf{x}^2$}};
\node [anchor=west,minimum width=3cm,minimum height=0.6cm,fill=blue!30] (y2box) at ([xshift=1pt]x2box.east) {\small{$\mathbf{y}^2$}};
\node [anchor=west] (xdots) at ([xshift=0.3cm]y2box.east) {$\cdots$};
\begin{pgfonlayer}{background}
\draw [->] ([yshift=-1pt,xshift=0.2cm]x1box.south) .. controls +(south:1cm) and +(north:1cm) .. ([yshift=1pt,xshift=0.5cm]x1.north west);
\draw [->] ([yshift=-1pt,xshift=0.0cm]x2box.south) .. controls +(south:3cm) and +(north:4cm) .. ([yshift=1pt,xshift=1.5cm]x2.north west);
\draw [->] ([yshift=-1pt,xshift=0.0cm]y1box.south) .. controls +(south:2cm) and +(north:2.5cm) .. ([yshift=1pt,xshift=0.5cm]y1.north);
\draw [->] ([yshift=-1pt,xshift=-0.0cm]y2box.south) -- ([yshift=-5.65cm,xshift=-0.0cm]y2box.south);
\end{pgfonlayer}
\node [anchor=south] (x1p) at (x1box.north) {\small{$\mathrm{Pr}_{\theta}(\mathbf{x}^1)$}};
\node [anchor=south] (y1p) at (y1box.north) {\small{$\mathrm{Pr}_{\theta}(\mathbf{y}^1|\mathbf{x}^1)$}};
\node [anchor=south] (x2p) at (x2box.north) {\small{$\mathrm{Pr}_{\theta}(\mathbf{x}^2|\mathbf{x}^1,\mathbf{y}^1)$}};
\node [anchor=south] (y2p) at (y2box.north) {\small{$\mathrm{Pr}_{\theta}(\mathbf{y}^2|\mathbf{x}^1,\mathbf{y}^1,\mathbf{x}^2)$}};
\node [anchor=south] (x1l) at ([yshift=0.2cm]x1p.north) {\small{$\mathrm{Loss} = 0$}};
\node [anchor=south] (y1l) at ([yshift=0.1cm]y1p.north) {\small{$\mathrm{Loss} \ne 0$}};
\node [anchor=south] (x2l) at ([yshift=0.2cm]x2p.north) {\small{$\mathrm{Loss} = 0$}};
\node [anchor=south] (y2l) at ([yshift=0.1cm]y2p.north) {\small{$\mathrm{Loss} \ne 0$}};
\end{scope}
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}
\def\ssep{1cm}
\def\nsize{0.3cm}
\tikzstyle{lnode} = [minimum width=7cm,minimum height=1.1cm,inner sep=2pt,draw,thick,fill=white];
\begin{scope}
\node [anchor=west] (x0) at (0,0) {\footnotesize{$x_0$}};
\node [anchor=center] (x1) at ([xshift=\ssep]x0.center) {\footnotesize{$x_1$}};
\node [anchor=center] (x2) at ([xshift=\ssep]x1.center) {\footnotesize{$x_2$}};
\node [anchor=center] (x3) at ([xshift=\ssep]x2.center) {\footnotesize{$\cdots$}};
\node [anchor=center] (x4) at ([xshift=\ssep]x3.center) {\footnotesize{$x_m$}};
\node [anchor=center] (y1) at ([xshift=\ssep]x4.center) {\footnotesize{$y_1$}};
\node [anchor=center] (y2) at ([xshift=\ssep]y1.center) {\footnotesize{$y_2$}};
\node [anchor=center] (y3) at ([xshift=\ssep]y2.center) {\footnotesize{$\cdots$}};
\node [anchor=center] (y4) at ([xshift=\ssep]y3.center) {\footnotesize{$y_n$}};
\node [anchor=north] (y4label) at ([yshift=0.1cm]y4.south) {\scriptsize{(Last Token $\langle \mathrm{EOS} \rangle$)}};
\draw [->] ([yshift=0.2cm]x0.center) -- ([yshift=0.55cm]x0.center);
\draw [->] ([yshift=0.2cm]x1.center) -- ([yshift=0.55cm]x1.center);
\draw [->] ([yshift=0.2cm]x2.center) -- ([yshift=0.55cm]x2.center);
\draw [->] ([yshift=0.2cm]x4.center) -- ([yshift=0.55cm]x4.center);
\draw [->] ([yshift=0.2cm]y1.center) -- ([yshift=0.55cm]y1.center);
\draw [->] ([yshift=0.2cm]y2.center) -- ([yshift=0.55cm]y2.center);
\draw [->] ([yshift=0.2cm]y4.center) -- ([yshift=0.55cm]y4.center);
\node [anchor=center] (ox0) at ([yshift=2.6cm]x0.center) {\footnotesize{$\mathbf{h}_{x_0}$}};
\node [anchor=center] (ox1) at ([yshift=2.6cm]x1.center) {\footnotesize{$\mathbf{h}_{x_1}$}};
\node [anchor=center] (ox2) at ([yshift=2.6cm]x2.center) {\footnotesize{$\mathbf{h}_{x_2}$}};
\node [anchor=center] (ox3) at ([yshift=2.6cm]x3.center) {\footnotesize{$\cdots$}};
\node [anchor=center] (ox4) at ([yshift=2.6cm]x4.center) {\footnotesize{$\mathbf{h}_{x_m}$}};
\node [anchor=center] (oy1) at ([yshift=2.6cm]y1.center) {\footnotesize{$\mathbf{h}_{y_1}$}};
\node [anchor=center] (oy2) at ([yshift=2.6cm]y2.center) {\footnotesize{$\mathbf{h}_{y_2}$}};
\node [anchor=center] (oy3) at ([yshift=2.6cm]y3.center) {\footnotesize{$\cdots$}};
\node [anchor=center] (oy4) at ([yshift=2.6cm]y4.center) {\footnotesize{$\mathbf{h}_{\mathrm{last}}$}};
\node [anchor=south,draw,thick,minimum width=9cm,minimum height=1.3cm] (llm) at ([yshift=0.4cm]x4.north) {\large{Transformer Decoder (LLM)}};
\node [anchor=east] (representation) at ([xshift=-0.3cm]ox0.west) {\scriptsize{Representation}};
\node [anchor=north west] (representation2) at ([yshift=0.1cm]representation.south west) {\scriptsize{at Each Position}};
\draw [<-] ([yshift=-0.25cm]ox0.center) -- ([yshift=-0.6cm]ox0.center);
\draw [<-] ([yshift=-0.25cm]ox1.center) -- ([yshift=-0.6cm]ox1.center);
\draw [<-] ([yshift=-0.25cm]ox2.center) -- ([yshift=-0.6cm]ox2.center);
\draw [<-] ([yshift=-0.25cm]ox4.center) -- ([yshift=-0.6cm]ox4.center);
\draw [<-] ([yshift=-0.25cm]oy1.center) -- ([yshift=-0.6cm]oy1.center);
\draw [<-] ([yshift=-0.25cm]oy2.center) -- ([yshift=-0.6cm]oy2.center);
\draw [<-] ([yshift=-0.25cm]oy4.center) -- ([yshift=-0.6cm]oy4.center);
\filldraw [fill=red!20,draw=white] ([xshift=-0.5cm,yshift=0.2cm]oy4.north west) -- ([xshift=0.5cm,yshift=0.2cm]oy4.north east) -- ([yshift=1cm,xshift=0]oy4.north east) -- ([yshift=1cm,xshift=0]oy4.north west) -- ([xshift=-0.5cm,yshift=0.2cm]oy4.north west);
\node [anchor=south] (reward) at ([yshift=1.2cm]oy4.north) {\footnotesize{Reward (Scalar)}};
\node [anchor=south] (Wr) at ([yshift=0.3cm]oy4.north) {\footnotesize{$\mathbf{W}_r$}};
\node [anchor=west] (linear) at ([xshift=0.5cm]Wr.east) {\scriptsize{Linear Map}};
\draw [->] ([yshift=-0.1cm]oy4.north) -- ([yshift=0.18cm]oy4.north);
\draw [<-] ([yshift=0.1cm]reward.south) -- ([yshift=-0.18cm]reward.south);
\end{scope}
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}
\def\blockwidth{10.5cm}
\def\nodewidth{2.7cm}
\def\nodeheight{1.5cm}
\def\ssep{0.9cm}
\tikzstyle{lnode} = [minimum width=\nodewidth,minimum height=\nodeheight,inner sep=2pt,draw,very thick,fill=white,rounded corners=1pt];
\begin{scope}
\node [lnode,anchor=west,align=center] (rewardmodel) at (0,0) {Reward Model\\[-0.1cm] \small{$r_{\phi}(\mathbf{x},\mathbf{y})$}};
\node [anchor=north west,draw,inner sep=3pt,fill=white] (rewardmodellabel) at ([xshift=-0.1cm,yshift=0.1cm]rewardmodel.north west) {\tiny{To Learn}};
\node [lnode,anchor=north west,align=center] (policy) at ([yshift=-2.0cm]rewardmodel.south west) {LLM Policy\\[-0.1cm] \small{$\mathrm{Pr}_{\theta}(y_t|\mathbf{x},\mathbf{y}_{<t})$}};
\node [anchor=north west,draw,inner sep=3pt,fill=white] (policylabel) at ([xshift=-0.1cm,yshift=0.1cm]policy.north west) {\tiny{To Learn}};
\node [lnode,anchor=west,align=center] (valuenet) at ([xshift=2.5cm]policy.east) {Value Function\\[-0.1cm] \small{$V_{\omega}(\mathbf{x},\mathbf{y}_{<t})$}};
\node [anchor=north west,draw,inner sep=3pt,fill=white] (valuenetlabel) at ([xshift=-0.1cm,yshift=0.1cm]valuenet.north west) {\tiny{To Learn}};
\node [lnode,anchor=east,align=center] (refmodel) at ([xshift=-0.6cm]policy.west) {Ref Model\\[-0.1cm] \small{$\mathrm{Pr}_{\theta_{\mathrm{old}}}(y_t|\mathbf{x},\mathbf{y}_{<t})$}};
\node [anchor=north west,draw,inner sep=3pt,fill=white] (refmodellabel) at ([xshift=-0.1cm,yshift=0.1cm]refmodel.north west) {\tiny{Fixed}};
\draw [->,very thick] ([xshift=1pt]refmodel.east) -- ([xshift=-1pt]policy.west);
\draw [->,very thick] ([xshift=-0.1cm,yshift=-1pt]rewardmodel.south) -- ([xshift=-0.1cm,yshift=1pt]policy.north);
\draw [->,very thick] ([xshift=-1pt]valuenet.west) -- ([xshift=1pt]policy.east);
\draw [->,very thick] ([xshift=0.3cm,yshift=-1pt]rewardmodel.south) .. controls +(south:2.0cm) and +(north:1.5cm) .. ([xshift=-0.0cm,yshift=1pt]valuenet.north);
\node [anchor=north west,align=left] (rmline) at ([xshift=0.4cm]rewardmodel.north east) {\small{Minimizing the loss based on}\\[-0.05cm] \small{the Bradley-Terry model}};
\node [anchor=north west] (rmline2) at ([yshift=-0.1cm,xshift=0.0cm]rmline.south west) {\scriptsize{$\min\limits_{\phi} -\frac{1}{|\mathcal{D}_r|} \sum\limits_{(\mathbf{x},\mathbf{y}_a,\mathbf{y}_b) \in \mathcal{D}_r} \log \sigma(r_{\phi}(\mathbf{x},\mathbf{y}_a) - r_{\phi}(\mathbf{x},\mathbf{y}_b))$}};
\begin{pgfonlayer}{background}
\node [anchor=north west,minimum width=\blockwidth,minimum height=2.6cm,draw,fill=gray!20,rounded corners=3pt] (rmtrainingbox) at ([xshift=-0.3cm,yshift=0.3cm]rewardmodel.north west) {};
\node [anchor=north west,minimum width=\blockwidth,minimum height=11.8cm,draw,fill=gray!20,rounded corners=3pt] (policytrainingbox) at ([xshift=-0.3cm,yshift=0.3cm]policy.north west) {};
\node [anchor=east,align=right] (rmboxlabel) at ([xshift=-0.2cm]rmtrainingbox.west) {\small{Reward Model}\\[-0.1cm] \small{Training}};
\node [anchor=east] (policyboxlabel) at ([xshift=-0.2cm]policytrainingbox.west) {\small{Policy Training}};
\end{pgfonlayer}
\node [anchor=south,draw,minimum width=\blockwidth,minimum height=0.7cm] (rmtrainingdata) at ([yshift=0.3cm]rmtrainingbox.north) {\small{Human preference data $\mathcal{D}_r = \{(\mathbf{x},\mathbf{y}_a, \mathbf{y}_b)\}$}};
\node [anchor=north,draw,minimum width=\blockwidth,minimum height=0.7cm] (policytrainingdata) at ([yshift=-0.3cm]policytrainingbox.south) {\small{Input-only data $\mathcal{D} = \{\mathbf{x}\}$}};
\draw [->] ([yshift=-1pt]rmtrainingdata.south) -- ([yshift=1pt]rmtrainingbox.north);
\draw [->] ([yshift=1pt]policytrainingdata.north) -- ([yshift=-1pt]policytrainingbox.south);
\node [anchor=center] (x0) at ([yshift=-4cm,xshift=0.5cm]policy.south) {\scriptsize{$x_0$}};
\node [anchor=center] (x1) at ([xshift=\ssep]x0) {\scriptsize{$x_1$}};
\node [anchor=center] (x2) at ([xshift=\ssep]x1) {\scriptsize{$\cdots$}};
\node [anchor=center] (x3) at ([xshift=\ssep]x2) {\scriptsize{$x_m$}};
\node [anchor=center] (y1) at ([xshift=\ssep]x3) {\scriptsize{$y_1$}};
\node [anchor=center] (y2) at ([xshift=\ssep]y1) {\scriptsize{$\cdots$}};
\node [anchor=center] (y3) at ([xshift=\ssep]y2) {\scriptsize{$y_{t-1}$}};
\draw [decoration={brace,amplitude=4pt},decorate,thin] ([xshift=0,yshift=-0.2cm]y3.east) -- ([xshift=0,yshift=-0.2cm]x0.west) node [pos=0.5,below,yshift=-0.1cm] {\tiny{State $(\mathbf{x},\mathbf{y}_{<t})$}};
\draw [->] ([yshift=0.2cm]x0.center) -- ([yshift=0.4cm]x0.center);
\draw [->] ([yshift=0.2cm]x1.center) -- ([yshift=0.4cm]x1.center);
\draw [->] ([yshift=0.2cm]x3.center) -- ([yshift=0.4cm]x3.center);
\draw [->] ([yshift=0.2cm]y1.center) -- ([yshift=0.4cm]y1.center);
\draw [->] ([yshift=0.2cm]y3.center) -- ([yshift=0.4cm]y3.center);
\node [anchor=center] (ox0) at ([yshift=1.85cm]x0) {\color{gray!50} \scriptsize{$x_1$}};
\node [anchor=center] (ox1) at ([xshift=\ssep]ox0) {\color{gray!50} \scriptsize{$x_2$}};
\node [anchor=center] (ox2) at ([xshift=\ssep]ox1) {\color{gray!50} \scriptsize{$\cdots$}};
\node [anchor=center] (ox3) at ([xshift=\ssep]ox2) {\color{gray!50} \scriptsize{$y_1$}};
\node [anchor=center] (oy1) at ([xshift=\ssep]ox3) {\color{gray!50} \scriptsize{$y_2$}};
\node [anchor=center] (oy2) at ([xshift=\ssep]oy1) {\color{gray!50} \scriptsize{$\cdots$}};
\node [anchor=center] (oy3) at ([xshift=\ssep]oy2) {\scriptsize{$y_t$}};
\node [anchor=west,align=left] (action) at ([xshift=0.0cm,yshift=-0.15cm]oy3.east) {\tiny{Action $y_t$}\\[-0.25cm] \tiny{(sampled with $\mathrm{Pr}_{\theta_{\mathrm{old}}}$)}};
\node [anchor=south,align=left] (evaluation) at ([yshift=0.3cm]oy2.north) {\scriptsize{Evaluate the state-action pair using the advantage}\\[-0.2cm] \scriptsize{function or the TD error (based on the reward}\\[-0.2cm] \scriptsize{model and the value function)}};
\draw [->,thin,dotted] ([xshift=-0cm,yshift=0.1cm]evaluation.south) .. controls +(south:0.3cm) and +(north:0.5cm) .. ([yshift=-0.05cm]oy3.north);
\draw [<-,gray!50] ([yshift=-0.2cm]ox0.center) -- ([yshift=-0.4cm]ox0.center);
\draw [<-,gray!50] ([yshift=-0.2cm]ox1.center) -- ([yshift=-0.4cm]ox1.center);
\draw [<-,gray!50] ([yshift=-0.2cm]ox3.center) -- ([yshift=-0.4cm]ox3.center);
\draw [<-,gray!50] ([yshift=-0.2cm]oy1.center) -- ([yshift=-0.4cm]oy1.center);
\draw [<-] ([yshift=-0.2cm]oy3.center) -- ([yshift=-0.4cm]oy3.center);
\node [anchor=south,draw,minimum width=6.5cm,minimum height=0.8cm] (llm) at ([yshift=0.3cm]x3.north) {LLM Policy};
\node [anchor=north west,align=left] (policytrainingtext) at ([yshift=-4.8cm]policy.south west) {\footnotesize{\textbf{LLM Policy}} \\ \scriptsize{Minimizing the clipped PPO loss}\\[-0.15cm] \scriptsize{with the penalty}};
\node [anchor=north west,align=left] (policytrainingtext2) at ([yshift=-0.3cm]policytrainingtext.south west) {\scriptsize{$\min\limits_{\theta} - \sum_{\mathbf{x} \in \mathcal{D}, \mathbf{y} \sim \Pr_{\theta_{\mathrm{old}}}(\cdot|\mathbf{x})} \sum_{t=1}^{T}$}\\[0.1cm] \scriptsize{$\Big[ \mathrm{Clip} \big(\frac{\mathrm{Pr}_{\theta}(y_t|\mathbf{x},\mathbf{y}_{<t})}{\mathrm{Pr}_{\theta_{\mathrm{old}}}(y_t|\mathbf{x},\mathbf{y}_{<t})} \big) A_t - $} \\[0.1cm] \hspace{0.2cm} \scriptsize{$ \beta \cdot \big( \log \mathrm{Pr}_{\theta}(y_t|\mathbf{x},\mathbf{y}_{<t}) - $}\\[-0.0cm] \scriptsize{\hspace{0.75cm} $\log \mathrm{Pr}_{\theta_{\mathrm{old}}}(y_t|\mathbf{x},\mathbf{y}_{<t}) \big) \Big]$}};
\node [anchor=north west,align=left] (valuenettrainingtext) at ([yshift=-4.8cm]valuenet.south west) {\footnotesize{\textbf{Value Function}} \\ \scriptsize{Minimizing the MSE between the}\\[-0.15cm] \scriptsize{computed return and the predicted}\\[-0.15cm] \scriptsize{state value}};
\node [anchor=north west,align=left] (valuenettrainingtext2) at (valuenettrainingtext.south west) {\scriptsize{$\min\limits_{\omega} \frac{1}{M} \sum_{\mathbf{x} \in \mathcal{D}} \sum_{t=1}^{T}$}\\[0.1cm] \scriptsize{$\big[r_t + \gamma V_{\omega}(\mathbf{x},\mathbf{y}_{< t+1}) - V_{\omega}(\mathbf{x},\mathbf{y}_{<t}) \big]^2$}};
\node [anchor=north west,align=left] (notes) at ([yshift=-0.2cm,xshift=-0.2cm]policytrainingtext2.south west) {\tiny{${\color{white}*}*$ $r_t = r(\mathbf{x},\mathbf{y}_{< t + 1})$ denotes the reward received as step $t$.}\\[-0.15cm] \tiny{$**$ $A_t$ denotes the advantage at step $t$, and can be defined as $r_t + \gamma V_{\omega}(\mathbf{x},\mathbf{y}_{<t+1}) - V_{\omega}(\mathbf{x},\mathbf{y}_{<t})$}};
\end{scope}
\end{tikzpicture}
\end{center}
\begin{tikzpicture}
\def\vstep{1.3cm}
\def\ssep{1.0cm}
\tikzstyle{snode} = [minimum width=2.5*\ssep,minimum height=1.2*\ssep,inner sep=2pt,draw,thick];
\tikzstyle{shadenode} = [minimum width=2.3*\ssep,minimum height=0.65*\ssep,inner sep=2pt,fill=gray!20];
\begin{scope}
\node [snode,anchor=west] (llm) at (0,0) {LLM};
\node [snode,anchor=south,rounded corners=3pt] (pretraindata) at ([yshift=\ssep]llm.north) {};
\node [anchor=north] (pretraindatalabel1) at (pretraindata.north) {\scriptsize{Pre-training Data}};
\node [anchor=south west] (pretraindatalabel2) at ([xshift=0.2em,yshift=0.2em]pretraindata.south west) {\tiny{I love the food here! ... }};
\node [anchor=south west] (pretraindatalabel3) at ([yshift=-0.15cm]pretraindatalabel2.north west) {\tiny{How can I get there? ...}};
\node [snode,anchor=south,rounded corners=3pt] (finetuningdata) at ([yshift=0.1cm]pretraindata.north) {};
\node [anchor=north] (finetuningdatalabel1) at (finetuningdata.north) {\scriptsize{SFT Data}};
\node [anchor=south west] (finetuningdatalabel2) at ([xshift=0.2em,yshift=0.2em]finetuningdata.south west) {\tiny{weather in London . ... }};
\node [anchor=south west] (finetuningdatalabel3) at ([yshift=-0.15cm]finetuningdatalabel2.north west) {\tiny{Write a poem about the }};
\begin{pgfonlayer}{background}
\node [shadenode,anchor=south,rounded corners=2pt] (pretraindatashade) at ([yshift=0.3em]pretraindata.south) {};
\node [shadenode,anchor=south,rounded corners=2pt] (finetuningdatashade) at ([yshift=0.3em]finetuningdata.south) {};
\end{pgfonlayer}
\draw [-{Stealth[length=3mm]},thick,double,double distance=1pt] ([yshift=-2pt]pretraindata.south) -- ([yshift=2pt]llm.north);
\node [anchor=west] (trainlabel) at ([yshift=0.6*\ssep,xshift=0.3em]llm.north) {\scriptsize{Pre-training \&}};
\node [anchor=north west] (trainlabel2) at ([yshift=0.5em]trainlabel.south west) {\scriptsize{Supervised fine-tuning}};
\node [anchor=north] (caption) at ([yshift=-0.8em]llm.south) {\small{(a) Learning an Initial LLM}};
\end{scope}
\begin{scope} [xshift=6cm]
\node [snode,anchor=west] (llm) at (0,0) {LLM};
\node [snode,anchor=west,rounded corners=3pt] (userdata) at ([xshift=1.0*\ssep]llm.east) {};
\node [anchor=north] (userdatalabel1) at (userdata.north) {\scriptsize{User Input}};
\node [anchor=south west] (userdatalabel2) at ([xshift=0.2em,yshift=0.2em]userdata.south west) {\tiny{environmentally friendly?}};
\node [anchor=south west] (userdatalabel3) at ([yshift=-0.15cm]userdatalabel2.north west) {\tiny{How can I live more }};
\draw [->,thick] ([xshift=-0.1em]userdata.west) -- ([xshift=0.1em]llm.east);
\node [snode,anchor=south,rounded corners=3pt] (predictdata) at ([yshift=1.0*\ssep]llm.north) {};
\node [anchor=north] (predictdatalabel1) at (predictdata.north) {\scriptsize{Model Output}};
\node [anchor=south west] (predictdatalabel2) at ([xshift=0.2em,yshift=0.2em]predictdata.south west) {\tiny{3. ............\ \ \ \ 4. ............}};
\node [anchor=south west] (predictdatalabel3) at ([yshift=-0.15cm]predictdatalabel2.north west) {\tiny{1. ............\ \ \ \ 2. ............}};
\draw [->,thick] ([yshift=0.1em]llm.north) -- ([yshift=-0.1em]predictdata.south);
\node [anchor=west] (predictlabel) at ([yshift=0.5*\ssep,xshift=0.3em]llm.north) {\scriptsize{Predicting}};
\node [snode,anchor=south,rounded corners=3pt] (compdata) at ([yshift=1.0*\ssep]predictdata.north) {};
\node [anchor=north] (compdatalabel1) at (compdata.north) {\scriptsize{Comparisons}};
\node [anchor=south west] (compdatalabel2) at ([xshift=0.2em,yshift=0.6em]compdata.south west) {\tiny{$\mathbf{y}_1 \succ \mathbf{y}_4 \succ \mathbf{y}_2 \succ \mathbf{y}_3$}};
\draw [->,thick] ([yshift=0.1em]predictdata.north) -- ([yshift=-0.1em]compdata.south);
\node [anchor=west] (complabel) at ([yshift=0.5*\ssep,xshift=0.3em]predictdata.north) {\scriptsize{Annotating Data with Human Preferences}};
\begin{pgfonlayer}{background}
\node [shadenode,minimum width=2.4*\ssep,anchor=south,rounded corners=2pt] (userdatashade) at ([yshift=0.3em]userdata.south) {};
\node [shadenode,anchor=south,rounded corners=2pt] (predictdatashade) at ([yshift=0.3em]predictdata.south) {};
\node [shadenode,anchor=south,rounded corners=2pt] (compdatashade) at ([yshift=0.3em]compdata.south) {};
\end{pgfonlayer}
\node [anchor=north] (caption) at ([yshift=-0.8em,xshift=0.5*\ssep]llm.south east) {\small{(b) Annotating Data with Human Preferences}};
\end{scope}
\begin{scope} [yshift=-10cm]
\node [snode,anchor=west] (reward) at (0,0) {Reward Model};
\node [snode,anchor=south,rounded corners=3pt] (compdata) at ([yshift=\ssep]reward.north) {};
\node [anchor=north] (compdatalabel1) at (compdata.north) {\scriptsize{Comparison Data}};
\node [anchor=south west] (compdatalabel2) at ([xshift=0.2em,yshift=0.5em]compdata.south west) {\hspace{0.0em} \tiny{$\{(\mathbf{x},\mathbf{y}_{k_1} \succ \mathbf{y}_{k_2})\}$}};
\begin{pgfonlayer}{background}
\node [shadenode,anchor=south,rounded corners=2pt] (compdatashade) at ([yshift=0.3em]compdata.south) {};
\end{pgfonlayer}
\draw [-{Stealth[length=3mm]},thick,double,double distance=1pt] ([yshift=-2pt]compdata.south) -- ([yshift=2pt]reward.north);
\node [anchor=west] (trainlabel) at ([yshift=0.5*\ssep,xshift=0.3em]reward.north) {\scriptsize{Training}};
\node [anchor=north] (caption) at ([yshift=-0.8em]reward.south) {\small{(c) Training the Reward Model}};
\end{scope}
\begin{scope} [yshift=-10cm,xshift=6cm]
\node [snode,anchor=west] (llm) at (0,0) {LLM};
\node [anchor=south] (llmlabel) at (llm.south) {\scriptsize{(Policy)}};
\node [snode,anchor=west,rounded corners=3pt] (userdata) at ([xshift=1.0*\ssep]llm.east) {};
\node [anchor=north] (userdatalabel1) at (userdata.north) {\scriptsize{Dataset $\mathcal{D}$}};
\node [anchor=south west] (userdatalabel2) at ([xshift=2em,yshift=0.5em]userdata.south west) {\scriptsize{$\mathbf{x} \sim \mathcal{D}$}};
\draw [->,thick] ([xshift=-0.1em]userdata.west) -- ([xshift=0.1em]llm.east);
\node [snode,anchor=south,rounded corners=3pt] (predictdata) at ([yshift=1.0*\ssep]llm.north) {};
\node [anchor=north] (predictdatalabel1) at (predictdata.north) {\scriptsize{Input-output Pairs}};
\node [anchor=south west] (predictdatalabel2) at ([xshift=2em,yshift=0.5em]predictdata.south west) {\scriptsize{$\{\mathbf{x},\mathbf{y}\}$}};
\draw [->,thick] ([yshift=0.1em]llm.north) -- ([yshift=-0.1em]predictdata.south);
\node [anchor=west] (predictlabel) at ([yshift=0.5*\ssep,xshift=0.3em]llm.north) {\scriptsize{Sampling $\mathbf{y}$ via the Policy $\Pr(\mathbf{y}|\mathbf{x})$}};
\node [snode,anchor=south] (rewardmodel) at ([yshift=1.0*\ssep]predictdata.north) {Reward Model};
\draw [->,thick] ([yshift=0.1em]predictdata.north) -- ([yshift=-0.1em]rewardmodel.south);
\node [snode,anchor=south,rounded corners=3pt] (rewarddata) at ([yshift=1.0*\ssep]rewardmodel.north) {};
\node [anchor=north] (rewarddatalabel1) at (rewarddata.north) {\scriptsize{Reward Scores}};
\node [anchor=south west] (rewarddatalabel2) at ([xshift=1.5em,yshift=0.5em]rewarddata.south west) {\scriptsize{$\{r(\mathbf{x},\mathbf{y})\}$}};
\draw [->,thick] ([yshift=0.1em]rewardmodel.north) -- ([yshift=-0.1em]rewarddata.south);
\node [anchor=west] (rewardlabel) at ([yshift=0.5*\ssep,xshift=0.3em]rewardmodel.north) {\scriptsize{Evaluate the Input-output Pairs}};
\begin{pgfonlayer}{background}
\node [shadenode,anchor=south,rounded corners=2pt] (userdatashade) at ([yshift=0.3em]userdata.south) {};
\node [shadenode,anchor=south,rounded corners=2pt] (predictdatashade) at ([yshift=0.3em]predictdata.south) {};
\node [shadenode,anchor=south,rounded corners=2pt] (rewarddatashade) at ([yshift=0.3em]rewarddata.south) {};
\end{pgfonlayer}
\node [anchor=north] (caption) at ([yshift=-0.8em,xshift=0.5*\ssep]llm.south east) {\small{(d) Training/Fine-tuning the Policy}};
\draw [thick,double,double distance=1pt] ([xshift=-1pt]rewarddata.west) .. controls +(210:1.5cm) and +(150:1.5cm) .. ([xshift=-3pt]llm.west) node [pos=0.5,left,xshift=1.0em,yshift=2.5em,rotate=90] (rlfinetuning) {\scriptsize{RL Fine-tuning}};
\draw [-{Stealth[length=3mm]}] ([xshift=-0.35em,yshift=0.07em]llm.west) -- ([xshift=-1pt,yshift=-0.23em]llm.west);
\end{scope}
\end{tikzpicture}
\begin{center}
\begin{tikzpicture}
\def\ssep{0.9cm}
\tikzstyle{lnode} = [minimum width=3cm,minimum height=1.0cm,inner sep=2pt,draw,thick,fill=white,rounded corners=1pt];
\begin{scope}
\node [anchor=west,fill=gray!30,inner sep=5pt] (preferencedata) at (0,0) {\Large{$\mathbf{y}_a \succ \mathbf{y}_b$}};
\node [anchor=south west,align=left] (preferencelabel) at ([yshift=0.1cm]preferencedata.north west) {\scriptsize{Preference}\\[-0.2cm] \scriptsize{Data}};
\node [lnode,anchor=west] (rewardmodel) at ([xshift=2.5cm]preferencedata.east) {Reward Model};
\node [lnode,anchor=south west] (valuefunction) at ([xshift=1.0cm,yshift=0.7cm]rewardmodel.east) {Value Function};
\node [lnode,anchor=north west] (policy) at ([xshift=1.0cm,yshift=-0.7cm]rewardmodel.east) {Policy};
\draw [->,thick] ([xshift=2pt]preferencedata.east)--([xshift=-2pt]rewardmodel.west) node [pos=0.5,above,align=left] {\scriptsize{training with MLE}};
\draw [->,thick] ([xshift=2pt,yshift=0.3cm]rewardmodel.east) -- ([xshift=-2pt,yshift=0.0cm]valuefunction.west);
\draw [->,thick] ([xshift=2pt,yshift=-0.3cm]rewardmodel.east) -- ([xshift=-2pt,yshift=0.0cm]policy.west);
\draw [->,dotted,very thick] ([xshift=-0.6cm,yshift=-2pt]valuefunction.south) .. controls +(-150:0.5cm) and +(150:0.5cm) .. ([xshift=-0.6cm,yshift=2pt]policy.north);
\draw [<-,dotted,very thick] ([xshift=0.6cm,yshift=-2pt]valuefunction.south) .. controls +(-30:0.5cm) and +(30:0.5cm) .. ([xshift=0.6cm,yshift=2pt]policy.north) node [pos=0.5,right,xshift=0.2cm,align=left] {\scriptsize{Training}\\[-0.2cm] \scriptsize{with PPO}};
\node [anchor=north] (caption) at ([yshift=-1.3cm]rewardmodel.south) {\small{(a) RLHF (PPO)}};
\end{scope}
\begin{scope}[yshift=-4.5cm]
\node [anchor=west,fill=gray!30,inner sep=5pt] (preferencedata) at (2cm,0) {\Large{$\mathbf{y}_a \succ \mathbf{y}_b$}};
\node [anchor=south west,align=left] (preferencelabel) at ([yshift=0.1cm]preferencedata.north west) {\scriptsize{Preference}\\[-0.2cm] \scriptsize{Data}};
\node [lnode,anchor=west] (policy) at ([xshift=3.5cm]preferencedata.east) {Policy};
\draw [->,thick] ([xshift=2pt]preferencedata.east)--([xshift=-2pt]policy.west) node [pos=0.5,above,align=left] {\scriptsize{training with MLE}};
\node [anchor=north west] (caption) at ([xshift=-2.3cm,yshift=-0.5cm]policy.south west) {\small{(b) DPO}};
\end{scope}
\end{tikzpicture}
\end{center}
% !Mode:: "TeX:UTF-8"
% !TEX encoding = UTF-8 Unicode
\begin{tikzpicture}
\def\nodewidth{0.55cm}
\def\nodeheight{0.55cm}
\tikzstyle{enode} = [minimum width=\nodewidth,minimum height=\nodeheight,inner sep=2pt];
%%% pre-training encoders
\begin{scope}
\node [enode,anchor=west] (w0) at (0,0) {$x_0$};
\node [enode,anchor=west] (w1) at ([xshift=\nodewidth]w0.east) {$x_1$};
\node [enode,anchor=west] (w2) at ([xshift=\nodewidth]w1.east) {$x_2$};
\node [enode,anchor=west] (w3) at ([xshift=\nodewidth]w2.east) {$x_3$};
\node [enode,anchor=west] (w4) at ([xshift=\nodewidth]w3.east) {$x_4$};
\node [enode,anchor=north] (w3mask) at ([yshift=0.4em]w3.south) {\footnotesize{(masked)}};
\node [enode,anchor=south] (e0) at ([yshift=\nodeheight]w0.north) {$\mathbf{e}_0$};
\node [enode,anchor=west] (e1) at ([xshift=\nodewidth]e0.east) {$\mathbf{e}_1$};
\node [enode,anchor=west] (e2) at ([xshift=\nodewidth]e1.east) {$\mathbf{e}_2$};
\node [enode,anchor=west] (e3) at ([xshift=\nodewidth]e2.east) {$\mathbf{e}_3$};
\node [enode,anchor=west] (e4) at ([xshift=\nodewidth]e3.east) {$\mathbf{e}_4$};
\draw [->] (w0.north) -- ([yshift=0]e0.south);
\draw [->] (w1.north) -- ([yshift=0]e1.south);
\draw [->] (w2.north) -- ([yshift=0]e2.south);
\draw [->] (w3.north) -- ([yshift=0]e3.south);
\draw [->] (w4.north) -- ([yshift=0]e4.south);
\node [anchor=south,minimum width=10*\nodewidth,minimum height=2*\nodeheight,draw,thick] (encoder) at ([yshift=\nodeheight]e2.north) {\large{Encoder}};
\draw [->] ([yshift=0pt]e0.north) -- ([yshift=\nodeheight-2pt]e0.north);
\draw [->] ([yshift=0pt]e1.north) -- ([yshift=\nodeheight-2pt]e1.north);
\draw [->] ([yshift=0pt]e2.north) -- ([yshift=\nodeheight-2pt]e2.north);
\draw [->] ([yshift=0pt]e3.north) -- ([yshift=\nodeheight-2pt]e3.north);
\draw [->] ([yshift=0pt]e4.north) -- ([yshift=\nodeheight-2pt]e4.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e0.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e0.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e1.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e1.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e2.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e2.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e3.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e3.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e4.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e4.north);
\node [anchor=south,minimum width=10*\nodewidth,minimum height=\nodeheight,draw,thick] (softmax) at ([yshift=1.2*\nodeheight]encoder.north) {Softmax};
\node [anchor=south west] (supervision1) at ([yshift=1.5*\nodeheight]softmax.north west) {\footnotesize{model reconstructs the masked token}};
\node [anchor=south west] (supervision2) at ([yshift=-0.4em]supervision1.north west) {\footnotesize{E.g., evaluate how well the}};
\node [anchor=south west] (supervision3) at ([yshift=-0.3em]supervision2.north west) {Self-supervision};
\draw [->,thick] ([yshift=1.5*\nodeheight,xshift=-0.8cm]softmax.north) .. controls +(south:0.6cm) and +(north:0.7cm) .. ([yshift=2pt]softmax.north);
\node [anchor=north] (caption) at ([yshift=-\nodeheight]w2.south) {\small{(a) Pre-training}};
\end{scope}
%%% applying pre-trained encoders
\begin{scope}[xshift=7cm]
\node [enode,anchor=west] (w0) at (0,0) {$x_0$};
\node [enode,anchor=west] (w1) at ([xshift=\nodewidth]w0.east) {$x_1$};
\node [enode,anchor=west] (w2) at ([xshift=\nodewidth]w1.east) {$x_2$};
\node [enode,anchor=west] (w3) at ([xshift=\nodewidth]w2.east) {$x_3$};
\node [enode,anchor=west] (w4) at ([xshift=\nodewidth]w3.east) {$x_4$};
\node [enode,anchor=south] (e0) at ([yshift=\nodeheight]w0.north) {$\mathbf{e}_0$};
\node [enode,anchor=west] (e1) at ([xshift=\nodewidth]e0.east) {$\mathbf{e}_1$};
\node [enode,anchor=west] (e2) at ([xshift=\nodewidth]e1.east) {$\mathbf{e}_2$};
\node [enode,anchor=west] (e3) at ([xshift=\nodewidth]e2.east) {$\mathbf{e}_3$};
\node [enode,anchor=west] (e4) at ([xshift=\nodewidth]e3.east) {$\mathbf{e}_4$};
\draw [->] (w0.north) -- ([yshift=0]e0.south);
\draw [->] (w1.north) -- ([yshift=0]e1.south);
\draw [->] (w2.north) -- ([yshift=0]e2.south);
\draw [->] (w3.north) -- ([yshift=0]e3.south);
\draw [->] (w4.north) -- ([yshift=0]e4.south);
\node [anchor=south,minimum width=10*\nodewidth,minimum height=2*\nodeheight,draw,thick] (encoder) at ([yshift=\nodeheight]e2.north) {\large{Pre-trained Encoder}};
\draw [->] ([yshift=0]e0.north) -- ([yshift=\nodeheight-2pt]e0.north);
\draw [->] ([yshift=0]e1.north) -- ([yshift=\nodeheight-2pt]e1.north);
\draw [->] ([yshift=0]e2.north) -- ([yshift=\nodeheight-2pt]e2.north);
\draw [->] ([yshift=0]e3.north) -- ([yshift=\nodeheight-2pt]e3.north);
\draw [->] ([yshift=0]e4.north) -- ([yshift=\nodeheight-2pt]e4.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e0.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e0.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e1.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e1.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e2.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e2.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e3.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e3.north);
\draw [->] ([yshift=3*\nodeheight+2pt]e4.north) -- ([yshift=3*\nodeheight+1.2*\nodeheight-2pt]e4.north);
\node [anchor=south,minimum width=10*\nodewidth,minimum height=\nodeheight,draw,thick] (softmax) at ([yshift=1.2*\nodeheight]encoder.north) {Prediction Network};
\node [anchor=south] (supervision1) at ([yshift=1.5*\nodeheight]softmax.north) {Output for Downstream Tasks};
\draw [->,thick] ([yshift=2pt]softmax.north) -- (supervision1.south);
\node [anchor=north] (caption) at ([yshift=-\nodeheight]w2.south) {\small{(b) Applying the Pre-trained Encoder}};
\end{scope}
\end{tikzpicture}
把课程讲义中需要使用的图片放在这个目录中。
示例:
- `cover.jpg`:封面图片,模板会自动读取。
- `example.png`:正文图片示例,模板会自动读取。
\noindent 近年来,大语言模型(LLMs)的蓬勃发展成为自然语言处理(NLP)领域最重要的进展之一。这一进展推进生成了可模拟人类认知、实现自然语言理解与生成的系统。这些系统甚至具备逻辑推理能力,而推理任务曾一直被视为人工智能领域极具挑战性的难题。得益于这些技术革新,NLP 取得了长足的进步,正式步入全新研究阶段,以往诸多研究难题,例如搭建可与人流畅沟通的对话交互系统等,正逐步迎刃而解。
在大规模数据上训练语言模型已将 NLP 研究带入了一个全新发展的阶段。长期以来,语言建模仅被视为底层基础技术,与通用人工智能的研发目标相差甚远,但如今该方向催生出具备涌现智能的系统:模型通过持续预测文本序列中的词汇,学习到一定程度的通用知识。研究表明,一个经过良好训练的单一 LLM 能够处理海量任务,并以极低的适配成本泛化至全新任务 \cite{bubeck-etal:2023sparks}。这一成果代表着人工智能向高阶通用形态迈出关键一步,也推动学界进一步探索,致力于将更强大的语言模型开发为基础模型。
本章将系统介绍大语言模型的预训练与微调全流程技术体系。首先,我们将阐述大语言模型的基础预训练技术,包括语言建模方法、主流模型架构、预训练策略以及指导模型规模扩张的缩放定律。在此基础上,展开提示学习相关方法研究,包含零样本、少样本提示、思维链提示及其各类优化手段。随后,重点阐述大模型微调技术,包括有监督微调和各类高效微调方法。进一步,讨论模型偏好对齐技术,涉及奖励模型构建、对齐策略、直接偏好优化及偏好数据自动生成方案。最后,探讨大语言模型的推理增强方法,包括测试时缩放策略、基于可校验奖励的强化学习以及从推理模型到智能体的演进路径。通过对上述内容的系统梳理,本章旨在为读者构建大模型从预训练到应用部署的完整技术认知体系。
\begin{learninggoals}
\begin{itemize}
\item 理解大语言模型预训练的基本原理,包括语言建模的目标、模型架构以及模型规模与训练数据之间的扩展规律,能够解释模型性能与计算资源之间的关系。
\item 掌握通过提示设计和有监督微调来适配预训练模型的方法,能够区分不同提示策略和高效微调技术的适用场景。
\item 了解模型偏好对齐与推理增强的核心思路,能够说明如何利用人类反馈优化模型行为,以及如何通过扩展推理时的计算来提升复杂任务的解决能力。
\end{itemize}
\end{learninggoals}
\ No newline at end of file
\section{大模型预训练技术}
\subsection{语言建模方法}
在引入严谨的定义之前,我们不妨先通过一个例子来理解语言建模在做什么。想象你正在读一句话的前半部分:\textit{``今天天气真''}。即便后续尚未给出,你大概已经能猜到接下来会是``好````不错''或``热''之类的词,而几乎不可能是``宪法````光合作用''或``平方根''。语言模型本质上就是在做这件``猜词''的事:给定上文,它估计每个候选词出现的概率,概率高的就是``合理''的续写,概率低的则不太可能出现在当前上下文之后。
更形式化地来看,语言建模的目标是计算一个 token 序列整体出现的概率。设 $\{x_0,x_1,...,x_m\}$ 为一个 token 序列,其中 $x_0$ 为起始符号 $\langle s \rangle$(或记为 $\langle \mathrm{SOS} \rangle$\footnote{在 BERT 模型中,起始符号也可以是 $[\mathrm{CLS}]$}。根据链式法则,该序列的联合概率可分解为一系列条件概率的乘积:
\begin{equation}
\Pr(x_0,...,x_m) = \prod_{i=0}^{m} \Pr(x_{i}\,|\,x_0,...,x_{i-1})
\label{eq:lm-basic-form}
\end{equation}
这一定义的直觉很简单:先把开头 $x_0$ 出现的概率算出来,再算``有了 $x_0$ 之后出现 $x_1$ 的概率'',接着算``在 $x_0$$x_1$ 已经有了的前提下出现 $x_2$ 的概率''……依此类推,整个句子的概率就是每一步条件概率的乘积\footnote{为处理 $i=0$ 的边界情况,约定 $\Pr(x_0|x_0,...,x_{i-1})\big|_{i=0}=\Pr(x_0)=1$,因此 $\Pr(x_0,...,x_m) = \Pr(x_1,...,x_m|x_0)$}
在深度学习时代,语言建模的一种典型方法是用深度神经网络来估计这些条件概率:模型接收历史 token 序列 $x_0,...,x_{i-1}$ 作为输入,输出词汇表 $\mathcal{V}$ 上的概率分布(记为 $\Pr(\cdot|x_0,...,x_{i-1})$)。其中,$\Pr(x_i|x_0,...,x_{i-1})$ 即为该分布中对应 token $x_i$ 的概率值。
有了训练好的语言模型后,最常见的操作是根据已有上下文预测下一个最可能出现的 token:
\begin{equation}
\hat{x}_i = \argmax_{x_i \in \mathcal{V}} \Pr(x_i\,|\,x_0,...,x_{i-1})
\end{equation}
将这一过程反复迭代,就构成了\emph{自回归生成}:每次选出当前最优的下一个 token,把它拼到上下文中,再以更新后的完整序列预测再下一个 token,如此递推。这一机制恰好对应了公式 (\ref{eq:lm-basic-form}) 中链式法则的逐项展开。
为了直观说明上述过程,考虑一个简单的续写场景。假设模型已见过前缀 $\langle s \rangle$ \texttt{The}(起始符后的第一个词是``The''),接下来需要生成三个词。每一步的决策如下:
\begin{table}[ht]
\centering
\begingroup
\renewcommand{\arraystretch}{1.4}
\begin{tabular}{l|l|p{10cm}}
\toprule
\textbf{当前上下文} & \textbf{预测} & \textbf{解读} \\
\midrule
$\langle s \rangle$ \texttt{The} & \texttt{cat} & 模型评估 $\Pr(\cdot|\langle s \rangle\ \text{The})$ 分布后,\texttt{cat} 得分最高——``The''后面常接名词。\\
\midrule
$\langle s \rangle$ \texttt{The cat} & \texttt{sat} & 给定``The cat'',模型判断最合理的下一个词是动词 \texttt{sat}\\
\midrule
$\langle s \rangle$ \texttt{The cat sat} & \texttt{on} & 有了``The cat sat''后,\texttt{on} 成为最高概率的续写(``sat on''是常见搭配)。\\
\bottomrule
\end{tabular}
\endgroup
\caption{语言模型基于前缀 $\langle s \rangle$ \texttt{The} 依次生成 \texttt{cat}\texttt{sat}\texttt{on} 的自回归过程。每一步中,模型根据当前上下文计算词汇表上的条件概率分布,并选出概率最大的词追加到上下文中;更新后的序列随即成为下一轮预测的输入。每一步的决策对应于链式分解中新增的一个条件概率因子。}
\label{tab:lm-generation-example}
\end{table}
经过这三步,模型生成了序列 \texttt{The \ cat \ sat \ on},而整句``The cat sat on''的联合概率正是各步条件概率的乘积。当然,基于同样的前缀,模型也可能在第一步选中 \texttt{dog}、第二步选中 \texttt{ran},从而生成另一条合理的续写路径——这正是语言模型作为一个概率系统的灵活之处。
接下来,我们将深入探讨 LLM 的构建、训练与应用。
\subsection{大语言模型架构}
\label{sec:decoder-only-transformers}
上一节从概率的角度介绍了语言建模的目标。本节我们来回答一个更具体的问题:什么样的神经网络架构能够实现这一目标?
\ref{fig:llm-transformer-decoder} 展示了当前主流 LLM 所采用的仅解码器(Decoder-only)Transformer 架构。尽管不同 LLM 在细节上各有所异,但它们共享同一个信息处理流水线:
\begin{enumerate}
\item \textbf{嵌入}:输入 token 序列 $\{x_0,\dots,x_{m-1}\}$ 被转换为一组 $d$ 维向量 $\{\mathbf{e}_0,\dots,\mathbf{e}_{m-1}\}$,每个向量是 token 嵌入与位置嵌入之和。
\item \textbf{Transformer 块堆叠}:这些向量依次通过 $L$ 个结构相同的 Transformer 块。每经过一个块,序列中每个位置的表示都会吸收更多来自上下文的语义信息。记第 $\ell$ 个块的输出为 $\mathbf{H}^{\ell} \in \mathbb{R}^{m \times d}$,其第 $i$ 行就是位置 $i$ 在当前深度下的上下文表示。
\item \textbf{输出层}:最后一个块的输出 $\mathbf{H}^{L}$ 通过一个 Softmax 层映射为词汇表上的概率分布:
\begin{equation}
\begin{bmatrix} \Pr(\cdot|x_0,\dots,x_{m-1}) \\ \vdots \\ \Pr(\cdot|x_0,x_1) \\ \Pr(\cdot|x_0) \end{bmatrix}
= \mathrm{Softmax}(\mathbf{H}^{L} \mathbf{W}^{o})
\label{eq:lm-output-layer}
\end{equation}
其中 $\mathbf{W}^{o} \in \mathbb{R}^{d \times |V|}$ 为输出投影矩阵。这恰好就是公式 (\ref{eq:lm-basic-form}) 中所需的每一步条件概率分布。
\end{enumerate}
每个 Transformer 块由两个核心子层构成:
\begin{itemize}
\item \textbf{掩码多头自注意力}:让每个 token 能够聚合其前方所有 token 的信息。``掩码''确保位置 $i$ 只能看到位置 $0$$i$ 的内容——这正是条件概率 $\Pr(x_i|x_0,\dots,x_{i-1})$ 的``只看上文''要求。
\item \textbf{前馈神经网络}(FFN):对每个位置的表示独立做非线性变换,为模型提供逐 token 的细粒度加工能力。
\end{itemize}
两个子层均配备\textbf{残差连接}:子层输出与其输入相加后再传递至下一阶段。这一设计使得信号即使在很深的网络中也能顺畅流动,是 LLM 能够堆叠数十乃至上百层的关键。
\begin{figure}[!t]
\centering
\input{figures/Chapter1/figure-lm-decoder-only-architecture.tex}
\caption{用于语言建模的 Transformer 解码器架构。核心组件为 $L$ 个堆叠的 Transformer 块,每个块包含一个掩码多头自注意力子层和一个 FFN 子层。输出层将最后一个块的上下文表示映射为下一个 token 的概率分布。}
\label{fig:llm-transformer-decoder}
\end{figure}
自注意力层是整个架构的精髓。其核心直觉是:对每个位置 $i$,模型生成一个查询向量(Query),用它去``询问''所有前方位置 $k \le i$——每个前方位置用一个键向量(Key)来回应,匹配度越高,该位置的值向量(Value)在最终聚合中的权重就越大。形式化地:
\begin{equation}
\mathrm{Att}_{\mathrm{qkv}}(\mathbf{Q},\mathbf{K},\mathbf{V}) =
\mathrm{Softmax}\!\left(\frac{\mathbf{Q}\mathbf{K}^{\mathrm{T}}}{\sqrt{d}} + \mathbf{Mask}\right)\mathbf{V}
\label{eq:qkv-attention-lm}
\end{equation}
其中 $\mathbf{Q},\mathbf{K},\mathbf{V} \in \mathbb{R}^{m \times d}$$\mathbf{Mask}$ 确保 $i < k$ 时注意力权重为 0,除以 $\sqrt{d}$ 则用于稳定训练。
为了使模型能同时关注不同类型的模式(例如一个头关注句法,另一个头关注语义),实际使用的是\textbf{多头注意力}:将 $\mathbf{Q}$$\mathbf{K}$$\mathbf{V}$ 投影到 $\tau$ 个不同的低维子空间中独立计算注意力,最后拼接:
\begin{equation}
\mathrm{MHA}(\mathbf{H}) =
\big[\mathrm{head}_1 \,\|\, \cdots \,\|\, \mathrm{head}_{\tau}\big]\, \mathbf{W}^{\mathrm{head}},
\qquad
\mathrm{head}_j = \mathrm{Att}_{\mathrm{qkv}}(\mathbf{H}\mathbf{W}_j^{q},\,\mathbf{H}\mathbf{W}_j^{k},\,\mathbf{H}\mathbf{W}_j^{v})
\label{eq:multihead-attention}
\end{equation}
其中 $\mathbf{W}_j^{q},\mathbf{W}_j^{k},\mathbf{W}_j^{v} \in \mathbb{R}^{d \times d/\tau}$$\mathbf{W}^{\mathrm{head}} \in \mathbb{R}^{d \times d}$
模型通过最大化对数似然 $\sum_{i=1}^{m} \log \Pr(x_i|x_0,\dots,x_{i-1})$ 进行训练。推理时采用自回归方式:模型输入当前序列,预测下一个 token $\hat{x}_i$,将其附加到序列尾部,再以更新后的序列预测 $\hat{x}_{i+1}$,反复进行直至生成结束符。
\ref{tab:llm-model-size-comparison} 列出了近年来代表性 LLM 的深度 $L$、宽度 $d$ 及注意力头数等架构参数,帮助读者建立对模型规模的直观感受。
\begin{table}[t]
\centering
\begingroup
\renewcommand{\arraystretch}{1.2}
\begin{tabular}{l | r | r | r | r}
LLM & \# of Params & Depth $L$ & Width $d$ & \# of Heads (Q/KV) \\ \hline
GPT-2 \cite{radford-etal:2019language} & 1.5B & 48 & 1,600 & 25/25 \\
GPT-3 \cite{brown-etal:2020language} & 175B & 96 & 12,288 & 96/96 \\ \hline
\multirow{3}{*}{LLaMA3.1 \cite{dubey2024llama}}
& 8B & 32 & 4,096 & 32/8 \\
& 70B & 80 & 8,192 & 64/8 \\
& 405B & 126 & 16,384 & 128/8 \\ \hline
\multirow{3}{*}{Qwen3 \cite{yang2025qwen3}}
& 0.6B & 28 & 1,024 & 16/8 \\
& 8B & 36 & 4,096 & 32/8 \\
& 235B$^\dagger$ & 94 & 4,096 & 64/4 \\ \hline
Gemma3 \cite{gemmateam2025gemma3} & 27B & 62 & 5,376 & 32/16 \\ \hline
DeepSeek-V3 \cite{liu2024deepseek} & 671B$^\dagger$ & 61 & 7,168 & 128/128 \\
Mistral-Large-3 \cite{mistral2025mistral3} & 673B$^\dagger$ & 61 & 7,168 & 128/128
\end{tabular}
\endgroup
\caption{部分大语言模型的架构参数对比。表中列出了各模型的参数量、层数($L$)、隐藏维度($d$)以及注意力头数($a/b$ 表示 $a$ 个 Query 注意力头,$b$ 个 Key/Value 注意力头)。标记 $^\dagger$ 的模型采用混合专家(Mixture-of-Experts, MoE)架构,表中给出的是总参数量;推理时每 token 实际激活的参数量分别为 Qwen3-235B $\sim$22B、DeepSeek-V3 $\sim$37B、Mistral-Large-3 $\sim$41B。}
\label{tab:llm-model-size-comparison}
\end{table}
\subsection{大语言模型预训练方法}
直观地说,预训练的本质就是把海量文本"喂"给模型,让它反复练习"给定上文,猜下一个词"这项任务。这一朴素的目标——在数学上即最大化训练数据上的对数似然——正是驱动 LLM 获得强大语言能力的核心机制。
\subsubsection{预训练数据}
预训练首先需要大量的文本数据。如表 \ref{tab:training-data-size-llm} 所示,现代 LLM 在预训练阶段通常消耗数万亿乃至数十万亿个 token,远超传统 NLP 模型数个数量级。
\begin{table}[t]
\centering
\begingroup
\renewcommand{\arraystretch}{1.2}
\begin{tabular}{l | r | r}
LLM & Year & \# of Tokens \\ \hline
GPT-3-175B \cite{brown-etal:2020language} & 2020 & 0.5T \\
LLaMA2-70B \cite{touvron-etal:2023llama} & 2023 & 2.0T \\
LLaMA3-8B \cite{dubey2024llama} & 2024 & 15T \\
DeepSeek-V3 \cite{liu2024deepseek} & 2024 & 14.8T \\
Qwen3-235B \cite{yang2025qwen3} & 2025 & 36T
\end{tabular}
\endgroup
\caption{部分 LLM 的预训练数据规模。}
\label{tab:training-data-size-llm}
\end{table}
除了规模之外,预训练数据的质量与构成同样决定了模型最终的能力边界。训练语料通常来自多个渠道:
\begin{itemize}
\item \textbf{网络爬取数据}:占比最大,但也包含大量低质量、重复甚至有害内容。已有工作表明 \cite{penedo-etal:2023refinedweb},通过系统性的过滤和清洗,可剔除约 90\% 的原始爬取数据而不损害模型性能。高质量的过滤流程已成为现代 LLM 预训练数据管线中不可或缺的一环。
\item \textbf{书籍与学术论文}:提供连贯的长文本和深度知识,有助于模型习得复杂的语言结构和事实性知识。
\item \textbf{编程代码}:被证明是提升模型推理能力的关键。将代码纳入训练数据不仅能增强代码生成能力,还会显著改善模型在逻辑推理和多步骤任务上的表现,尤其是需要思维链(Chain-of-Thought)提示的复杂场景。
\item \textbf{多语言语料}:使单一模型能够处理多种语言。但低资源语种的表现仍然受限于对应语料的体量与质量。
\end{itemize}
需要指出的是,数据的多样性和质量之间需要谨慎权衡——过度过滤可能导致某些知识或语言现象的流失,而过滤不足则引入噪声。此外,训练数据中的偏见问题(如性别刻板印象)和隐私风险(如模型记忆并复现个人身份信息)也是数据准备阶段需要关注的工程挑战,通常通过数据去偏、匿名化处理以及部署阶段的安全防护系统加以缓解。
\subsubsection{训练目标}
预训练的核心数学目标简洁而自然:最大化模型在所有训练序列上逐 token 预测的对数似然。
设训练集 $\mathcal{D}$$K$ 个 token 序列组成。对于任意序列 $\mathbf{x} = x_0\ldots x_m$,模型在该序列上的对数似然为:
\begin{equation}
\mathcal{L}_{\theta}(\mathbf{x}) = \sum_{i=1}^{m} \log \Pr\nolimits_{\theta}(x_i \mid x_0,\dots,x_{i-1})
\label{eq:llm-training-loss}
\end{equation}
其中 $\theta$ 表示模型参数,$\Pr_{\theta}(x_i \mid x_0,\dots,x_{i-1})$ 正是上一节所定义的、由 Transformer 输出层 Softmax 给出的条件概率。预训练的目标是找到参数 $\hat{\theta}$ 使得整体对数似然最大:
\begin{equation}
\hat{\theta} = \argmax_{\theta} \sum_{\mathbf{x} \in \mathcal{D}} \mathcal{L}_{\theta}(\mathbf{x})
\label{eq:llm-training-mle}
\end{equation}
在实际实现中,最大化对数似然等价于最小化交叉熵损失。具体而言,在序列的每个位置 $i$,模型输出一个概率分布 $\mathbf{p}_{i+1}^{\theta} = \Pr_{\theta}(\cdot|x_0,\dots,x_i)$,将其与真实 token $x_{i+1}$ 的 one-hot 分布 $\mathbf{p}_{i+1}^{\mathrm{gold}}$ 计算交叉熵,并沿所有位置求和。整个训练集上的优化目标为:
\begin{equation}
\hat{\theta} = \argmin_{\theta} \sum_{\mathbf{x} \in \mathcal{D}} \sum_{i=0}^{m-1} \mathrm{CrossEntropy}\big(\mathbf{p}_{i+1}^{\theta},\; \mathbf{p}_{i+1}^{\mathrm{gold}}\big)
\label{eq:llm-training-loss-min}
\end{equation}
公式 (\ref{eq:llm-training-mle}) 与 (\ref{eq:llm-training-loss-min}) 在数学上完全等价,前者体现概率建模的视角,后者对应工程实现中的损失函数。通过优化得到的模型即可用于计算任意上下文下的 token 条件概率,进而支持自回归文本生成。
\subsection{缩放定律}
\label{sec:llm-training-at-scale}
\subsubsection{缩放定律}
直观而言,缩放定律(Scaling Laws)回答这样一个问题:如果我把模型做得更大、喂更多数据、投入更多算力,模型性能能提升多少?
$\mathcal{L}$ 表示模型的测试损失(如交叉熵),$N$ 为模型参数量,$D$ 为训练数据量(以 token 计)。大量实验表明,$\mathcal{L}$$N$$D$ 之间存在幂律(power-law)关系——即损失随资源的增加按幂函数规律下降:
\begin{equation}
\mathcal{L}(N) = aN^{b}, \qquad \mathcal{L}(D) = cD^{d}
\label{eq:scaling-power-law-basic}
\end{equation}
其中 $a,b,c,d$ 为经验拟合参数,$b,d$ 均为负数(损失随规模增大而降低)。图 \ref{fig:power-law-scaling-for-model-size-and-dataset-size} 展示了这一关系的典型形态。
\cite{kaplan-etal:2020scaling} 最早在语言模型中系统性地验证了这一规律。他们的核心发现是:在给定计算预算 $C$ 的条件下,模型性能主要由 $N$$D$ 共同决定,而非单独由其中一方主导。换言之,单纯增大模型而不配以足够的数据,边际收益会迅速衰减。
在此基础上,\cite{hoffmann-etal:2022training} 提出了更为精确的 Chinchilla 缩放定律。他们将测试损失同时建模为 $N$$D$ 的函数,并加上一个不可消除的误差下界 $\epsilon_{\infty}$(不可约误差):
\begin{equation}
\mathcal{L}(N,D) = \frac{A}{N^{\alpha}} + \frac{B}{D^{\beta}} + \epsilon_{\infty}
\label{eq:scaling-chinchilla}
\end{equation}
其中通过大规模实验拟合得到 $\alpha \approx 0.34$$\beta \approx 0.28$$\epsilon_{\infty} \approx 1.69$
Chinchilla 缩放定律的核心洞见在于最优配比:对于一个给定的计算预算,模型参数 $N$ 与训练数据 $D$ 应当以相近的速度增长——这被称为"计算最优"(compute-optimal)训练策略。Chinchilla 的研究发现,此前的许多大模型(如 GPT-3 的某些版本)都是"参数过大而数据不足"的,同等算力下若减少参数并增加数据量,反而能获得更低的损失。
\begin{importantnote}
预训练的核心目标是最小化交叉熵损失,而缩放定律揭示了模型参数量与训练数据量必须同步增长才能获得最优性能。Chinchilla 定律指出,在给定算力下,参数和数据应等比例扩大,避免“大模型小数据”的低效配置。这一结论直接指导了后续模型规模和数据量的设计决策。
\end{importantnote}
\begin{figure}[!t]
\centering
\input{figures/Chapter1/figure-model-performance-against-model-size-and-training-dataset-size.tex}
\caption{测试损失与模型参数量 $N$ 及训练数据量 $D$ 的关系。左:$\mathcal{L}(N) = \big( \frac{N}{8.8 \times 10^{13}} \big)^{-0.076}$;右:$\mathcal{L}(D) = \big( \frac{D}{5.4 \times 10^{13}} \big)^{-0.095}$。均基于 \cite{kaplan-etal:2020scaling} 报告的拟合结果。}
\label{fig:power-law-scaling-for-model-size-and-dataset-size}
\end{figure}
缩放定律对于 LLM 研发布局具有重要的指导意义。其一,它使得在训练前即可大致预测给定资源配置下的模型表现,从而优化计算预算的分配。其二,它确认了"持续扩大规模"这一技术路线至今仍处于收益区间内——表 \ref{tab:training-data-size-llm} 所示的训练量从 0.5T 飙升至 36T,正是缩放定律驱动的结果。
值得一提的是,$\mathcal{L}$ 的幂律下降还伴随着模型能力在质上的跃迁。\cite{wei-etal:2022emergent} 观察到,当模型规模突破某一阈值时,某些能力(如多步推理、上下文理解)会从接近随机水平骤升至显著优于 baseline——这一现象被称为涌现能力(emergent abilities)。涌现能力的存在,为缩放定律的实用价值提供了另一层佐证:不断扩张的模型规模不仅能带来量上的损失降低,更可能触发质的飞跃。
\begin{figure}[!t]
\centering
\input{figures/Chapter1/figure-a-curve-of-scaling-laws}
\caption{测试误差随训练数据量变化的缩放曲线 \cite{hestness-etal:2017deep}。在大多数实用区间内,误差按幂律衰减;当数据量极大时衰减趋缓,但误差始终存在一个大于零的下界(不可约误差)。}
\label{fig:scaling-power-law-curve}
\end{figure}
\subsubsection{分布式训练}
\label{sec:llm-distributed-training}
现代 LLM 动辄数百亿乃至数千亿参数,单张 GPU 的显存远不足以装载完整模型,更无法在合理时间内完成训练。因此,分布式并行训练成为支撑 LLM 规模化的关键技术基石。当前主流方案包含三类并行策略,常组合使用(如 Megatron-LM 提出的三维并行 \cite{narayanan-etal:2021efficient}):
\medskip
\noindent\textbf{数据并行}。数据并行是最基本、最广泛使用的并行策略。其思路简洁直观:将每个训练批次(mini-batch)拆分为 $N$ 份子批次,分发至 $N$ 个工作节点。每个节点持有完整的模型副本,独立完成前向和反向传播并计算局部梯度,随后通过通信操作(如 All-Reduce)聚合各节点的局部梯度,得到全局梯度后更新参数。在通信开销可忽略的理想条件下,数据并行能实现接近 $N$ 倍的训练加速。
然而,数据并行的前提是每个节点能装下完整模型。随着模型参数量突破单卡显存上限,单纯的模型复制策略不再可行——此时需要在模型结构层面进行拆分,即模型层面的并行。
\medskip
\noindent\textbf{流水线并行}。流水线并行将模型的不同层(或层组)分配到不同设备上。一个直观但不实用的做法是让各层串行执行——这会导致任一时刻仅有一个设备在计算,其余全部空闲。流水线并行(如 GPipe \cite{huang-etal:2019gpipe})通过引入微批次(micro-batch)机制来解决这一低效问题:将一个 mini-batch 进一步切分为若干个 micro-batch,各设备处理完当前 micro-batch 后立即传递给下游,随即开始处理下一个 micro-batch。这样一来,不同设备上的计算便在时间维度上实现了重叠,大幅减少了空闲时间。micro-batch 数量越多,流水线气泡(bubble)越小;但 batch 过小也会降低 GPU 算力利用率,需在实际部署中权衡。
\medskip
\noindent\textbf{张量并行}。张量并行从另一个维度切入:不拆分模型层,而是拆分单层内部的计算。以 FFN 子层中的矩阵乘法 $\mathbf{h} \mathbf{W}_h$ 为例($\mathbf{W}_h \in \mathbb{R}^{d \times d_h}$),可将 $\mathbf{W}_h$ 沿列切分为 $M$ 个子矩阵 $\{\mathbf{W}_h^1,\dots,\mathbf{W}_h^M\}$,每块大小为 $d \times (d_h/M)$。于是:
\begin{equation}
\mathbf{h} \mathbf{W}_h = \big[\,\mathbf{h} \mathbf{W}_h^1 \;\|\; \cdots \;\|\; \mathbf{h} \mathbf{W}_h^M \,\big]
\end{equation}
$M$ 个设备分别计算各自对应的子矩阵乘法,最后拼接即可得到与单设备计算完全一致的输出。这一思想同样适用于自注意力层中的 Q、K、V 投影矩阵。张量并行的优势在于:它将单个 GPU 无法容纳的大矩阵运算拆解为多个 GPU 上可并行执行的小矩阵运算,且数学上等价,不会引入额外的近似误差。
\medskip
\noindent\textbf{混合精度训练}。除并行策略外,降低单步计算开销同样至关重要。混合精度训练是最广泛采用的优化手段之一:在前向和反向传播中使用半精度(如 FP16 或 BF16)以加速计算,同时维护一份全精度(FP32)的主权重副本用于参数更新 \cite{micikevicius-etal:2018mixed}。这一技术在显著降低显存占用和计算时间的同时,通过损失缩放(loss scaling)等技巧保证了训练的数值稳定性。
\medskip
\noindent\textbf{实际挑战}。需要指出,分布式训练在工程实践中远不止并行策略选型那么简单。通信开销、节点同步延迟(慢节点拖累整体)、硬件故障容错、网络拓扑设计、计算与通信重叠(overlap)、负载均衡等问题,均对系统设计与调优提出严苛要求。在实际的大模型训练中,上述三类并行策略通常会被组合使用,以在算力、显存和通信三个维度间取得最优平衡。
% \subsection{本章小结}
% 回顾全章,大语言模型的技术体系可以被归纳为一个看似朴素的核心公式——链式法则将语言建模转化为条件概率的乘积,Transformer 架构为这一概率分解提供了可计算的神经网络实现,预训练则通过最大化对数似然将海量文本中的语言知识压缩进模型参数。换言之,LLM 并未依赖某种全新的理论突破,而是将"根据上文预测下一个词"这一简单目标,在足够大的模型和足够多的数据上执行到了极致。
% 然而,"足够大"本身即是挑战。缩放定律告诉我们,性能随规模的提升仍处于幂律区间,训练数据量从 0.5T 膨胀到 36T 并非终点;分布式训练体系则让我们能够在工程上将这种规模扩张付诸实施。这意味着,当前 LLM 的能力边界并不由算法理论划定,而在相当程度上受制于算力、工程与数据的边界——这些边界仍在持续向外扩展。
% 也正因如此,理解本章所介绍的基本原理并非终点,而是进一步探索 LLM 的起点:预训练完成后,模型如何被适配到具体任务?如何与人类偏好对齐?如何降低部署成本?这些将是后续章节所要回答的问题。
\ No newline at end of file
\section{提示学习方法}
\subsection{Zero/One/Few-Shot 提示方法}
在与大语言模型交互时,我们输入的文本称为提示(prompt),模型据此生成回复。基于同一个预训练模型,只需更换提示即可完成翻译、问答、写作等多种任务,无需重新训练。本节旨在介绍提示设计的基本方法与思路,帮助读者更有效地利用大语言模型完成下游任务。
根据提示中包含的示例数量,可将其大致划分为三类:零样本(zero-shot,不提供示例)、单样本(one-shot,提供一个示例)和少样本(few-shot,提供多个示例)。本节将围绕这三类方法展开讨论。需要说明的是,提示的实际效果与最佳表述方式往往因模型而异,因此本文不针对特定大语言模型,而是总结一些具有通用性的指导原则。
\subsubsection{提示的基本概念}
在深入介绍这三类提示方法之前,我们先明确提示的基本概念及其构造方式。“提示”一词含义多样,在本章中,我们将其定义为LLM的输入文本,记作$\mathbf{x}$。LLM在给定输入$\mathbf{x}$的条件下,通过最大化概率$\Pr(\mathbf{y}\mid\mathbf{x})$生成输出文本$\mathbf{y}$。提示作为生成条件,可以包含任何有助于描述问题和提供信息的内容。
提示通常通过提示模板(简称模板)\cite{liu-etal:2023pre}获得。模板是一段含有占位符或变量的文本,各占位符可填入具体信息。以下展示两个向LLM征求周末建议的模板示例:
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\hspace{2em} 请给我一些有趣的周末建议。
\hspace{2em} \underline{\hspace{2em}}
\end{tcolorbox}
\vspace{0.5em}
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\hspace{2em} 如果 $\{*\mathrm{premise}*\}$,你有什么有趣的周末建议吗?
\hspace{2em} \underline{\hspace{2em}}
\end{tcolorbox}
\vspace{0.5em}
第一个模板直接请求建议,因此不含变量;第二个模板中,变量$\{*\mathrm{premise}*\}$需由用户指定,作为给出建议的前提条件。例如,若输入:
\begin{eqnarray}
\mathrm{premise} & = & \text{这个周末天气很好} \nonumber
\end{eqnarray}
则可生成如下提示:
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\hspace{2em} 如果这个周末天气很好,
\hspace{2em} 你有什么有趣的周末建议吗?
\hspace{2em} \underline{\hspace{2em}}
\end{tcolorbox}
\vspace{0.5em}
除了自由格式的文本,另一种常见做法是采用“名称: 内容”的结构组织提示,明确标识问题与答案的位置。例如,用“问”和“答”分别表示问题与答案,可构建如下问答模板:
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\hspace{2em} 问:$\{*\mathrm{question}*\}$
\hspace{2em} 答:\underline{\hspace{2em}}
\end{tcolorbox}
\vspace{0.5em}
这种“问: ... 答: ...”结构既可单独使用,也可重复多次并填入具体的问答实例,从而构成后文将要介绍的小样本提示的基本形式。
除了直接描述任务,许多提示还会包含一段系统信息(system message),用于设定模型的角色、能力与行为约束。系统信息帮助LLM更好地理解当前任务的背景,从而生成更符合预期的回复。以下是一个语法纠错任务的提示,其中第一条语句便是系统信息:
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begingroup
\renewcommand{\arraystretch}{1.0}
\setlength{\tabcolsep}{6pt}
\begin{tabular}{r l}
{\color{gray} \footnotesize{系统}} & 你是一位乐于助人的助手,擅长语法纠正。 \\ [0.2cm]
{\color{gray} \footnotesize{用户}} & 请将下面的句子修改为语法正确的英文: \\ [0.1cm]
& 输入:She don't like going to the park. \\
& 输出:\underline{\hspace{2em}}
\end{tabular}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
上述提示中没有给出任何示例,仅凭指令和系统信息引导模型完成任务。这种不提供示例、直接要求模型解决问题的提示方式,即为零样本提示。关于这一方法的更多讨论,以及通过增加示例实现的单样本和少样本学习,将在下一节详细展开。
\subsubsection{上下文学习}
学习可以在推理过程中发生。上下文学习(in-context learning)正是这样一种方法:提示中包含若干问题求解的演示,LLM从这些演示中“学习”如何解决新问题。由于这一过程不更新模型参数,上下文学习可被视为无需额外训练或微调,便能有效激活和重组预训练知识的手段。这使得LLM能够快速适应新任务,拓展了预训练模型在无任务特定调整下的能力边界。
我们可以通过对比零样本学习、单样本学习和少样本学习来具体说明上下文学习。在上一节中,我们已经看到了一个语法纠错的零样本学习示例——它直接要求模型纠正句子,不提供任何示例。在单样本学习中,我们添加一个已完成的纠错示例作为演示,让LLM从这个新增的经验中学习:
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begingroup
\renewcommand{\arraystretch}{1.0}
\setlength{\tabcolsep}{6pt}
\begin{tabular}{r l}
{\color{gray} \footnotesize{系统}} & 你是一位乐于助人的助手,擅长语法纠正。 \\ [0.2cm]
{\color{gray} \footnotesize{用户}} & 请将下面的句子修改为语法正确的英文: \\ [0.1cm]
& 输入:There is many reasons to celebrate. \\
& 输出:There are many reasons to celebrate. \\ [0.2cm]
{\color{gray} \footnotesize{用户}} & 请将下面的句子修改为语法正确的英文: \\ [0.1cm]
& 输入:She don't like going to the park. \\
& 输出:\underline{\hspace{2em}}
\end{tabular}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
在此基础上增加更多演示,便构成少样本学习:
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begingroup
\renewcommand{\arraystretch}{1.0}
\setlength{\tabcolsep}{6pt}
\begin{tabular}{r l}
{\color{gray} \footnotesize{系统}} & 你是一位乐于助人的助手,擅长语法纠正。 \\ [0.2cm]
{\color{gray} \footnotesize{用户}} & 请将下面的句子修改为语法正确的英文: \\ [0.1cm]
& 输入:There is many reasons to celebrate. \\
& 输出:There are many reasons to celebrate. \\ [0.2cm]
{\color{gray} \footnotesize{用户}} & 请将下面的句子修改为语法正确的英文: \\ [0.1cm]
& 输入:Me and my friend goes to the gym every day. \\
& 输出:My friend and I go to the gym every day. \\ [0.2cm]
{\color{gray} \footnotesize{用户}} & 请将下面的句子修改为语法正确的英文: \\ [0.1cm]
& 输入:She don't like going to the park. \\
& 输出:\underline{\hspace{2em}}
\end{tabular}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
在少样本学习中,我们实质上提供了一个从输入到输出的映射模式,LLM尝试遵循这一模式进行预测。上下文学习的有效性在很大程度上依赖于提示质量和模型的基础能力:一方面,我们需要通过提示工程精心设计演示内容,以帮助模型更有效地从中学习;另一方面,能力更强的LLM往往能更好地利用上下文学习执行新任务。例如,若希望LLM将因纽特语翻译为英语,但模型在预训练阶段严重缺乏因纽特语数据,那么无论怎样设计提示,都很难获得高质量的翻译。此时,更好的策略是用更多相关数据继续训练模型,而非一味优化提示。
关于上下文学习为何在预训练过程中涌现,以及为何在推理时起作用,一个直观的理解是:预训练模型已经获得了解决问题的能力,但面对新问题时可能同时存在多种合理的预测路径。提供演示可以引导模型遵循“正确”的路径。此外,研究者也从贝叶斯推断 \cite{xie-etal:2022an}、梯度下降 \cite{dai-etal:2023can,von-etal:2023transformers}、线性回归 \cite{akyurek-etal:2023what} 和元学习 \cite{garg-etal:2022can} 等角度对上下文学习进行了理论解释。
\subsection{进阶提示方法}
\label{sec:advanced-prompting}
\noindent 我们已经介绍了与 LLM 提示相关的基本概念,并展示了许多用于 NLP 任务的提示。现在我们来探讨几种增强提示有效性的技术。
\subsubsection{提示工程策略}
\noindent 设计提示是高度经验性的。总的来说,对于同一任务,有很多方法可以提示LLM,我们需要进行多次试错才能找到一个令人满意的提示。为了更高效地编写好的提示,可以遵循某些策略。常见的提示原则示例包括:
\begin{itemize}
\item \vspace{0.5em} \textbf{尽可能清晰地描述任务}。当我们应用LLM解决问题时,我们需要提供对问题的精确、具体、清晰的描述,并指示LLM按照我们的期望执行。当我们希望LLM的输出满足某些期望时,这一点尤其重要。例如,假设我们对气候变化感到好奇。一个要求LLM提供一些信息的简单提示是
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\hspace{2em} 告诉我关于气候变化的信息。
\hspace{2em} \underline{\hspace{2em}}
\end{tcolorbox}
\vspace{0.5em}
由于这个指令太过笼统,LLM可能会生成一个涉及气候变化任何方面的回应,而这可能与我们的特定兴趣不符。在这种情况下,我们可以使用更具体、更详细的提示。现在,假设我们打算向一个10岁的孩子解释气候变化。一个这样的例子是
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begingroup
\setlength{\leftskip}{2em}
\setlength{\rightskip}{2em}
向一个10岁的孩子解释气候变化的原因和影响。谈谈它如何影响天气、海平面和气温。同时,提及人们正在做的一些有助于解决问题的事情。请尝试用简单的语言解释,并且不超过500个词。
\underline{\hspace{2em}}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
\item \vspace{0.3em} \textbf{引导LLM思考}。LLM已经展现出了惊人的“思考”能力。一个常见的例子是,发展成熟的LLM在被认为极具挑战性的数学推理任务中取得了令人印象深刻的表现。在提示工程中,LLM的“思考”能力需要通过适当的提示来激活,特别是对于那些需要大量推理的问题。在许多情况下,被指示“思考”的LLM与被指示直接执行任务的同一个LLM相比,可以产生完全不同的结果。例如,\citet{kojima-etal:2022large}发现,仅仅在每个提示的末尾附加“Let's think step by step”就可以提高LLM在多个推理任务上的性能。有多种方法可以提示LLM进行“思考”。一种方法是指示LLM在得出最终答案之前,生成推理问题的步骤。例如,考虑一个解决数学问题的任务。下面是该任务的一个简单提示。
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begingroup
\setlength{\leftskip}{2em}
\setlength{\rightskip}{2em}
你是一位数学家。你将收到一个数学问题。请解决这个问题。
\underline{\hspace{2em}}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
由于解决数学问题需要详细的推理过程,如果LLM试图直接得出答案,很可能会犯错。因此,我们可以明确要求LLM在得出结论之前遵循给定的推理过程。
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begingroup
\setlength{\leftskip}{2em}
\setlength{\rightskip}{2em}
你是一位数学家。在解决数学问题时,你将遵循以下详细的推理步骤。
\vspace{0.5em}
第1步:问题解读。
数学家仔细倾听你的问题,并理解你提出的数学挑战的复杂细节。
\vspace{0.5em}
第2步:策略制定。
数学家凭借其渊博的知识,选择针对特定类型数学问题(无论是代数、微积分还是几何)的最有效策略。
\vspace{0.5em}
第3步:详细计算。
数学家以精准和专业的方式,逐步执行必要的计算,并遵守所有数学原则。
\vspace{0.5em}
第4步:解答回顾。
在提供最终答案之前,数学家会仔细检查计算的准确性,并为解答提供简洁的解释或理由。
\vspace{0.5em}
你将收到一个数学问题。请解决这个问题。
\vspace{0.5em}
$\{*\mathrm{problem}*\}$
\vspace{0.5em}
\underline{\hspace{2em}}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
另一种方法是通过多轮交互,先让模型给出初步答案,再提示其评估、修正,从而引出更可靠的最终结果。
\item \vspace{0.3em} \textbf{提供参考信息}。如前一节所述,我们可以在提示中包含示例,让LLM通过这些示例进行情境学习,从而了解如何执行任务。事实上,鉴于LLM卓越的语言理解能力,我们可以在提示中加入任何类型的文本,这样模型就可以基于更丰富的上下文进行预测。在许多应用中,我们拥有与用户查询相关的各种信息。我们通常不希望LLM进行无约束的预测,而是希望LLM生成的输出局限于相关文本。一个这样的例子是RAG,其中用户查询的相关文本是通过调用IR系统提供的,然后我们提示LLM基于这些提供的相关文本生成回应。下面的提示展示了一个例子。
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begingroup
\setlength{\leftskip}{2em}
\setlength{\rightskip}{2em}
你是一位能为输入查询生成答案的专家。现在你收到了一个查询和相应的上下文信息。请基于此上下文信息生成答案。注意,你需要用自己的话来提供答案,而不是简单地从提供的上下文中复制。
\vspace{0.5em}
上下文信息: $\{*\mathrm{IR\textrm{-}result}*\}$
\vspace{0.3em}
查询: $\{*\mathrm{query}*\}$
\vspace{0.3em}
\underline{\hspace{2em}}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
在处理现实世界的问题时,我们通常拥有有助于产生更好答案的先验知识和额外信息。在提示中考虑这些信息通常有助于改善结果。当上下文足够可靠时,甚至可以要求模型仅基于给定文本回答,避免幻觉。
\end{itemize}
\vspace{0.5em}
上面,我们只讨论了几种编写好提示的策略。此外,提示的呈现格式(如使用字段、引号或代码风格)也对结果有显著影响,这些细节需要在实践中不断体会。当然,还有很多这样的方法,需要通过实践来形成自己的方法。感兴趣的读者可以参考各种在线文档以获取更多信息,例如OpenAI关于GPT系列模型的手册\footnote{参见 \url{https://platform.openai.com/docs/guides/prompt-engineering/six-strategies-for-getting-better-results}}
\subsubsection{思维链}
\label{sec:chain-of-thought}
\noindent 思维链(\mindex{CoT})使 LLM 能够针对复杂问题生成逐步的推理过程,从而以更接近人类认知的方式处理任务。与直接输出结论不同,CoT 要求 LLM 自行产生推理步骤,或从提示中给出的详尽推理演示中学习。为具体说明 CoT,不妨考虑文献中常用的一类代数计算问题。设问题为:
\vspace{0.8em}
\hspace{5em} \textit{计算数字 2、4 和 6 的平均值。}
\vspace{0.8em}
\noindent 可直接将该问题作为输入,提示 LLM 作答。
\vspace{0.5em}
\begin{tcolorbox}[frame style={draw=none}]
\begingroup
\setlength{\leftskip}{2em}
\setlength{\rightskip}{2em}
Q: Please calculate the average of the numbers $2$, $4$, and $9$.
\vspace{0.3em}
A: \underline{The answer is $6$.}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
此时 LLM 通常难以直接给出正确结果。一种简单的改进措施是在提示中加入同类问题的问答示例,使 LLM 从中获得参照。
\vspace{0.5em}
\begin{tcolorbox}[frame style={draw=none}]
\begingroup
\setlength{\leftskip}{2em}
\setlength{\rightskip}{2em}
Q: Please calculate the average of the numbers $1$, $3$, $5$, and $7$.
\vspace{0.3em}
A: The answer is $4$.
\vspace{0.7em}
Q: Please calculate the average of the numbers $2$, $4$, and $9$.
\vspace{0.3em}
A: \underline{The answer is $7$.}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
然而,仅提供问答对仍不足以使 LLM 掌握正确的推理方式。CoT 方法的核心理念在于,LLM 不仅学习问题和答案之间的映射关系,还能从推导答案所涉及的详细步骤中获益。为此,可在提示中加入若干推理步骤,构造 CoT 风格的提示。
\vspace{0.5em}
\begin{tcolorbox}[frame style={draw=none}]
\begingroup
\setlength{\leftskip}{2em}
\setlength{\rightskip}{2em}
Q: Please calculate the mean square of the numbers 1, 3, 5, and 7.
\vspace{0.3em}
A: \textcolor[RGB]{154,245,175}{Calculate the square of each number: $1^2 = 1$, $3^2=9$, $5^2=25$, and $7^2=49$. Sum the squares, $1+9+25+49=84$. There are $4$ numbers in total. Divide the sum by the number of items, $84 / 4 = 21$.} The answer is $21$.
\vspace{0.7em}
Q: Please calculate the average of the numbers $2$, $4$, and $9$.
\vspace{0.3em}
A: \underline{\textcolor[RGB]{154,245,175}{Calculate $2 + 4 + 9$, which equals $15$. There are three numbers. Divide the}}
\underline{\textcolor[RGB]{154,245,175}{total sum by the count, resulting in $15/3=5$.} The answer is $5$.}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
\noindent 上例中以绿色标示的部分即为推理步骤。通过展示同类问题的完整推理过程,LLM 得以学会如何进行逻辑推导,从而生成通向正确答案的解题路径。
\begin{importantnote}
思维链(CoT)是提升复杂推理最有效的提示技巧,其本质是强制模型生成中间推理步骤,将多步问题拆解为可验证的序列。无论是少样本(提供推理示例)还是零样本(添加“逐步思考”指令),都能显著提高数学、逻辑等任务的正确率。关键在于确保推理步骤完整且连贯,而非仅关注最终答案。
\end{importantnote}
CoT 提示在实际应用中表现出若干优势。其一,CoT 将复杂问题拆解为一系列顺序化的子步骤,这一过程在一定程度上模拟了人类解决复杂问题的行为模式,因此尤其适用于需要多步推理的任务。其二,CoT 使推理过程更加透明,所有中间步骤均可见,便于使用者理解并解释结论的形成依据。其三,当使用者能够看到并理解推理逻辑时,其对模型输出的信任度往往会提高,这在医学、教育和金融等对可解释性要求较高的领域尤为重要。
上述方法需要提供一个或多个包含推理步骤的示例,通常称为少样本 CoT。与之相对,零样本 CoT 无需提供示例,而是通过在提示中加入特定指令,要求 LLM 执行逐步推理。例如,下面给出一个零样本 CoT 提示的实例。
\vspace{0.5em}
\begin{tcolorbox}[frame style={draw=none}]
\begingroup
\setlength{\leftskip}{2em}
\setlength{\rightskip}{2em}
Q: Please calculate the average of the numbers $2$, $4$, and $9$.
\vspace{0.3em}
A: \textcolor[RGB]{154,245,175}{Let's think step-by-step.}
\vspace{0.3em}
\uline{We have three numbers: $2$, $4$, and $9$. Add these numbers together, $2+4+9=15$. Determine how many numbers there are, which in this case is three. The average is calculated by dividing the total sum by the number of elements. Completing the division gives $15/3 = 5$. So the answer is $5$.}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
在“让我们一步一步思考”这一指令的引导下,LLM 被要求生成详细的推理步骤。\citet{kojima-etal:2022large} 的研究指出,此类指令有时会使 LLM 只输出推理过程而不给出最终结论。此时,可通过第二轮提示将第一轮的输入和输出结合,再交由 LLM 继续生成,从而提取出正确答案。此外,除“一步一步思考”外,其他类似指令(如“请先展示思考过程”或“让我们有逻辑地分析”)同样可用于引导推理。
在结束本节之前,有必要审视 CoT 在实际应用中面临的若干局限。其一,少样本 CoT 依赖高质量的多步推理示例,而这些示例的获取(无论是自动还是手动)均存在一定难度。其二,缺乏将复杂问题分解为适当子步骤的标准方法论,这在很大程度上依赖于使用者的经验。其三,中间步骤的累积错误可能对最终结论的准确性产生不利影响。关于 CoT 优缺点更为全面的讨论,可参考近年来的综述文献 \cite{chu-etal:2023survey,yu-etal:2023towards,zhang-etal:2023igniting}
\subsection{学习提示}
\label{sec:learning-prompts}
\noindent 前面介绍的提示策略均为人工设计。手工设计高质量提示不仅费力,而且存在明显局限:第一,不同大语言模型对提示的敏感程度各异,要找到通用而有效的提示往往需要大量试错,成本高昂;第二,完全依赖人类直觉容易遗漏那些不太直观但可能十分有效的提示,限制了提示的多样性;第三,人工编写的提示时常复杂冗长,既增加了推理计算开销,也不一定带来性能增益。
为克服这些问题,研究者提出了一系列自动学习提示的技术,目标是自动创建、优化和表示提示,以便更高效地解决下游任务。围绕这一目标,本节讨论以下两个核心问题:
\begin{itemize}
\item 如何为语言模型自动设计和优化提示?
\item 除文本字符串外,提示是否还有其他表示形式?如何学习这些表示?
\end{itemize}
\subsubsection{提示优化}
\label{sec:prompt-optimization}
\noindent 自动提示优化的目标是用算法发现针对特定任务的最优离散提示,这可以视为\mindex{自动机器学习} (AutoML) 的一种实例,与\mindex{神经架构搜索} (NAS) 的思路颇为相似。一个通用的提示优化框架通常包含三个关键组件:
\begin{itemize}
\item \textbf{提示搜索空间}。定义所有可供探索的提示。例如,可以围绕若干种子提示,通过编辑、扩展等方式生成多样化的候选。
\item \textbf{性能评估}。对每个候选提示,将其送入语言模型并在验证集上衡量下游任务表现,以此作为提示质量的度量。
\item \textbf{搜索策略}。决定如何遍历搜索空间。通常采用迭代方式:每一步选取当前最有希望的一组提示进行评估,并据此生成新的候选,循环往复,直至输出表现最佳的提示。
\end{itemize}
目前广泛使用的一类方法利用大语言模型自身来实现上述组件,形成\mindex{基于大语言模型的提示优化}。以 \citet{zhou-etal:2023large} 的工作为例,该方法交替执行以下步骤:
\begin{itemize}
\item \textbf{初始化}。构造候选提示池。既可以由人工编写少量初始提示,也可以借助语言模型,根据任务描述或少量输入-输出示例自动生成初始提示。
\item \textbf{评估}。将候选池中的每个提示喂给语言模型,在验证集上计算预定义的性能指标(如准确率或对数似然),得到对应的质量分数。
\item \textbf{修剪}。若候选池过大,可依据评估分数保留得分较高的一部分提示,舍弃其余,以减轻后续步骤的计算负担。
\item \textbf{扩展}。以当前保留的提示为基础,利用语言模型生成语义相近或有变化的新提示(例如通过改写、局部编辑等方式),并加入候选池,从而不断探索更广的搜索空间。
\end{itemize}
评估、修剪、扩展三步可以反复进行,使系统在迭代中逐步发现更优的提示。扩展阶段的具体实现可以多种多样:它既可以是对提示进行释义或施加编辑操作,也可以引入反馈-修订循环,让语言模型根据输出质量对提示进行自我修正。但无论形式如何变化,核心理念始终一致——将提示设计转化为一个可由语言模型驱动的搜索与优化问题。
需要指出的是,提示实际上具有结构化特征,通常包含指令、示例等多个组成部分。目前大多数自动提示优化工作侧重于学习更好的\textbf{指令},即生成能有效引导模型完成任务的文本说明;同时,研究也在向自动选择或生成示例等方向延伸。
\subsubsection{软提示}
\label{sec:soft-prompts}
\noindent 自然语言提示(即硬提示)虽然直观,但往往冗长且计算开销大,反复输入相同长提示更是低效。注意到硬提示作为离散 token 序列,实际上会被大语言模型编码为连续向量,这引出一个自然的问题:能否直接使用这些连续表示作为更紧凑的提示?为此,我们引入 \mindex{软提示}(soft prompt)的概念,视其为提示的隐藏、分布式表示。与人类可读的硬提示不同,软提示是模型内部的、可适应的隐式模式,以实值向量形式存在,更便于模型处理。
\vspace{0.5em}
\begin{tcolorbox}[frame style={draw=none}]
\begingroup
\setlength{\leftskip}{2em}
\setlength{\rightskip}{2em}
Translate the sentence into Chinese.
\vspace{0.5em}
Consider it done!
\vspace{0.5em}
\underline{\hspace{2em}}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
\begin{figure}[!t]
\centering
\input{figures/Chapter2/figure-hard-prompt-and-soft-prompt}
\caption{硬提示与软提示示意图。这里,硬提示是我们为了执行任务而输入给 LLM 的指令。LLM 正常对该指令进行编码,其对应的中间表示可被视为一种软提示。}
\label{fig:hard-and-soft-prompt-examples}
\end{figure}
\noindent 例如,提示 ``Translate the sentence into Chinese.'' 可视为硬提示,其 token 序列经 LLM 转换为向量序列 $\mathbf{h}_1...\mathbf{h}_5$,这些向量即可看作一种软提示(图 \ref{fig:hard-and-soft-prompt-examples})。但软提示不必来自自然语言的翻译;它们可以完全脱离具体文本,直接作为连续参数学习得到。这样的表示密集、低维且可优化,训练和推理成本远低于长硬提示,在重复使用同一提示的场景中尤为实用。
上述思想催生了一系列通过学习连续向量来高效适配大语言模型的方法。典型代表是参数高效微调中的软提示:\textbf{前缀微调}(prefix tuning)在每一 Transformer 层的输入前添加可训练的前缀向量作为软提示~\cite{li-liang:2021prefix}\textbf{提示微调}(prompt tuning)则仅在嵌入层加入可学习的软提示嵌入,而保持模型其余参数不变~\cite{lester-etal:2021power}。这些软提示通过与原有表示交互,将任务知识注入模型,以极低的参数成本实现适配。
另一类方法从上下文压缩的视角学习软提示。为替代冗长的指令或示例,可将完整上下文压缩为少量连续向量(软提示),并通过知识蒸馏或循环聚合训练得到。例如,\citet{chevalier-etal:2023adapting} 将长上下文划分为片段,逐步使用摘要 token 累积记忆,最终生成固定大小的软提示表示整个上下文。此类压缩表示进一步拓展了软提示在高效推理中的应用。
总而言之,软提示架起了高效计算与灵活任务适配的桥梁,已成为提示工程与参数高效微调的核心概念之一。
\section{大模型微调技术}
\input{section/3.3sub/part1}
\subsection{有监督微调方法}
\input{section/3.3sub/part2}
\subsection{高效微调方法}
\input{section/3.3sub/part3}
% \subsection{微调概述}
% \noindent 一旦我们预训练好一个 LLM,我们就可以应用它来执行各种 NLP 任务。传统上,语言模型被用作其他系统的组件,例如,它们被广泛应用于为统计机器翻译系统中的翻译打分。相比之下,在生成式 AI 中,LLM 被视为完整的系统,并通过利用其生成特性来解决 NLP 问题。一种常见的方法是用文本描述我们想要解决的任务,然后提示 LLM 根据这个描述生成文本。这是一个标准的文本生成任务,我们从给定的上下文中继续或补全文本。
% 更正式地说,设 $\mathbf{x}=x_0...x_m$ 表示用户给出的上下文的 token 序列,$\mathbf{y}=y_1...y_n$ 表示上下文之后的 token 序列。那么,LLM 的推理可以定义为基于 $\mathbf{x}$ 寻找最可能序列 $\mathbf{y}$ 的问题:
% \begin{eqnarray}
% \hat{\mathbf{y}} & = & \argmax_{\mathbf{y}} \log \Pr(\mathbf{y} | \mathbf{x}) \nonumber \\
% & = & \argmax_{\mathbf{y}} \sum_{i=1}^{n} \log \Pr(y_i|x_0,...,x_m,y_1,...,y_{i-1})
% \end{eqnarray}
% \noindent 这里的 $\sum_{i=1}^{n} \log \Pr(y_i|x_0,...,x_m,y_1,...,y_{i-1})$ 实质上与等式 (\ref{eq:lm-basic-form-log}) 的右侧表达的是同一件事。它建模的是从位置 $m+1$ 而不是位置 $0$ 开始预测 token 的对数概率。在本章及后续章节中,我们将使用独立的变量 $\mathbf{x}$ 和 $\mathbf{y}$ 来区分 LLM 的输入和输出,尽管它们可以被看作是同一序列的子序列。采用这种表示法,我们可以看到上述等式的形式与 NLP 中其他文本生成模型(如神经机器翻译模型)中使用的形式非常相似。
% 为了说明 LLM 是如何应用的,考虑确定一个给定句子的语法正确性的问题。我们可以定义一个这样的模板
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \hspace{4em} \{*sentence*\}
% \hspace{4em} Question: Is this sentence grammatically correct?
% \hspace{4em} Answer: \underline{\hspace{2em}}
% \end{tcolorbox}
% \vspace{0.5em}
% \noindent 这里的 $\underline{\hspace{1em}}$ 代表我们打算生成的文本。\{*sentence*\} 是一个占位符变量,它将被用户提供的实际句子替换。例如,假设我们有一个句子 ``\textit{John seems happy today.}''。我们可以用这个句子替换模板中的 \{*sentence*\},从而得到语言模型的输入
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \hspace{4em} John seems happy today.
% \hspace{4em} Question: Is this sentence grammatically correct?
% \hspace{4em} Answer: \underline{\hspace{2em}}
% \end{tcolorbox}
% \vspace{0.5em}
% 为了执行该任务,语言模型会接收到上下文 $\mathbf{x}=$``John seems happy today .$\backslash$n Question : Is this sentence grammatically correct?$\backslash$n Answer :''\footnote{$\backslash$n 是用于换行的特殊字符。}。然后它会根据该上下文生成以下文本作为答案。例如,如果 ``Yes'' 这个文本是在给定上下文下具有最大预测概率的文本,语言模型就可能输出 ``Yes''(即 $\mathbf{y} = \textrm{``Yes''}$)。
% 同样地,我们可以定义更多的模板来解决其他任务。例如,我们可以使用以下模板将一个英文句子翻译成中文
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \hspace{4em} \{*sentence*\}
% \hspace{4em} Question: What is the Chinese translation of this English sentence?
% \hspace{4em} Answer: \underline{\hspace{2em}}
% \end{tcolorbox}
% \vspace{0.5em}
% \noindent 或者使用一个类似指令的模板
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \hspace{4em} \{*sentence*\}
% \hspace{4em} Translate this sentence from English into Chinese.
% \hspace{4em} \underline{\hspace{2em}}
% \end{tcolorbox}
% \vspace{0.5em}
% \noindent 或者使用一个类似代码的模板。
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \hspace{4em} [src-lang] = English [tgt-lang] = Chinese [input] = \{*sentence*\}
% \hspace{4em} [output] = \underline{\hspace{2em}}
% \end{tcolorbox}
% \vspace{0.5em}
% 上述模板提供了一种简单而有效的方法来``提示''单个 LLM 执行各种任务,而无需调整模型的结构。然而,这种方法要求 LLM 能够识别并遵循指令或问题。一种方法是将包含指令及其相应响应的训练样本整合到预训练数据集中。虽然这种方法很直接,但从头开始构建和训练 LLM 的计算成本非常高。此外,要使指令遵循数据对预训练有效,需要大量此类数据,但为所有感兴趣的任务收集大规模标记数据非常困难。
% 第二种方法是通过微调来适配 LLM,这已成为近期研究中事实上的标准。这样,在预训练阶段学到的 token 预测能力就可以泛化到完成新任务上。微调背后的思想是,在预训练中已经获得了一些通用的语言知识,但我们需要一种机制来激活这些知识,以便将其应用于新任务。为此,我们可以使用指令遵循数据对模型参数进行轻微的微调。这种方法被称为 \mindex{instruction fine-tuning}。
% 一个指令微调样本,由一个 token 序列表示,可以看作是一个由输入和期望输出组成的元组。这里的输入包括指令、系统信息(或系统前缀)以及任何其他用户提供的信息\footnote{系统信息是指在输入开头添加的一系列 token,用于引导 LLM 的行为,例如,\textit{你是一个乐于助人的助手,不应输出有害内容}。}。为了说明这一点,请看以下示例(蓝色文本=输入,下划线文本=输出)。
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begin{center}
% \parbox{0.9\textwidth}
% {
% {\color{blue} Read the following sentence, and answer whether it is grammatically correct.}
% {\color{blue} LLMs are powerful models but are expensive to build.}
% \vspace{0.3em}
% \underline{Yes}
% }
% \end{center}
% \end{tcolorbox}
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begin{center}
% \parbox{0.9\textwidth}
% {
% {\color{blue} LLMs are powerful models but are expensive to build.}
% {\color{blue} Does this sentence make sense grammatically?}
% {\color{blue} Answer Option}
% {\color{blue} - Yes}
% {\color{blue} - No}
% \vspace{0.3em}
% \underline{Yes}
% }
% \end{center}
% \end{tcolorbox}
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begin{center}
% \parbox{0.9\textwidth}
% {
% {\color{blue} Yes or No? Yes = the following sentence is grammatically correct. No = it contains grammatical errors.}
% {\color{blue} LLMs are powerful models but are expensive to build.}
% \vspace{0.3em}
% \underline{Yes}
% }
% \end{center}
% \end{tcolorbox}
% \vspace{0.5em}
% 所有这些样本都描述了相同的二分类任务,但使用了不同的指令。为了增加微调数据的多样性并使 LLM 具有泛化能力,我们可以使用指令定义更多的任务。
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begin{center}
% \parbox{0.9\textwidth}
% {
% {\color{blue} Write a short story about three characters.}
% {\color{blue} Character 1: A boy whose name is Andy.}
% {\color{blue} Character 2: Andy's dog.}
% {\color{blue} Character 3: Andy's friend, Jane.}
% \vspace{0.3em}
% \uline{In the heart of a bustling city, Andy, his loyal dog Rocket, and his friend Jane discovered a hidden garden beneath the glow of twilight. Surrounded by ancient trees and whispering winds, they shared dreams and secrets, forging an unbreakable bond. As the night deepened, they made a pact under the stars to always find magic in the mundane. Together, they realized that true adventure lay in the moments spent with friends, and every day was a treasure waiting to be discovered.}
% }
% \end{center}
% \end{tcolorbox}
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begin{center}
% \parbox{0.9\textwidth}
% {
% {\color{blue} Show me a recipe for making ice cream.}
% \vspace{0.3em}
% \underline{Ingredients:}
% \underline{2 cups heavy cream; 1 cup milk; 3/4 cup sugar; 2 tsp vanilla extract;}
% \underline{Pinch of salt.}
% \underline{Steps:}
% \underline{1. Whisk all ingredients until sugar dissolves.}
% \underline{2. Chill mixture for 2 hours in the fridge.}
% \underline{3. Freeze in a shallow dish, stirring every 30 minutes, 3-4 times.}
% \underline{4. Freeze solid for 2 hours.}
% \underline{5. Serve your homemade ice cream.}
% }
% \end{center}
% \end{tcolorbox}
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begin{center}
% \parbox{0.9\textwidth}
% {
% {\color{blue} If you buy 5 apples and each apple costs \$1.20, how much do you spend in total? }
% \vspace{0.3em}
% \underline{\$6.00}
% }
% \end{center}
% \end{tcolorbox}
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begin{center}
% \parbox{0.9\textwidth}
% {
% {\color{blue} Write a Python program to calculate the sum of squares of the following numbers.}
% {\color{blue} 1 , 2 , 10 , -9 , 78}
% \vspace{0.3em}
% \underline{numbers = [1,2,10,-9 ,78]}
% \underline{sum\_of\_squares = sum(x**2 for x in numbers)}
% \underline{print(sum\_of\_squares)}
% }
% \end{center}
% \end{tcolorbox}
% \vspace{0.5em}
% 要获得指令遵循能力,需要一定量的微调数据。这些数据可能包括多样的指令和可能的响应。研究发现,扩展微调任务的数量有助于提高 LLM 的性能 \cite{chung-etal:2022scaling}。请注意,虽然更多的微调数据是有利的,但其数据量通常比预训练数据小几个数量级。例如,LLM 可以用数万或数十万个样本进行微调,如果这些样本质量很高,甚至可以用更少的样本 \cite{zhou-etal:2023lima,chen-etal:2023alpagasus},而预训练这类模型可能需要数十亿或数万亿个 token,导致计算需求大得多,训练时间也更长 \cite{touvron-etal:2023llama}。
% 同样值得注意的是,我们不应期望微调数据能覆盖我们打算应用 LLM 的所有下游任务。对于预训练+微调方法如何工作的一个普遍理解是,LLM 在预训练阶段已经获得了理解指令和生成响应的知识。然而,这些能力直到我们引入某种形式的监督后才被完全激活。当我们用相对少量的标记数据对模型进行微调时,通用的指令遵循行为就会出现。结果是,我们可以实现一定程度的 \mindex{zero-shot learning}:经过微调的模型可以处理那些它们没有被明确训练或微调过的新任务 \cite{sanh-etal:2022multitask,wei-etal:2022finetuned}。这种零次学习能力将生成式 LLM 与早期的预训练模型(如 BERT)区分开来,后者主要针对特定任务进行微调。
% 一旦我们准备好了一系列指令描述的数据,微调过程就相对简单了。这个过程可以看作是一个标准的训练过程,与预训练类似,但使用的是一个规模小得多的训练数据集。设 $\mathcal{D}_{\mathrm{tune}}$ 为微调数据集,$\hat{\theta}$ 为通过预训练优化的模型参数。我们可以修改等式 (\ref{eq:llm-training-mle}) 来获得微调的目标
% \begin{eqnarray}
% \tilde{\theta} & = & \argmax_{\hat{\theta}^+} \sum_{\mathrm{sample} \in \mathcal{D}_{\mathrm{tune}}} \mathcal{L}_{\hat{\theta}^+}(\mathrm{sample}) \label{eq:llm-fine-tuning-mle}
% \end{eqnarray}
% \noindent 这里的 $\tilde{\theta}$ 表示最优参数。符号 $\hat{\theta}^+$ 的使用意味着微调是从预训练参数 $\hat{\theta}$ 开始的。
% 对于每个 $\mathrm{sample} \in \mathcal{D}_{\mathrm{tune}}$,我们将其分为一个输入段 $\mathbf{x}_{\mathrm{sample}}$ 和一个输出段 $\mathbf{y}_{\mathrm{sample}}$,即,
% \begin{eqnarray}
% \mathrm{sample} & = & [\mathbf{y}_{\mathrm{sample}},\mathbf{x}_{\mathrm{sample}}]
% \end{eqnarray}
% 然后我们将损失函数定义为
% \begin{eqnarray}
% \mathcal{L}_{\hat{\theta}^+}(\mathrm{sample}) & = & -\log \mathrm{Pr}_{\hat{\theta}^+}(\mathbf{y}_{\mathrm{sample}}|\mathbf{x}_{\mathrm{sample}})
% \end{eqnarray}
% \noindent 换句话说,我们计算的是子序列 $\mathbf{y}_{\mathrm{sample}}$ 上的损失,而不是整个序列的损失。在该等式的反向传播的实际实现中,序列 $[\mathbf{y}_{\mathrm{sample}},\mathbf{x}_{\mathrm{sample}}]$ 像往常一样在前向传播中构建。然而,在反向传播中,误差梯度只通过网络中对应于 $\mathbf{y}_{\mathrm{sample}}$ 的部分进行反向传播,网络的其余部分保持不变。作为一个例子,考虑一个序列
% \begin{equation}
% \underbrace{\textrm{{\color{blue} $\langle s \rangle$ Square this number .}}\ \textrm{{\color{blue} $2$ .}}}_{\textrm{Context (Input)}}\ \ \underbrace{\textrm{\underline{The result is $4$ .}}}_{\textrm{Prediction (Output)}}\nonumber
% \end{equation}
% \noindent 损失仅针对 \underline{The result is $4$ .} 计算和反向传播。
% 指令微调也需要大量的工程工作。为了获得满意的结果,可能需要尝试不同的学习率、批量大小、微调步数等设置。这通常需要多次微调运行和评估。微调的成本和实验工作量仍然至关重要,不应被忽视,尽管它们远低于预训练阶段。
% 虽然我们在这里以指令微调作为一个说明性例子,但微调技术在开发各种 LLM 中扮演着重要角色,并且应用更为广泛。例子包括使用对话数据将 LLM 微调为聊天机器人,以及适配这些模型以处理非常长的序列。微调的广泛应用促使研究人员改进这些技术,例如设计更高效的微调算法。虽然关于微调的研究成果丰硕,但在本节中我们只是浅尝辄止地介绍了所涉及的关键步骤。我们将在后续章节中看到关于这个主题的更详细的讨论。
\subsection{微调概述}
\noindent 预训练完成后,大型语言模型(LLM)可被直接用于解决各类 NLP 任务。其工作方式本质上是\textbf{文本补全}:给定一段上下文,模型生成最可能的后续文本。形式化地,设上下文为 token 序列 $\mathbf{x}=x_0...x_m$,待生成序列为 $\mathbf{y}=y_1...y_n$,则推理过程可定义为
\begin{eqnarray}
\hat{\mathbf{y}} & = & \argmax_{\mathbf{y}} \log \Pr(\mathbf{y} | \mathbf{x}) \nonumber \\
& = & \argmax_{\mathbf{y}} \sum_{i=1}^{n} \log \Pr(y_i|x_0,...,x_m,y_1,...,y_{i-1}) \label{eq:inf}
\end{eqnarray}
这与标准语言模型的对数概率形式完全一致,区别仅在于预测从位置 $m+1$ 开始。因此,只需将任务包装成合适的\textbf{提示模板},就能让同一个 LLM 处理多种问题。例如,判断句子语法是否正确,可构造如下输入:
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\hspace{4em} John seems happy today.
\hspace{4em} Question: Is this sentence grammatically correct?
\hspace{4em} Answer: \underline{\hspace{2em}}
\end{tcolorbox}
\vspace{0.5em}
模型在上下文后生成 ``Yes'' 即给出了判断。同样地,机器翻译可以使用 ``Translate this sentence from English into Chinese:'' 这样的指令模板,甚至可以写成类似代码的配置块。这种统一范式的强大之处在于,无需为每个任务设计专门的模型结构。
然而,让 LLM 稳定地“听懂”指令并非天然具备的能力。一种直接的思路是将大量(指令,响应)样本混入预训练数据,但这需要海量标注,且从头训练的计算代价极高。因此,目前主流且高效的做法是\textbf{指令微调}(instruction fine-tuning):在预训练模型的基础上,用一套规模小得多但质量高的指令数据集 $\mathcal{D}_{\text{tune}}$ 对参数进行轻微调整,从而激活模型在预训练中已习得的通用语言知识,使其泛化到各种指令驱动的任务上。
\textbf{指令微调数据}由输入-输出对构成。输入部分融合了任务指令、可选的系统提示以及用户提供的内容,输出部分则为对应的标准回答。以下给出了几个典型的微调样本,它们覆盖了分类、生成、推理和代码等不同任务,并且同一任务(如语法判别)可以对应风格迥异的指令,从而增加数据多样性:
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begin{center}
\parbox{0.9\textwidth}{
{\color{blue} Read the following sentence, and answer whether it is grammatically correct.}
{\color{blue} LLMs are powerful models but are expensive to build.}
\vspace{0.3em}
\underline{Yes}
}
\end{center}
\end{tcolorbox}
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begin{center}
\parbox{0.9\textwidth}{
{\color{blue} Write a short story about three characters: a boy named Andy, his dog, and his friend Jane.}
\vspace{0.3em}
\uline{In the heart of a bustling city, Andy, his loyal dog Rocket, and his friend Jane discovered a hidden garden beneath the glow of twilight...}
}
\end{center}
\end{tcolorbox}
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begin{center}
\parbox{0.9\textwidth}{
{\color{blue} If you buy 5 apples and each apple costs \$1.20, how much do you spend in total?}
\vspace{0.3em}
\underline{\$6.00}
}
\end{center}
\end{tcolorbox}
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begin{center}
\parbox{0.9\textwidth}{
{\color{blue} Write a Python program to calculate the sum of squares of the following numbers: 1, 2, 10, -9, 78.}
\vspace{0.3em}
\underline{\texttt{numbers = [1,2,10,-9,78]}}
\underline{\texttt{sum\_of\_squares = sum(x**2 for x in numbers)}}
\underline{\texttt{print(sum\_of\_squares)}}
}
\end{center}
\end{tcolorbox}
\vspace{0.5em}
这些示例中,输入部分(蓝色)在计算损失时会被屏蔽,模型仅学习预测下划线标注的输出部分。研究指出,扩大微调任务的数量和指令的多样性可以显著提升模型性能 \cite{chung-etal:2022scaling}。但值得强调的是,微调数据的体量远小于预训练语料——通常数万至数十万条高质量样本即已足够 \cite{zhou-etal:2023lima},而预训练则需要数十亿甚至数万亿 token,两者在计算开销上差了几个数量级。
更为关键的是,经过指令微调后,模型往往表现出\textbf{零样本泛化}\mindex{zero-shot learning})能力:它不仅能完成训练时见过的指令类型,还能应对从未被显式训练过的全新任务描述 \cite{sanh-etal:2022multitask,wei-etal:2022finetuned}。这种能力正是生成式 LLM 与早期 BERT 等“针对每个下游任务独立微调”范式的分水岭。背后的原理通常被理解为,预训练已经储备了充足的语言理解和推理基础,指令微调只是引入了一种监督信号,将潜藏的指令遵循与任务解决能力“唤醒”。
\noindent \textbf{微调的数学形式}与预训练高度相似,但目标函数聚焦于输出段。设预训练得到的参数为 $\hat{\theta}$,微调数据集为 $\mathcal{D}_{\text{tune}}$,则最优参数 $\tilde{\theta}$ 为:
\begin{eqnarray}
\tilde{\theta} = \argmax_{\hat{\theta}^+} \sum_{\mathrm{sample} \in \mathcal{D}_{\text{tune}}} \mathcal{L}_{\hat{\theta}^+}(\mathrm{sample}) \label{eq:llm-fine-tuning-mle}
\end{eqnarray}
对每个样本,我们将其切分为输入段 $\mathbf{x}_{\text{sample}}$ 和输出段 $\mathbf{y}_{\text{sample}}$,损失函数只作用于后者:
\begin{eqnarray}
\mathcal{L}_{\hat{\theta}^+}(\mathrm{sample}) = -\log \mathrm{Pr}_{\hat{\theta}^+}(\mathbf{y}_{\text{sample}}|\mathbf{x}_{\text{sample}})
\end{eqnarray}
在具体实现中,前向传播照常对整个拼接序列进行计算,但反向传播时,误差梯度仅流经 $\mathbf{y}_{\text{sample}}$ 对应的位置。考虑这样一个序列:
\begin{equation}
\underbrace{\textrm{{\color{blue} $\langle s \rangle$ Square this number .}}\ \textrm{{\color{blue} $2$ .}}}_{\text{Input}}\ \ \underbrace{\textrm{\underline{The result is $4$ .}}}_{\text{Output}}\nonumber
\end{equation}
损失仅基于 “The result is 4 .” 这几个 token 计算并回传,输入部分不产生任何梯度。这种方式确保模型专注于学习如何根据指令生成正确的响应,而非重复记忆输入内容。
指令微调在实际操作中仍需要一定的工程调参——学习率、批大小、训练步数等超参数需要仔细摸索。不过,由于其数据规模和计算量远不及预训练,多次实验的整体成本是完全可控的。正是这种低成本、高效率的特性,使得微调成为适配 LLM 的通用技术栈核心:除了指令微调,它还被广泛用于将基座模型转化为对话助手、适配超长上下文、进行领域知识注入等。后续章节将深入探讨更高效的微调算法以及在更多场景下的应用。
\ No newline at end of file
% \subsubsection{微调数据获取}
% \noindent 微调数据至关重要,因此近期许多大语言模型 (LLM) 的研究都致力于为指令微调开发各种数据集。与大多数机器学习工作一样,数据获取通常有两种方法 --- 手动数据生成和自动数据生成。
% \paragraph{人工生成数据}
% \noindent 一种直接的方法是招募人类标注员为感兴趣的任务创建输入-输出对。与传统 NLP 中的数据标注(例如文本分类,标注员只需根据指南为收集的文本分配标签)不同,为 LLM 创建微调数据需要更多的步骤和精力,因此更具挑战性。假设我们想为英汉机器翻译任务获取微调数据。第一步是编写一个提示模板来描述任务并清晰地格式化问题。例如,
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begingroup
% \renewcommand{\arraystretch}{1.0}
% \setlength{\tabcolsep}{6pt}
% \begin{tabular}{r l}
% {\color{gray} \small{Instruction}} & Translate the text from English to Chinese. \\ [0.2cm]
% {\color{gray} \small{User Input}} & $\{*\mathrm{text}*\}$ \\ [0.2cm]
% {\color{gray} \small{Output}} & \underline{$\{*\mathrm{translation}*\}$}
% \end{tabular}
% \endgroup
% \end{tcolorbox}
% \vspace{0.5em}
% 然后,我们收集源文本和目标文本对(即,中文文本及其对应的翻译),并替换变量 $\{*\mathrm{text}*\}$ 和 $\{*\mathrm{translation}*\}$ 来生成微调样本。例如,给定一对英文和中文句子
% \begin{center}
% \begin{tabular}{r c l}
% How's the weather today? & $\to$ & 今天天气怎么样? \\
% \multicolumn{1}{c}{$\{*\mathrm{text}*\}$} & & \multicolumn{1}{c}{$\{*\mathrm{translation}*\}$}
% \end{tabular}
% \end{center}
% \noindent 我们可以使用提示模板生成一个微调样本,如下所示
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begingroup
% \renewcommand{\arraystretch}{1.0}
% \setlength{\tabcolsep}{6pt}
% \begin{tabular}{r l}
% {\color{gray} \small{Instruction}} & Translate the text from English to Chinese. \\ [0.2cm]
% {\color{gray} \small{User Input}} & How's the weather today? \\ [0.2cm]
% {\color{gray} \small{Output}} & \underline{ 今天天气怎么样? }
% \end{tabular}
% \endgroup
% \end{tcolorbox}
% \vspace{0.5em}
% \noindent 即,
% \begin{eqnarray}
% \mathbf{x} & = & \text{将文本从英文翻译成中文。$\backslash$n 今天天气怎么样?} \nonumber \\
% \mathbf{y} & = & \text{ 今天天气怎么样? } \nonumber
% \end{eqnarray}
% \noindent 我们可以使用这个 $(\mathbf{x},\mathbf{y})$ 对来微调 LLM,如上一小节所述。
% 这里的一个困难是,对于同一个任务,有许多不同的方式来编写提示模板,不同的人可能会制作出质量和复杂性各异的提示模板。有时,我们可能会编写指令过于复杂或冗长的提示模板。有时,我们甚至可能不确切知道目标任务是什么以及如何描述它。一个广泛采用的策略是为现有的 NLP 任务创建提示模板,因为已经有很多成熟的 NLP 问题和基准 \cite{bach-etal:2022promptsource,wang-etal:2022super,mishra-etal:2022cross}。在这种情况下,可以给标注员提供原始的任务描述和许多示例。然后,他们可以用自己的方式来表达如何提示 LLM 执行这些任务。请注意,虽然这种方法可以简化创建和编写提示的过程,但我们仍然需要标注框架和众包系统来管理工作并进行质量控制。例如,我们通常需要设计标注指南和统一的提示模板编写格式,特别是当许多标注员为同一个任务做贡献时。从现有 NLP 任务中引出提示的一个优点是,一旦提示模板开发完成,就可以很容易地使用原始任务中的已标注样本来生成提示。例如,给定一个用于英汉翻译的双语数据集,我们可以通过用该数据集中的句子对填充上述模板中的空位,轻松创建大量微调示例。
% 另一种方法是直接使用互联网上自然存在的可用数据。一个常见的例子是从问答网站收集问答对,以便为开放域问答任务微调 LLM \cite{joshi-etal:2017triviaqa}。许多问答领域的基准都是以这种方式构建的,因为问题类型繁多,一小群人不可能想出所有类型的问题。相反,使用这些网站的数据可以确保 LLM 微调数据在数量和质量上达到良好或可接受的水平。
% 除了利用现有资源,开发微调数据集的另一种直接方法是众包数据。一种简单的方法是允许用户输入任何问题,然后由人工给出回答,或者由 LLM 自动生成回答,再由人工进行标注和校正。因此,可以捕捉真实的用户行为,从而为大量传统 NLP 任务未涵盖的``新''问题收集输入和输出。
% 与构建微调数据集相关的一个问题是,我们通常希望数据尽可能多样化。许多研究发现,增加微调数据的多样性可以提高 LLM 的鲁棒性和泛化能力。因此,在 LLM 微调数据集中引入更多样化的提示和任务引起了相当大的兴趣。我们将在第 \ref{sec:instruction-generalization} 节中进一步讨论微调的泛化问题。
% \paragraph{自动生成数据}
% \noindent 人工生成数据的一个局限性在于,其质量和多样性在很大程度上取决于人类的经验和创造力。因此,如果我们希望大语言模型能够处理广泛的任务,即有效执行任何指令,那么依赖人工标注数据进行大语言模型微调通常效率不高。此外,这类数据的覆盖范围可能有限,甚至可能包含标注者自身引入的偏见。另一种方法是自动生成数据。例如,我们可以通过众包收集一些问题,并使用一个经过良好微调的大语言模型来生成这些问题的答案。然后,这些问答对像往常一样被用作微调样本。这种方法虽然非常简单,但已被广泛应用于为大语言模型生成大规模微调数据。
% 上述生成合成微调数据的方法与自然语言处理中用于数据增强的方法类似。如果我们有一个大语言模型,我们可以针对任何输入生成一个预测。对不同的输入重复这个过程,我们就能创建足够数量的微调样本。这种方法对于使用一个已经微调好的大语言模型来微调新的大语言模型特别有用。然而,这种方法的一个缺点是,它依赖于人工制作或收集的输入来生成数据,而这些输入可能不适合用于泛化大语言模型。在许多大语言模型应用中,一个重大的挑战来自于用户提出的大量问题和请求,其中许多问题和请求在现有的自然语言处理任务和数据集中并未涵盖。在这些情况下,不仅需要生成预测,还需要生成输入本身。
% 这里我们以 \mindex{自指令} 为例,说明如何生成大语言模型微调样本 \cite{wang-etal:2023selfinstruct,honovich-etal:2023unnatural}。其思想是,我们可以提示一个大语言模型通过学习其他指令来创建一个新指令。给定这个指令,大语言模型就可以填充其他字段(例如用户输入)并生成预测。图 \ref{fig:self-instruct} 展示了自指令的示意图。下面我们简要概述所涉及的关键步骤。
% \begin{figure}[!t]
% \centering
% \input{figures/Chapter3/figure-self-instruct}
% \caption{自指令 \cite{wang-etal:2023self} 方法示意图。该方法维护一个指令池以及相应的输入-输出样本。最初,该池包含一些人工编写的指令和样本。每次从池中抽取几条指令。然后,提示一个 LLM 根据抽取的指令生成新的指令和样本。最后,新生成的指令和样本经过过滤后被添加到池中。}
% \label{fig:self-instruct}
% \end{figure}
% \begin{itemize}
% \item \vspace{0.5em} Self-instruct 算法维护一个任务池。最初,它包含一些手工制作的种子任务,每个任务都有一个指令和一个输入-输出样本。随着算法的进行,LLM 生成的指令和样本将被添加到这个池中。
% \item \vspace{0.3em} 在每一步,都会从指令池中抽取少量指令。例如,我们可以随机选择一些人类编写的指令和一些 LLM 生成的指令,以确保多样性。
% \item \vspace{0.3em} 然后,所选指令被用作演示示例。因此,LLM 可以从这些示例中进行上下文学习,并生成一个新的指令。下面是提示 LLM 的一个示例模板。
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begingroup
% \setlength{\leftskip}{2em}
% \setlength{\rightskip}{2em}
% 我们为您提供了一些执行某些任务的不同指令。请根据这些指令生成一条新的指令。
% \vspace{0.2cm}
% 任务 1: $\{\mathrm{instruction}1\}$
% \vspace{0.2cm}
% 任务 2: $\{\mathrm{instruction}2\}$
% \vspace{0.2cm}
% 任务 3: $\{\mathrm{instruction}3\}$
% \vspace{0.2cm}
% 任务 4: $\{\mathrm{instruction}4\}$
% \vspace{0.2cm}
% 新任务: \underline{\hspace{2em}}
% \endgroup
% \end{tcolorbox}
% \vspace{0.5em}
% \item \vspace{0.3em} 给定生成的指令,然后提示 LLM 通过填写剩余的输入字段并生成相应的输出来完成样本。下面是一个提示模板。
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begingroup
% \setlength{\leftskip}{2em}
% \setlength{\rightskip}{2em}
% 我们为您提供了一组输入-输出样本,每个样本都由一个指令、一个用户输入和一个输出组成。请根据这些样本生成一个新样本。
% \vspace{0.2cm}
% 样本 1: $\{\mathrm{instruction}1\}$
% \vspace{0.1cm}
% 输入: $\{\mathrm{user\text{-}input}1\}$
% \vspace{0.1cm}
% 输出: $\{\mathrm{output}1\}$
% \vspace{0.2cm}
% 样本 2: $\{\mathrm{instruction}2\}$
% \vspace{0.1cm}
% 输入: $\{\mathrm{user\text{-}input}2\}$
% \vspace{0.1cm}
% 输出: $\{\mathrm{output}2\}$
% \vspace{0.2cm}
% 新样本: $\{\mathrm{new\text{-}instruction}\}$
% \vspace{0.1cm}
% \underline{\hspace{2em}}
% \endgroup
% \end{tcolorbox}
% \vspace{0.5em}
% \item \vspace{0.3em} 这个新生成的样本会通过一些启发式规则进行检查(例如,过滤掉与池中已有的样本或指令相似的样本或指令)。如果通过检查,该样本和指令就会被添加到池中。
% \end{itemize}
% \vspace{0.5em}
% 这个生成过程可以重复多次,以获得足够数量的微调样本。请注意,上面我们只展示了用于生成指令和微调样本的简单提示模板。当然,我们可以开发更好的模板来生成更多样化、更准确的指令和微调样本。例如,对于文本分类等某些任务,大语言模型可能倾向于产生有偏见的预测,例如,大多数生成的样本都属于同一个类别。在这种情况下,我们可以调整不同字段的生成顺序。更具体地说,我们可以用某个先验来指定输出(即类别),并提示大语言模型在给定指令和输出的情况下生成用户输入。这种方法类似于 \mindex{输入反转},即大语言模型根据指定的输出来生成输入 \cite{longpre-etal:2023flan}。
% 使用大语言模型生成的指令和微调样本已成为开发大语言模型的一种常用方法,特别是在手动开发此类数据的成本高昂,以至于大多数研究团队无法承担的情况下。在一些经过良好微调的大语言模型中,它们的微调数据集包含了一定数量的合成数据,并且这些数据已被证明是有用的 \cite{ouyang-etal:2022training,taori-etal:2023alpaca,chiang-etal:2023vicuna}。关于为大语言模型微调生成合成数据,已有进一步的研究。例如,可以通过引入进化算法来生成更多样化的指令 \cite{xu-etal:2024wizardlm},或者在更高级的微调过程中使用合成数据作为监督信号 \cite{chen-etal:2024self}。最近,在预训练阶段使用合成数据也引起了相当大的兴趣 \cite{gunasekar-etal:2023textbooks,allal-etal:2024cosmopedia}。
% 在许多应用中,一个现实世界的情景是,给定一个任务,我们可以收集或标注相对少量的微调数据,例如,我们可以聘请专家为特定领域的问答任务创建问题。但这些数据的数量和多样性通常是不够的。在这种情况下,我们可以使用自指令技术来生成更多样化的问答对,从而扩充微调数据。这提供了一种从一个种子微调样本集开始引导大语言模型的方法。请注意,使用自生成数据是一种常见的做法,并且早已在自然语言处理中得到应用。例如,这种方法已成功应用于解析和机器翻译 \cite{charniak:1997statistical,sennrich-etal:2016improving}。
\subsubsection{微调数据获取}
\noindent 与预训练可以方便地获取海量原始文本不同,SFT 依赖高质量的人工标注或精心筛选的指令‑响应对。数据的准确性、多样性和任务相关性直接决定了微调后模型的性能。研究表明,数万条高质量样本便足以有效激活模型的指令遵循能力,而低质量数据反而会损害模型。因此,数据的构建与审查成为 SFT 中最为耗时却最为关键的环节。微调数据的获取方式主要有人工生成和自动生成两类。
\paragraph{人工生成数据}
一种直接的方法是招募人类标注员为感兴趣的任务创建输入-输出对。与传统NLP标注不同,为LLM构造微调数据需要更多的步骤。首先需要设计提示模板来描述任务。例如,为英汉翻译任务可定义如下模板:
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begingroup
\renewcommand{\arraystretch}{1.0}
\setlength{\tabcolsep}{6pt}
\begin{tabular}{r l}
{\color{gray} \small{Instruction}} & Translate the text from English to Chinese. \\ [0.2cm]
{\color{gray} \small{User Input}} & $\{*\mathrm{text}*\}$ \\ [0.2cm]
{\color{gray} \small{Output}} & \underline{$\{*\mathrm{translation}*\}$}
\end{tabular}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
收集源文本与目标文本对(例如``How's the weather today?'' $\to$ ``今天天气怎么样?''),将变量$\{*\mathrm{text}*\}$$\{*\mathrm{translation}*\}$替换为具体句子,即可形成微调样本:
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begingroup
\renewcommand{\arraystretch}{1.0}
\setlength{\tabcolsep}{6pt}
\begin{tabular}{r l}
{\color{gray} \small{Instruction}} & Translate the text from English to Chinese. \\ [0.2cm]
{\color{gray} \small{User Input}} & How's the weather today? \\ [0.2cm]
{\color{gray} \small{Output}} & \underline{今天天气怎么样?}
\end{tabular}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
如此构造的$(\mathbf{x},\mathbf{y})$对可直接用于微调。
这里的困难在于同一任务存在多种编写提示的方式,不同标注员产出的模板质量和风格各异。一种广泛采用的策略是为现有NLP任务开发提示模板\cite{bach-etal:2022promptsource,wang-etal:2022super,mishra-etal:2022cross}:标注员在了解原始任务描述和示例后,用自己的表达方式写出提示。模板确定后,利用原任务已有的标注数据便可批量生成微调样本。此外,也可直接使用互联网上的自然数据,如从问答网站收集问答对来构建开放域问答数据\cite{joshi-etal:2017triviaqa},以保证问题类型的广覆盖。众包是另一重要途径:收集真实用户提出的问题,由人工回答或先由LLM生成再经人工修正,从而捕获传统NLP任务未涵盖的``新''问题。大量研究表明,提升微调数据的多样性有助于增强LLM的鲁棒性和泛化能力,我们将在第~\ref{sec:instruction-generalization}~节进一步讨论。
\paragraph{自动生成数据}
\label{sec:instruction-generalization}
人工方法受限于标注者的经验和创造力,覆盖范围与扩展效率有限。自动生成方法中,最直接的方式是利用一个已微调好的LLM为收集到的问题批量生成答案,形成合成微调样本。为进一步解决输入本身多样性的问题,\textbf{自指令}\mindex{Self-instruct}\cite{wang-etal:2023selfinstruct,honovich-etal:2023unnatural}提供了一种系统化的方案:维护一个任务指令池,初始包含少量人工编写的种子任务及样本。每次从池中抽取若干条指令作为示例,提示LLM生成一条新的指令;随后再提示LLM为该指令生成对应的用户输入和输出,构成完整的微调样本。新样本经过启发式规则过滤(如去除与池中已有数据高度重复的样本)后加入池中,如此迭代便可扩充出大规模、多样化的微调数据集。
实际应用中,生成顺序可灵活调整。例如,对于文本分类等任务,为避免LLM倾向于生成某一固定类别的伪样本,可采用\mindex{输入反转}(input reversal)策略:先指定输出的类别标签,再提示LLM生成与其匹配的输入\cite{longpre-etal:2023flan}。自指令及其变体已被广泛应用于LLM开发,Alpaca、Vicuna等模型的微调数据集均包含一定比例的合成数据\cite{ouyang-etal:2022training,taori-etal:2023alpaca,chiang-etal:2023vicuna}。后续工作进一步引入进化算法以生成更多样化的指令\cite{xu-etal:2024wizardlm},或将合成数据用作自提升的监督信号\cite{chen-etal:2024self};近期在预训练阶段使用合成数据同样引起了关注\cite{gunasekar-etal:2023textbooks,allal-etal:2024cosmopedia}
在只有少量种子数据的小样本场景下,自指令技术也可充当数据增强手段,从种子集出发引导LLM生成更多样本,实现数据层面的自举。这种利用模型自身生成训练数据的实践在自然语言处理中由来已久,例如早年在句法分析和统计机器翻译中便取得了显著成效\cite{charniak:1997statistical,sennrich-etal:2016improving}
\subsubsection{监督微调}
\noindent 将前面介绍的指令微调付诸实践的最直接方式,就是\textbf{监督微调}(Supervised Fine‑Tuning, SFT)。如前所述,该方法利用已标注的输入‑输出对来调整模型参数,使模型学会根据指令生成期望的响应。本节首先对这一过程进行形式化描述,然后将其扩展到多轮对话场景,最后讨论实践中需要关注的几个关键问题。
\vspace{0.5em}
\noindent\textbf{单轮预测的形式化。}
设输入序列为 $\mathbf{x}=x_0\dots x_m$(包含指令和用户提供的内容),对应的输出序列为 $\mathbf{y}=y_1\dots y_n$。SFT 数据集 $\mathcal{D}$ 由大量这样的 $(\mathbf{x}, \mathbf{y})$ 对所构成。下表给出了一些典型示例:
\begin{center}
\begin{tabular}{l | l}
$\mathbf{x}$(指令 + 用户输入) & $\mathbf{y}$(输出) \\ \hline
\colorbox{gray!30}{Summarize the following article.} & $\{*\textrm{summary}*\}$ \\
Article: In recent years, solar energy has seen ... & \\ \hline
\colorbox{gray!30}{Analyze the sentiment of the following review.} & Positive \\
Review: I absolutely loved the new dining experience... & \\ \hline
\colorbox{gray!30}{Translate the following sentence into French.} & La pratique aide \\
Sentence: practice indeed helps. & effectivement. \\ \hline
\colorbox{gray!30}{Classify the following email as spam or not spam.} & Spam \\
Text: Congratulations! You've won a \$500 gift card... & \\
\end{tabular}
\end{center}
\noindent 我们的目标是最大化在给定输入下生成正确输出的条件概率。沿用概述部分的记号,令 $\hat{\theta}$ 为预训练得到的参数,则微调后的最优参数 $\tilde{\theta}$ 由下式给出(为书写简洁,后文将用 $\theta$ 表示从 $\hat{\theta}$ 开始调整后的参数):
\begin{eqnarray}
\tilde{\theta} = \argmax_{\theta} \sum_{(\mathbf{x},\mathbf{y}) \in \mathcal{D}} \log \mathrm{Pr}_{\theta}(\mathbf{y}|\mathbf{x})
\label{eq:sft-objective}
\end{eqnarray}
其中 $\log \mathrm{Pr}_{\theta}(\mathbf{y}|\mathbf{x}) = \sum_{i=1}^{n} \log \mathrm{Pr}_{\theta}(y_i|\mathbf{x},\mathbf{y}_{<i})$,即只对输出部分的 token 计算交叉熵损失。
与标准语言模型训练不同,这里我们不关心输入序列 $\mathbf{x}$ 自身的生成概率。在实际实现中,通常将 $[\mathbf{x}, \mathbf{y}]$ 拼接为一个完整序列进行前向传播,而在反向传播时强行将输入部分的损失设为 $0$,仅保留输出部分的梯度。这一过程可以借助链式法则清晰地表达为
\begin{eqnarray}
\log \mathrm{Pr}_{\theta}(\mathbf{x},\mathbf{y}) &=& \underbrace{\log \mathrm{Pr}_{\theta}(\mathbf{x})}_{\text{设为 } 0} + \underbrace{\log \mathrm{Pr}_{\theta}(\mathbf{y}|\mathbf{x})}_{\text{实际计算损失}}
\label{eq:sft-chain}
\end{eqnarray}
\ref{fig:sft-forward-backward} 给出了这种“前向计算全序列,反向仅传播输出部分”的训练方式示意图。由此可见,SFT 本质上就是一种修改了损失掩码的标准语言模型训练。
\begin{figure}[!t]
\centering
\input{figures/Chapter3/figure-sft-forward-and-backward}
\caption{LLM 的监督微调图示。我们将输入和输出拼接成一个单一序列。在前向传播过程中,我们照常运行 LLM。在反向传播过程中,我们仅计算输出部分的损失,而将输入部分的损失简单地设置为 0。}
\label{fig:sft-forward-backward}
\end{figure}
\vspace{0.5em}
\noindent\textbf{扩展到多轮对话。}
上述单轮预测假定每次交互相互独立,但在聊天机器人等真实应用中,往往需要进行多轮对话。假设一段对话包含 $K$ 轮,每轮依次为用户输入 $\mathbf{x}^k$ 和助手回复 $\mathbf{y}^k$,整体构成序列 $\mathbf{x}^1,\mathbf{y}^1,\dots,\mathbf{x}^K,\mathbf{y}^K$。我们希望模型在每一轮都能基于已有的对话历史生成恰当的回复,即最大化
\begin{eqnarray}
\tilde{\theta} = \argmax_{\theta} \sum_{k=1}^{K} \log \mathrm{Pr}_{\theta}(\mathbf{y}^k|\mathbf{x}^1,\mathbf{y}^1,\dots,\mathbf{x}^k)
\label{eq:multi-turn-obj}
\end{eqnarray}
一种朴素的实现方式是对每个 $k$ 分别运行一次 LLM,但这样做的效率极低。更高效的做法与单轮情形类似:将整个对话序列 $[\mathbf{x}^1,\mathbf{y}^1,\dots,\mathbf{x}^K,\mathbf{y}^K]$ 视为一个长序列,在单次前向传播中完成所有轮次的损失计算。应用链式法则展开:
\begin{eqnarray}
\log \mathrm{Pr}_{\theta}(\mathbf{x}^1,\mathbf{y}^1,\dots,\mathbf{x}^K,\mathbf{y}^K) &=& \underbrace{\log \mathrm{Pr}_{\theta}(\mathbf{x}^1)}_{\text{设为 }0} + \underbrace{\log \mathrm{Pr}_{\theta}(\mathbf{y}^1|\mathbf{x}^1)}_{\text{损失}} + \cdots \nonumber \\
&+& \underbrace{\log \mathrm{Pr}_{\theta}(\mathbf{x}^K|\mathbf{x}^1,\mathbf{y}^1,\dots,\mathbf{y}^{K-1})}_{\text{设为 }0} \nonumber \\
&+& \underbrace{\log \mathrm{Pr}_{\theta}(\mathbf{y}^K|\mathbf{x}^1,\mathbf{y}^1,\dots,\mathbf{x}^K)}_{\text{损失}}
\label{eq:multi-turn-chain}
\end{eqnarray}
我们只需将每一处用户输入项对应的对数概率损失设为 $0$,仅计算助手回复部分的损失,便能在单次运行中达到式(\ref{eq:multi-turn-obj})所描述的目标。图\ref{fig:sft-conversational-models} 展示了多轮对话 SFT 的训练范式。最终,整体训练目标依然可以简洁地写为
\begin{eqnarray}
\tilde{\theta} = \argmax_{\theta} \sum_{\mathrm{seq} \in \mathcal{D}} \log \mathrm{Pr}_{\theta}(\mathrm{seq})
\end{eqnarray}
其中 $\mathrm{seq}$ 即上述完整对话序列,损失掩码模式保证了模型仅学习如何生成助手回复,而不会去预测用户提问。
\begin{figure}[!t]
\centering
\input{figures/Chapter3/figure-sft-multi-round-prediction}
\caption{对话模型的监督微调示意图。在此,LLM 充当聊天机器人,根据对话历史响应每个请求。对话在用户和聊天机器人之间交替进行。在 SFT 中,我们像在标准 LLM 中一样将整个对话视为一个序列,但仅针对 LLM 的响应计算损失。}
\label{fig:sft-conversational-models}
\end{figure}
\vspace{0.5em}
\noindent 总而言之,监督微调是连接通用预训练模型与具体应用场景的核心桥梁。通过精心设计单轮或多轮的指令数据,并配合合理的训练策略,我们能够高效地赋予 LLM 强大且泛化的任务解决能力。
\subsubsection{使用更少数据进行微调}
指令微调常依赖大规模数据集,例如 FLAN 汇集了 1836 个任务、约 1500 万个样本 \cite{longpre-etal:2023flan}。在如此庞大的数据上对大型语言模型进行全参数更新,计算开销极高。参数高效方法通过只更新少量参数来缓解这一问题,但微调数据本身仍可能包含合成噪声和偏差。另一种高效思路是从数据入手:\textbf{只选用最相关、最有影响力的样本进行微调},在保持模型更新质量的同时大幅压缩数据量。形式化地,给定大规模数据集 $\mathcal{D}$,我们希望选出一个高信息密度的子集 $\mathcal{S}^* \subset \mathcal{D}$,使基于该子集微调得到的模型 $\theta^*(\mathcal{S}^*)$ 在目标任务上的损失尽可能逼近使用全量数据的效果:
\begin{equation}
\mathcal{S}^* = \arg\min_{\mathcal{S}\subseteq\mathcal{D},\,|\mathcal{S}|=k} \mathcal{L}_{\text{task}}\bigl(\theta^*(\mathcal{S})\bigr).
\end{equation}
多项工作验证了这一思路。\citet{zhou-etal:2023lima} 精心设计提示并收集样本,构建了仅包含 1000 个样本的指令遵循数据集 LIMA。使用该数据微调的 LLaMA 65B 模型,其竞争力可与投入更多微调努力的模型相媲美,甚至更优:在人类偏好评估中,LIMA-65B 对 DaVinci003 的胜率达 58\%,而用 52k 条数据微调的 Alpaca-65B 仅获 47\% 的胜率。这说明模型的多任务响应能力无需在所有指令类型上微调即可激发。\citet{chen-etal:2024alpagasus} 则利用 GPT-3.5 为每条指令-回答对打分,从 Alpaca-52K 中筛选出 9000 个最高质量样本构成 AlpaGasus-9K;仅用该子集微调,在 AlpacaEval 上的表现即持平甚至超过全量数据。这些结果进一步印证了“数据质量优先于数量”的观点。此外,LESS 等方法通过计算每个训练样本对验证损失的梯度影响来筛选最有价值的样本:
\begin{equation}
\mathcal{I}(z) \approx -\nabla_\theta \ell(z)^\top \mathbf{H}^{-1} \nabla_\theta \ell(\mathcal{D}_{\text{val}}),
\end{equation}
其中 $\mathbf{H}$ 为海森矩阵。选择影响力最大的子集,可高效驱动模型收敛。总体而言,使用更少但更高质量的数据训练 NLP 模型,往往能带来更好效果。
上述发现与 NLP 的传统认知有所不同:复杂任务的执行能力可以通过少量标注数据激活,而非依赖大规模监督训练。一种解释是,生成正确响应的能力已在预训练阶段习得,但指令-响应的映射在推理时并非高概率路径。微调只需对模型进行微小调整即可使其遵循指令,所需的训练量远少于预训练。这与\textbf{\mindex{表面对齐假说}(superficial alignment hypothesis)}密切相关:知识的主体在预训练期间已经建立,后续的微调或对齐仅对表层行为进行修正,不会显著改变底层知识库 \cite{zhou-etal:2023lima}。图~\ref{fig:alignment-hypothesis} 直观地展示了这一思想:预训练构筑了模型能力的大厦,微调只是完成表层的意图对齐装修。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\filldraw[fill=gray!10, draw=gray!50] (-0.5,-0.5) rectangle (8.5,5.5);
\node[anchor=north] at (4,5.5) {\textbf{模型知识与能力(预训练建立)}};
\filldraw[fill=blue!20, draw=blue!40, thick] (1,0.5) rectangle (7,2.8);
\node at (4,1.65) {\Large 核心知识库};
\draw[->, very thick, red] (4,2.8) -- (4,3.9);
\node at (4.3,3.35) [right] {\small 表面对齐(微调)};
\filldraw[fill=red!15, draw=red!40, thick] (2.2,3.9) rectangle (5.8,4.9);
\node at (4,4.4) {\small 指令遵循 / 偏好对齐};
\end{tikzpicture}
\caption{表面对齐假说示意:预训练完成知识与能力的主体构建,微调仅需少量数据实现浅层的行为对齐。}
\label{fig:alignment-hypothesis}
\end{figure}
% 进一步的观察表明,微调甚至不必严格依赖成对的指令-响应数据。\citet{hewitt-etal:2024instruction} 发现,仅对响应进行微调而无需对应指令,也能隐式地实现指令遵循。
% 与上述讨论紧密相关的是\textbf{样本效率}(\mindex{sample efficiency})。若一种方法能从少量样本中有效学习,则称其是样本高效的。与预训练相比,指令微调正是样本高效的。从机器学习角度看,样本高效方法可视为对数据空间的高效采样,能最优地利用稀缺数据。因此,许多基于采样的学习技术(如强化学习算法)可从中受益,例如在人类偏好对齐中通过奖励模型高效采样偏好数据 \cite{liu-etal:2024statistical},或在策略学习中提高采样效率 \cite{wang-etal:2024esrl}。
\subsubsection{参数高效微调方法}
即使有监督微调的数据量远小于预训练,在数十亿参数的模型上进行全参数微调仍消耗可观的 GPU 显存与计算时间。这促使了参数高效微调(Parameter-Efficient Fine-Tuning, PEFT)方法的产生。与更新全部参数的全参数微调(Full Fine-Tuning, FFT)不同,PEFT 冻结预训练模型的大部分权重,仅训练少量附加参数或特定层。早期的 PEFT 方法通过在 Transformer 层之间插入小型全连接网络(\mindex{adapter}\cite{houlsby-etal:2019adapter},或在输入端添加可学习的连续提示向量(如 \mindex{prefix tuning}\mindex{prompt tuning})来引导生成 \cite{li-liang:2021prefix,lester-etal:2021prompt}。但这些方法往往在推理时引入额外延迟,或在复杂任务上难以追平全参数微调的性能上限。
为克服上述局限,\citet{hu-etal:2021lora} 提出了低秩自适应(Low-Rank Adaptation, \mindex{LoRA})。其核心假设是:微调过程中的权重更新 $\Delta W$ 具有较低的“内在秩”。如图~\ref{fig:lora} 所示,对于预训练权重 $W_0 \in \mathbb{R}^{d \times k}$,LoRA 将其更新分解为两个低秩矩阵的乘积:
\begin{equation}
W' = W_0 + \Delta W = W_0 + B A,\quad \text{其中 } B \in \mathbb{R}^{d \times r},\; A \in \mathbb{R}^{r \times k},\; r \ll \min(d,k).
\end{equation}
训练时 $W_0$ 被冻结,仅优化 $B$$A$。秩 $r$ 通常取 4、8 或 16,可带来巨大的参数缩减。以 LLaMA-7B 的某个注意力投影矩阵 ($d=k=4096$) 为例,若 $r=16$,原始矩阵参数量约 $4096^2 \approx 16.7\text{M}$,而 LoRA 的参数量仅 $2 \times 4096 \times 16 \approx 131\text{K}$,减少超过 100 倍。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[
>=Latex,
every node/.style={font=},
frozen/.style={rounded corners=6pt, draw=gray!60, fill=gray!10, line width=1pt},
train/.style={rounded corners=6pt, draw=orange!70!black, fill=orange!12, line width=1pt},
plusnode/.style={circle, draw=gray!60, fill=gray!10, line width=1pt, minimum size=0.9cm, font=\bfseries},
iobox/.style={rounded corners=4pt, draw=gray!55, fill=gray!6, line width=0.8pt, minimum width=1.2cm, minimum height=0.9cm},
sub/.style={font=\scriptsize, gray!40!black},
arrow/.style={->, gray!45!black, line width=0.8pt},
]
% ---- 输入 x ----
\node[iobox] (x) at (0,0) {$x$};
\coordinate (split) at (1.3,0);
\draw[arrow] (x.east) -- (split);
% ---- 上方路径:W0(冻结)----
\node[frozen, minimum width=3.4cm, minimum height=2.0cm] (w0) at (4.2,2.0) {};
\node[font=\bfseries] at (4.2,2.25) {$W_0$};
\node[sub] at (4.2,1.85) {冻结, $d \times k$};
\node[sub] at (4.2,1.6) {不参与梯度更新};
% ---- 下方路径:先 A(r x k,压缩到秩 r)----
\node[train, minimum width=1.5cm, minimum height=1.0cm] (a) at (3.6,-2.0) {};
\node[font=\bfseries] at (3.6,-1.85) {$A$};
\node[sub] at (3.6,-2.2) {$r \times k$};
% ---- 再 B(d x r,展开回维度 d)----
\node[train, minimum width=1.5cm, minimum height=2.6cm] (b) at (5.6,-2.0) {};
\node[font=\bfseries] at (5.6,-1.6) {$B$};
\node[sub] at (5.6,-2.9) {$d \times r$};
% ---- 分支:x 进入上下两条路径 ----
\draw[arrow] (split) |- (w0.west);
\draw[arrow] (split) |- (a.west);
% ---- 下方路径内部:A 先算,结果送入 B ----
\draw[arrow] (a.east) -- (b.west);
% ---- 汇合点(+)----
\node[plusnode] (plus) at (8.0,0) {$+$};
\draw[arrow] (w0.east) -| (plus.north);
\draw[arrow] (b.east) -| (plus.south);
% ---- 输出 h ----
\node[iobox] (h) at (9.6,0) {$h$};
\draw[arrow] (plus.east) -- (h.west);
% ---- 底部说明数据流向 ----
\node[sub] at (4.6,-3.7) {输入 $x$ 先经过 $A$,再经过 $B$$h = W_0x + BAx$};
\end{tikzpicture}
\caption{LoRA 示意图。输入 $x$ 从左侧进入,分两条路径向右传播:上方路径穿过冻结的预训练权重 $W_0$;下方路径依次穿过可训练的低秩矩阵 $A$$r \times k$,先将输入压缩到秩 $r$)和 $B$$d \times r$,再展开回维度 $d$),两条路径最终在右侧汇合相加,得到输出 $h = W_0x + BAx$}
\label{fig:lora}
\end{figure}
LoRA 的一大优势是训练后低秩矩阵可直接合并回原权重 ($W' = W_0 + BA$),推理时不会引入任何额外延迟。近年来涌现出众多 LoRA 变体:QLoRA 将 $W_0$ 以 4-bit NormalFloat 量化存储并配合分页优化器,使得在单张 48GB 消费级 GPU 上微调 65B 模型成为可能 \cite{dettmers-etal:2023qlora};DoRA 则将权重矩阵解耦为幅度与方向分别学习,进一步提升了微调的稳定性和任务表现 \cite{liu-etal:2024dora}
表~\ref{tab:peft-memory} 对比了全参数微调与主流 PEFT 方法在 7B 模型上的显存占用(混合精度训练)。PEFT 方法将可训练参数量降低数个数量级,显著缓解了显存压力,使大模型微调走向“平民化”。
\begin{table}[htbp]
\centering
\caption{全参数微调与 PEFT 方法的资源对比(以 7B 模型,序列长度 512,批次大小 1 为例)}
\label{tab:peft-memory}
\begin{tabular}{lccc}
\hline
\textbf{方法} & \textbf{可训练参数占比} & \textbf{GPU 显存占用 (估)} & \textbf{推理延迟} \\
\hline
全参数微调 (FFT) & 100\% & $\sim$55 GB & 无额外 \\
Adapter & 3\%--5\% & $\sim$20 GB & 略有增加 \\
Prefix/Prompt Tuning & $<$1\% & $\sim$16 GB & 略有增加 \\
LoRA ($r$=16) & 0.1\%--0.5\% & $\sim$18 GB & \textbf{无增加} \\
QLoRA (4-bit) & 0.1\%--0.5\% & $\sim$10 GB & 无增加 \\
\hline
\end{tabular}
\end{table}
\begin{importantnote}
LoRA 通过低秩分解将权重更新压缩为两个小矩阵的乘积,仅训练极少量参数即可达到接近全量微调的性能。其核心优势在于推理时零延迟(可合并回原权重),且显存占用大幅降低。需注意秩r的选择会影响表现,通常 8~16 即可,过大则失去高效性。
\end{importantnote}
PEFT 方法之所以能用极少参数达到全参数微调的效果,可从\textbf{内在维度}\mindex{intrinsic dimensionality})假说得到解释 \cite{li-etal:2018intrinsic}。该假说认为,尽管预训练模型参数规模庞大,但适配特定下游任务的有效解空间实际上处于一个低维子流形中。如图~\ref{fig:intrinsic-dim} 所示,预训练已将参数 $\theta_0$ 置于良好的损失盆地内,微调只需在其周围的低维子空间中寻找适合任务的 $\theta^*$
\begin{equation}
\theta^* = \theta_0 + P \cdot \delta,\quad \delta \in \mathbb{R}^m,\; m \ll |\theta|,
\end{equation}
其中 $P$ 为投影矩阵。LoRA 这类低秩方法恰好契合了低维更新的特性,能高效捕捉任务特定的变化,无需在高维参数空间进行昂贵搜索。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.9]
\draw[fill=gray!10] (0,0) ellipse (5cm and 3.5cm);
\draw[fill=gray!20] (0.5,-0.3) ellipse (3.8cm and 2.6cm);
\draw[fill=gray!30] (1.0,-0.6) ellipse (2.5cm and 1.7cm);
\node[align=center] at (1.8,-1.5) {\small 低损失};
\fill[blue] (1.2,-0.8) circle (0.12) node[below right] {$\theta_0$ (预训练)};
\fill[red] (0.6,0.2) circle (0.12) node[above left] {$\theta^*$ (微调后)};
\draw[->, thick, dashed, >=stealth] (1.2,-0.8) -- (0.6,0.2);
\node at (2.5,2.5) {\textbf{损失盆地}};
\draw[red, thick] (2.5,-2.2) ellipse (1.2cm and 0.5cm);
\node[red] at (3.8,-2.5) {\small 低维子空间};
\draw[->, red, thick, >=stealth] (2.8,-1.5) -- (2.8,-1.9);
\end{tikzpicture}
\caption{内在维度假说示意:预训练模型处于宽广的损失盆地中,微调只需在低维子空间内进行微小扰动即可适配下游任务。}
\label{fig:intrinsic-dim}
\end{figure}
% \subsubsection{使用更少数据进行微调}
% 尽管 SFT 的数据规模远小于预训练,但在包含数十亿参数的模型上进行全参数微调依然需要消耗可观的 GPU 内存和计算时间。这催生了参数高效微调(PEFT)技术的快速发展,例如 LoRA、适配器(Adapter)等方法。它们仅更新少量额外参数,从而大幅降低资源需求。
% \noindent 随着指令微调的日益普及,对大规模、高质量微调数据的需求激增。例如,FLAN 微调数据集由 1,836 个任务汇编而成,包含 1500 万个样本 \cite{longpre-etal:2023flan}。用如此庞大的数据集微调 LLM 通常是一项计算成本高昂的任务,尤其是考虑到更新 LLM 中大量的参数是资源密集型的。缓解此问题的一种方法是探索高效的模型训练方法,例如,可以使用参数高效的方法仅更新模型的一小部分。然而,许多微调数据集包含大量合成数据,其中错误和偏见仍然不可避免。
% 另一种高效微调的方法是只考虑最相关和最有影响力的样本进行微调。这样,我们可以在保持模型更新质量的同时,减少需要处理的数据量。有几种方法可以实现这一点。例如,\citet{zhou-etal:2023lima} 通过精心设计提示并从各种 NLP 任务中收集样本,构建了一个仅包含 1,000 个样本的指令遵循数据集。他们表明,使用该数据集微调的 LLaMa 65B 模型,其竞争力可与那些投入更多微调努力的模型相媲美,甚至更优。这表明 LLM 能够适应并响应多样的任务,而无需在所有类型的指令遵循数据上进行微调。\citet{chen-etal:2024alpagasus} 开发了一个基于 GPT-3.5 模型的系统,用于评估每个指令遵循样本的质量。因此,他们可以从现有数据集中筛选出高质量的样本,用更少的微调样本展现出更好的微调性能。研究人员还开发了一些方法,利用启发式规则来选择或筛选数据 \cite{zhao-etal:2024long,ge-etal:2024clustering},或者优先考虑对微调过程影响更大的数据 \cite{xia-etal:2024less}。事实上,这些方法大多可以看作是更大数据选择和筛选方法家族中的实例。而且,使用更高质量(但可能数量更少)的数据通常对训练 NLP 模型有益。
% 指令微调领域的发现与 NLP 的传统观点有所不同:模型处理复杂问题的能力可以通过少量标注数据来激活,而不需要大量的监督数据进行广泛训练。一种可能的解释是,根据指令生成正确响应的能力已在预训练期间学到,但这种指令-响应的映射在推理过程中概率不高。微调可以对模型进行微小调整,使其遵循指令,这比预训练所需的训练量要少得多。这与所谓的 \mindex{superficial alignment hypothesis} (表面对齐假说)密切相关,该假说认为,学习主要发生在预训练期间,而后续的微调或对齐阶段并不会对 LLM 的底层知识库做出重大贡献 \cite{zhou-etal:2023lima}。由于模型的核心能力和知识已在预训练中建立,因此只需相对较少的训练微调工作,即可实现与用户需求对齐的有效微调。这意味着用极少量数据微调 LLM 是可能的。从另一个方向看,微调可能不必局限于成对的指令-响应数据。例如,\citet{hewitt-etal:2024instruction} 发现,仅对响应进行微调而无需相应的指令,也可以隐式地实现指令遵循。
% 与此处的讨论相关的一个概念是样本效率。如果一种机器学习方法能够从少量训练样本中有效学习,那么它就被称为 \mindex{sample efficient} (样本高效的)。从这个意义上说,与预训练相比,指令微调是样本高效的。从机器学习的角度来看,样本高效的方法可以被视为对数据空间进行高效采样的方式,并且由于它们能最优化地利用稀缺数据而具有优势。因此,基于采样的学习技术,例如许多强化学习算法,可以从这些样本高效的方法中受益。例如,在人类偏好对齐中,我们可以通过奖励模型高效地采样偏好数据 \cite{liu-etal:2024statistical},或者在策略学习中提高采样效率 \cite{wang-etal:2024esrl}。
% \subsubsection{参数高效微调方法}
% \noindent 缓解计算成本高昂问题的另一种途径是采用参数高效微调(Parameter-Efficient Fine-Tuning, PEFT)方法。与更新模型所有参数的全参数微调(Full Fine-Tuning, FFT)不同,PEFT 旨在冻结预训练模型的大部分权重,仅训练少量附加参数或特定层。早期的 PEFT 方法主要通过在 Transformer 层之间插入小型的全连接网络(即 \mindex{adapter})来实现 \cite{houlsby-etal:2019adapter},或者通过在输入端添加可学习的连续提示向量(如 \mindex{prefix tuning} 和 \mindex{prompt tuning})来引导模型生成 \cite{li-liang:2021prefix,lester-etal:2021prompt}。然而,这些方法在推理阶段往往会引入额外的计算延迟,或者在复杂任务上难以达到全参数微调的性能上限。
% \noindent 为了克服上述局限性,\citet{hu-etal:2021lora} 提出了低秩自适应(Low-Rank Adaptation, \mindex{lora})方法。LoRA 的核心思想是假设模型在微调过程中的权重更新也具有较低的“内在秩”。因此,它冻结了预训练的权重矩阵,并将可训练的更新分解为两个低秩矩阵的乘积(即 $\Delta W = BA$)。在训练时,仅更新这两个低秩矩阵,从而大幅减少了可训练参数的数量(通常减少到原来的 0.1\% 甚至更少)。LoRA 的一个显著优势在于,训练完成后,低秩矩阵可以直接合并到原始权重中,这意味着在推理阶段不会引入任何额外的延迟。近年来,基于 LoRA 的变体不断涌现,例如 \citet{dettmers-etal:2023qlora} 提出的 QLoRA,通过结合 4-bit 量化和分页优化器,使得在单张消费级 GPU 上微调 65B 参数规模的模型成为可能;而 \citet{liu-etal:2024dora} 提出的 DoRA 则进一步解耦了权重矩阵的幅度和方向,提升了微调的稳定性和任务性能。
% \noindent PEFT 方法之所以能够以极少的参数达到媲美全参数微调的效果,其背后的理论机制值得深入探讨。一种可能的解释与 \mindex{intrinsic dimensionality} (内在维度)假说密切相关 \cite{li-etal:2018intrinsic}。该假说认为,尽管预训练 LLM 拥有数以百亿计的参数,但其在学习特定下游任务时,有效的解空间实际上存在于一个低维的子流形中。换言之,预训练过程已经为模型赋予了强大的通用表示能力,使其处于一个较好的损失盆地(loss basin)中。微调的本质并非从头学习新知识,而是沿着这个低维子空间进行微小的参数扰动,以适配特定任务的数据分布。因此,像 LoRA 这样的低秩方法恰好契合了这种低维更新的特性,能够高效地捕捉任务特定的变化,而无需在整个高维参数空间中进行昂贵的搜索。
% \noindent 与此处的讨论相关的一个概念是 \mindex{compute-memory trade-off} (计算-内存权衡)。在资源受限的场景下,全参数微调不仅受限于 GPU 显存,还面临优化器状态和激活值存储的巨大压力。PEFT 方法通过极大地减少可训练参数,显著降低了内存占用,使得大模型的微调变得更加平民化。从这个意义上说,PEFT 与前述的样本高效方法在目标上高度一致:前者优化了“参数空间”的利用效率,后者优化了“数据空间”的利用效率。在实际应用中,将参数高效(如 LoRA)与数据高效(如高质量数据筛选)相结合,往往能产生显著的协同效应。例如,在使用极少量高质量数据进行微调时,配合 LoRA 可以有效防止模型在微小数据集上发生过拟合,从而在极低的计算成本下实现最优的指令对齐效果。
\section{大模型偏好对齐技术}
\subsection{对齐概述}
\input{section/3.4sub/part1}
\subsection{奖励模型构建方法}
\input{section/3.4sub/part2}
\subsection{基于 PPO 的强化学习人类反馈训练}
\input{section/3.4sub/part3}
\subsection{直接偏好优化}
\input{section/3.4sub/part4}
\subsection{偏好数据自动生成}
\input{section/3.4sub/part5}
\ No newline at end of file
\noindent 指令微调让大语言模型能够适应那些可被清晰定义的任务,这类问题本质上属于\mindex{alignment}问题。对齐,就是引导模型的行为以符合人类意图,我们期望模型不仅能遵循指令,还要做到无偏见、真实且无害。例如,一个负责任的模型应当拒绝回答“如何制造武器”这类有害请求。与对齐紧密相关的是人工智能安全,其目标在于构建安全且有益的智能系统,确保系统在正常使用乃至被滥用或恶意攻击的情况下,都能保持鲁棒、安全,且行为符合人类的主观预期。对齐之所以困难,是因为人类价值观复杂多样且不断变化,有时我们只有看到模型的回应,才能判断它是否符合期望。因此,对齐不单是针对预设任务的微调,更是一个需要模型与真实世界不断交互、持续学习的宏大问题。
在大规模无标签数据上完成预训练后,对齐通常会经历两个步骤:
\begin{itemize}
\item \vspace{0.5em} \mindex{监督微调} (\mindex{SFT})。使用新的、面向特定任务的标注数据继续训练模型。最常用的技术是指令微调,使模型与“遵循指令”这一预期行为对齐。
\item \vspace{0.3em} \mindex{从人类反馈中学习}。即使经过监督微调,模型仍可能生成不真实、有偏见或有害的内容。此时可以收集人类对模型输出的评估反馈,并利用这些反馈进一步训练模型,实现更深层的对齐。
\end{itemize}
\vspace{0.5em}
一种主流的从人类反馈中学习的方法,是将其当作一个强化学习问题,称为\mindex{reinforcement learning from human feedback} (\mindex{RLHF})。该方法包含两个组件:
\begin{itemize}
\item \vspace{0.5em} \mindex{Agent}。“智能体”就是我们要训练的大语言模型。它从环境接收文本,并输出另一段文本返回给环境,其策略由模型本身的分布定义:$\Pr(\mathbf{y} | \mathbf{x})$
\item \vspace{0.3em} \mindex{Reward Model}。奖励模型充当了环境的替身,它负责对智能体生成的输出序列给出一个数值奖励,以此表征输出的好坏。
\end{itemize}
\vspace{0.5em}
RLHF 涉及两个学习任务:一、利用人类反馈训练奖励模型;二、在奖励模型的指导下,用强化学习算法优化策略。关键步骤可以归纳为:a) 通过预训练和指令微调获得初始策略;b) 为每个输入生成多个候选输出,并收集人类对这些输出的偏好排序;c) 基于排序结果学习奖励模型;d) 以奖励模型为监督信号,对策略进行强化学习微调。图\ref{fig:llm-rlhf-overview} 展示了这一流程。
\begin{figure}[!t]
\centering
\input{figures/Chapter4/figure-rlhf-overview}
\caption{RLHF 概述。其中涉及 4 个关键步骤:a) 使用预训练和监督微调训练初始 LLM(即策略);b) 通过对 LLM 的输出进行排序来收集人类偏好数据;c) 使用排序结果训练奖励模型;d) 基于奖励模型对策略进行 RL 微调。双线箭头表示训练或微调。}
\label{fig:llm-rlhf-overview}
\end{figure}
下面我们通过一个具体的例子来理解 RLHF 的基本思想。假设我们已有一个经过预训练和指令微调的大语言模型,用户输入:
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begin{center}
\parbox{0.9\textwidth}
{
怎样才能更环保地生活?
}
\end{center}
\end{tcolorbox}
\vspace{0.5em}
模型通过采样生成了4个不同的回答:
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begin{center}
\setlength{\tabcolsep}{4pt}
\begin{tabular}{p{0.22\textwidth} p{0.72\textwidth}}
输出1 ($\mathbf{y}_1$): & 考虑改用电动汽车或自行车代替传统汽车,以减少碳排放,保护我们的星球。 \\
输出2 ($\mathbf{y}_2$): & 尝试极简主义生活方式,减少所拥有的物品,从而降低消费以及制造和处置带来的环境影响。 \\
输出3 ($\mathbf{y}_3$): & 脱离电网,自己用可再生能源发电并收集雨水,实现完全自给自足,摆脱对不可再生资源的依赖。 \\
输出4 ($\mathbf{y}_4$): & 多支持本地农产品,这既能减少食品长途运输的碳足迹,又能享受新鲜健康的食物。
\end{tabular}
\end{center}
\end{tcolorbox}
\vspace{0.5em}
让标注者直接为每个输出打出绝对分数往往比较困难,但对他们来说,判断输出之间的相对优劣则要容易得多。于是可以得到一个偏好排序,例如:
\begin{equation}
\mathbf{y}_1 \succ \mathbf{y}_4 \succ \mathbf{y}_2 \succ \mathbf{y}_3 \nonumber
\end{equation}
利用这类排序数据,我们便可以训练奖励模型。奖励模型通常采用与目标大语言模型相似但规模更小的架构。给定输入$\mathbf{x}$和输出$\mathbf{y}_k$,将它们拼接成一个序列$\mathrm{seq}_k = [\mathbf{x},\mathbf{y}_k]$,并在末尾添加一个特殊符号(如$\langle \backslash s \rangle$),然后取 Transformer 最顶层该符号位置的输出作为整个序列的表示,最后经过一个线性层得到奖励分数$R(\mathbf{x},\mathbf{y}_k)$。训练时常采用成对排序损失:
\begin{eqnarray}
\mathrm{Loss}_{\omega}(\mathcal{D}_r) = -\mathbb{E}_{(\mathbf{x},\mathbf{y}_{k_1},\mathbf{y}_{k_2}) \sim \mathcal{D}_r} \log(\mathrm{Sigmoid}(R_{\omega}(\mathbf{x},\mathbf{y}_{k_1}) - R_{\omega}(\mathbf{x},\mathbf{y}_{k_2})))
\end{eqnarray}
其中$\omega$为奖励模型参数,$\mathcal{D}_r$是由输入和输出对构成的偏好数据集。如果模型预测的排序与人类标注不一致,该损失就会施加惩罚。通过最小化这一损失,我们得到奖励模型$R_{\hat{\omega}}(\cdot)$,它可以为任意输入-输出对提供连续的奖励分数,成为后续策略学习的监督信号。
在策略学习阶段,一个直接的目标是最大化期望奖励:
\begin{eqnarray}
\tilde{\theta} = \argmax_{\hat{\theta}^+} \mathbb{E}_{(\mathbf{x},\mathbf{y}_{\hat{\theta}^+}) \sim \mathcal{D}_{\mathrm{rlft}}} R_{\hat{\omega}}(\mathbf{x},\mathbf{y}_{\hat{\theta}^+})
\end{eqnarray}
其中$\mathbf{x}$从输入数据集中采样,$\mathbf{y}_{\hat{\theta}^+}$则从当前策略分布$\mathrm{Pr}_{\hat{\theta}^+}(\mathbf{y}|\mathbf{x})$中采样。实践中通常会采用更高级的强化学习算法,如\mindex{proximal policy optimization} (\mindex{PPO}),以获得更稳定的训练效果,详细算法将在后续章节展开。
% 为什么不直接将偏好学习视为标准监督学习问题?因为要让标注者精确描述人类的价值观,并给出完美对齐的输出,往往异常困难;相比之下,对已有输出进行偏好排序就简单得多。RLHF 通过学习奖励模型来捕捉人类偏好,再用它来指导策略训练,尤其适用于那些“演示难、评判易”的场景。此外,强化学习的探索机制还能让模型发现标注数据之外的潜在有益策略,从而进一步提升对齐效果。
% \noindent 指令微调提供了一种简单的方法,使大语言模型能够适应可以被明确定义的任务。这个问题可以被广泛地归类为一个 \mindex{alignment} 问题。在这里,对齐指的是引导大语言模型的行为以符合人类意图的过程。这种引导可以来自标记数据、人类反馈或任何其他形式的人类偏好。例如,我们希望大语言模型不仅能准确地遵循指令,还要做到无偏见、真实和无害。因此,我们需要监督模型,使其符合人类的价值观和期望。一个常见的例子是,当我们询问一个大语言模型如何制造武器时,如果它没有经过仔细的对齐,它可能会提供一份关键步骤的清单。然而,一个负责任的模型应该能够识别并避免回应有害或非法信息的请求。在这种情况下,对齐对于确保大语言模型以负责任的方式并根据道德准则行事至关重要。
% 与对齐相关的一个概念是人工智能安全。人工智能的一个终极目标是构建安全且对社会有益的智能系统。为了实现这一目标,我们应确保这些系统在任何现实世界使用条件下,甚至在滥用或恶意使用的情况下,都能保持鲁棒、安全和主观。对于大语言模型,可以通过适当的人类指导来提高其安全性,例如人类标记的数据以及在应用过程中与用户的互动。
% 对齐是困难的,因为人类的价值观和期望是多样且不断变化的。有时,我们很难精确地描述人类想要什么,除非我们看到大语言模型对用户请求的响应。这使得对齐不再是在预定义任务上调整大语言模型的问题,而是一个更大的问题,即通过与现实世界的互动来训练它们。
% 由于对控制人工智能系统的担忧,关于大语言模型对齐问题的研究激增。通常,在大语言模型在大规模无标签数据上进行预训练后,会采用两个对齐步骤。
% \begin{itemize}
% \item \vspace{0.5em} \mindex{监督微调} (\mindex{SFT})。这涉及在新的、面向任务的、有标记的数据上继续训练预训练的 LLM。一种常用的 SFT 技术是指令微调。如前一小节所述,通过从指令-响应标注数据中学习,LLM 可以与遵循指令的预期行为对齐,从而能够执行各种指令描述的任务。监督微调可以看作是遵循预训练+微调的范式,并为适配 LLM 提供了一种相对直接的方法。
% \item \vspace{0.3em} \mindex{从人类反馈中学习}。在 LLM 完成预训练和监督微调后,如果给予适当的提示,它就可以用来响应用户的请求。但该模型可能会生成不真实、有偏见或有害的内容。为了使 LLM 与用户更加对齐,一种简单的方法是直接从人类反馈中学习。例如,给定用户提供的一些指令和输入,专家被要求根据他们的偏好和兴趣来评估模型的响应情况。然后,这些反馈被用来进一步训练 LLM,以实现更好的对齐。
% \end{itemize}
% \vspace{0.5em}
% 一种从人类反馈中学习的典型方法是将其视为一个强化学习 (RL) 问题,称为 \mindex{reinforcement learning from human feedback} (\mindex{RLHF}) \cite{ouyang-etal:2022training}。RLHF 方法最初是为了解决一般的序列决策问题而提出的 \cite{christiano-etal:2017deep},后来成功地应用于 GPT 系列模型的开发中 \cite{stiennon-etal:2020learning}。作为一种强化学习方法,RLHF 的目标是通过最大化从环境中获得的某种奖励来学习一个策略。具体来说,RLHF 中构建了两个组件:
% \begin{itemize}
% \item \vspace{0.5em} \mindex{Agent}。 ``智能体'' (agent),也称为 LM ``智能体'',是我们想要训练的 LLM。该``智能体''通过与环境交互来运作:它从环境中接收一段文本,并输出另一段文本返回给环境。``智能体''的策略是由 LLM 定义的函数,即 $\Pr(\mathbf{y} | \mathbf{x})$。
% \item \vspace{0.3em} \mindex{Reward Model}。 奖励模型是环境的代理。每当``智能体''生成一个输出序列时,奖励模型都会为该输出序列分配一个数值分数 (即奖励)。这个分数告诉``智能体''输出序列的好坏程度。
% \end{itemize}
% \vspace{0.5em}
% 在 RLHF 中,我们需要执行两个学习任务:1) 奖励模型学习,即利用人类对智能体输出的反馈来训练奖励模型;2) 策略学习,即使用强化学习算法,在奖励模型的指导下优化策略。以下是 RLHF 所涉及关键步骤的简要概述。
% \begin{itemize}
% \item \vspace{0.5em} 使用预训练和指令微调构建初始策略。
% \item \vspace{0.3em} 使用该策略为每个输入生成多个输出,然后收集关于这些输出的人类反馈(例如,对输出进行比较)。
% \item \vspace{0.3em} 从人类反馈中学习奖励模型。
% \item \vspace{0.3em} 在奖励模型的监督下微调策略。
% \end{itemize}
% \vspace{0.5em}
% 图 \ref{fig:llm-rlhf-overview} 展示了 RLHF 的概览。鉴于本节仅作为大语言模型概念的简要介绍,将不包括对 RLHF 技术的详细讨论。我们将通过一个简单的例子来说明 RLHF 背后的基本思想。
% \begin{figure}[!t]
% \centering
% \input{figures/Chapter4/figure-rlhf-overview}
% \caption{RLHF 概述。其中涉及 4 个关键步骤:a) 使用预训练和监督微调来训练一个初始 LLM(即策略);b) 通过对 LLM 的输出进行排序来收集人类偏好数据;c) 使用排序结果训练一个奖励模型;d) 基于该奖励模型对策略进行 RL 微调。双线箭头表示训练或微调。}
% \label{fig:llm-rlhf-overview}
% \end{figure}
% 假设我们已经通过预训练和指令微调训练好了一个大语言模型。这个大语言模型被部署用于响应用户的请求。例如,用户可能会输入
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begin{center}
% \parbox{0.9\textwidth}
% {
% How can I live a more environmentally friendly life?
% }
% \end{center}
% \end{tcolorbox}
% \vspace{0.5em}
% 我们使用这个大语言模型通过对输出空间进行采样来生成 4 个不同的输出(表示为 $\{\mathbf{y}_1, ..., \mathbf{y}_4\}$)
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begin{center}
% \parbox{0.9\textwidth}
% {
% \begingroup
% \setlength{\tabcolsep}{1pt}
% \begin{tabular}{L{0.18\textwidth} L{0.74\textwidth} }
% Output 1 ($\mathbf{y}_1$): & Consider switching to an electric vehicle or bicycle instead of traditional cars to reduce carbon emissions and protect our planet. \\
% Output 2 ($\mathbf{y}_2$): & Adopt a minimalist lifestyle. Own fewer possessions to reduce consumption and the environmental impact of manufacturing and disposal. \\
% Output 3 ($\mathbf{y}_3$): & Go off-grid. Generate your own renewable energy and collect rainwater to become completely self-sufficient and reduce reliance on non-renewable resources. \\
% Output 4 ($\mathbf{y}_4$): & Support local farm products to reduce the carbon footprint of transporting food, while enjoying fresh, healthy food.
% \end{tabular}
% \endgroup
% }
% \end{center}
% \end{tcolorbox}
% \vspace{0.5em}
% 然后,我们要求标注员评估这些输出。一种直接的方法是为每个输出分配一个评级分数。在这种情况下,奖励模型学习问题可以被构建为训练一个回归模型的任务。但为大语言模型的输出给出数值分数对标注员来说并非易事。通常很难设计一个所有标注员都能认同并轻松遵循的标注标准。另一种在大语言模型开发中更流行的方法是对这些输出进行排序。例如,上述输出的一个可能排序是
% \begin{equation}
% \mathbf{y}_1 \succ \mathbf{y}_4 \succ \mathbf{y}_2 \succ \mathbf{y}_3 \nonumber
% \end{equation}
% 然后使用这个排序结果来训练一个奖励模型。通常,RLHF 中的奖励模型是一个语言模型,它与目标大语言模型共享相同的架构,但模型尺寸更小。给定输入 $\mathbf{x}$ 和输出 $\mathbf{y}_k$,我们将它们连接起来形成一个序列 $\mathrm{seq}_k = [\mathbf{x},\mathbf{y}_k]$。这个序列使用强制解码从左到右进行处理。由于在语言建模中,每个位置只能访问其左侧的上下文,因此最顶层 Transformer 层在第一个位置的输出不能用作序列的表示。取而代之的是,在序列的末尾添加一个特殊符号(例如 $\langle \backslash s \rangle$),并且 Transformer 层堆栈的相应输出被视为整个序列的表示。在这个表示之上构建一个输出层,例如一个线性变换层,以生成奖励,表示为 $R(\mathrm{seq}_k)$ 或 $R(\mathbf{x},\mathbf{y}_k)$。
% 我们使用排序损失来训练这个奖励模型。例如,一个成对排序损失函数可以写成如下形式
% \begin{eqnarray}
% \mathrm{Loss}_{\omega}(\mathcal{D}_r) & = & -\mathbb{E}_{(\mathbf{x},\mathbf{y}_{k_1},\mathbf{y}_{k_2}) \sim \mathcal{D}_r} \log(\mathrm{Sigmoid}(R_{\omega}(\mathbf{x},\mathbf{y}_{k_1}) - R_{\omega}(\mathbf{x},\mathbf{y}_{k_2})))
% \end{eqnarray}
% \noindent 其中 $\omega$ 表示奖励模型的参数,$\mathcal{D}_r$ 表示一个由输入和一对输出组成的元组集合。$(\mathbf{x},\mathbf{y}_{k_1},\mathbf{y}_{k_2}) \sim \mathcal{D}_r$ 是一个采样操作,它以一定的概率从 $\mathcal{D}_r$ 中抽取一个样本 $(\mathbf{x},\mathbf{y}_{k_1},\mathbf{y}_{k_2})$。举个例子,假设我们首先以均匀分布抽取一个模型输入 $\mathbf{x}$,然后以给定 $\mathbf{x}$ 的条件下 $\mathbf{y}_{k_1} \succ \mathbf{y}_{k_2}$ 的概率(表示为 $\Pr(\mathbf{y}_{k_1} \succ \mathbf{y}_{k_2} | \mathbf{x})$)抽取一对模型输出。相应的损失函数由下式给出
% \begin{eqnarray}
% & & \mathrm{Loss}_{\omega}(\mathcal{D}_r) \nonumber \\
% & = & -\sum \Pr(\mathbf{x}) \cdot \Pr(\mathbf{y}_{k_1} \succ \mathbf{y}_{k_2} | \mathbf{x}) \cdot \log(\mathrm{Sigmoid}(R_{\omega}(\mathbf{x},\mathbf{y}_{k_1}) - R_{\omega}(\mathbf{x},\mathbf{y}_{k_2}))) \nonumber \\
% & = & -\frac{1}{K} \sum \Pr(\mathbf{y}_{k_1} \succ \mathbf{y}_{k_2} | \mathbf{x}) \cdot \log(\mathrm{Sigmoid}(R_{\omega}(\mathbf{x},\mathbf{y}_{k_1}) - R_{\omega}(\mathbf{x},\mathbf{y}_{k_2})))
% \end{eqnarray}
% \noindent 其中 $K$ 表示采样中涉及的模型输入数量。虽然这些函数的形式可能看起来很复杂,但它们的思想很简单:如果模型预测的两个输出的排序与人类标记的排序不同,我们就惩罚模型。相反,如果预测的排序与人类标记的排序匹配,模型则会获得奖励。
% 我们可以通过最小化上述排序损失来训练奖励模型
% \begin{eqnarray}
% \hat{\omega} & = & \argmin_{\omega} \mathrm{Loss}_{\omega}(\mathcal{D}_r)
% \end{eqnarray}
% \noindent 得到的模型 $R_{\hat{\omega}}(\cdot)$ 可以用来评估任何给定的输入和输出对。请注意,尽管奖励模型是使用基于排序的目标进行训练的,但它被用于评分。这使得它能够提供连续的监督信号,这对于训练其他模型非常有益。
% 我们现在转向策略学习问题。一个通常采用的目标是在一组输入-输出对上最大化奖励,得到一个用于强化学习微调的简单训练目标
% \begin{eqnarray}
% \tilde{\theta} & = & \argmax_{\hat{\theta}^+} \mathbb{E}_{(\mathbf{x},\mathbf{y}_{\hat{\theta}^+}) \sim \mathcal{D}_{\mathrm{rlft}}} R_{\hat{\omega}}(\mathbf{x},\mathbf{y}_{\hat{\theta}^+})
% \end{eqnarray}
% \noindent 其中,最优参数 $\tilde{\theta}$ 是通过微调预训练参数 $\hat{\theta}$ 得到的。$\mathcal{D}_{\mathrm{rlft}}$ 是强化学习微调数据集。对于每个样本 $(\mathbf{x},\mathbf{y}_{\hat{\theta}^+})$,$\mathbf{x}$ 从一个准备好的输入序列数据集中采样,而 $\mathbf{y}_{\hat{\theta}^+}$ 则从策略给出的分布 $\mathrm{\Pr}_{\hat{\theta}^+}(\mathbf{y}|\mathbf{x})$ 中采样。
% 在实践中,通常使用更高级的强化学习算法,例如 \mindex{proximal policy optimization} (\mindex{PPO}),以实现更稳定的训练和更好的性能。我们将强化学习算法的详细讨论留到本书的后续部分,在这些部分中,RLHF 被广泛用于对齐。
% 这里出现了一个有趣的问题:为什么不将从人类偏好中学习视为一个标准的监督学习问题呢?这个问题与我们前面关于数据标注困难的讨论密切相关。通常,描述人类的价值观和目标是具有挑战性的,而让人类提供完全对齐的输出则更加困难。作为一种替代方案,标注给定模型输出列表的偏好提供了一个更简单的任务。通过这样做,我们可以创建一个理解人类偏好的模型,然后该模型可以用作训练策略的奖励模型。从机器学习的角度来看,RLHF 对于那些智能体的期望行为难以展示但易于被人类识别的场景特别有用。RLHF 的另一个优点是它能够探索样本空间。通过采用采样技术,用强化学习训练的模型可以超越带标注的数据集,去探索额外的样本。这种探索能力使得 RLHF 能够发现那些仅从标记数据中不易察觉的潜在有益策略。
\label{sec:training-reward-models}
\noindent 奖励模型在通用强化学习框架中扮演着核心角色,是计算价值函数的基础。本节将讨论如何训练这样的奖励模型。
在 RLHF 中,奖励模型是一个将输入和输出 token 序列对映射为标量的神经网络。给定输入 $\mathbf{x}$ 和输出 $\mathbf{y}$,奖励值可以表示为
\begin{eqnarray}
r & = & \mathrm{Reward}(\mathbf{x},\mathbf{y})
\end{eqnarray}
其中 $\mathrm{Reward}(\cdot)$ 即奖励模型。$r$ 衡量的是在给定输入 $\mathbf{x}$ 的条件下,输出 $\mathbf{y}$ 与期望行为的一致程度。如前所述,我们假设 $\mathbf{x}$$\mathbf{y}$ 都是完整的文本,这意味着奖励模型需要基于整个序列的语义来给出评判。在具体实现中,我们会将 $\mathbf{x}$$\mathbf{y}$ 拼接后送入模型,模型在序列的每个位置都会产生一个输出,但我们只取序列末尾(即 $\mathbf{y}$ 的最后一个 token 之后)的表示来计算最终奖励;中间位置的输出可以约定为 0 或其他占位值。为保持符号简洁,下文直接用 $r(\mathbf{x},\mathbf{y})$ 表示奖励模型 $\mathrm{Reward}(\mathbf{x},\mathbf{y})$
实现奖励模型的方式有很多。一种简单的做法是基于预训练的大语言模型来构建。具体地,我们将 $\mathbf{x}$$\mathbf{y}$ 拼接成一个单一的 token 序列 $\mathrm{seq}_{\mathbf{x},\mathbf{y}} = [\mathbf{x}, \mathbf{y}]$,将其输入一个预训练的 LLM,并从最顶层 Transformer 层的每个位置获取表示。我们取出最后一个位置的表示 $\mathbf{h}_{\mathrm{last}}$,并通过一个线性变换将其映射为标量:
\begin{eqnarray}
r(\mathbf{x},\mathbf{y}) & = & \mathbf{h}_{\mathrm{last}} \mathbf{W}_{r}
\end{eqnarray}
其中 $\mathbf{h}_{\mathrm{last}}$$d$ 维向量,$\mathbf{W}_{r}$$d \times 1$ 的线性映射矩阵。该架构如图 \ref{fig:reward-model-architecture} 所示。
\begin{figure}[!t]
\centering
\input{figures/Chapter4/figure-reward-model-architecture}
\caption{基于 Transformer 的奖励模型架构。模型主体仍是一个 LLM,其 Transformer 解码器作为序列表示模型。我们抽取解码器在最后一个位置的表示作为整个序列 $[\mathbf{x},\mathbf{y}]$ 的表示,再通过一个线性映射将其转换为标量奖励分数。}
\label{fig:reward-model-architecture}
\end{figure}
训练奖励模型的第一步是收集关于若干生成输出的人类反馈。给定输入 $\mathbf{x}$,我们先用 LLM 生成多个候选输出 $\{\mathbf{y}_1,\dots,\mathbf{y}_N\}$。人类反馈通常可以通过以下几种方式获得:
\begin{itemize}
\item \textbf{成对比较}\textbf{成对排序}):给定两个不同的输出,人类专家指出哪一个更好。
\item \textbf{评分}:人类专家为每个输出单独给出一个数值分数(例如 1--5 分)。
\item \textbf{列表排序}:人类专家直接对一组输出进行整体排序。
\end{itemize}
这里我们聚焦于成对比较反馈,因为它是 RLHF 中最简单且最常用的人类反馈形式之一。在该设置下,每次从候选池 $\{\mathbf{y}_1,\dots,\mathbf{y}_N\}$ 中随机抽取两个输出 $(\mathbf{y}_a,\mathbf{y}_b)$,呈现给人类专家,并要求其根据清晰度、相关性、准确性等标准表明偏好。偏好结果可以编码为二元标签:$\mathbf{y}_a \succ \mathbf{y}_b$ 表示 $\mathbf{y}_a$ 更受偏好,$\mathbf{y}_b \succ \mathbf{y}_a$ 则相反。
为了从成对比较数据中学习奖励模型,我们需要一个将偏好结果与奖励分数联系起来的概率模型。\mindex{Bradley-Terry model} \cite{bradley-and-terry:rank} 正是这样一种简单且广泛使用的模型,它估计一个项目比另一个项目更受偏好的概率。适配到当前符号,$\mathbf{y}_a$$\mathbf{y}_b$ 更受偏好的概率可写为
\begin{eqnarray}
\Pr(\mathbf{y}_a \succ \mathbf{y}_b \mid \mathbf{x}) & = & \frac{e^{r(\mathbf{x},\mathbf{y}_a)}}{e^{r(\mathbf{x},\mathbf{y}_a)} + e^{r(\mathbf{x},\mathbf{y}_b)}} \nonumber \\
& = & \frac{e^{r(\mathbf{x},\mathbf{y}_a)-r(\mathbf{x},\mathbf{y}_b)}}{e^{r(\mathbf{x},\mathbf{y}_a)-r(\mathbf{x},\mathbf{y}_b)}+1} \nonumber \\
& = & \mathrm{Sigmoid}\bigl(r(\mathbf{x},\mathbf{y}_a)-r(\mathbf{x},\mathbf{y}_b)\bigr)
\end{eqnarray}
训练奖励模型时,我们希望奖励模型赋予被偏好输出更高的分数,也就是最大化上述偏好概率。由此可以得到基于 Bradley-Terry 模型的损失函数
\begin{eqnarray}
\mathcal{L}_r(\phi) & = & -\mathbb{E}_{(\mathbf{x},\mathbf{y}_a,\mathbf{y}_b) \sim \mathcal{D}_r} \bigl[ \log \mathrm{Pr}_{\phi}(\mathbf{y}_a \succ \mathbf{y}_b \mid \mathbf{x}) \bigr] \label{eq:pairwise-reward-loss-expectation}
\end{eqnarray}
其中 $(\mathbf{x},\mathbf{y}_a,\mathbf{y}_b)$ 采样自包含输入及其偏好输出对的人类标注数据集 $\mathcal{D}_r$$\phi$ 表示奖励模型的参数(包括 Transformer 解码器的参数和线性映射矩阵 $\mathbf{W}_{r}$)。实际中通常假设样本均匀采样,因此可以用求和代替期望:
\begin{eqnarray}
\mathcal{L}_r(\phi) & = & -\frac{1}{|\mathcal{D}_r|} \sum_{(\mathbf{x},\mathbf{y}_a,\mathbf{y}_b) \in \mathcal{D}_r} \log \mathrm{Pr}_{\phi}(\mathbf{y}_a \succ \mathbf{y}_b \mid \mathbf{x}) \label{eq:pairwise-reward-loss-sum}
\end{eqnarray}
训练的目标是找到最小化该损失的最优参数 $\hat{\phi}$
\begin{eqnarray}
\hat{\phi} & = & \argmin_{\phi} \mathcal{L}_r(\phi)
\end{eqnarray}
由于奖励模型本身也是一个 LLM,我们可以直接复用标准的 Transformer 训练流程,只需将交叉熵损失替换为公式 (\ref{eq:pairwise-reward-loss-sum}) 中的成对比较损失。训练完成后,我们便可用训练好的奖励模型 $r_{\hat{\phi}}(\cdot)$ 来为目标 LLM 的对齐提供监督信号。
值得注意的是,尽管我们使用成对比较数据来训练奖励模型,但在后续的对齐阶段,奖励模型是独立地为每个输入-输出对打分的。成对排序的训练目标使奖励模型对输出之间的细微质量差异足够敏感,而我们依赖其输出的连续标量分数来指导策略优化。这样做的一个优势在于:无论训练时选用何种具体的排序损失,只要最终能获得一个可靠的标量奖励函数,它就可以无缝嵌入到统一的 RLHF 对齐框架中。
\ No newline at end of file
\label{sec:rlhf-training-llms}
有了奖励模型之后,下一步就是让大语言模型学会生成高奖励的回复。直观地,我们希望找到策略 $\pi_{\theta}$,使其在输入分布下的期望奖励最大:
\begin{equation}
J(\theta) = \mathbb{E}_{\mathbf{x}\sim\mathcal{D}}\;\mathbb{E}_{\mathbf{y}\sim\pi_{\theta}(\cdot|\mathbf{x})}\big[r(\mathbf{x},\mathbf{y})\big],
\end{equation}
同时还要保持生成文本的流畅与合理,避免为追求奖励而输出无意义内容。直接优化上式几乎不可能,因为文本生成是离散且不可微的。强化学习中的策略梯度定理为此提供了可行方案。
策略梯度方法的核心思想是:通过采样估计目标函数 $J(\theta)$ 的梯度,再利用梯度上升更新参数。为了降低梯度估计的方差,通常不直接使用原始奖励,而是引入优势函数 $A(s_t,a_t)$,衡量在状态 $s_t$ 下采取动作 $a_t$ 比平均水平好多少。优势可通过一个独立的价值模型 $V$ 来近似,例如用时序差分误差 $r_t + \gamma V(s_{t+1}) - V(s_t)$ 作为估计。在此框架下,一条轨迹 $\tau=(s_1,a_1,\dots,s_T,a_T)$ 的效用可写为
\begin{equation}
U(\tau;\theta) = \sum_{t=1}^{T} \log \pi_{\theta}(a_t|s_t)\,A(s_t,a_t),
\end{equation}
优化目标即最小化负期望效用:
\begin{equation}
\mathcal{L}(\theta) = -\mathbb{E}_{\tau}\big[U(\tau;\theta)\big].
\end{equation}
这个形式的直观解释很清晰:若某动作的优势为正,就提高其对数概率;若为负,则压低概率。
将上述形式迁移到语言生成任务,输入为 $\mathbf{x}$,模型逐 token 生成回答 $\mathbf{y}=(y_1,\dots,y_T)$。状态是已生成的前缀 $(\mathbf{x},\mathbf{y}_{<t})$,动作是下一个 token $y_t$。损失函数变为
\begin{equation}
\mathcal{L}(\theta) = -\mathbb{E}_{(\mathbf{x},\mathbf{y})}\Big[\sum_{t=1}^{T} \log \pi_{\theta}(y_t|\mathbf{x},\mathbf{y}_{<t})\,A(\mathbf{x},\mathbf{y}_{<t},y_t)\Big].
\end{equation}
在典型的 RLHF 设定中,数据集只有输入 $\mathbf{x}$,输出 $\mathbf{y}$ 由策略 $\pi_{\theta}$ 自身采样,所以实际使用的损失为
\begin{equation}
\mathcal{L}(\theta) = -\mathbb{E}_{\mathbf{x}\sim\mathcal{D}}\;\mathbb{E}_{\mathbf{y}\sim\pi_{\theta}(\cdot|\mathbf{x})}\Big[U(\mathbf{x},\mathbf{y};\theta)\Big].
\end{equation}
然而,每次更新都从当前策略重新采样所有输出,计算开销极大。一种更高效的做法是:固定一个旧策略(称为参考策略 $\pi_{\theta_{\mathrm{ref}}}$)来收集数据,再利用重要性采样将旧数据重用于新策略的评估。对于完整序列,有如下等价关系:
\begin{equation}
J(\theta) = \mathbb{E}_{\tau\sim\pi_{\theta_{\mathrm{ref}}}}\Big[\frac{\Pr_{\theta}(\tau)}{\Pr_{\theta_{\mathrm{ref}}}(\tau)}\,R(\tau)\Big].
\end{equation}
该形式称为替代目标,它将采样和评估解耦,让我们可以用同一批旧样本多次更新策略。落实到 token 层面,效用函数中的对数概率被替换为当前策略与参考策略的概率比:
\begin{equation}
U(\tau;\theta) = \sum_{t=1}^{T} \frac{\pi_{\theta}(a_t|s_t)}{\pi_{\theta_{\mathrm{ref}}}(a_t|s_t)}\,A(s_t,a_t).
\end{equation}
比率大于 1 意味着当前策略比参考策略更偏好该动作,反则反之。
单纯使用概率比会带来新的问题:比率波动剧烈,梯度的方差可能非常大,导致训练不稳定。为解决这一问题,近端策略优化(PPO)同时引入了两种约束。第一,对单步概率比进行截断,将其限制在 $[1-\epsilon,\,1+\epsilon]$ 的区间内,避免某一步更新幅度过大:
\begin{equation}
U_{\mathrm{clip}}(\tau;\theta) = \sum_{t=1}^{T} \min\Big(\frac{\pi_{\theta}}{\pi_{\theta_{\mathrm{ref}}}},\;
\operatorname{clip}\big(\frac{\pi_{\theta}}{\pi_{\theta_{\mathrm{ref}}}}, 1-\epsilon, 1+\epsilon\big)\Big)\,A(s_t,a_t).
\end{equation}
(为简洁省略了时间下标;实际使用中会根据优势的正负分别采用不同的截断方式,使更新更为保守。)第二,在目标函数中加入显式的 KL 惩罚项,强制当前策略的概率分布整体不要偏离参考策略太远。惩罚项取两策略在序列上的对数概率差:
\begin{equation}
\mathrm{Penalty} = \sum_{t=1}^{T}\Big[\log \pi_{\theta}(a_t|s_t) - \log \pi_{\theta_{\mathrm{ref}}}(a_t|s_t)\Big].
\end{equation}
将二者结合,得到 PPO 的最终目标
\begin{equation}
U_{\mathrm{ppo}}(\tau;\theta) = U_{\mathrm{clip}}(\tau;\theta) - \beta\,\mathrm{Penalty},
\label{eq:ppo-objective}
\end{equation}
其中 $\beta$ 控制惩罚的强度。这一设计既防止了局部更新过于激进,也在全局上保证了策略平稳演进,因此被广泛用于大语言模型的 RLHF 训练。
映射回语言模型的符号,对于输入 $\mathbf{x}$ 和输出 $\mathbf{y}$,完整的 PPO 目标可以写成
\begin{align}
U(\mathbf{x},\mathbf{y};\theta) = \sum_{t=1}^{T} \min\Big(&\frac{\pi_{\theta}(y_t|\mathbf{x},\mathbf{y}_{<t})}{\pi_{\theta_{\mathrm{ref}}}(y_t|\mathbf{x},\mathbf{y}_{<t})},\;
\operatorname{clip}(\cdots, 1-\epsilon, 1+\epsilon)\Big)\,A(\mathbf{x},\mathbf{y}_{<t},y_t) \nonumber \\
&- \beta\sum_{t=1}^{T}\bigl[\log \pi_{\theta}(y_t|\cdot) - \log \pi_{\theta_{\mathrm{ref}}}(y_t|\cdot)\bigr].
\end{align}
相应地,策略训练的损失为 $\mathcal{L}(\theta) = -\mathbb{E}_{\mathbf{x}\sim\mathcal{D}}\mathbb{E}_{\mathbf{y}\sim\pi_{\theta}(\cdot|\mathbf{x})}[U(\mathbf{x},\mathbf{y};\theta)]$。其中优势 $A$ 由价值模型提供,奖励则来自已训练好的奖励模型。
实现上述训练流程需要同时维护四个基于 Transformer 解码器的模型:
\begin{itemize}
\item \textbf{奖励模型} $r_{\phi}$:将输入与输出拼接映射为一个标量奖励,由人类偏好数据训练得到。
\item \textbf{价值模型} $V_{\omega}$:预测从当前状态开始的未来累积奖励,为计算优势提供基线,常与奖励模型共享架构。
\item \textbf{参考模型} $\pi_{\theta_{\mathrm{ref}}}$:策略训练的起点(如经过指令微调的模型),在 RLHF 过程中参数冻结,为重要性采样和 KL 惩罚提供基准。
\item \textbf{目标策略} $\pi_{\theta}$:即我们需要训练的语言模型,在奖励与价值的双重监督下不断更新。
\end{itemize}
这些模型的训练是交替进行的:首先用预训练模型初始化上述四个模型;接着收集人类偏好数据训练奖励模型;然后固定奖励模型和参考模型,同时优化价值模型和策略。每一步中,策略生成输出序列,价值模型给出优势估计,策略通过最小化 PPO 损失来更新,价值模型则通过最小化价值预测的均方误差来更新。循环往复,直到策略能够稳定地生成符合人类偏好的回复。
\ No newline at end of file
\label{sec:dpo}
基于 PPO 的 RLHF 虽然有效,但需要同时维护奖励模型、价值模型、参考模型和策略四个网络,训练流程较为复杂。一个自然的想法是:能否跳过显式的奖励建模,直接利用人类偏好数据优化语言模型?\textbf{直接偏好优化(DPO)}正是沿着这一思路提出的方法 \cite{rafailov-etal:2024direct}。图 \ref{fig:rlhf-vs-dpo} 展示了标准 RLHF 方法与 DPO 方法的对比。
\begin{figure}[!t]
\centering
\input{figures/Chapter4/figure-rlhf-vs-dpo}
\caption{标准的 RLHF (PPO) 与 DPO。在 RLHF 中,人类偏好数据用于训练一个奖励模型,该模型随后被用于训练策略以及价值函数。在 DPO 中,人类偏好数据的使用更加直接,策略直接在此数据上进行训练,无需训练奖励模型。}
\label{fig:rlhf-vs-dpo}
\end{figure}
回顾 RLHF 中策略训练的出发点,它是一个带 KL 惩罚的目标函数
\begin{equation}
\tilde{\theta} = \argmin_{\theta}\; \mathbb{E}_{\mathbf{x}\sim\mathcal{D}}\,
\mathbb{E}_{\mathbf{y}\sim\pi_{\theta}(\cdot|\mathbf{x})}\Big[
-r(\mathbf{x},\mathbf{y}) + \beta\big(\log\pi_{\theta}(\mathbf{y}|\mathbf{x}) - \log\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x})\big)
\Big],
\label{eq:dpo-start}
\end{equation}
其中 $r(\mathbf{x},\mathbf{y})$ 是奖励模型给出的分数,超参数 $\beta$ 控制策略与参考模型 $\pi_{\theta_{\mathrm{ref}}}$ 的接近程度。该目标本质上鼓励模型生成高奖励且不偏离太远的回复。
这个优化问题存在一个重要的闭式解。通过将目标视为关于分布 $\pi_{\theta}$ 的泛函并求极值,可以证明最优策略 $\pi^*$ 满足
\begin{equation}
\pi^*(\mathbf{y}|\mathbf{x}) \propto \pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x})\,
\exp\!\Big(\frac{1}{\beta}\,r(\mathbf{x},\mathbf{y})\Big).
\label{eq:dpo-optimal-policy}
\end{equation}
也就是说,最优策略是在参考策略的基础上,按奖励的指数倍数重新加权得到的分布。对该式两边取对数并整理,可以反过来把奖励表示为策略的函数
\begin{equation}
r(\mathbf{x},\mathbf{y}) = \beta\,\log\frac{\pi^*(\mathbf{y}|\mathbf{x})}{\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x})} + \beta\log Z(\mathbf{x}),
\label{eq:dpo-reward}
\end{equation}
其中 $Z(\mathbf{x})$ 是与 $\mathbf{y}$ 无关的归一化常数。
现在,将这一奖励表达式代入训练奖励模型时所使用的 Bradley–Terry 偏好模型中。在 RLHF 框架下,我们假设人类偏好 $\mathbf{y}_a \succ \mathbf{y}_b$ 的概率为
\begin{equation}
\Pr(\mathbf{y}_a \succ \mathbf{y}_b\mid\mathbf{x}) = \sigma\big(r(\mathbf{x},\mathbf{y}_a) - r(\mathbf{x},\mathbf{y}_b)\big),
\end{equation}
其中 $\sigma$ 是 sigmoid 函数。用式 (\ref{eq:dpo-reward}) 替换奖励后,两项中的 $\beta\log Z(\mathbf{x})$ 恰好相消,得到
\begin{equation}
\Pr\nolimits_{\theta}(\mathbf{y}_a \succ \mathbf{y}_b\mid\mathbf{x}) =
\sigma\!\Big(\beta\log\frac{\pi_{\theta}(\mathbf{y}_a|\mathbf{x})}{\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}_a|\mathbf{x})}
- \beta\log\frac{\pi_{\theta}(\mathbf{y}_b|\mathbf{x})}{\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}_b|\mathbf{x})}\Big).
\label{eq:dpo-preference}
\end{equation}
这里的关键在于,归一化常数 $Z(\mathbf{x})$ 被消掉了,偏好概率仅依赖于策略和参考模型输出的对数比。这意味着我们不再需要显式训练奖励模型,可以直接用人类偏好数据训练策略。
由此,DPO 的损失函数直接定义为负对数似然,形式与训练奖励模型时几乎一致,只是参数变成了策略的 $\theta$
\begin{equation}
\mathcal{L}_{\mathrm{dpo}}(\theta) = -\mathbb{E}_{(\mathbf{x},\mathbf{y}_a,\mathbf{y}_b)\sim\mathcal{D}_r}
\Big[\log \Pr\nolimits_{\theta}(\mathbf{y}_a \succ \mathbf{y}_b\mid\mathbf{x})\Big].
\label{eq:dpo-loss}
\end{equation}
最小化这一损失,等价于增大被人类偏好的回复相较于参考模型的概率比,同时压低不被偏好的回复的概率比。
与 PPO 相比,DPO 将整个对齐过程简化为一个\textbf{监督式的分类任务}:只需从数据集中读取人类偏好对,直接更新语言模型参数,无需在线采样、价值网络和奖励网络。这使得 DPO 的训练更加稳定、样本效率更高,且易于实现。当然,DPO 本质上是一种离线方法,无法像在线 RL 那样通过主动探索发现超越固定数据集的新策略,但它凭借极简的流程已经成为目前最流行的人类偏好对齐方法之一。
\begin{importantnote}
直接偏好优化(DPO)省去了奖励模型的训练和强化学习采样,将偏好对齐转化为一个分类损失:通过比较两个输出相对于参考模型的概率比来直接更新策略。这一方法简化了流程且训练更稳定,但本质上是离线的,无法像在线 RL 那样探索新路径。理解其与 RLHF 的异同是掌握对齐技术的关键。
\end{importantnote}
% \noindent 虽然学习奖励模型是强化学习中的一个标准步骤,但它使得整个训练过程比监督学习复杂得多。训练一个可靠的奖励模型本身就不是一项容易的任务,而一个训练不佳的奖励模型会极大地影响策略学习的结果。我们现在考虑一种替代的对齐方法,称为 \mindex{直接偏好优化} (\mindex{DPO}),它通过消除显式建模奖励的需要来简化训练框架 \cite{rafailov-etal:2024direct}。该方法直接根据用户偏好优化策略,而不是开发一个单独的奖励模型。因此,我们可以以一种类似监督学习的方式实现人类偏好对齐。图 \ref{fig:rlhf-vs-dpo} 展示了标准 RLHF 方法与 DPO 方法的比较。
% \begin{figure}[!t]
% \centering
% \input{figures/Chapter4/figure-rlhf-vs-dpo}
% \caption{标准的 RLHF (PPO) 与 DPO。在 RLHF 中,人类偏好数据用于训练一个奖励模型,该模型随后被用于训练策略以及价值函数。在 DPO 中,人类偏好数据的使用更加直接,策略直接在此数据上进行训练,无需训练奖励模型。}
% \label{fig:rlhf-vs-dpo}
% \end{figure}
% 在推导 DPO 目标之前,让我们首先回顾一下 RLHF 中使用的策略训练目标。如第 \ref{sec:rlhf-training-llms} 节所述,策略通常通过优化带有惩罚项的损失函数进行训练。DPO 方法假设一个简单的损失函数,其中给定输入 $\mathbf{x}$ 的输出 $\mathbf{y}$ 的质量由奖励模型 $r(\mathbf{x},\mathbf{y})$ 评估。因此,训练目标由下式给出
% \begin{eqnarray}
% \tilde{\theta} & = & \argmin_{\theta} \mathbb{E}_{\mathbf{x} \sim \mathcal{D}} \mathbb{E}_{\mathbf{y} \sim \pi_{\theta}(\cdot|\mathbf{x})} \big[ \underbrace{-r(\mathbf{x},\mathbf{y})}_{\text{loss}} + \beta \underbrace{(\log \pi_{\theta}(\mathbf{y}|\mathbf{x}) - \log \pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x}) )}_{\text{penalty}} \big] \label{eq:dpo-basic-objective}
% \end{eqnarray}
% \noindent 请注意,在这个优化问题中,只有项 $\pi_{\theta}(\mathbf{y}|\mathbf{x})$ 依赖于目标策略 $\pi_{\theta}(\cdot)$。奖励模型 $r(\mathbf{x}, \mathbf{y})$ 和参考模型 $\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x})$ 都被假定在给定 $\mathbf{x}$ 和 $\mathbf{y}$ 的情况下是固定的。与 PPO 相比,这是一个很强的假设,但正如稍后将展示的,它简化了问题,并且对于推导 DPO 目标至关重要。
% 由于 $\theta$ 是我们想要优化的变量,我们重新排列等式 (\ref{eq:dpo-basic-objective}) 的右侧,以将 $\pi_{\theta}(\mathbf{y}|\mathbf{x})$ 分离为一个独立的项:
% \begin{eqnarray}
% \tilde{\theta} & = & \argmin_{\theta} \mathbb{E}_{\mathbf{x} \sim \mathcal{D}} \mathbb{E}_{\mathbf{y} \sim \pi_{\theta}(\cdot|\mathbf{x})} \big[ \beta \log \pi_{\theta}(\mathbf{y}|\mathbf{x}) - \beta \log \pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x}) - r(\mathbf{x},\mathbf{y}) \big] \nonumber \\
% & = & \argmin_{\theta} \mathbb{E}_{\mathbf{x} \sim \mathcal{D}} \mathbb{E}_{\mathbf{y} \sim \pi_{\theta}(\cdot|\mathbf{x})} \big[ \log \pi_{\theta}(\mathbf{y}|\mathbf{x}) - \big(\log \pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x}) + \frac{1}{\beta} r(\mathbf{x},\mathbf{y}) \big) \big] \nonumber \\
% & = & \argmin_{\theta} \mathbb{E}_{\mathbf{x} \sim \mathcal{D}} \mathbb{E}_{\mathbf{y} \sim \pi_{\theta}(\cdot|\mathbf{x})} \big[ \underbrace{\log \pi_{\theta}(\mathbf{y}|\mathbf{x})}_{\text{dependent on $\theta$}} - \underbrace{\log \pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x}) \exp \big(\frac{1}{\beta} r(\mathbf{x},\mathbf{y}) \big)}_{\text{not dependent on $\theta$}} \big] \label{eq:dpo-raw-objective-with-no-normalization}
% \end{eqnarray}
% 这个方程将目标函数定义为 $y$ 的对数概率分布函数与另一个 $y$ 的函数之间的差。这种形式的目标函数似乎并不"理想",因为我们通常希望看到两个分布之间的差异,以便我们可以将这种差异解释为分布之间的某种散度。一个简单的想法是将第二项(即 $ \log \pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x}) \exp(\frac{1}{\beta} r(\mathbf{x},\mathbf{y}))$)转换为在 $\mathbf{y}$ 定义域上的对数概率分布。如果我们将 $ \pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x}) \exp(\frac{1}{\beta} r(\mathbf{x},\mathbf{y}))$ 视为 $y$ 的未归一化概率,我们可以通过除以一个归一化因子将其转换为归一化概率:
% \begin{eqnarray}
% Z(\mathbf{x}) & = & \sum_{\mathbf{y}} \pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x}) \exp \big(\frac{1}{\beta} r(\mathbf{x},\mathbf{y}) \big)
% \end{eqnarray}
% 因此,我们可以通过下式定义一个概率分布
% \begin{eqnarray}
% \pi^*(\mathbf{y}|\mathbf{x}) & = & \frac{\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x}) \exp \big(\frac{1}{\beta} r(\mathbf{x},\mathbf{y}) \big)}{Z(\mathbf{x})}
% \end{eqnarray}
% 然后我们将等式 (\ref{eq:dpo-raw-objective-with-no-normalization}) 重写为
% \begin{eqnarray}
% \tilde{\theta} & = & \argmin_{\theta} \mathbb{E}_{\mathbf{x} \sim \mathcal{D}} \mathbb{E}_{\mathbf{y} \sim \pi_{\theta}(\cdot|\mathbf{x})} \Big[ \log \pi_{\theta}(\mathbf{y}|\mathbf{x}) - \log \frac{\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x}) \exp \big(\frac{1}{\beta} r(\mathbf{x},\mathbf{y}) \big)}{Z(\mathbf{x} \big)} \nonumber \\
% & & \hspace{10.5em} - \log Z(\mathbf{x}) \Big] \nonumber \\
% & = & \argmin_{\theta} \mathbb{E}_{\mathbf{x} \sim \mathcal{D}} \mathbb{E}_{\mathbf{y} \sim \pi_{\theta}(\cdot|\mathbf{x})} \Big[ \log \pi_{\theta}(\mathbf{y}|\mathbf{x}) - \log \pi^{*}(\mathbf{y}|\mathbf{x}) - \log Z(\mathbf{x}) \Big] \nonumber \\
% & = & \argmin_{\theta} \mathbb{E}_{\mathbf{x} \sim \mathcal{D}} \bigg[ \mathbb{E}_{\mathbf{y} \sim \pi_{\theta}(\cdot|\mathbf{x})} \Big[ \log \pi_{\theta}(\mathbf{y}|\mathbf{x}) - \log \pi^{*}(\mathbf{y}|\mathbf{x}) \Big] \nonumber \\
% & & \hspace{6.5em} - \mathbb{E}_{\mathbf{y} \sim \pi_{\theta}(\cdot|\mathbf{x})} \big[ \log Z(\mathbf{x}) \big] \bigg] \nonumber \\
% & = & \argmin_{\theta} \mathbb{E}_{\mathbf{x} \sim \mathcal{D}} \Big[ \underbrace{\mathrm{KL} \big(\pi_{\theta}(\cdot|\mathbf{x})\ ||\ \pi^{*}(\cdot|\mathbf{x}) \big)}_{\text{KL divergence}} - \underbrace{\log Z(\mathbf{x})}_{\text{constant wrt. $\theta$}} \Big]
% \end{eqnarray}
% 由于 $\log Z(\mathbf{x})$ 与 $\theta$ 无关,它不影响 $\argmin_{\theta}$ 操作的结果,可以从目标中移除。现在我们获得了一个新的训练目标,它通过最小化 $\pi_{\theta}(\cdot|\mathbf{x})$ 和 $\pi^{*}(\cdot|\mathbf{x})$ 之间的 KL 散度来找到最优策略 $\pi_{\theta}$
% \begin{eqnarray}
% \tilde{\theta} & = & \argmin_{\theta} \mathbb{E}_{\mathbf{x} \sim \mathcal{D}} \Big[ \mathrm{KL} \big(\pi_{\theta}(\cdot|\mathbf{x})\ ||\ \pi^{*}(\cdot|\mathbf{x}) \big) \Big]
% \end{eqnarray}
% 显然,这个优化问题的解由下式给出
% \begin{eqnarray}
% \pi_{\theta}(\mathbf{y}|\mathbf{x}) & = & \pi^{*}(\mathbf{y}|\mathbf{x}) \nonumber \\
% & = & \frac{\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x}) \exp \big(\frac{1}{\beta} r(\mathbf{x},\mathbf{y}))}{Z(\mathbf{x} \big)}
% \end{eqnarray}
% 给定这个方程,我们可以使用目标模型 $\pi_{\theta}(\mathbf{y}|\mathbf{x})$、参考模型 $\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x})$ 和归一化因子 $Z(\mathbf{x})$ 来表示奖励 $r(\mathbf{x},\mathbf{y})$:
% \begin{eqnarray}
% r(\mathbf{x},\mathbf{y}) & = & \beta \left(\log \frac{\pi_{\theta}(\mathbf{y}|\mathbf{x})}{\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}|\mathbf{x})} + \log Z(\mathbf{x}) \right) \label{eq:dpo-reward-model-expression}
% \end{eqnarray}
% 这很有趣,因为我们最初试图使用奖励模型 $r(\mathbf{x},\mathbf{y})$ 来学习策略 $\pi_{\theta}(\cdot)$,但最终却得到了基于策略的奖励模型表示。给定等式 (\ref{eq:dpo-reward-model-expression}) 中定义的奖励模型,我们可以将其应用于 Bradley-Terry 模型来计算偏好概率(另见第 \ref{sec:training-reward-models} 节):
% \begin{eqnarray}
% \mathrm{Pr}_{\theta}(\mathbf{y}_a \succ \mathbf{y}_b | \mathbf{x}) & = & \mathrm{Sigmoid}(r(\mathbf{x},\mathbf{y}_a)-r(\mathbf{x},\mathbf{y}_b)) \nonumber \\
% & = & \mathrm{Sigmoid}\bigg(\beta \Big(\log \frac{\pi_{\theta}(\mathbf{y}_a|\mathbf{x})}{\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}_a|\mathbf{x})} + \log Z(\mathbf{x}) \Big) - \nonumber \\
% & & \hspace{4.3em} \beta \Big(\log \frac{\pi_{\theta}(\mathbf{y}_b|\mathbf{x})}{\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}_b|\mathbf{x})} + \log Z(\mathbf{x}) \Big) \bigg) \nonumber \\
% & = & \mathrm{Sigmoid}\bigg( \beta \log \frac{\pi_{\theta}(\mathbf{y}_a|\mathbf{x})}{\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}_a|\mathbf{x})} - \beta \log \frac{\pi_{\theta}(\mathbf{y}_b|\mathbf{x})}{\pi_{\theta_{\mathrm{ref}}}(\mathbf{y}_b|\mathbf{x})} \bigg)
% \end{eqnarray}
% 这个公式非常优雅,因为它将奖励的差异转换为了比率函数的差异,并且我们不需要计算 $Z(\mathbf{x})$ 的值。一个直接的结果是,我们不再需要奖励模型,而只需要目标策略和参考模型来计算偏好的概率。最后,我们可以通过最小化以下 DPO 损失函数来训练目标策略
% \begin{eqnarray}
% \mathcal{L}_{\mathrm{dpo}}(\theta) & = & -\mathbb{E}_{(\mathbf{x},\mathbf{y}_a,\mathbf{y}_b) \sim \mathcal{D}_r} \big[ \log \mathrm{Pr}_{\theta}(\mathbf{y}_a \succ \mathbf{y}_b | \mathbf{x}) \big]
% \end{eqnarray}
% \noindent 这个损失函数的形式与 RLHF 中训练奖励模型所用的损失函数非常相似(见等式 (\ref{eq:pairwise-reward-loss-expectation}))。但需要注意的是,这里的损失函数依赖于策略的参数(即 $\theta$),而不是奖励模型的参数(即 $\phi$)。
% DPO 的主要优势在于其简单性和效率。DPO 的目标非常直接——它直接针对基于偏好的反馈进行优化,而不是依赖于单独开发的奖励模型。此外,DPO 通常样本效率更高,因为它从一个固定的数据集中学习,而不需要 PPO 中使用的计算成本高昂的采样过程。这使得 DPO 成为一种流行的人类偏好对齐方法,尤其是在通过强化学习开发和应用奖励模型具有挑战性的情况下。
% DPO 可以被广义地视为一种 \mindex{离线强化学习} 方法,其中训练数据是预先收集和固定的,并且没有探索过程。相比之下,像 PPO 这样的在线强化学习方法,需要通过与环境的交互(使用奖励模型作为代理)来探索新状态,也有其独特的优势。在线强化学习的好处之一是它允许智能体通过从实时反馈中学习来不断适应环境的变化。这意味着,与离线方法不同,在线方法不受预先收集数据的静态性质的限制,可以发现新的问题解决策略。此外,探索可以帮助智能体覆盖更广泛的状态-动作对,从而提高泛化能力。这对于大型语言模型可能是一个重要的优势,因为泛化被认为是应用此类大型模型的一个关键方面。
\noindent 基于人类偏好学习是当前对齐大语言模型有效且流行的方法,但标注偏好数据成本高昂。人类反馈不仅扩展性有限,还因其固有的主观性容易引入偏见。为此,AI 反馈方法可以突破人类标注的限制,提升可扩展性与一致性。
生成偏好数据与生成指令微调数据类似,可直接利用大语言模型。给定输入,先让模型生成成对输出,再提示其为每对输出标注偏好。下面是一个让大语言模型为一对客服回复标注偏好标签的示例。
\vspace{0.5em}
\begin{tcolorbox}[frame empty]
\begingroup
\setlength{\leftskip}{2em}
\setlength{\rightskip}{2em}
设想一个客户服务场景,客户提出了如下请求。你将审阅两个回复,并指出哪一个更优。请注意,好的回复应礼貌、清晰、简洁,直接回应客户的关切,提供有用的信息或解决方案,并保持积极的语气。
\vspace{0.3cm}
请求:
\vspace{0.1cm}
\textit{您好,我注意到我的订单还没到,按计划几天前就该送达了。能否帮我查一下目前的状态?谢谢!}
\vspace{0.2cm}
回复 A:
\vspace{0.1cm}
\textit{非常抱歉给您造成延误,我完全理解这多么令人失望。我们正在尽最大努力尽快为您解决。}
\vspace{0.3cm}
回复 B:
\vspace{0.1cm}
\textit{嗨,这种事难免的!您的包裹该到的时候自然会到,不用着急。}
\vspace{0.3cm}
\underline{偏好回复 A。}
\endgroup
\end{tcolorbox}
\vspace{0.5em}
收集到偏好标签后,即可结合输出对和输入来训练奖励模型。为提升标注质量,可加入示例或采用思维链(CoT)等高级提示技术,例如在提示中展示带有推理过程的偏好示例。
除了偏好标签,我们还可以获得每个标签的概率 \cite{lee-etal:2023rlaif}。一种简单做法是从模型输出的概率中提取标签 token(如“A”“B”)的概率,再使用 Softmax 等归一化技术转换为分布,以此作为训练奖励模型的逐点监督信号。
数据生成虽然容易扩展,但准确性和多样性依然关键,这不仅关乎偏好标注,还涉及模型输入与输出的质量,通常需要借助不同模型、多样提示和上下文示例来保证 \cite{cui-etal:2024ultra}\citet{dubois-etal:2024alpacafarm} 指出,无论通过人类反馈还是 AI 反馈训练,成对偏好数据的多样性都十分重要。
AI 反馈高度可扩展且相对客观,但更适合有明确客观指标的任务。反之,当需要对齐人类价值观、主观偏好或复杂情境时,人类反馈更具优势。实践中可将二者结合,兼顾人类洞察与 AI 反馈的可扩展性。
% \noindent 尽管从人类偏好中学习是对齐大语言模型的一种有效且流行的方法,但标注偏好数据的成本很高。使用人类反馈不仅面临可扩展性有限的问题,还可能引入偏见,因为人类反馈本质上是主观的。因此,可以转向 AI 反馈方法来解决这些可扩展性和一致性问题,而不受人类标注员的限制。
% 与为指令微调生成数据一样,使用大语言模型生成偏好数据也很简单。给定一组输入,我们首先使用一个大语言模型生成成对的输出。然后,我们提示大语言模型为每对输出及其对应的输入标注偏好。下面是一个提示大语言模型为一对客户服务响应生成偏好标签的示例。
% \vspace{0.5em}
% \begin{tcolorbox}[frame empty]
% \begingroup
% \setlength{\leftskip}{2em}
% \setlength{\rightskip}{2em}
% Consider a customer service scenario where a customer poses a request. You will review two responses to this request. Please indicate which response is preferred. Note that a good response should be courteous, clear, and concise. It should address the customer's concern directly, provide helpful information or a solution, and maintain a positive tone.
% \vspace{0.3cm}
% Request:
% \vspace{0.1cm}
% \textit{Hello, I noticed that my order hasn't arrived yet, though it was scheduled to arrive several days ago. Could you please update me on its status? Thank you!}
% \vspace{0.2cm}
% Response A:
% \vspace{0.1cm}
% \textit{I'm very sorry for the delay and understand how disappointing this can be. We're doing our best to sort this out quickly for you.}
% \vspace{0.3cm}
% Response B:
% \vspace{0.1cm}
% \textit{Hey, stuff happens! Your package will get there when it gets there, no need to stress.}
% \vspace{0.3cm}
% \underline{Response A is preferred.}
% \endgroup
% \end{tcolorbox}
% \vspace{0.5em}
% 一旦收集到这些偏好标签,我们就可以将它们与输出对和输入一起用于训练奖励模型。当然,我们可以考虑展示一些示例或使用高级提示技术(如 CoT)来提高标注性能。例如,我们可以在提示中包含一个示例,根据 CoT 的基本原理说明两个响应中为何偏好其中一个以及如何偏好。
% 除了偏好标签,我们还可以获得与每个标签相关的概率 \cite{lee-etal:2023rlaif}。一种简单的方法是从大语言模型输出的概率中提取标签 token(如"A"和"B")的概率。然后,我们可以使用 Softmax 函数或其他归一化技术将这些概率重新归一化为标签上的分布。这些偏好标签的概率可以作为训练奖励模型的逐点监督信号。
% 对于数据生成,虽然很容易扩大规模,但通常需要确保数据的准确性和多样性。在这里,数据质量和多样性问题不仅涉及偏好标注,还涉及模型的输入和输出。因此,我们通常需要使用各种技术来获取大规模、高质量的数据。例如,可以通过使用不同的大语言模型、提示、上下文演示等来生成多样化的模型输出和注释 \cite{cui-etal:2024ultra}。\citet{dubois-etal:2024alpacafarm} 报告称,无论是通过人类反馈还是 AI 反馈来训练大语言模型,成对偏好数据的可变性都很重要。
% 虽然从 AI 反馈中学习具有高度可扩展性且通常是客观的,但这种方法更适合于那些有明确客观性能指标的定义明确的任务。相比之下,当需要将 AI 系统与人类价值观、偏好以及需要理解微妙或主观背景的复杂现实世界任务对齐时,从人类反馈中学习更具优势。这些方法可以结合起来训练大语言模型,使其既能受益于人类的洞察力,又能利用 AI 反馈的可扩展性。
\ No newline at end of file
\section{大语言模型推理增强方法}
% 常见的缩放策略
% (1)并行缩放:best-of-n sampling
% (2)顺序缩放 (确认一下)
% 12.2 Learning to Reason
% 10.4.5 测试时间对齐方法
% 10.4.4 逐步对齐方法 (讲PRM(可参考10.4.1 更好的奖励建模))
\input{section/3.5sub/part1}
\input{section/3.5sub/part2}
\input{section/3.5sub/part3}
\input{section/3.5sub/part4}
\subsection{推理概述}
历史上,人工智能研究将推理分为三种主要的逻辑形式:\textbf{演绎}(从已知前提推导具体结论,如数学证明)、\textbf{归纳}(从具体例子推断一般规则或模式)和\textbf{溯因}(为一组观察结果寻找最合理的解释)~\cite{peirce1931:collected}。长期以来,主流机器学习和自然语言处理(NLP)研究主要集中在归纳任务上。例如,文本分类和命名实体识别等经典 NLP 应用可以被视为归纳任务:模型被训练以从大型标注数据集中泛化出映射函数。
然而,随着大语言模型(LLM)的发展,研究人员正试图解决更困难的推理问题,如复杂数学、智能体规划(agentic planning)和科学发现,这些通常需要严格的演绎和溯因推理。现代 LLM 研究表明,标准的预训练目标(即在海量文本语料库上进行下一个词预测)足够强大,能够帮助模型内化语言和世界的知识,包括潜在的推理能力。然而,主要挑战在于推理在预训练阶段并未被显式建模。因此,当面对复杂查询时,LLM 会尝试直接输出最终答案,而不是执行逐步的逻辑推导。
例如,考虑一个简单的应用题:
\begin{quote}
\textit{如果约翰的年龄是玛丽的两倍,而玛丽在3年后将是10岁,那么约翰现在多大?}
\end{quote}
基础的 LLM 可能会通过简单关联关键词“两倍”和“10”输出一个错误的猜测,如“20”。相比之下,具备推理能力的 LLM 可能会在回答前显式生成逻辑链:
\begin{quote}
\textit{玛丽现在的年龄是 $10 - 3 = 7$ 岁。因此,约翰的年龄是 $7 \times 2 = 14$ 岁。}
\end{quote}
要使用 LLM 解决复杂的演绎和溯因任务,必须显式激活这些潜在的推理能力,无论是通过思维链(CoT)等提示技术,还是通过强化学习等先进的后训练范式。
为了更好地理解推理问题,我们可以使用认知心理学中的双过程理论(dual-process theory)来审视 LLM 的能力:系统 1 和系统 2 思维~\cite{kahneman:2011thinking}
\begin{itemize}
\item \vspace{0.5cm} \textbf{系统 1(直觉思维):} 在人类认知中,系统 1 自动、快速地运行,几乎或根本不需要耗费精力。在 LLM 的语境下,系统 1 思维对应于模型在单次前向传播中直接输出最终答案的范式。这种模式效率很高,通常足以应对简单的问答等归纳任务。然而,当面对复杂的多步问题时,系统 1 思维容易出现逻辑跳跃和幻觉。
\item \vspace{0.3cm} \textbf{系统 2(审慎思维):} 这种模式缓慢且有意识,需要逐步的逻辑推导。对于 LLM 而言,当引导模型在推理时分配额外的计算资源以显式生成中间推理路径时,系统 2 思维便被激活。通过将思考过程表示为文本,LLM 能够分解复杂任务,验证中间结论,并在得出最终答案前维持一条连贯的逻辑链。
\end{itemize}
\vspace{0.3cm}
对于 LLM 推理而言,核心目标是激活系统 2 的能力。为了理解这一点,我们必须超越传统的符号逻辑。在经典人工智能中,推理通常被定义为在结构化知识库上应用硬编码的逻辑规则。然而,对于 LLM 来说,推理是一种内在的文本生成过程。它可以被定义为生成一系列连贯的中间步骤以得出最终答案的能力。在某种意义上,LLM 中的推理是一种为更难的问题动态分配更多推理计算资源的机制。归根结底,LLM 的推理是将潜在的系统 2 思维显式编码为一系列思考步骤(推理步骤)的能力。
推理在 NLP 研究中至关重要,因为它在多个具有挑战性的领域有着广泛的应用:
\begin{itemize}
\item \vspace{0.2cm} \textbf{数学:} 解决数学问题需要严格的逻辑。模型不能仅仅根据文本模式猜测答案。相反,它需要逐步应用数学规则来计算正确结果。
\item \vspace{0.2cm} \textbf{编程:} 编写软件远比简单地预测下一个词要困难。模型必须根据特定的用户需求和严格的代码逻辑生成代码。特别是在解决复杂问题时,将抽象需求转化为可运行的代码需要逐步推理。
\item \vspace{0.2cm} \textbf{智能体规划:} 当人工智能作为自主智能体(如网络助手或机器人)行动时,它必须将庞大、复杂的目标分解为小的、可执行的步骤。它还需要提前规划,并在出现问题时调整其行动。
\item \vspace{0.2cm} \textbf{科学发现:} 协助科学家需要分析数据、提出新假设并对其进行仔细验证。这在很大程度上依赖于逐步的逻辑演绎和溯因。
\end{itemize}
\vspace{0.3cm}
% 除了解决更难的问题,生成推理步骤还具有实际优势。最重要的是,它使模型具有可解释性。因为模型将其思考过程以文本形式写了下来,人类可以阅读并检查其逻辑,甚至能准确找出错误发生的位置。这使得系统更容易被验证和信任。此外,逐步推理有助于减少幻觉。面对难题时,模型不再进行毫无根据的猜测,而是将其最终答案建立在自身的逻辑步骤之上,从而使输出更加可靠。
% \subsubsection{从 LLM 到推理模型}
% 在 LLM 占据主导地位之前,AI 中的推理主要依赖于严格的规则和逻辑。研究人员使用符号 AI 和语义解析等方法,将人类语言转化为目标形式,如知识图谱或数据库查询。虽然这些经典系统中的逻辑非常清晰且易于追踪,但系统本身却很脆弱。如果遇到未知的词汇,或者某一条小规则失效,整个流程就会崩溃。
% 随着 LLM 的出现,推理问题被重新定义为文本生成任务,并可以通过面向推理的技术得到更好的解决。LLM 时代推理模型的发展可以分为三个阶段:直接猜测答案、被提示去思考,以及最终学会如何思考。
% 早期的语言模型主要被训练来预测下一个词。当被问及问题时,它们会尝试立即输出答案。这种快速猜测对简单问题或写作任务很有效。但对于复杂的数学或逻辑问题,它却表现不佳。模型经常出错或产生幻觉,因为它们没有办法暂停下来并逐步解决问题。
% 第一个突破出现在研究人员意识到 LLM 已经具备了隐藏的推理能力:它们只需要被唤醒。通过简单地使用思维链(CoT)提示~\cite{wei-etal:2022chain}——要求模型“一步步思考”——模型被强制在给出答案前写下其思考过程。然而,这种方法仍然严重依赖人类的提示,并没有真正教会模型底层的逻辑规则。
% 最近更大的颠覆性进展是使用强化学习来训练模型进行推理。强化学习方法不再仅仅教模型生成文本,而是通过试错来训练这些模型。当模型找到正确的逻辑路径并得出正确答案时,它会获得奖励。随着时间的推移,像 OpenAI 的 o1~\cite{openai:2024learning} 和 DeepSeek-R1~\cite{deepseek:2025r1} 这样的模型展现出了令人印象深刻的推理能力,例如探索不同的想法、反复检查自己的步骤,以及自主纠正错误(回溯)。
% 这些自我纠正和探索能力引出了一个重要的研究方向:推理时计算缩放(inference-time scaling)\cite{snell-etal:2024scaling}。其理念非常直观:通过在生成过程中分配更多的计算资源,模型可以进行更深入的思考以解决更难的问题。理想情况下,模型会根据问题的实际难度来调整其思考时间。然而,目前的许多推理模型采取了一种更简单的方法——它们对所有查询都应用冗长的思考过程。
% 从更宏观的角度来看,在推理时缩放计算资源对一般的推理能力都有益。即使 LLM 没有经过专门的推理训练,我们仍然可以通过简单地给它更多的计算资源来“思考”,从而提升其解决问题的能力。例如,我们可以要求模型生成多条不同的推理路径并对最佳路径进行投票,或者使用搜索算法来帮助其找到正确的逻辑步骤。
\ No newline at end of file
\subsection{测试时缩放}
大语言模型的卓越性能在很大程度上得益于训练阶段的计算扩展——通过使用更多数据、更大模型和更多算力来提升能力。然而,对于数学证明、逻辑推理等复杂任务,仅靠训练扩展是不够的。这类任务需要模型按步骤进行逻辑推演,而所有可能的推理路径是无法在训练中穷尽的。一种更高效的理念是将部分计算资源从训练转移到推断阶段,让模型在运行时动态地构建推理过程,即“花更多时间思考”。这种在模型推断阶段通过增加计算量来提升推理表现的方法,就称为测试时缩放(test-time scaling,也称测试时扩展)。
测试时缩放的核心优势在于计算资源分配的灵活性。对于简单问题,模型可以快速作答;对于困难问题,则可以分配更多的算力,允许模型探索不同的推理思路,从而有更大概率得到正确答案。实践表明,一个规模较小的模型,若在测试时被给予额外的时间进行推理,其表现常常能超越那些被要求立即给出答案的大模型。正因如此,测试时缩放成为增强大语言模型推理能力的一条极具吸引力的技术路径。接下来的两个小节将分别从生成多条推理路径和构建搜索过程这两个角度,介绍实现测试时缩放的典型方法。
\subsubsection{采样多条推理路径}
\label{sec:sampling-reasoning-paths}
实现测试时缩放最直接的方法,就是为同一个输入生成多条不同的推理路径,而非仅生成一条。通过扩大推理路径的候选池,模型就获得了更多“尝试”不同逻辑走向并找到正确答案的机会。
要生成多样化的路径,需要在文本生成中注入可控的随机性。常用的策略是温度采样:在预测下一个词时,模型原本会输出一个概率分布。引入温度参数 $T>0$ 后,会在归一化指数函数之前对各个词的得分进行缩放。较高的温度会令概率分布变得平坦,使原本低概率的词也有机会被选中,从而鼓励模型探索更广泛的语言选择。为避免完全随机的退化输出,温度采样常与词表截断技术结合:
\begin{itemize}
\item \textbf{Top-$k$ 采样}:每步只保留概率最高的 $k$ 个候选词,重新分配概率后从中采样。
\item \textbf{核采样(Top-$p$ 采样)}:动态地选择累计概率刚刚超过阈值 $p$ 的最小候选词集合,适应不同语境下的不确定性。
\end{itemize}
通过这些策略,对于输入查询 $\mathbf{x}$,大语言模型可生成 $N$ 条候选推理路径:
\begin{eqnarray}
\mathcal{Y}^{\mathrm{top}} = \{\hat{\mathbf{y}}^1, \dots, \hat{\mathbf{y}}^N\}.
\end{eqnarray}
这里的 $N$ 代表了测试时计算预算。$N$ 越大,探索的路径就越多。接下来,需要从这组候选路径中选出最优的一条或综合出最终答案。两种经典方法是 Best-of-$N$$N$选优)和自一致性。
\paragraph{1. Best-of-$N$ (BoN)}
该方法引入一个验证器 $v$ 为每条候选路径打分,分值反映其逻辑合理性与正确性。最终选择得分最高的路径:
\begin{eqnarray}
\hat{\mathbf{y}}_{\mathrm{best}} = \argmax_{\hat{\mathbf{y}} \in \{\hat{\mathbf{y}}^1, \dots, \hat{\mathbf{y}}^N\}} v(\mathbf{x}, \hat{\mathbf{y}}).
\end{eqnarray}
可以将其类比为一次课堂测验:$N$ 个学生各自写下解题过程,老师(验证器)批阅所有草稿并挑出得分最高的一份。显然,Best-of-$N$ 的效果严重依赖验证器的质量。如果验证器容易被表面上正确但逻辑谬误的答案欺骗,就可能导致选中的路径不可靠。验证器的设计将在第~\ref{sec:reasoning-verification}节中详细讨论。
\paragraph{2. 自一致性}
自一致性绕过显式的评分器,转而利用“多数真理”的直觉:对于复杂推理题,通常有多条不同的推理路径能导向同一个正确答案,而错误路径的答案则较为分散。因此,我们只需从每条路径中抽取出最终答案 $\hat{a}^k = \mathrm{Extract}(\hat{\mathbf{y}}^k)$,然后对 $N$ 个答案进行多数投票:
\begin{eqnarray}
\hat{a}_{\mathrm{sc}} = \argmax_{a \in A} \sum_{k=1}^N \mathbbm{1}(\mathrm{Extract}(\hat{\mathbf{y}}^k) = a),
\end{eqnarray}
其中 $\mathbbm{1}(\cdot)$ 是指示函数。这种方法简单且无需额外训练,在答案形式明确的任务中非常有效。
在使用以上方法时,一个不可忽视的实践要点是平衡路径的\textbf{多样性与质量}。较高的温度能提升多样性,增加覆盖正确推理的机会,但也可能使单条路径的文本质量下降,甚至出现逻辑混乱。因此,需要根据任务特点仔细调节采样参数(温度 $T$ 以及 $k$$p$ 的值),在探索的广度与单路输出的可靠性之间找到最优点。
\begin{importantnote}
测试时缩放通过增加推理时的计算量(如生成多条候选路径或进行树搜索)来提升复杂任务表现。其中,自一致性利用多数投票筛选答案,无需额外验证器;而 Best-of-N 则依赖可靠的评分模型。实践时需平衡路径多样性与质量,过高的温度虽能增加覆盖却可能引入噪声。缩放收益随计算量增加会趋于饱和,根本上限仍受模型自身知识储备制约。
\end{importantnote}
\subsubsection{推理的搜索策略}
\label{sec:search-strategies-for-reasoning}
除了通过增加采样数量来扩展候选池,测试时缩放还可以从更结构化的视角来实现:将寻找最优推理路径视为一个搜索问题。其核心思想是,当我们给予模型额外的算力去“思考”时,本质上是在允许它探索一个由中间推理步骤构成的逻辑空间,并以系统化的方式定位其中的高价值路径。
要应用经典搜索算法,首先需要将大语言模型的推理过程映射为搜索的标准组件:
\begin{itemize}
\item \textbf{搜索状态}:状态是模型当前已构建的上下文。在步骤 $t$,状态 $s_t$ 由原始问题 $\mathbf{x}$ 和已生成的部分推理内容拼接而成。
\item \textbf{动作}:生成下一个推理步骤,即一段文本片段 $\bar{\mathbf{y}}_{t+1}$
\item \textbf{状态转移}:将新生成的片段追加到当前状态末尾,形成 $s_{t+1} = (s_t, \bar{\mathbf{y}}_{t+1})$
\item \textbf{状态评估函数}:给当前状态打分,评估其通往正确答案的潜力。评估可基于简单规则,也可使用训练好的神经网络。
\item \textbf{终止条件}:当模型输出明确的最终答案,或搜索步数达到预设上限时停止。
\end{itemize}
基于这套形式化描述,多种成熟的搜索算法便可直接用于推理。最简单的如广度优先或深度优先搜索,模型每步可以展开多个候选思路并向前探索,必要时回溯。对于需要长远规划或存在延迟反馈的任务,蒙特卡洛树搜索能够动态平衡探索新想法与利用已知有希望的方向,有时还会结合外部反馈或自我修正来改进搜索质量(例如思维树等工作 \cite{yao-etal:2024tree})。
需要指出,通过搜索来扩展测试时计算虽能稳定提升推理表现,但其收益并非随搜索量线性增长。当搜索强度超过某个阈值后,性能提升会趋于饱和。根本原因在于,模型本身的知识储备构成了能力的上限:若模型完全不具备解决某一问题所需的核心知识,再庞大的搜索也无法凭空创造出正确的推理逻辑;而过量的搜索还容易生成大量语义重复的路径,徒增计算开销而贡献甚微。
% \subsection{测试时间缩放}
% 推断时推理(Inference-time Reasoning)与推断阶段的扩展(scaling during inference)密切相关。在本节中,我们将扩展之前的讨论,并更加关注如何提高大语言模型(LLMs)的推理性能。
% \subsubsection{推断时计算扩展}
% 大语言模型的成功在很大程度上依赖于计算能力的扩展。提高这些模型性能的一种常见方法是训练时扩展(training-time scaling)。通过简单地使用更多的训练数据、更大的模型规模以及更多的计算资源,研究人员已经构建出了非常强大的大语言模型 \cite{kaplan-etal:2020scaling,hoffmann-etal:2022training}。
% 然而,仅仅扩展训练过程对于复杂的推理任务来说是不够的。与简单的文本生成不同,推理需要一步步的逻辑和规划。在训练期间向模型展示所有可能的推理路径是不可能的。如果我们仅仅依靠训练来解决困难的数学或逻辑问题,我们将需要海量的数据。理想情况下,我们希望模型能够使用简单的逻辑步骤在运行时(on the fly)构建自己的推理路径。因此,在推断阶段激活这种推理能力更具吸引力。
% 正因如此,一个自然的想法是在推断阶段扩展计算资源。推断时扩展(inference-time scaling)允许模型在给出最终答案之前,花费更多时间探索不同的推理步骤,而不是在训练期间耗尽所有计算能力。
% 这种方法的一个明显优势是,它允许更灵活、更高效地利用计算资源。例如,对于复杂的数学或逻辑问题,大语言模型可以分配更多的时间和算力,从而“思考”更长的时间。在许多情况下,在推断阶段被给予额外时间进行推理的较小模型,甚至可以超越那些被迫立即给出答案的更大模型。
% 我们可以将推断时扩展视为一个\textbf{搜索扩展}(search scaling)问题。当我们给予模型额外的算力去“思考”时,本质上是在允许它探索更大的可能逻辑步骤空间,从而有更大的机会找到最优解。这引出了三个关键问题:
% \begin{itemize}
% \item \vspace{0.5em} 如何生成不同的推理路径?
% \item \vspace{0.3em} 如何在可能的步骤中搜索以找到最佳步骤?
% \item \vspace{0.3em} 如何检查中间步骤和最终答案是否正确?
% \end{itemize}
% \vspace{0.5em}
% % 在接下来的小节中,我们将通过讨论三个主题来解答这些问题:采样多条推理路径(第~\ref{sec:sampling-reasoning-paths}节)、推理的搜索策略(第~\ref{sec:search-strategies-for-reasoning}节)以及验证(第~\ref{sec:reasoning-verification}节)。
% \subsubsection{采样多条推理路径}
% \label{sec:sampling-reasoning-paths}
% 在推断时扩展计算资源最直接的方法是为单个输入生成多条(而不仅仅是一条)推理路径。通过生成多条不同的路径,我们为大语言模型提供了更多机会来找到正确的逻辑和相应的答案。
% 为了实现这一点,通常的做法是在文本生成过程中引入随机性。一种典型的方法是使用温度采样(temperature sampling),并结合top-$k$或核采样(nucleus / top-$p$ sampling)。在标准生成中,在每个位置 $i$,大语言模型基于其logits $u_{y_i}$,输出下一个token $y_i$在词表 $V$ 上的概率分布 $\Pr(\cdot | \mathbf{x}, \mathbf{y}_{< i})$。温度采样通过在应用Softmax函数之前,用温度参数 $T > 0$ 缩放logits来修改该分布:
% \begin{eqnarray}
% \Pr(y_i | \mathbf{x}, \mathbf{y}_{< i}) & = & \frac{\exp(u_{y_i} / T)}{\sum_{y'_i \in V} \exp(u_{y'_i} / T)}.
% \end{eqnarray}
% \noindent 较高的温度会使分布变得平坦,从而鼓励模型探索更广泛的推理步骤。然而,不受限制的随机性可能会导致退化或不符合逻辑的文本,因为它偶尔会从低概率token的“长尾”中进行采样。为了保持生成路径的质量,温度采样通常与词表截断方法结合使用:
% \begin{itemize}
% \item \vspace{0.5em} \textbf{Top-$k$采样}将采样池限制为最有可能的 $k$ 个下一个token,并在它们之间重新分配概率质量。
% \item \vspace{0.3em} \textbf{核采样(top-$p$采样)}使用动态截断策略。它选择累积概率超过阈值 $p$ 的最小排名靠前的token集合。
% \end{itemize}
% \vspace{0.5em}
% 通过应用这些随机解码策略,给定输入查询 $\mathbf{x}$,大语言模型可以采样出一组多样化的输出序列(即候选推理路径):
% \begin{eqnarray}
% \mathcal{Y}^{\mathrm{top}} & = & \{\hat{\mathbf{y}}^1, \dots, \hat{\mathbf{y}}^N\}.
% \end{eqnarray}
% \noindent 这里的 $N$ 代表我们的推断时计算预算。$N$ 越大,我们花在探索不同推理路径上的算力就越多。一旦我们有了这组多样化的路径,下一个问题就是如何选择最佳路径。为此,有两种广泛使用的方法:Best-of-$N$(N选优)和自一致性(self-consistency)。
% \paragraph{1. Best-of-$N$ (BoN)}
% 我们在第 \ChapterLLMTuning\ 节中已经介绍了Best-of-$N$采样,作为推断时对齐(inference-time alignment)的一种方法。在那种情况下,我们使用奖励模型 $r(\mathbf{x}, \hat{\mathbf{y}})$ 来选择最符合人类偏好的输出。
% 对于推理任务,BoN采样的概念保持不变,但目标发生了转变。“评判者”通常不再是偏好奖励模型,而是一个旨在评估推理路径逻辑合理性的验证器(我们可以将其表示为通用评分函数 $v$)。验证器为每条采样路径分配一个分数 $v(\mathbf{x}, \hat{\mathbf{y}})$,我们选择最大化该分数的最佳推理路径 $\hat{\mathbf{y}}_{\mathrm{best}}$:
% \begin{eqnarray}
% \hat{\mathbf{y}}_{\mathrm{best}} & = & \argmax_{\hat{\mathbf{y}} \in \{\hat{\mathbf{y}}^1, \dots, \hat{\mathbf{y}}^N\}} v(\mathbf{x}, \hat{\mathbf{y}}).
% \end{eqnarray}
% \noindent 然后从这条得分最高的路径中提取最终答案。为了理解BoN过程,让我们考虑一个直观的类比。想象一个有 $N$ 个学生的班级在解决一个数学问题。一位老师(验证器)给他们的所有草稿纸打分,然后选出得分最高的学生。
% 虽然BoN采样很强大,但其有效性取决于验证器的质量。如果验证器较弱,它很容易被偶然得出正确最终答案的错误逻辑所欺骗(即奖励作弊,reward hacking)。验证器的设计将在第~\ref{sec:reasoning-verification}节中更详细地讨论。
% \paragraph{2. 自一致性}
% 通过学习的验证器选择最佳路径的另一种替代方法是自一致性。自一致性依赖于这样的直觉:在复杂的推理任务中,通常存在多条不同的逻辑路径可以得出相同的正确答案,而错误的推理路径则可能是多样的并导致随机错误。自一致性不对推理过程进行评分,而是通过对最终答案进行多数投票来直接边缘化(marginalizes out)推理路径。形式上,设 $A$ 为所有可能最终答案的集合。对于每条采样路径 $\hat{\mathbf{y}}^k$,我们提取其最终预测答案 $\hat{a}^k = \mathrm{Extract}(\hat{\mathbf{y}}^k)$。自一致性策略选择在 $N$ 条采样路径中出现频率最高的答案 $\hat{a}_{\mathrm{sc}}$:
% \begin{eqnarray}
% \hat{a}_{\mathrm{sc}} & = & \argmax_{a \in A} \sum_{k=1}^N \mathbbm{1}(\mathrm{Extract}(\hat{\mathbf{y}}^k) = a),
% \end{eqnarray}
% \noindent 其中 $\mathbbm{1}(\cdot)$ 是指示函数。
% \paragraph{3. 在采样中平衡多样性与质量}
% 在为推断时计算扩展采样多条推理路径时,我们需要在多样性和质量之间寻求权衡。在推理任务中,我们通常希望有一组多样化的采样路径,这增加了包含正确逻辑的机会。一个常见的启发式方法是提高采样温度。通过使分布更加均匀,采样过程避免了仅集中在少数几条高概率路径上。因此,低概率路径更有可能被选中,模型可以在 $N$ 个样本中探索更广泛的推理路径。然而,随着温度的升高,尽管样本的多样性提高了,但单条路径的平均逻辑质量可能会下降。
% 为了在多样性和质量之间取得平衡,一种直接的方法是在推断阶段通过引入显式的惩罚或奖励项来修改搜索目标。在这种设置下,多样性可以在数学上建模为一个与原始生成概率相结合的函数。更正式地,修改后的搜索目标可以写为:
% \begin{eqnarray}
% \mathrm{score}(\mathbf{x},\mathbf{y}) & = & \log \Pr(\mathbf{y}|\mathbf{x}) + \lambda D(\mathbf{y},\mathcal{Y}^{\mathrm{top}}). \label{diversity-quality-inference}
% \end{eqnarray}
% \noindent 这里的 $\log \Pr(\mathbf{y} | \mathbf{x})$ 倾向于概率较高的输出,而 $D(\mathbf{y},\mathcal{Y}^{\mathrm{top}})$ 则作为惩罚或奖励项,鼓励相对于采样路径 $\mathcal{Y}^{\mathrm{top}}$ 的多样性。超参数 $\lambda$ 控制这种权衡的强度。
% $D(\cdot)$ 的设计可以非常灵活。例如,一个简单的惩罚可能会降低在 $\mathcal{Y}^{\mathrm{top}}$ 中已经频繁出现的token或 $n$-gram的分数。相反,可以设计一个奖励函数来奖励明显的语义差异。这样,大语言模型就能产生新的推理结构,而不是对同一想法的微小重述。
% 从更宏观的角度来看,这种采样困境本质上是一个探索-利用(exploration-exploitation)权衡,这在强化学习(RL)中已被深入讨论 \cite{Sutton-and-Barto:2018RL}。例如,大语言模型对齐的最新方法将大语言模型视为一个策略 $\pi_\theta$,可以对其进行优化以生成既多样又高质量的推理路径。在基于RL的微调(如PPO)中,策略被训练为最大化奖励 $R$,同时受到针对参考模型 $\pi_{\mathrm{ref}}$ 的KL散度惩罚的约束,或者通过熵项 $\mathrm{Entropy}(\pi_\theta)$ 进行正则化。一个简单的训练目标采取以下形式:
% \begin{eqnarray}
% \max_{\pi_\theta} \mathbb{E}_{\mathbf{y} \sim \pi_\theta} [R(\mathbf{x}, \mathbf{y})] - \lambda D_{\mathrm{KL}}(\pi_\theta\ \| \ \pi_{\mathrm{ref}}), \label{diversity-quality-rl}
% \end{eqnarray}
% \noindent 其中 $R(\mathbf{x}, \mathbf{y})$ 代表奖励,用于衡量推理路径 $\mathbf{y}$ 对于给定问题 $\mathbf{x}$ 有多好。
% 如果我们将这个RL目标与上面讨论的推断时搜索目标进行比较,我们可以看到它们之间有明显的联系。具体来说,公式(\ref{diversity-quality-rl})中的期望奖励项 $\mathbb{E}_{\mathbf{y} \sim \pi_\theta} [R(\mathbf{x}, \mathbf{y})]$ 对应于公式(\ref{diversity-quality-inference})中的生成概率 $\log \Pr(\mathbf{y}|\mathbf{x})$。这两项都鼓励模型生成高质量的推理路径。同样,公式(\ref{diversity-quality-rl})中的KL散度惩罚 $- \lambda D_{\mathrm{KL}}(\pi_\theta\ \| \ \pi_{\mathrm{ref}})$ 对应于公式(\ref{diversity-quality-inference})中的多样性项 $\lambda D(\mathbf{y},\mathcal{Y}^{\mathrm{top}})$。KL惩罚防止模型每次崩溃为生成非常相似的响应。同样,多样性项迫使推断过程探索更广泛的路径,而不是重复最有可能的路径。
% 尽管有这种形式上的相似性,但需要注意的是,这两种方法在大语言模型开发的不同阶段起作用。RL是一种训练方法。一旦训练完成,模型就知道如何生成既好又多样的推理路径。然而,这通常需要额外的训练样本和计算资源,成本高昂。相比之下,推断时方法作用于已经训练好的模型。它们试图在文本生成过程中通过调整搜索规则来动态寻找这种平衡。但这种灵活性是以额外的人工调优为代价的。例如,必须为不同的任务仔细调整像 $\lambda$ 这样的超参数,以达到多样性和质量之间所需的平衡。
% \subsubsection{推理的搜索策略}
% \label{sec:search-strategies-for-reasoning}
% 在实践中,寻找最优推理路径可以直接转化为一个经典的搜索问题:我们从初始状态开始,采取行动生成一个推理步骤,将当前状态扩展为一组新状态,并重复这个过程直到满足终止条件。由于我们在第 \ChapterEncDec\ 章和第 \ChapterLLMInference\ 章中已经介绍了几种经典的搜索算法(如树搜索和束搜索),我们在这里不再重温这些算法的细节。相反,我们重点关注如何将推理路径的搜索映射到这些经典的搜索框架上。
% 为了将搜索算法应用于大语言模型推理,我们需要定义搜索问题的标准组件:
% \begin{itemize}
% \item \vspace{0.5em} \textbf{搜索状态}:对于大语言模型推理,状态是模型迄今为止构建的上下文。具体而言,在步骤 $t$,状态 $s_t$ 由原始查询 $\mathbf{x}$ 与到目前为止生成的部分推理路径组合而成:$s_t = (\mathbf{x}, \bar{\mathbf{y}}_1, \dots, \bar{\mathbf{y}}_t)$。
% \item \vspace{0.3em} \textbf{动作}:动作是生成下一个推理步骤(文本片段)$\bar{\mathbf{y}}_{t+1}$。
% \item \vspace{0.3em} \textbf{状态转移模型}:采取行动只是将新的文本片段附加到现有状态上。这会产生下一个状态:$s_{t+1} = (s_t, \bar{\mathbf{y}}_{t+1}) = (\mathbf{x}, \bar{\mathbf{y}}_1, \dots, \bar{\mathbf{y}}_t, \bar{\mathbf{y}}_{t+1})$。
% \item \vspace{0.3em} \textbf{状态评估函数}:这是一个评分函数,用于评估当前状态在通向最终答案过程中的逻辑正确性。例如,为了评估搜索状态,我们可以使用硬编码的启发式规则,或训练好的神经验证器。
% \item \vspace{0.3em} \textbf{终止条件}:停止搜索的标准。当模型显式生成最终答案序列,或者搜索达到预定义的计算预算(例如,探索节点的最大深度)时,就会发生这种情况。
% \end{itemize}
% \vspace{0.5em}
% 一旦将推理任务构建为搜索问题,就可以应用各种经典的搜索算法来有效地探索推理空间。例如,简单的树搜索方法,如广度优先搜索和深度优先搜索,可用于对大语言模型的推理过程进行建模。在这些方法中,模型在每一步探索多个候选想法并对其进行评估,以决定是继续沿路径前进,还是在遇到死胡同时回溯。对于具有较高不确定性或延迟奖励的任务,通常采用蒙特卡洛树搜索。这种方法有助于模型在探索新的推理路径和利用已知步骤之间取得平衡。有时它甚至结合外部环境反馈和自我反思来改进搜索 \cite{yao-etal:2024tree,hao-etal:2023reasoning,zhou-etal:2024language}。
% 为了解决树结构的冗余问题(即多条不同的推理路径可能会收敛于同一个中间结论),一些方法将推理空间建模为图(如有向无环图,DAG)\cite{zhang-etal:2025cumulative}。通过这种方式,模型可以构建更高效的搜索空间结构,并捕捉逻辑演绎的非线性相互依赖关系。除了外部搜索算法,还可以将搜索动态直接内化到大语言模型的参数中。例如,通过在A*等启发式搜索算法的执行轨迹上训练大语言模型,它们可以学习隐式地生成搜索过程和最优计划,而无需依赖外部控制脚本 \cite{lehnert-etal:2025beyond}。
% 虽然通过搜索扩展推断时计算是一种强大的策略,但搜索工作量与推理性能之间的关系并不是简单的线性关系。直觉上,我们可能期望给模型更多的时间来探索会持续产生更好的答案。然而,在实践中,性能的提升很快就会达到瓶颈。一个主要原因是大语言模型固有的能力上限:如果模型缺乏解决问题的知识,再多的搜索也无法凭空创造出正确的逻辑。此外,随着搜索工作量的增加,生成路径的多样性会下降,这意味着模型浪费算力去探索那些几乎提供不了新价值的冗余想法。
\ No newline at end of file
\subsection{基于可校验奖励的强化学习方法}
\label{sec:reasoning-verification}
在推理增强中,我们通常需要一个验证器来判断中间步骤或最终答案的正确性,从而为强化学习提供奖励信号。常见的验证器有三类:给出步骤级标量分数的\mindex{过程奖励模型}(PRM),以自然语言输出评语的\mindex{生成式验证器},以及依靠外部工具严格判断的\mindex{形式符号验证器}。下面我们依次介绍它们的工作原理和训练方式。
\paragraph{1. 过程奖励模型}
验证推理结果主要有两种范式:\mindex{结果奖励模型}(ORMs)和\mindex{过程奖励模型}(PRMs)\cite{uesato-etal:2022solving,lightman-etal:2024lets}。ORM 仅评估最终答案,虽然容易训练,但它存在“假阳性”问题。考虑一个简单的数学问题:
\vspace{0.5em}
\begin{quote}
\textit{求解 $x$$2x + 4 = 10$}
\end{quote}
\vspace{0.5em}
模型可能生成如下推理路径:
\vspace{0.5em}
\begin{quote}
\textit{$2x + 4 = 10$ 意味着 $x$ 必须是 $10$ 的一半,即 $5$}
\textit{但是 $5 - 2 = 3$}
\textit{因此,$x = 3$}
\end{quote}
\vspace{0.5em}
虽然最终答案($3$)正确,但逻辑步骤完全是错的。如果只用 ORM,这条路径可能获得高分,从而强化了错误的推理。为了解决这个问题,PRM 会评估路径中每一步的正确性。给定推理路径 $\mathbf{y} = \{\bar{\mathbf{y}}_1, \dots, \bar{\mathbf{y}}_{n_p}\}$,PRM 基于输入 $\mathbf{x}$ 和之前的步骤 $\bar{\mathbf{y}}_{<t}$,为当前步骤 $\bar{\mathbf{y}}_t$ 打一个正确性分数 $r_t$
\begin{eqnarray}
r_t & = & v_{\mathrm{prm}}(\bar{\mathbf{y}}_t | \mathbf{x}, \bar{\mathbf{y}}_{<t}).
\end{eqnarray}
这里的 $v_{\mathrm{prm}}(\cdot)$ 是一个训练好的神经网络。整个路径的验证分数可以取所有步骤分的最小值(即只有“最薄弱的一环”也合理,才认为推理正确):
\begin{eqnarray}
v(\mathbf{x}, \mathbf{y}) & = & \min_{1 \le t \le n_p} v_{\mathrm{prm}}(\bar{\mathbf{y}}_t | \mathbf{x}, \bar{\mathbf{y}}_{<t}).
\end{eqnarray}
当然也可以简单地对步骤分取平均。
训练 PRM 需要步骤级的监督信号,构建训练数据通常有两种方法:
\begin{itemize}
\item \textbf{人工标注}:人类专家将模型生成的中间步骤标记为“正确”或“错误”。这种方法准确,但成本高、难以扩展,尤其在需要领域专家的高级任务上。
\item \textbf{通过 Rollout 自动验证}:如果我们拥有已知最终答案的数据集,就可以用自动方法估计步骤价值 \cite{wang-etal:2024math}。从中间步骤 $\bar{\mathbf{y}}_t$ 出发,让模型继续生成多条完整路径。如果这些路径中很大比例最终得到了正确答案 $a^*$,我们就可以认为该步骤走在正确的轨道上。步骤的估计值 $\hat{r}_t$ 可以定义为达到正确答案的期望概率:
\begin{eqnarray}
\hat{r}_t & = & \mathbb{E}_{\mathbf{y}_{>t} \sim \pi_\theta} [ \mathbbm{1}(\mathrm{Extract}(\mathbf{y}_{>t}) = a^*) ].
\end{eqnarray}
有了这些估计值后,就可以训练 PRM 去预测 $\hat{r}_t$
\end{itemize}
\paragraph{2. 生成式验证器}
上面的验证器都需要修改模型架构并额外训练(例如加上分类头)。另一种自然的想法是直接利用大语言模型的文本生成能力来评价推理步骤,这就是\textbf{生成式验证}(generative verification)\cite{zheng-etal:2023judging}。它就像一位老师在试卷边缘写评语,而不只是给一个分数。
为了看清它是怎么工作的,我们来看一个例子。假设模型在求解方程组:
\vspace{0.5em}
\begin{quote}
\textit{问题:已知 $2x + y = 10$$x - y = 2$,求解 $x$$y$}
\end{quote}
\vspace{0.5em}
模型已经生成了两个正确的步骤:
\vspace{0.5em}
\begin{quote}
\textit{从第二个方程,用 $y$ 表示 $x$$x = y + 2$}
\textit{$x$ 代入第一个方程:$2(y + 2) + y = 10$}
\end{quote}
\vspace{0.5em}
接下来它尝试第三步,但出现了计算错误:
\vspace{0.5em}
\begin{quote}
\textit{展开方程得到 $2y + 2 + y = 10$,化简为 $3y + 2 = 10$}
\end{quote}
\vspace{0.5em}
我们把问题、前两步以及当前步骤拼接起来,再加上一条验证指令(例如“给定前面的步骤,检查当前步骤中的逻辑是否正确并解释你的结论”),一起输入模型。生成式验证器可能会输出这样的文本评论:
\vspace{0.5em}
\begin{quote}
\textit{评论:错误。分配 $2$$(y+2)$ 时必须对括号内每一项都乘以 $2$,应得到 $2y + 4$,而不是 $2y + 2$。正确展开为 $2y + 4 + y = 10$,化简为 $3y + 4 = 10$}
\end{quote}
\vspace{0.5em}
形式化地,设查询为 $\mathbf{z}$,验证指令为 $\mathbf{c}$,推理历史为 $\bar{\mathbf{y}}_{\le t}$。将它们拼接后输入大语言模型,即可生成评论 $\mathbf{p}_t$
\begin{eqnarray}
\mathbf{p}_t & \sim & \Pr(\cdot | \mathbf{z}, \bar{\mathbf{y}}_{\le t}, \mathbf{c}).
\end{eqnarray}
这直接利用了预训练语言模型的生成能力,无需额外训练。图 \ref{fig:pre-generative-verfication} 展示了这一流程。
\begin{figure}[!t]
\centering
\input{figures/Chapter5/figure-generative-verfication}
\caption{生成式验证}
\label{fig:pre-generative-verfication}
\end{figure}
当某些搜索算法需要数值分数时,我们可以测量模型在评论开头输出“正确”等指示词的概率,将验证分数定义为:
\begin{eqnarray}
v(\mathbf{z}, \bar{\mathbf{y}}_{\le t}, \mathbf{c}) & = & \Pr(y_{\mathrm{anchor}} = \text{``Correct''} | \mathbf{z}, \bar{\mathbf{y}}_{\le t}, \mathbf{c}).
\end{eqnarray}
与标量奖励模型相比,生成式验证器有几个优点。第一,它可解释——文本评语能清楚地说明判断依据,方便开发者诊断问题。第二,它支持自我纠正——评语通常会指出错误的具体位置和修正方法,模型可以直接据此修改当前状态,而不必丢弃整条路径。第三,它对架构的改动最小——同一个具有强大推理能力的大语言模型可以同时充当求解器和验证器。
\paragraph{3. 形式符号系统作为验证器}
神经验证器虽然强大,但仍然可能出现幻觉或逻辑错误。对于数学、代码等需要严格精确的任务,我们还可以使用\textbf{形式符号系统}作为验证器。例如,在代码优化中,形式化等价检查器(如 Alive2)可以自动验证新生成的代码是否保持了与原始代码完全相同的语义。这类系统严格遵循数学和逻辑规则,判断是 100\% 可靠的。
要用上形式验证,模型必须将推理步骤生成为可执行代码或形式化陈述,而不仅仅是自然语言。比如,在解决数学问题时,模型可以写一个 Python 脚本来执行某步中间计算,然后由外部工具运行这段代码。验证分数可以直接基于执行是否成功来定义:
\begin{eqnarray}
v(\mathbf{x}, \bar{\mathbf{y}}_{<t}, \bar{\mathbf{y}}_t) & = & \begin{cases}
1, & \text{if } E(\bar{\mathbf{y}}_t) \text{ executes successfully} \\
0, & \text{otherwise}.
\end{cases}
\end{eqnarray}
如果执行失败,形式环境通常会返回一条错误消息(如 \texttt{TypeError}),这条消息可以反馈给模型,帮助它理解错误并修正逻辑。
这种方法完全消除了验证器本身产生“假阳性”的可能。不过,它也存在局限:它要求模型具备较强的编码或形式化能力,将抽象推理转化为严格代码并不容易;而且,对于写文章、规划旅行这类开放式任务,推理很难映射成刚性规则,形式系统并不适用。
% \subsection{基于可校验奖励的强化学习方法}
% \label{sec:reasoning-verification}
% % 参考龙哥rl书的第5章
% % 格式校验、结果奖励、GRPO
% % 最后一小节可以讲大语言模型推理评估方法
% 接下来,我们讨论推理中的验证问题。请注意,虽然我们在本节中重点关注推断时方法,但推理模型中使用的许多验证器仍然需要训练。因此,我们在接下来的讨论中也会介绍一些关于训练这些模型的细节。
% \paragraph{1. 过程奖励模型}
% 验证推理结果主要有两种范式:\mindex{结果奖励模型}(ORMs)和\mindex{过程奖励模型}(PRMs)\cite{uesato-etal:2022solving,lightman-etal:2024lets}。ORM仅评估推理路径的最终答案。虽然ORM更容易训练,但它们通常不足以胜任推理任务,因为它们存在“假阳性”(false positives)问题。为了说明这一点,考虑一个简单的数学问题:
% \vspace{0.5em}
% \begin{quote}
% \textit{求解 $x$:$2x + 4 = 10$。}
% \end{quote}
% \vspace{0.5em}
% 模型可能会生成以下推理路径:
% \vspace{0.5em}
% \begin{quote}
% \textit{$2x + 4 = 10$ 意味着 $x$ 必须是 $10$ 的一半,即 $5$。}
% \textit{但是 $5 - 2 = 3$。}
% \textit{因此,$x = 3$。}
% \end{quote}
% \vspace{0.5em}
% 虽然最终答案($3$)是正确的,但逻辑步骤完全是错误的。如果我们只使用ORM,这条路径可能会获得高分,从而意外地强化了错误的逻辑。为了解决这个问题,我们可以使用PRM来评估推理路径中每个单独步骤的正确性。给定推理路径 $\mathbf{y} = \{\bar{\mathbf{y}}_1, \dots, \bar{\mathbf{y}}_{n_p}\}$,PRM在输入 $\mathbf{x}$ 和所有先前步骤 $\bar{\mathbf{y}}_{<t}$ 的条件下,为当前步骤 $\bar{\mathbf{y}}_t$ 分配一个正确性分数 $r_t$。我们可以将步骤级验证函数定义为:
% \begin{eqnarray}
% r_t & = & v_{\mathrm{prm}}(\bar{\mathbf{y}}_t | \mathbf{x}, \bar{\mathbf{y}}_{<t}).
% \end{eqnarray}
% \noindent 这里的 $v_{\mathrm{prm}}(\cdot)$ 是一个经过训练以预测正确性分数的神经网络。例如,我们可以在现有大语言模型的输出表示上附加一个简单的分类头(如线性层后接sigmoid激活函数)。
% 为了确定整个推理路径的整体质量,我们可以聚合这些步骤级分数。一种简单的方法是取所有步骤中的最低分。换句话说,只有当其“最薄弱的环节”在逻辑上合理时,我们才认为推理是正确的。验证分数则由下式给出:
% \begin{eqnarray}
% v(\mathbf{x}, \mathbf{y}) & = & \min_{1 \le t \le n_p} v_{\mathrm{prm}}(\bar{\mathbf{y}}_t | \mathbf{x}, \bar{\mathbf{y}}_{<t}).
% \end{eqnarray}
% \noindent 或者,我们可以将验证分数定义为步骤级分数的平均值:
% \begin{eqnarray}
% v(\mathbf{x}, \mathbf{y}) & = & \frac{1}{n_p}\sum_{1 \le t \le n_p} v_{\mathrm{prm}}(\bar{\mathbf{y}}_t | \mathbf{x}, \bar{\mathbf{y}}_{<t}).
% \end{eqnarray}
% 为了训练验证模型,我们需要一个带注释的数据集。与训练只需要知道路径最末端最终正确答案的ORM不同,训练PRM需要步骤级的监督。构建PRM训练数据集有两种常用的方法:
% \begin{itemize}
% \item \vspace{0.5em} \textbf{人工标注}:人类专家手动解决问题,并将大语言模型生成的每个中间步骤标记为“正确”或“错误”。虽然这种方法可以产生准确的验证器,但成本高昂且难以扩展,特别是对于需要领域专家的高级数学或编码任务。
% \item \vspace{0.3em} \textbf{通过Rollout(展开)进行自动验证}:我们也可以使用自动方法来估计步骤的价值 \cite{wang-etal:2024math}。例如,如果我们有一个已知最终答案的数据集(如数学竞赛),我们可以使用\mindex{蒙特卡洛rollout}。从中间步骤 $\bar{\mathbf{y}}_t$ 开始,我们提示大语言模型继续生成多条完整的路径。如果这些路径中有很大比例最终达到了正确的最终真实答案 $a^*$,我们可以假设中间步骤 $\bar{\mathbf{y}}_t$ 走在正确的轨道上。形式上,步骤的估计值 $\hat{r}_t$ 可以定义为达到正确答案的期望概率:
% \begin{eqnarray}
% \hat{r}_t & = & \mathbb{E}_{\mathbf{y}_{>t} \sim \pi_\theta} [ \mathbbm{1}(\mathrm{Extract}(\mathbf{y}_{>t}) = a^*) ].
% \end{eqnarray}
% \noindent 这里的 $\mathbf{y}_{>t}$ 是从 $\bar{\mathbf{y}}_t$ 开始的剩余路径,$\mathrm{Extract}(\mathbf{y}_{>t})$ 表示从路径 $\mathbf{y}_{>t}$ 中提取的答案。一旦通过这些rollout估计出这些值,就可以训练PRM来预测 $\hat{r}_t$。
% \end{itemize}\vspace{0.5em}
% \paragraph{2. 生成式验证器}
% 上述所有验证器都需要额外的架构修改和训练工作,例如,我们需要训练一个分类头来输出数值分数。鉴于大语言模型强大的文本生成能力,另一种自然的方法是直接提示大语言模型评估推理步骤,并以自然语言生成验证反馈。这种方法被称为\textbf{生成式验证}(generative verification)\cite{zheng-etal:2023judging}。
% 我们不是修改模型架构以输出单个标量值,而是将验证任务构建为一个标准的文本生成问题。通过将查询和中间步骤反馈给大语言模型,我们可以让它充当评判者。这意味着模型就像一位在试卷边缘写评语的老师,而不仅仅是给出一个最终分数。
% 为了理解这是如何工作的,让我们看一个简单的例子。假设模型正在求解一个方程组:
% \vspace{0.5em}
% \begin{quote}
% \textit{问题:给定 $2x + y = 10$ 和 $x - y = 2$,求解 $x$ 和 $y$。}
% \end{quote}
% \vspace{0.5em}
% 我们假设模型已经生成了两个正确的步骤:
% \vspace{0.5em}
% \begin{quote}
% \textit{从第二个方程,我们可以用 $y$ 表示 $x$:$x = y + 2$。}
% \textit{将 $x$ 代入第一个方程:$2(y + 2) + y = 10$。}
% \end{quote}
% \vspace{0.5em}
% 现在它正在尝试第三步,其中包含一个计算错误:
% \vspace{0.5em}
% \begin{quote}
% \textit{展开方程得到 $2y + 2 + y = 10$,化简为 $3y + 2 = 10$。}
% \end{quote}
% \vspace{0.5em}
% 我们可以将原始查询、步骤1和2以及当前步骤3与验证提示(例如,“\textit{给定前面的步骤,检查当前步骤中的逻辑是否正确并解释你的结论。}”)结合起来。然后,生成式验证器可能会输出这样的文本评论:
% \vspace{0.5em}
% \begin{quote}
% \textit{评论:错误。在当前步骤中,将第2步中的 $2$ 分配到 $(y + 2)$ 上时,必须将括号内的两项都乘以 $2$。应该是 $2y + 4$,而不是 $2y + 2$。正确的展开是 $2y + 4 + y = 10$,化简为 $3y + 4 = 10$。}
% \end{quote}
% \vspace{0.5em}
% 为了形式化这个生成式验证过程,我们假设原始输入 $\mathbf{x}$ 由查询 $\mathbf{z}$ 和指令 $\mathbf{c}$ 组成,该指令要求模型根据上下文验证该步骤。我们可以将查询 $\mathbf{z}$、推理历史 $\bar{\mathbf{y}}_{\le t}$ 和指令 $\mathbf{c}$ 拼接成一段长文本。然后,我们将这段文本作为输入馈送给大语言模型,并获得自然语言评论 $\mathbf{p}_t$:
% \begin{eqnarray}
% \mathbf{p}_t & \sim & \Pr(\cdot | \mathbf{z}, \bar{\mathbf{y}}_{\le t}, \mathbf{c})
% \end{eqnarray}
% \noindent 这里的 $\Pr(\cdot)$ 表示现有大语言模型建模的概率分布。因此,我们可以直接使用强大的大语言模型进行准确的验证而无需训练。图 \ref{fig:pre-generative-verfication} 说明了这种方法。
% \begin{figure}[!t]
% \centering
% \input{figures/Chapter5/figure-generative-verfication}
% \caption{生成式验证}
% \label{fig:pre-generative-verfication}
% \end{figure}
% 虽然自然语言评论对人类很有用,但某些搜索算法(如树搜索)可能需要数值分数来对不同的推理路径进行排名。为了从生成式验证器中提取连续分数,一种常见的方法是测量模型在其评论的第一个词输出指示token(如“正确”或“错误”)的概率。因此,数值验证分数可以定义为:
% \begin{eqnarray}
% v(\mathbf{z}, \bar{\mathbf{y}}_{\le t}, \mathbf{c}) & = & \Pr(y_{\mathrm{anchor}} = \text{``Correct''} | \mathbf{z}, \bar{\mathbf{y}}_{\le t}, \mathbf{c})
% \end{eqnarray}
% \noindent 这里的 $y_{\mathrm{anchor}}$ 是指示token,通常使用启发式方法从输出文本中提取。
% 生成式验证器比传统的标量奖励模型有几个优势。首先,它们为问题解决提供了可解释性。因为验证器用文本解释其推理,开发者可以诊断为什么接受或不接受某条推理路径。其次,它们支持自我纠正。由于文本评论通常会明确指出如何修复错误(例如在我们的例子中提供正确的展开式 $2y + 4$),推理系统可以直接整合这些反馈来纠正其当前状态并继续搜索,而不是简单地丢弃整条路径。第三,它们对架构的修改最小。只要当前的大语言模型具有强大的推理能力,完全相同的模型就可以同时用作问题解决者和验证器。
% \paragraph{3. 形式符号系统作为验证器}
% 虽然神经验证器被广泛使用,但它们有一个共同的弱点:它们仍然是神经网络。这意味着它们也可能产生幻觉或犯逻辑错误。对于需要严格精确度的任务,如复杂的数学或软件工程,我们也可以使用形式符号系统作为验证器。例如,在代码优化任务中,当模型尝试优化中间表示代码(如LLVM-IR)时,形式等价检查器(如Alive2)可以自动验证新生成的代码是否保持了与原始代码完全相同的语义。
% 形式系统严格遵循预定义的数学和逻辑规则,因此它们的判断是100\%可靠的。要将形式系统用作验证器,大语言模型必须将其推理步骤生成为可执行代码或形式化数学陈述,而不仅仅是普通的自然语言。例如,在解决数学问题时,大语言模型可能会编写一个小的Python脚本来执行特定的中间计算。然后系统使用外部工具执行这段代码。验证过程非常直接:
% \begin{itemize}
% \item \vspace{0.5em} \textbf{成功}:如果代码无错误运行并产生有效输出,则该推理步骤被验证为正确。
% \item \vspace{0.3em} \textbf{失败}:如果代码抛出错误(如语法错误或数学违规),则该推理步骤被验证为错误。
% \end{itemize}
% \vspace{0.5em}
% 形式上,设中间步骤 $\bar{\mathbf{y}}_t$ 为生成的代码块或形式逻辑,设 $E$ 表示形式执行环境。验证分数可以定义为:
% \begin{eqnarray}
% v(\mathbf{x}, \bar{\mathbf{y}}_{<t}, \bar{\mathbf{y}}_t) & = & \begin{cases}
% 1, & \text{if } E(\bar{\mathbf{y}}_t) \text{ executes successfully} \\
% 0, & \text{otherwise}
% \end{cases}
% \end{eqnarray}
% 这种方法有一个明显的优势:它消除了由验证器幻觉引起的“假阳性”。此外,当执行失败时,形式系统通常会生成一条错误消息(例如 \texttt{TypeError} 或编译失败)。这条错误消息可以反馈给大语言模型,帮助模型理解哪里出了问题,并在下一次尝试中纠正其逻辑。
% 然而,使用形式符号系统也有其局限性。首先,它要求大语言模型具备强大的编码或形式化技能,因为将抽象推理转化为严格的代码是困难的。其次,虽然形式系统非常适合数学和编码,但它们不适用于开放式的现实世界任务(如写文章或计划旅行),在这些任务中,推理不能轻易映射到僵化的逻辑规则上。
\ No newline at end of file
\subsection{从推理大模型到智能体}
推理模型不仅提升了语言模型在静态问题上的表现,更催生了一类能够感知、规划、行动与反思的自主智能体。本节介绍三种将“系统2”推理注入智能体构建的核心方法:通过显式思维链驱动行动的 ReAct 框架、基于失败经验进行自我纠正的 Reflexion 方法,以及将规划建模为树搜索的 Tree of Thoughts 算法。这些方法共同揭示了推理如何成为连接认知与行动的桥梁。
\subsubsection{ReAct 框架}
传统的提示链方法通常一次性生成完整计划,难以应对动态环境中的不确定性。ReAct(Reasoning and Acting)框架~\cite{yao-etal:2023react} 将问题求解建模为一个交替生成“思维”与“动作”的序列,使模型能够在执行过程中不断根据观察结果调整计划。
具体而言,在时间步 $t$,模型接收当前上下文 $c_t$(包括任务描述、工具说明以及历史交互),先采样一个思维 $T_t$,再基于该思维采样一个动作 $A_t$
\begin{eqnarray}
T_t &\sim& \pi_\theta(\cdot \mid c_t), \\
A_t &\sim& \pi_\theta(\cdot \mid c_t, T_t).
\end{eqnarray}
环境根据 $A_t$ 返回观察 $O_t$,新的上下文更新为 $c_{t+1} = c_t \oplus (T_t, A_t, O_t)$。这一循环持续进行,直到模型输出一个结束动作或达到最大步数。
一个经典的例子是让智能体在电商网站为用户选购笔记本电脑。原始 LLM 可能直接生成一个含错误参数的搜索指令,而 ReAct 智能体的交互过程如下:
\vspace{0.5em}
\begin{quote}
\textit{思维:用户需要视频剪辑用的电脑,需要高性能 GPU 和至少 32 GB 内存。我先用关键词搜索。}
\textit{动作:search("视频剪辑笔记本电脑")}
\textit{观察:返回 50 条商品,但摘要中没有显示内存信息。}
\textit{思维:我需要筛选内存条件。页面上有“高级筛选”按钮,我可以点击它并设置内存为 32 GB。}
\textit{动作:click("高级筛选"); select("内存", "32GB")}
\textit{观察:列表刷新,剩余 8 个匹配商品。}
\end{quote}
\vspace{0.5em}
这种“思维—动作—观察”的循环带来了两个关键优势:(1) 思维为动作提供了可解释的决策依据;(2) 每一轮观察都可以用来修正后续的推理方向,使系统具备在线适应能力。在实际部署中,动作集通常被实现为工具 API 调用,模型通过生成特定格式的文本(如 \texttt{Action: search("...")})来触发工具,环境则将执行结果以文本形式回传。
ReAct 的局限性在于其本质是线性链式推理,面对包含多个分支或需要长程前瞻的复杂任务时,可能陷入局部次优甚至无法找到可行路径。为此,后续方法引入了更结构化的搜索机制。
\subsubsection{Reflexion 方法}
在现实交互中,即使是一次完整的 ReAct 轨迹也可能以失败告终。与其从头重试,Reflexion 方法~\cite{shinn-etal:2023reflexion} 赋予智能体“从错误中学习”的能力:每次失败后,模型会生成一段文本化的反思,并将其存入长期记忆,作为后续尝试的提示。
形式化地,记任务输入为 $\mathbf{x}$,第 $k$ 次尝试生成的轨迹为 $\tau_k$,最终环境给出的二进制成功信号为 $s_k \in \{0,1\}$。若 $s_k = 0$,系统将当前轨迹与失败信号输入大语言模型,让其总结失败原因和修正策略,生成反思文本 $R_k$
\begin{eqnarray}
R_k &=& \text{LLM}\bigl(\mathbf{x}, \tau_k, \text{``Task failed. Analyze the error and suggest a fix.''}\bigr).
\end{eqnarray}
反思 $R_k$ 被追加到记忆 $\mathbf{M}$ 中。在第 $k+1$ 次尝试时,智能体除了任务描述 $\mathbf{x}$ 外,还会看到记忆 $\mathbf{M}$ 作为额外上下文,从而有意识地规避之前犯过的错误。整个过程可以看作在语言空间中进行的一种轻量级“经验回放”。
仍以选购电脑为例,假设某次尝试中智能体在筛选后直接选择了一款外观漂亮的机型,却忽略了 GPU 型号,导致最终购买结果不符合要求。失败后,Reflexion 模块可能输出如下反思:
\vspace{0.5em}
\begin{quote}
\textit{反思:上次失败是因为我只关注了内存筛选,而没有验证 GPU 是否符合视频剪辑的需求。在下一步选择具体产品时,必须检查其 GPU 是否为独立高性能显卡(如 RTX 4060 及以上)。}
\end{quote}
\vspace{0.5em}
在下一轮尝试中,这条反思被加入提示,智能体在筛选后会主动检查每款候选品的 GPU 参数,从而显著提高成功率。
Reflexion 方法的优雅之处在于它完全复用了同一个大语言模型来实现“反思者”的角色,无需额外训练。不过,反思的质量很大程度上依赖于模型本身的诊断能力;对于需要精密逻辑的任务,单纯的文本反思可能不足以纠正常见的计算或逻辑谬误,此时可结合后续的树搜索或形式验证器进行强化。
\subsubsection{Tree of Thoughts 算法}
当任务具有明确的中间状态和可评价的子目标时,仅靠线性的“思维—动作”链或记忆化的反思仍显不足。Tree of Thoughts(ToT)算法~\cite{yao-etal:2023tree} 将规划形式化为在“思维树”上的搜索问题:每一个节点代表一个部分推理结果,边代表从当前思考状态到下一步思考的过渡,搜索算法负责在树的生长过程中探索有希望的分支并剪除低质量路径。
ToT 的一次典型执行包含两个核心组件:
\begin{itemize}
\item \textbf{候选生成}:给定当前状态 $s$,大语言模型一次性生成 $k$ 个不同的下一步思维 $\{t^{(1)}, \dots, t^{(k)}\}$
\item \textbf{状态评估}:使用同一大语言模型(或一个专用的验证器)为每个候选思维打分。一种常见的方法是让模型以特定句式(如“这一步骤正确/可能/不可能的”)进行评价,然后将其对“正确”等锚定词的概率映射为分值:
\begin{eqnarray}
V(s, t^{(i)}) &=& P_\theta\bigl(y_{\mathrm{anchor}} = \text{``certain''} \mid s, t^{(i)}, \text{评估指令}\bigr).
\end{eqnarray}
\end{itemize}
根据不同的任务特性,可以选择宽度优先搜索(BFS)或深度优先搜索(DFS)。以 BFS 为例,算法在每一层保留分值最高的 $b$ 个状态,然后基于这些状态继续生成下一步候选,直至达到终止条件(如找到公认的最优答案或预算耗尽)。
Tree of Thoughts 在需要全局规划和回溯的任务(如数学证明、创意写作大纲生成、复杂策略游戏)上取得了远超简单 ReAct 的表现。但它的计算开销更大,因为每次扩展都需要多次调用大语言模型;因此,动态分配计算资源、在简单步骤上减少采样数量,是提升其实用性的重要方向。
\vspace{0.3cm}
总结起来,ReAct 提供了智能体与环境交互的基本骨架,Reflexion 为其注入了从失败中自我改进的元认知能力,而 Tree of Thoughts 则通过结构化搜索赋予了智能体应对复杂规划问题的深度推理手段。这些方法共同构成了当前基于大语言模型构建推理型智能体的技术基石。
\ No newline at end of file
\section{本章小结}
本章围绕大语言模型从预训练到应用部署的完整技术链路,构建了一套系统性的认知框架。我们从最基础的语言建模目标出发——利用链式法则将序列联合概率分解为逐步条件概率的乘积,进而理解了仅解码器Transformer架构如何为这一概率分解提供可计算的神经网络实现。预训练的本质被还原为一个看似朴素却极其强大的目标:在海量文本上最大化逐token预测的对数似然。缩放定律则揭示了模型参数量与训练数据量之间的幂律关系,Chinchilla定律进一步指出二者必须等比例增长才能达到计算最优,这一洞见直接指导了从GPT-3的0.5T到Qwen3的36T训练数据的规模跃迁。分布式训练中的数据并行、流水线并行与张量并行则为这种规模扩张提供了工程上的可行性保障。
在预训练奠定的通用能力之上,我们学习了如何通过提示学习将模型适配到具体任务。零样本、单样本与少样本提示构成了上下文学习的基本谱系,而思维链提示通过强制模型生成中间推理步骤,显著提升了多步推理任务的准确率。提示工程从人工设计走向自动化优化,软提示则突破了离散文本的限制,将提示表示为可学习的连续向量,在效率与灵活性之间取得了平衡。
微调技术是将通用预训练模型转化为任务专用模型的核心手段。有监督微调通过精心设计的指令--响应对激活模型的指令遵循能力,其数据量远小于预训练却能有效唤醒潜在能力,这与表面对齐假说高度一致。参数高效微调方法,尤其是LoRA,通过低秩分解将权重更新压缩为两个小矩阵的乘积,以极少的可训练参数达到接近全量微调的效果,且推理时零额外延迟,使大模型微调真正走向了普及化。
偏好对齐技术解决了模型``能做什么''与``应该做什么''之间的鸿沟。RLHF通过人类偏好排序训练奖励模型,再以PPO等强化学习算法优化策略,使模型输出符合人类期望。DPO则通过数学推导将奖励建模与策略优化合二为一,将复杂的四模型训练流程简化为一个监督式分类任务,大幅降低了对齐的工程门槛。偏好数据的自动生成进一步缓解了对人类标注的依赖,使对齐过程具备了更强的可扩展性。
推理增强是本章的最后一个主题,也是当前最活跃的研究前沿。测试时缩放将计算资源从训练阶段转移到推断阶段,通过采样多条推理路径或构建结构化搜索来动态分配``思考时间''。基于可校验奖励的强化学习为推理过程提供了可靠的监督信号,过程奖励模型、生成式验证器和形式符号系统各有适用场景。从推理模型到智能体的演进——ReAct的``思维--动作--观察''循环、Reflexion的失败反思机制、Tree of Thoughts的树搜索规划——标志着大语言模型正从被动的文本生成器走向能够感知、规划、行动与自我修正的自主系统。
展望未来,大语言模型的技术演进仍面临诸多开放性问题。缩放定律尚未触及天花板,但算力与数据的边际成本正在攀升,如何在有限资源下实现更高效的训练与推理将是持续的工程挑战。对齐问题远未解决,人类价值观的复杂性与动态性决定了这不可能是一次性的训练目标,而需要模型在与真实世界的持续交互中不断校准。推理能力的边界仍在拓展,如何将形式化验证的严谨性与神经网络的灵活性深度融合,如何让智能体在开放环境中实现长程规划与鲁棒决策,都是亟待突破的方向。此外,多模态融合、长上下文处理、模型安全与可解释性等议题也将在后续章节中逐步展开。本章所建立的预训练、提示、微调、对齐与推理增强的技术体系,既是理解当前大模型能力边界的钥匙,也是通往更强大、更安全、更通用的人工智能系统的起点。
\begin{exercisebox}
\begin{enumerate}
\item 设一个 token 序列为 $\{x_0,x_1,x_2,x_3\}$,其中 $x_0=\langle s\rangle$。根据链式法则,写出该序列的联合概率 $\Pr(x_0,x_1,x_2,x_3)$ 的分解式。
\item 判断下列说法是否正确,并简要说明理由:
“根据 Chinchilla 缩放定律,若要提升模型性能,应优先增加模型参数量,而非增加训练数据量。”
\item 下面哪个选项是思维链(Chain-of-Thought)提示的核心特征?
A. 在提示中提供多个完整的问答示例
B. 强制模型生成中间的推理步骤后再给出答案
C. 使用可训练的连续向量代替文本指令
D. 要求模型直接输出最终结果以提高效率
\item 在有监督微调(SFT)中,为什么需要将损失函数仅作用于输出部分(即助手的回复),而不作用于输入部分(即用户的指令)?请简要解释。
\item 对于一个参数量为 $d \times k$ 的预训练权重矩阵,若采用 LoRA 进行微调,秩取 $r$,则新增的可训练参数量是多少?(用 $d$$k$$r$ 表示)
\item 在 RLHF 流程中,Bradley-Terry 模型用于训练奖励模型时,其损失函数使用成对比较数据而非绝对评分数据。这种做法的优势是什么?
\item 某模型在数学推理任务上表现不佳,你作为技术人员,可以从哪些技术层面尝试改进?请至少写出两个方面,并简要说明理由。
\end{enumerate}
\end{exercisebox}
% <details>
% <summary>参考答案(供教师使用)</summary>
% 1. $\Pr(x_0,x_1,x_2,x_3)=\Pr(x_0)\cdot\Pr(x_1|x_0)\cdot\Pr(x_2|x_0,x_1)\cdot\Pr(x_3|x_0,x_1,x_2)$,其中约定 $\Pr(x_0)=1$。
% 2. **错误**。Chinchilla 缩放定律指出,在给定计算预算下,模型参数量 $N$ 与训练数据量 $D$ 应以相近的速度同步增长,二者需要平衡,“参数过大而数据不足”会导致算力浪费。
% 3. **B**。
% 4. 输入部分(指令)不需要模型“预测”,模型在推理时会直接获得用户输入的指令;我们只希望模型学习如何根据指令生成正确回复,而非学习预测用户会问什么。因此,仅输出部分的梯度参与参数更新。
% 5. $r \times (d + k)$,即 $d \times r + r \times k$。
% 6. 标注者直接给出绝对分数主观性强且难以一致,而判断两个输出之间的相对优劣更直观、更可靠。成对比较数据更容易收集,且 Bradley-Terry 模型可将这种相对偏好转化为可训练的连续奖励信号。
% 7. (答案不唯一,合理即可)
% 示例一:使用思维链提示(如添加“逐步思考”指令),激活模型的推理能力。
% 示例二:收集更多数学推理数据,进行有监督微调,强化模型在多步逻辑推导上的表现。
% 示例三:在推理时采用 Best-of-N 采样,生成多条候选路径后选出最优答案。
% </details>
# 中文课程讲义 LaTeX 模板
主文件是 `aml_notes.tex`,建议使用 XeLaTeX 编译:
```powershell
xelatex aml_notes.tex
xelatex aml_notes.tex
```
第二次编译用于生成完整目录和交叉引用。图片可以放在 `figures/``images/` 目录中,然后用 `\includegraphics` 插入。
常用修改位置:
- 课程信息:修改 `\coursename``\semester``\teacher``\school`
- 新增章节:使用 `\chapter{章节标题}`
- 新增小节:使用 `\section{小节标题}``\subsection{小节标题}`
- 插入表格:参考模板中的 `table``longtable` 示例。
- 插入图片:把图片放入 `figures/`,参考模板中的 `figure` 示例。
如果使用 Overleaf,也请选择 XeLaTeX 作为编译器。
This source diff could not be displayed because it is too large. You can view the blob instead.
@book{edwards:1994historical,
title={The historical development of the calculus},
author={Edwards, CH Jr},
year={1994},
publisher={Springer Science \& Business Media}
}
@book{dunham:2005calculus,
title={The calculus gallery: Masterpieces from Newton to Lebesgue},
author={Dunham, William},
year={2005},
publisher={Princeton University Press}
}
@book{apostol:1991calculus,
title={Calculus, Volume 1},
author={Apostol, Tom M},
year={1991},
publisher={John Wiley \& Sons}
}
@book{spivak:2006calculus,
title={Calculus},
author={Spivak, Michael},
year={2006},
publisher={Cambridge University Press}
}
@book{hartman:2002ordinary,
title={Ordinary differential equations},
author={Hartman, Philip},
year={2002},
publisher={SIAM}
}
@book{hirsch-etal:2013differential,
title={Differential equations, dynamical systems, and an introduction to chaos},
author={Hirsch, Morris W and Smale, Stephen and Devaney, Robert L},
year={2013},
publisher={Academic press}
}
@book{stoer-etal:1980introduction,
title={Introduction to numerical analysis},
author={Stoer, Josef and Bulirsch, Roland and Bartels, R and Gautschi, Walter and Witzgall, Christoph},
year={1980},
publisher={Springer}
}
@book{richard:2015numerical,
title={Numerical Analysis},
author={ Richard L. Burden and J. Douglas Faires and Annette M. Burden},
year={2015},
publisher={Cengage Learning}
}
@article{qian:1999momentum,
title={On the momentum term in gradient descent learning algorithms},
author={Qian, Ning},
journal={Neural networks},
volume={12},
number={1},
pages={145--151},
year={1999},
publisher={Elsevier}
}
@article{polyak:1964some,
title={Some methods of speeding up the convergence of iteration methods},
author={Polyak, Boris T},
journal={Ussr computational mathematics and mathematical physics},
volume={4},
number={5},
pages={1--17},
year={1964},
publisher={Elsevier}
}
@article{su-etal:2016differential,
title={A differential equation for modeling Nesterov's accelerated gradient method: Theory and insights},
author={Su, Weijie and Boyd, Stephen and Candes, Emmanuel J},
journal={Journal of Machine Learning Research},
volume={17},
number={153},
pages={1--43},
year={2016}
}
@book{norris:1998markov,
title={Markov chains},
author={Norris, James R},
year={1998},
publisher={Cambridge university press}
}
@article{vaswani-etal:2017attention,
title={Attention is all you need},
author={Vaswani, Ashish and Shazeer, Noam and Parmar, Niki and Uszkoreit, Jakob and Jones, Llion and Gomez, Aidan N and Kaiser, {\L}ukasz and Polosukhin, Illia},
journal={Advances in neural information processing systems},
volume={30},
year={2017}
}
@article{brown-etal:2020language,
title={Language models are few-shot learners},
author = {Brown, Tom and Mann, Benjamin and Ryder, Nick and Subbiah, Melanie and Kaplan, Jared D and Dhariwal, Prafulla and Neelakantan, Arvind and Shyam, Pranav and Sastry, Girish and Askell, Amanda and Agarwal, Sandhini and Herbert-Voss, Ariel and Krueger, Gretchen and Henighan, Tom and Child, Rewon and Ramesh, Aditya and Ziegler, Daniel and Wu, Jeffrey and Winter, Clemens and Hesse, Chris and Chen, Mark and Sigler, Eric and Litwin, Mateusz and Gray, Scott and Chess, Benjamin and Clark, Jack and Berner, Christopher and McCandlish, Sam and Radford, Alec and Sutskever, Ilya and Amodei, Dario},
journal={Advances in neural information processing systems},
volume={33},
pages={1877--1901},
year={2020}
}
@article{bai-etal:2023qwen,
title={Qwen technical report},
author={Jinze Bai and Shuai Bai and Yunfei Chu and Zeyu Cui and Kai Dang and Xiaodong Deng and Yang Fan and Wenbin Ge and Yu Han and Fei Huang and Binyuan Hui and Luo Ji and Mei Li and Junyang Lin and Runji Lin and Dayiheng Liu and Gao Liu and Chengqiang Lu and Keming Lu and Jianxin Ma and Rui Men and Xingzhang Ren and Xuancheng Ren and Chuanqi Tan and Sinan Tan and Jianhong Tu and Peng Wang and Shijie Wang and Wei Wang and Shengguang Wu and Benfeng Xu and Jin Xu and An Yang and Hao Yang and Jian Yang and Shusheng Yang and Yang Yao and Bowen Yu and Hongyi Yuan and Zheng Yuan and Jianwei Zhang and Xingxuan Zhang and Yichang Zhang and Zhenru Zhang and Chang Zhou and Jingren Zhou and Xiaohuan Zhou and Tianhang Zhu},
journal={arXiv preprint arXiv:2309.16609},
year={2023}
}
@article{liu-etal:2024deepseek,
title={Deepseek-v3 technical report},
author={Aixin Liu and Bei Feng and Bing Xue and Bingxuan Wang and Bochao Wu and Chengda Lu and Chenggang Zhao and Chengqi Deng and Chenyu Zhang and Chong Ruan and Damai Dai and Daya Guo and Dejian Yang and Deli Chen and Dongjie Ji and Erhang Li and Fangyun Lin and Fucong Dai and Fuli Luo and Guangbo Hao and Guanting Chen and Guowei Li and H. Zhang and Han Bao and Hanwei Xu and Haocheng Wang and Haowei Zhang and Honghui Ding and Huajian Xin and Huazuo Gao and Hui Li and Hui Qu and J. L. Cai and Jian Liang and Jianzhong Guo and Jiaqi Ni and Jiashi Li and Jiawei Wang and Jin Chen and Jingchang Chen and Jingyang Yuan and Junjie Qiu and Junlong Li and Junxiao Song and Kai Dong and Kai Hu and Kaige Gao and Kang Guan and Kexin Huang and Kuai Yu and Lean Wang and Lecong Zhang and Lei Xu and Leyi Xia and Liang Zhao and Litong Wang and Liyue Zhang and Meng Li and Miaojun Wang and Mingchuan Zhang and Minghua Zhang and Minghui Tang and Mingming Li and Ning Tian and Panpan Huang and Peiyi Wang and Peng Zhang and Qiancheng Wang and Qihao Zhu and Qinyu Chen and Qiushi Du and R. J. Chen and R. L. Jin and Ruiqi Ge and Ruisong Zhang and Ruizhe Pan and Runji Wang and Runxin Xu and Ruoyu Zhang and Ruyi Chen and S. S. Li and Shanghao Lu and Shangyan Zhou and Shanhuang Chen and Shaoqing Wu and Shengfeng Ye and Shengfeng Ye and Shirong Ma and Shiyu Wang and Shuang Zhou and Shuiping Yu and Shunfeng Zhou and Shuting Pan and T. Wang and Tao Yun and Tian Pei and Tianyu Sun and W. L. Xiao and Wangding Zeng and Wanjia Zhao and Wei An and Wen Liu and Wenfeng Liang and Wenjun Gao and Wenqin Yu and Wentao Zhang and X. Q. Li and Xiangyue Jin and Xianzu Wang and Xiao Bi and Xiaodong Liu and Xiaohan Wang and Xiaojin Shen and Xiaokang Chen and Xiaokang Zhang and Xiaosha Chen and Xiaotao Nie and Xiaowen Sun and Xiaoxiang Wang and Xin Cheng and Xin Liu and Xin Xie and Xingchao Liu and Xingkai Yu and Xinnan Song and Xinxia Shan and Xinyi Zhou and Xinyu Yang and Xinyuan Li and Xuecheng Su and Xuheng Lin and Y. K. Li and Y. Q. Wang and Y. X. Wei and Y. X. Zhu and Yang Zhang and Yanhong Xu and Yanhong Xu and Yanping Huang and Yao Li and Yao Zhao and Yaofeng Sun and Yaohui Li and Yaohui Wang and Yi Yu and Yi Zheng and Yichao Zhang and Yifan Shi and Yiliang Xiong and Ying He and Ying Tang and Yishi Piao and Yisong Wang and Yixuan Tan and Yiyang Ma and Yiyuan Liu and Yongqiang Guo and Yu Wu and Yuan Ou and Yuchen Zhu and Yuduan Wang and Yue Gong and Yuheng Zou and Yujia He and Yukun Zha and Yunfan Xiong and Yunxian Ma and Yuting Yan and Yuxiang Luo and Yuxiang You and Yuxuan Liu and Yuyang Zhou and Z. F. Wu and Z. Z. Ren and Zehui Ren and Zhangli Sha and Zhe Fu and Zhean Xu and Zhen Huang and Zhen Zhang and Zhenda Xie and Zhengyan Zhang and Zhewen Hao and Zhibin Gou and Zhicheng Ma and Zhigang Yan and Zhihong Shao and Zhipeng Xu and Zhiyu Wu and Zhongyu Zhang and Zhuoshu Li and Zihui Gu and Zijia Zhu and Zijun Liu and Zilin Li and Ziwei Xie and Ziyang Song and Ziyi Gao and Zizheng Pan},
journal={arXiv preprint arXiv:2412.19437},
year={2024}
}
@inproceedings{he-etal:2016deep,
title={Deep residual learning for image recognition},
author={He, Kaiming and Zhang, Xiangyu and Ren, Shaoqing and Sun, Jian},
booktitle={Proceedings of the IEEE conference on computer vision and pattern recognition},
pages={770--778},
year={2016}
}
@inproceedings{wang-etal:2019learning,
title={Learning Deep Transformer Models for Machine Translation},
author={Wang, Qiang and Li, Bei and Xiao, Tong and Zhu, Jingbo and Li, Changliang and Wong, Derek F and Chao, Lidia S},
booktitle={Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics},
pages={1810--1822},
year={2019}
}
@inproceedings{baevski-etal:2019adaptive,
title={Adaptive Input Representations for Neural Language Modeling},
author={Alexei Baevski and Michael Auli},
booktitle={International Conference on Learning Representations},
year={2019}
}
@inproceedings{lu-etal:2020understanding,
title={Understanding and Improving Transformer From a Multi-Particle Dynamic System Point of View.},
author={Lu, Yiping and Li, Zhuohan and He, Di and Sun, Zhiqing and Dong, Bin and Qin, Tao and Wang, Liwei and Liu, Tie-yan},
booktitle={ICLR 2020 Workshop on Integration of Deep Neural Models and Differential Equations},
year={2020}
}
@article{strang:1968construction,
title={On the construction and comparison of difference schemes},
author={Strang, Gilbert},
journal={SIAM journal on numerical analysis},
volume={5},
number={3},
pages={506--517},
year={1968},
publisher={SIAM}
}
@inproceedings{li-etal:2022ode,
title={ODE Transformer: An Ordinary Differential Equation-Inspired Model for Sequence Generation},
author={Li, Bei and Du, Quan and Zhou, Tao and Jing, Yi and Zhou, Shuhan and Zeng, Xin and Xiao, Tong and Zhu, Jingbo and Liu, Xuebo and Zhang, Min},
booktitle={Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)},
pages={8335--8351},
year={2022}
}
@article{li-etal:2024predictor,
title={Predictor-corrector enhanced transformers with exponential moving average coefficient learning},
author={Li, Bei and Zheng, Tong and Wang, Rui and Liu, Jiahao and Guo, Junliang and Tan, Xu and Xiao, Tong and Zhu, JingBo and Wang, Jingang and Cai, Xunliang},
journal={Advances in Neural Information Processing Systems},
volume={37},
pages={20358--20382},
year={2024}
}
@inproceedings{liu-etal:2025iiet,
title={IIET: Efficient Numerical Transformer via Implicit Iterative Euler Method},
author={Liu, Xinyu and Li, Bei and Liu, Jiahao and Ruan, Junhao and Jiao, Kechen and Tang, Hongyin and Wang, Jingang and Xiao, Tong and Zhu, Jingbo},
booktitle={Proceedings of the 2025 Conference on Empirical Methods in Natural Language Processing},
pages={8955--8969},
year={2025}
}
@book{hairer-and-wanner:1996solving,
title={Solving Ordinary Differential Equations II},
author={Hairer, Ernst and Wanner, Gerhard},
year={1996},
publisher={Cambridge university press}
}
@article{neyshabur:2017implicit,
title={Implicit regularization in deep learning},
author={Neyshabur, Behnam},
journal={arXiv preprint arXiv:1709.01953},
year={2017}
}
@inproceedings{miyato-etal:2018spectral,
title={Spectral Normalization for Generative Adversarial Networks},
author={Miyato, Takeru and Kataoka, Toshiki and Koyama, Masanori and Yoshida, Yuichi},
booktitle={International Conference on Learning Representations},
year={2018}
}
@article{gouk-etal:2021regularisation,
title={Regularisation of neural networks by enforcing lipschitz continuity},
author={Gouk, Henry and Frank, Eibe and Pfahringer, Bernhard and Cree, Michael J},
journal={Machine Learning},
volume={110},
number={2},
pages={393--416},
year={2021},
publisher={Springer}
}
@article{haber-and-ruthotto:2017stable,
title={Stable architectures for deep neural networks},
author={Haber, Eldad and Ruthotto, Lars},
journal={Inverse problems},
volume={34},
number={1},
year={2017},
publisher={IOP Publishing}
}
@inproceedings{dehghani-etal:2018universal,
title={Universal Transformers},
author={Mostafa Dehghani and Stephan Gouws and Oriol Vinyals and Jakob Uszkoreit and Lukasz Kaiser},
booktitle={International Conference on Learning Representations},
year={2019}
}
@inproceedings{lan-etal:2020albert,
title={ALBERT: A Lite BERT for Self-supervised Learning of Language Representations},
author={Zhenzhong Lan and Mingda Chen and Sebastian Goodman and Kevin Gimpel and Piyush Sharma and Radu Soricut},
booktitle={International Conference on Learning Representations},
year={2020}
}
@inproceedings{huang-etal:2017densely,
title={Densely connected convolutional networks},
author={Huang, Gao and Liu, Zhuang and Van Der Maaten, Laurens and Weinberger, Kilian Q},
booktitle={Proceedings of the IEEE conference on computer vision and pattern recognition},
pages={4700--4708},
year={2017}
}
@inproceedings{wang-etal:2018multi,
title={Multi-layer representation fusion for neural machine translation},
author={Wang, Qiang and Li, Fuxue and Xiao, Tong and Li, Yanyang and Li, Yinqiao and Zhu, Jingbo},
booktitle={Proceedings of the 27th international conference on computational linguistics},
pages={3015--3026},
year={2018}
}
@article{chen-etal:2018neural,
title={Neural ordinary differential equations},
author={Chen, Ricky TQ and Rubanova, Yulia and Bettencourt, Jesse and Duvenaud, David K},
journal={Advances in neural information processing systems},
volume={31},
year={2018}
}
@article{coddington:1955theory,
title={Theory of Ordinary Differential Equations},
author={Coddington, EA},
journal={McGraw-Hill Book Company},
year={1955}
}
@book{tao:2011introduction,
title={An introduction to measure theory},
author={Tao, Terence},
volume={126},
year={2011},
publisher={American Mathematical Society}
}
@article{hoogeboom-etal:2019integer,
title={Integer discrete flows and lossless compression},
author={Hoogeboom, Emiel and Peters, Jorn and Van Den Berg, Rianne and Welling, Max},
journal={Advances in Neural Information Processing Systems},
volume={32},
year={2019}
}
@article{tran2019-etal:discrete,
title={Discrete flows: Invertible generative models of discrete data},
author={Tran, Dustin and Vafa, Keyon and Agrawal, Kumar and Dinh, Laurent and Poole, Ben},
journal={Advances in Neural Information Processing Systems},
volume={32},
year={2019}
}
@article{dinh-etal:2014nice,
title={Nice: Non-linear independent components estimation},
author={Dinh, Laurent and Krueger, David and Bengio, Yoshua},
journal={arXiv preprint arXiv:1410.8516},
year={2014}
}
@inproceedings{dinh-etal:2017density,
title={Density estimation using Real NVP},
author={Dinh, Laurent and Sohl-Dickstein, Jascha and Bengio, Samy},
booktitle={International Conference on Learning Representations},
year={2017}
}
@article{papamakarios-etal:2017masked,
title={Masked autoregressive flow for density estimation},
author={Papamakarios, George and Pavlakou, Theo and Murray, Iain},
journal={Advances in neural information processing systems},
volume={30},
year={2017}
}
@article{kingma-etal:2016improved,
title={Improved variational inference with inverse autoregressive flow},
author={Kingma, Durk P and Salimans, Tim and Jozefowicz, Rafal and Chen, Xi and Sutskever, Ilya and Welling, Max},
journal={Advances in neural information processing systems},
volume={29},
year={2016}
}
@article{kingma-and-dhariwal:2018glow,
title={Glow: Generative flow with invertible 1x1 convolutions},
author={Kingma, Durk P and Dhariwal, Prafulla},
journal={Advances in neural information processing systems},
volume={31},
year={2018}
}
@inproceedings{behrmann-etal:2019invertible,
title={Invertible residual networks},
author={Behrmann, Jens and Grathwohl, Will and Chen, Ricky TQ and Duvenaud, David and Jacobsen, J{\"o}rn-Henrik},
booktitle={International conference on machine learning},
pages={573--582},
year={2019},
organization={PMLR}
}
@article{chen-etal:2019residual,
title={Residual flows for invertible generative modeling},
author={Chen, Ricky TQ and Behrmann, Jens and Duvenaud, David K and Jacobsen, J{\"o}rn-Henrik},
journal={Advances in neural information processing systems},
volume={32},
year={2019}
}
@book{batchelor:2000introduction,
title={An introduction to fluid dynamics},
author={Batchelor, George Keith},
year={2000},
publisher={Cambridge university press}
}
@article{croitoru-etal:2023diffusion,
title={Diffusion models in vision: A survey},
author={Croitoru, Florinel-Alin and Hondru, Vlad and Ionescu, Radu Tudor and Shah, Mubarak},
journal={IEEE transactions on pattern analysis and machine intelligence},
volume={45},
number={9},
pages={10850--10869},
year={2023},
publisher={IEEE}
}
@article{yang-etal:2023diffusion,
title={Diffusion models: A comprehensive survey of methods and applications},
author={Yang, Ling and Zhang, Zhilong and Song, Yang and Hong, Shenda and Xu, Runsheng and Zhao, Yue and Zhang, Wentao and Cui, Bin and Yang, Ming-Hsuan},
journal={ACM computing surveys},
volume={56},
number={4},
pages={1--39},
year={2023},
publisher={ACM New York, NY, USA}
}
@misc{song:2021generative,
author = {Yang Song},
title = {Generative Modeling by Estimating Gradients of the Data Distribution},
year = {2021},
url = {https://yang-song.net/blog/2021/score/}
}
@misc{dieleman:2023perspectives,
author = {Sander Dieleman},
title = {Perspectives on diffusion},
year = {2023},
url = {https://sander.ai/2023/07/20/perspectives.html}
}
@inproceedings{kingma-and-gao:2023understanding,
title={Understanding Diffusion Objectives as the {ELBO} with Simple Data Augmentation},
author={Diederik P Kingma and Ruiqi Gao},
booktitle={Thirty-seventh Conference on Neural Information Processing Systems},
year={2023}
}
@article{luo:2022understand,
title={Understanding Diffusion Models: A Unified Perspective},
author={Calvin Luo},
year={2022},
journal={arXiv}
}
@article{kingma-and-welling:2019introduction,
title={An introduction to variational autoencoders},
author={Kingma, Diederik P and Welling, Max},
journal={Foundations and Trends{\textregistered} in Machine Learning},
volume={12},
number={4},
pages={307--392},
year={2019},
publisher={Now Publishers}
}
@article{kingma-and-welling:2013auto,
title={Auto-encoding variational bayes},
author={Kingma, Diederik P and Welling, Max},
journal={arXiv preprint arXiv:1312.6114},
year={2013}
}
@article{bank-etal:2023autoencoders,
title={Autoencoders},
author={Bank, Dor and Koenigstein, Noam and Giryes, Raja},
journal={Machine learning for data science handbook: data mining and knowledge discovery handbook},
pages={353--374},
year={2023},
publisher={Springer}
}
@book{oksendal:2013stochastic,
title={Stochastic differential equations: an introduction with applications},
author={Oksendal, Bernt},
year={2013},
publisher={Springer Science \& Business Media}
}
@book{karatzas-and-shreve:2014brownian,
title={Brownian motion and stochastic calculus},
author={Karatzas, Ioannis and Shreve, Steven},
year={2014},
publisher={springer}
}
@article{anderson:1982reverse,
title = {Reverse-time diffusion equation models},
journal = {Stochastic Processes and their Applications},
volume = {12},
number = {3},
pages = {313-326},
year = {1982},
author = {Brian D.O. Anderson}
}
@article{hyvarinen-and-dayan:2005estimation,
title={Estimation of non-normalized statistical models by score matching.},
author={Hyv{\"a}rinen, Aapo and Dayan, Peter},
journal={Journal of Machine Learning Research},
volume={6},
number={4},
year={2005}
}
@article{vincent:2011connection,
title={A connection between score matching and denoising autoencoders},
author={Vincent, Pascal},
journal={Neural computation},
volume={23},
number={7},
pages={1661--1674},
year={2011},
publisher={MIT Press}
}
@article{kloeden-and-pearson:1977numerical,
title={The numerical solution of stochastic differential equations},
author={Kloeden, Peter E and Pearson, RA},
journal={The ANZIAM Journal},
volume={20},
number={1},
pages={8--12},
year={1977},
publisher={Cambridge University Press}
}
@article{song-and-ermon:2019generative,
title={Generative modeling by estimating gradients of the data distribution},
author={Song, Yang and Ermon, Stefano},
journal={Advances in neural information processing systems},
volume={32},
year={2019}
}
@article{ho-etal:2020denoising,
title={Denoising diffusion probabilistic models},
author={Ho, Jonathan and Jain, Ajay and Abbeel, Pieter},
journal={Advances in neural information processing systems},
volume={33},
pages={6840--6851},
year={2020}
}
@inproceedings{song-etal:2021scorebased,
title={Score-Based Generative Modeling through Stochastic Differential Equations},
author={Yang Song and Jascha Sohl-Dickstein and Diederik P Kingma and Abhishek Kumar and Stefano Ermon and Ben Poole},
booktitle={International Conference on Learning Representations},
year={2021}
}
@book{kloeden-and-platen:1992numerical,
title={Numerical Solution of Stochastic Differential Equations},
author={Peter E. Kloeden and Eckhard Platen},
year={1992},
publisher={Springer Berlin}
}
@book{robert-etal:1999monte,
title={Monte Carlo statistical methods},
author={Robert, Christian P and Casella, George and Casella, George},
volume={2},
year={1999},
publisher={Springer}
}
@inproceedings{lipman-etal:2023flow,
title={Flow Matching for Generative Modeling},
author={Yaron Lipman and Ricky T. Q. Chen and Heli Ben-Hamu and Maximilian Nickel and Matthew Le},
booktitle={The Eleventh International Conference on Learning Representations },
year={2023}
}
@inproceedings{liu-etal:2023flow,
title={Flow Straight and Fast: Learning to Generate and Transfer Data with Rectified Flow},
author={Xingchao Liu and Chengyue Gong and qiang liu},
booktitle={The Eleventh International Conference on Learning Representations },
year={2023}
}
@inproceedings{albergo-etal:2023building,
title={Building Normalizing Flows with Stochastic Interpolants},
author={Michael Samuel Albergo and Eric Vanden-Eijnden},
booktitle={The Eleventh International Conference on Learning Representations },
year={2023}
}
@article{lu-etal:2022dpm,
title={DPM-solver: A fast ode solver for diffusion probabilistic model sampling in around 10 steps},
author={Lu, Cheng and Zhou, Yuhao and Bao, Fan and Chen, Jianfei and Li, Chongxuan and Zhu, Jun},
journal={Advances in neural information processing systems},
volume={35},
pages={5775--5787},
year={2022}
}
@article{lu-etal:2025dpmpp,
title={Dpm-solver++: Fast solver for guided sampling of diffusion probabilistic models},
author={Lu, Cheng and Zhou, Yuhao and Bao, Fan and Chen, Jianfei and Li, Chongxuan and Zhu, Jun},
journal={Machine Intelligence Research},
pages={1--22},
year={2025},
publisher={Springer}
}
@article{ren-etal:2025fast,
title={Fast solvers for discrete diffusion models: Theory and applications of high-order algorithms},
author={Ren, Yinuo and Chen, Haoxuan and Zhu, Yuchen and Guo, Wei and Chen, Yongxin and Rotskoff, Grant M and Tao, Molei and Ying, Lexing},
journal={arXiv preprint arXiv:2502.00234},
year={2025}
}
@inproceedings{zhou-etal:2024fast,
title={Fast ode-based sampling for diffusion models in around 5 steps},
author={Zhou, Zhenyu and Chen, Defang and Wang, Can and Chen, Chun},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
pages={7777--7786},
year={2024}
}
@inproceedings{zhang-and-chenl:2022fast,
title={Fast Sampling of Diffusion Models with Exponential Integrator},
author={Qinsheng Zhang and Yongxin Chen},
booktitle={NeurIPS 2022 Workshop on Score-Based Methods},
year={2022}
}
@article{zhao-etal:2023unipc,
title={Unipc: A unified predictor-corrector framework for fast sampling of diffusion models},
author={Zhao, Wenliang and Bai, Lujia and Rao, Yongming and Zhou, Jie and Lu, Jiwen},
journal={Advances in Neural Information Processing Systems},
volume={36},
pages={49842--49869},
year={2023}
}
@inproceedings{liu-etal:2022pseudo,
title={Pseudo Numerical Methods for Diffusion Models on Manifolds},
author={Luping Liu and Yi Ren and Zhijie Lin and Zhou Zhao},
booktitle={International Conference on Learning Representations},
year={2022}
}
@article{karras-etal:2022elucidating,
title={Elucidating the design space of diffusion-based generative models},
author={Karras, Tero and Aittala, Miika and Aila, Timo and Laine, Samuli},
journal={Advances in neural information processing systems},
volume={35},
pages={26565--26577},
year={2022}
}
@article{zhang-etal:2025:flow,
title={Flow Straighter and Faster: Efficient One-Step Generative Modeling via MeanFlow on Rectified Trajectories},
author={Xinxi Zhang and Shiwei Tan and Quang Nguyen and Quan Dao and Ligong Han and Xiaoxiao He and Tunyu Zhang and Alen Mrdovic and Dimitris Metaxas},
year={2025},
journal={arXiv preprint arXiv:2511.23342}
}
@inproceedings{yang-etal:2025text,
title={Text-to-Image Rectified Flow as Plug-and-Play Priors},
author={Xiaofeng Yang and Chen Cheng and Xulei Yang and Fayao Liu and Guosheng Lin},
booktitle={The Thirteenth International Conference on Learning Representations},
year={2025}
}
@inproceedings{esser-etal:2024scaling,
title={Scaling rectified flow transformers for high-resolution image synthesis},
author={Patrick Esser and Sumith Kulal and Andreas Blattmann and Rahim Entezari and Jonas M{\" u}ller and Harry Saini and Yam Levi and Dominik Lorenz and Axel Sauer and Frederic Boesel and Dustin Podell and Tim Dockhorn and Zion English and Kyle Lacey and Alex Goodwin and Yannik Marek and Robin Rombach},
booktitle={Forty-first international conference on machine learning},
year={2024}
}
@article{greenberg-etal:2025demystifying,
title={Demystifying flux architecture},
author={Greenberg, Or},
journal={arXiv preprint arXiv:2507.09595},
year={2025}
}
@inproceedings{yan-etal:2024perflow,
title={Pe{RF}low: Piecewise Rectified Flow as Universal Plug-and-Play Accelerator},
author={Hanshu Yan and Xingchao Liu and Jiachun Pan and Jun Hao Liew and qiang liu and Jiashi Feng},
booktitle={The Thirty-eighth Annual Conference on Neural Information Processing Systems},
year={2024}
}
@article{saini-etal:2025rectified,
title={Rectified-CFG++ for Flow Based Models},
author={Saini, Shreshth and Gupta, Shashank and Bovik, Alan C},
journal={arXiv preprint arXiv:2510.07631},
year={2025}
}
@inproceedings{song-etal:2023consistency,
title={Consistency Models},
author={Song, Yang and Dhariwal, Prafulla and Chen, Mark and Sutskever, Ilya},
booktitle={International Conference on Machine Learning},
pages={32211--32252},
year={2023},
organization={PMLR}
}
@inproceedings{sauer-etal:2024adversarial,
title={Adversarial diffusion distillation},
author={Sauer, Axel and Lorenz, Dominik and Blattmann, Andreas and Rombach, Robin},
booktitle={European Conference on Computer Vision},
pages={87--103},
year={2024},
organization={Springer}
}
@article{luo-etal:2023latent,
title={Latent consistency models: Synthesizing high-resolution images with few-step inference},
author={Luo, Simian and Tan, Yiqin and Huang, Longbo and Li, Jian and Zhao, Hang},
journal={arXiv preprint arXiv:2310.04378},
year={2023}
}
@article{luo-etal:2023lcm,
title={Lcm-lora: A universal stable-diffusion acceleration module},
author={Luo, Simian and Tan, Yiqin and Patil, Suraj and Gu, Daniel and Von Platen, Patrick and Passos, Apolin{\' a}rio and Huang, Longbo and Li, Jian and Zhao, Hang},
journal={arXiv preprint arXiv:2311.05556},
year={2023}
}
@article{zheng-etal:2024trajectory,
title={Trajectory consistency distillation},
author={Zheng, Jianbin and Hu, Minghui and Fan, Zhongyi and Wang, Chaoyue and Ding, Changxing and Tao, Dacheng and Cham, Tat-Jen},
journal={CoRR},
year={2024}
}
@article{wang-etal:2024phased,
title={Phased consistency models},
author={Wang, Fu-Yun and Huang, Zhaoyang and Bergman, Alexander and Shen, Dazhong and Gao, Peng and Lingelbach, Michael and Sun, Keqiang and Bian, Weikang and Song, Guanglu and Liu, Yu and others},
journal={Advances in neural information processing systems},
volume={37},
pages={83951--84009},
year={2024}
}
@inproceedings{lu-and-song:2025simplifying,
title={Simplifying, Stabilizing and Scaling Continuous-time Consistency Models},
author={Cheng Lu and Yang Song},
booktitle={The Thirteenth International Conference on Learning Representations},
year={2025}
}
@article{shen-etal:2025efficient,
title={Efficient diffusion models: A survey},
author={Hui Shen and Jingxuan Zhang and Boning Xiong and Rui Hu and Shoufa Chen and Zhongwei Wan and Xin Wang and Yu Zhang and Zixuan Gong and Guangyin Bao and Chaofan Tao and Yongfeng Huang and Ye Yuan and Mi Zhang},
journal={arXiv preprint arXiv:2502.06805},
year={2025}
}
@article{radford-etal:2018improving,
title={Improving language understanding by generative pre-training},
author={Radford, Alec and Narasimhan, Karthik and Salimans, Tim and Sutskever, Ilya},
year={2018},
journal={OpenAI},
publisher={OpenAI}
}
@inproceedings{gu-etal:2018non,
title={Non-autoregressive neural machine translation},
author={Gu, J and Bradbury, J and Xiong, C and Li, VOK and Socher, R},
booktitle={International Conference on Learning Representations (ICLR)},
year={2018}
}
@article{li-etal:2022diffusion,
title={Diffusion-lm improves controllable text generation},
author={Li, Xiang and Thickstun, John and Gulrajani, Ishaan and Liang, Percy S and Hashimoto, Tatsunori B},
journal={Advances in neural information processing systems},
volume={35},
pages={4328--4343},
year={2022}
}
@article{strudel-etal:2022self,
title={Self-conditioned embedding diffusion for text generation},
author={Strudel, Robin and Tallec, Corentin and Altch{\'e}, Florent and Du, Yilun and Ganin, Yaroslav and Mensch, Arthur and Grathwohl, Will and Savinov, Nikolay and Dieleman, Sander and Sifre, Laurent and Leblond, R{\' e}mi},
journal={arXiv preprint arXiv:2211.04236},
year={2022}
}
@inproceedings{gong-etal:2023diffuseq,
title={DiffuSeq: Sequence to Sequence Text Generation with Diffusion Models},
author={Shansan Gong and Mukai Li and Jiangtao Feng and Zhiyong Wu and Lingpeng Kong},
booktitle={The Eleventh International Conference on Learning Representations },
year={2023}
}
@inproceedings{mikolov-etal:2013efficient,
title={Efficient Estimation of Word Representations in Vector Space},
author={Mikolov, Tomas and Chen, Kai and Corrado, Greg and Dean, Jeffrey},
booktitle={Proceedings of the International Conference on Learning Representations (ICLR 2013)},
year={2013}
}
@inproceedings{mikolov-etal:2013distributed,
author = {Mikolov, Tomas and Sutskever, Ilya and Chen, Kai and Corrado, Greg and Dean, Jeffrey},
title = {Distributed Representations of Words and Phrases and Their Compositionality},
year = {2013},
booktitle = {Proceedings of the 26th International Conference on Neural Information Processing Systems - Volume 2},
pages = {3111-3119}
}
@inproceedings{devlin-etal:2019bert,
title={BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding},
author={Devlin, Jacob and Chang, Ming-Wei and Lee, Kenton and Toutanova, Kristina},
booktitle={Proceedings of the 2019 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, Volume 1 (Long and Short Papers)},
pages={4171--4186},
year={2019}
}
@inproceedings{rombach-etal:2022high,
title={High-resolution image synthesis with latent diffusion models},
author={Rombach, Robin and Blattmann, Andreas and Lorenz, Dominik and Esser, Patrick and Ommer, Bj{\"o}rn},
booktitle={Proceedings of the IEEE/CVF conference on computer vision and pattern recognition},
pages={10684--10695},
year={2022}
}
@article{barrault-etal:2024large,
title={Large concept models: Language modeling in a sentence representation space},
author={Barrault, Lo{\"\i}c and Duquenne, Paul-Ambroise and Elbayad, Maha and Kozhevnikov, Artyom and Alastruey, Belen and Andrews, Pierre and Coria, Mariano and Couairon, Guillaume and Costa-juss{\`a}, Marta R and Dale, David and others},
journal={arXiv preprint arXiv:2412.08821},
year={2024}
}
@article{brown-etal:1993mathematics,
title = "The Mathematics of Statistical Machine Translation: Parameter Estimation",
author="Brown, Peter F. and Della Pietra, Stephen A. and Della Pietra, Vincent J. and Mercer, Robert L.",
journal = "Computational Linguistics",
volume = "19",
number = "2",
year = "1993",
pages = "263--311"
}
@article{xiao-etal:2023survey,
title={A survey on non-autoregressive generation for neural machine translation and beyond},
author={Xiao, Yisheng and Wu, Lijun and Guo, Junliang and Li, Juntao and Zhang, Min and Qin, Tao and Liu, Tie-yan},
journal={IEEE Transactions on Pattern Analysis and Machine Intelligence},
volume={45},
number={10},
pages={11407--11427},
year={2023},
publisher={IEEE}
}
@misc{li-etal:2025fixed,
title={Beyond Fixed: Training-Free Variable-Length Denoising for Diffusion Large Language Models},
author={Jinsong Li and Xiaoyi Dong and Yuhang Zang and Yuhang Cao and Jiaqi Wang and Dahua Lin},
year={2025},
eprint={2508.00819},
archivePrefix={arXiv}
}
@misc{yang-etal:2025diffusion,
title={Diffusion LLM with Native Variable Generation Lengths: Let [EOS] Lead the Way},
author={Yicun Yang and Cong Wang and Shaobo Wang and Zichen Wen and Biqing Qi and Hanlin Xu and Linfeng Zhang},
year={2025},
eprint={2510.24605},
archivePrefix={arXiv}
}
@article{austin-etal:2021structured,
title={Structured denoising diffusion models in discrete state-spaces},
author={Austin, Jacob and Johnson, Daniel D and Ho, Jonathan and Tarlow, Daniel and Van Den Berg, Rianne},
journal={Advances in neural information processing systems},
volume={34},
pages={17981--17993},
year={2021}
}
@article{sahoo-etal:2024simple,
title={Simple and effective masked diffusion language models},
author={Sahoo, Subham and Arriola, Marianne and Schiff, Yair and Gokaslan, Aaron and Marroquin, Edgar and Chiu, Justin and Rush, Alexander and Kuleshov, Volodymyr},
journal={Advances in Neural Information Processing Systems},
volume={37},
pages={130136--130184},
year={2024}
}
@article{nie-etal:2025large,
title={Large language diffusion models},
author={Nie, Shen and Zhu, Fengqi and You, Zebin and Zhang, Xiaolu and Ou, Jingyang and Hu, Jun and Zhou, Jun and Lin, Yankai and Wen, Ji-Rong and Li, Chongxuan},
journal={arXiv preprint arXiv:2502.09992},
year={2025}
}
@article{zhang-etal:2025corrective,
title={Corrective Diffusion Language Models},
author={Zhang, Shuibai and Peng, Fred Zhangzhi and Zhang, Yiheng and Pan, Jin and Chrysos, Grigorios G},
journal={arXiv preprint arXiv:2512.15596},
year={2025}
}
@inproceedings{rutte-etal:2025generalized,
title={Generalized Interpolating Discrete Diffusion},
author={R{\"u}tte, Dimitri von and Janis Fluri and Yuhui Ding and Antonio Orvieto and Bernhard Sch{\"o}lkopf and Thomas Hofmann},
booktitle={Forty-second International Conference on Machine Learning},
year={2025}
}
@inproceedings{clark-etal:2019electra,
title={ELECTRA: Pre-training Text Encoders as Discriminators Rather Than Generators},
author={Clark, Kevin and Luong, Minh-Thang and Le, Quoc V and Manning, Christopher D},
booktitle={Proceedings of International Conference on Learning Representations},
year={2019}
}
@article{raffel-etal:2020exploring,
title={Exploring the limits of transfer learning with a unified text-to-text transformer.},
author = {Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu},
journal={Journal of Machine Learning Research},
volume={21},
number={140},
pages={1--67},
year={2020}
}
@article{joshi-etal:2020spanbert,
title={SpanBERT: Improving pre-training by representing and predicting spans},
author={Joshi, Mandar and Chen, Danqi and Liu, Yinhan and Weld, Daniel S and Zettlemoyer, Luke and Levy, Omer},
journal={Transactions of the association for computational linguistics},
volume={8},
pages={64--77},
year={2020},
publisher={MIT Press}
}
@article{gu-etal:2019levenshtein,
title={Levenshtein transformer},
author={Gu, Jiatao and Wang, Changhan and Zhao, Junbo},
journal={Advances in neural information processing systems},
volume={32},
year={2019}
}
@article{reid-etal:2022diffuser,
title={Diffuser: Discrete diffusion via edit-based reconstruction},
author={Reid, Machel and Hellendoorn, Vincent J and Neubig, Graham},
journal={arXiv preprint arXiv:2210.16886},
year={2022}
}
@inproceedings{tay-etal:2023ul2,
title={{UL}2: Unifying Language Learning Paradigms},
author={Yi Tay and Mostafa Dehghani and Vinh Q. Tran and Xavier Garcia and Jason Wei and Xuezhi Wang and Hyung Won Chung and Dara Bahri and Tal Schuster and Steven Zheng and Denny Zhou and Neil Houlsby and Donald Metzler},
booktitle={The Eleventh International Conference on Learning Representations },
year={2023}
}
@inproceedings{lewis-etal:2020bart,
title={BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension},
author={Lewis, Mike and Liu, Yinhan and Goyal, Naman and Ghazvininejad, Marjan and Mohamed, Abdelrahman and Levy, Omer and Stoyanov, Veselin and Zettlemoyer, Luke},
booktitle={Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics},
pages={7871--7880},
year={2020}
}
@inproceedings{zhu-etal:2020freelb,
title={FreeLB: Enhanced Adversarial Training for Natural Language Understanding},
author={Chen Zhu and Yu Cheng and Zhe Gan and Siqi Sun and Tom Goldstein and Jingjing Liu},
booktitle={International Conference on Learning Representations},
year={2020}
}
@inproceedings{jang-etal:2017categorical,
title={Categorical Reparameterization with Gumbel-Softmax},
author={Eric Jang and Shixiang Gu and Ben Poole},
booktitle={International Conference on Learning Representations},
year={2017}
}
@inproceedings{maddison-etal:2017the,
title={The Concrete Distribution: A Continuous Relaxation of Discrete Random Variables},
author={Chris J. Maddison and Andriy Mnih and Yee Whye Teh},
booktitle={International Conference on Learning Representations},
year={2017}
}
@article{hoogeboom-etal:2021argmax,
title={Argmax flows and multinomial diffusion: Learning categorical distributions},
author={Hoogeboom, Emiel and Nielsen, Didrik and Jaini, Priyank and Forr{\'e}, Patrick and Welling, Max},
journal={Advances in neural information processing systems},
volume={34},
pages={12454--12465},
year={2021}
}
@inproceedings{lou-etal:2024discrete,
title={Discrete diffusion modeling by estimating the ratios of the data distribution},
author={Lou, Aaron and Meng, Chenlin and Ermon, Stefano},
booktitle={Proceedings of the 41st International Conference on Machine Learning},
pages={32819--32848},
year={2024}
}
@inproceedings{lee-etal:2018deterministic,
title={Deterministic Non-Autoregressive Neural Sequence Modeling by Iterative Refinement},
author={Lee, Jason and Mansimov, Elman and Cho, Kyunghyun},
booktitle={Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing},
pages={1173--1182},
year={2018}
}
@inproceedings{ghazvininejad-etal:2019mask,
title={Mask-Predict: Parallel Decoding of Conditional Masked Language Models},
author={Ghazvininejad, Marjan and Levy, Omer and Liu, Yinhan and Zettlemoyer, Luke},
booktitle={Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP)},
pages={6112--6121},
year={2019}
}
@inproceedings{koh-etal:2025conditional,
title={Conditional [MASK] Discrete Diffusion Language Model},
author={Koh, Hyukhun and Jhang, Minha and Kim, Dohyung and Lee, Sangmook and Jung, Kyomin},
booktitle={Proceedings of the 2025 Conference on Empirical Methods in Natural Language Processing},
pages={8910--8934},
year={2025}
}
@article{gat-etal:2024discrete,
title={Discrete flow matching},
author={Gat, Itai and Remez, Tal and Shaul, Neta and Kreuk, Felix and Chen, Ricky TQ and Synnaeve, Gabriel and Adi, Yossi and Lipman, Yaron},
journal={Advances in Neural Information Processing Systems},
volume={37},
pages={133345--133385},
year={2024}
}
@article{monsefi-etal:2025fs,
title={Fs-dfm: Fast and accurate long text generation with few-step diffusion language models},
author={Monsefi, Amin Karimi and Bhendawade, Nikhil and Ciosici, Manuel Rafael and Culver, Dominic and Zhang, Yizhe and Belousova, Irina},
journal={arXiv preprint arXiv:2509.20624},
year={2025}
}
@inproceedings{peebles-and-xie:2023scalable,
title={Scalable diffusion models with transformers},
author={Peebles, William and Xie, Saining},
booktitle={Proceedings of the IEEE/CVF international conference on computer vision},
pages={4195--4205},
year={2023}
}
@article{xiao-and-zhu:2023introduction,
title={Introduction to transformers: an nlp perspective},
author={Xiao, Tong and Zhu, Jingbo},
journal={arXiv preprint arXiv:2311.17633},
year={2023}
}
@article{ho-eatl:2020denoising,
title={Denoising diffusion probabilistic models},
author={Ho, Jonathan and Jain, Ajay and Abbeel, Pieter},
journal={Advances in neural information processing systems},
volume={33},
pages={6840--6851},
year={2020}
}
@inproceedings{salimans-and-ho:2022progressive,
title={Progressive Distillation for Fast Sampling of Diffusion Models},
author={Tim Salimans and Jonathan Ho},
booktitle={International Conference on Learning Representations},
year={2022}
}
@inproceedings{campbell-etal:2024generative,
title={Generative Flows on Discrete State-Spaces: Enabling Multimodal Flows with Applications to Protein Co-Design},
author={Campbell, Andrew and Yim, Jason and Barzilay, Regina and Rainforth, Tom and Jaakkola, Tommi},
booktitle={International Conference on Machine Learning},
pages={5453--5512},
year={2024},
organization={PMLR}
}
@inproceedings{shaul-etal:2025flow,
title={Flow Matching with General Discrete Paths: A Kinetic-Optimal Perspective},
author={Neta Shaul and Itai Gat and Marton Havasi and Daniel Severo and Anuroop Sriram and Peter Holderrieth and Brian Karrer and Yaron Lipman and Ricky T. Q. Chen},
booktitle={The Thirteenth International Conference on Learning Representations},
year={2025}
}
@article{gillespie:1977exact,
title={Exact stochastic simulation of coupled chemical reactions},
author={Gillespie, Daniel T},
journal={The journal of physical chemistry},
volume={81},
number={25},
pages={2340--2361},
year={1977},
publisher={ACS Publications}
}
@article{gillespie:2001approximate,
title={Approximate accelerated stochastic simulation of chemically reacting systems},
author={Gillespie, Daniel T},
journal={The Journal of chemical physics},
volume={115},
number={4},
pages={1716--1733},
year={2001},
publisher={AIP Publishing}
}
@article{lecun-etal:2006tutorial,
title={A tutorial on energy-based learning},
author={LeCun, Yann and Chopra, Sumit and Hadsell, Raia and Ranzato, M and Huang, Fujie and others},
journal={Predicting structured data},
volume={1},
year={2006}
}
@article{lipman-etal:2024flow,
title={Flow matching guide and code},
author={Lipman, Yaron and Havasi, Marton and Holderrieth, Peter and Shaul, Neta and Le, Matt and Karrer, Brian and Chen, Ricky TQ and Lopez-Paz, David and Ben-Hamu, Heli and Gat, Itai},
journal={arXiv preprint arXiv:2412.06264},
year={2024}
}
@inproceedings{zheng-etal:2023dpmsolverv,
title={{DPM}-Solver-v3: Improved Diffusion {ODE} Solver with Empirical Model Statistics},
author={Kaiwen Zheng and Cheng Lu and Jianfei Chen and Jun Zhu},
booktitle={Thirty-seventh Conference on Neural Information Processing Systems},
year={2023},
url={https://openreview.net/forum?id=9fWKExmKa0}
}
@article{Guo-etal:2025DeepSeekR1,
author = {Daya Guo and
Dejian Yang and
Haowei Zhang and
Junxiao Song and
Peiyi Wang and
Qihao Zhu and
Runxin Xu and
Ruoyu Zhang and
Shirong Ma and
Xiao Bi and
Xiaokang Zhang and
Xingkai Yu and
Yu Wu and
Z. F. Wu and
Zhibin Gou and
Zhihong Shao and
Zhuoshu Li and
Ziyi Gao and
Aixin Liu and
Bing Xue and
Bingxuan Wang and
Bochao Wu and
Bei Feng and
Chengda Lu and
Chenggang Zhao and
Chengqi Deng and
Chong Ruan and
Damai Dai and
Deli Chen and
Dongjie Ji and
Erhang Li and
Fangyun Lin and
Fucong Dai and
Fuli Luo and
Guangbo Hao and
Guanting Chen and
Guowei Li and
Hao Zhang and
Hanwei Xu and
Honghui Ding and
Huazuo Gao and
Hui Qu and
Hui Li and
Jianzhong Guo and
Jiashi Li and
Jingchang Chen and
Jingyang Yuan and
Jinhao Tu and
Junjie Qiu and
Junlong Li and
J. L. Cai and
Jiaqi Ni and
Jian Liang and
Jin Chen and
Kai Dong and
Kai Hu and
Kaichao You and
Kaige Gao and
Kang Guan and
Kexin Huang and
Kuai Yu and
Lean Wang and
Lecong Zhang and
Liang Zhao and
Litong Wang and
Liyue Zhang and
Lei Xu and
Leyi Xia and
Mingchuan Zhang and
Minghua Zhang and
Minghui Tang and
Mingxu Zhou and
Meng Li and
Miaojun Wang and
Mingming Li and
Ning Tian and
Panpan Huang and
Peng Zhang and
Qiancheng Wang and
Qinyu Chen and
Qiushi Du and
Ruiqi Ge and
Ruisong Zhang and
Ruizhe Pan and
Runji Wang and
R. J. Chen and
R. L. Jin and
Ruyi Chen and
Shanghao Lu and
Shangyan Zhou and
Shanhuang Chen and
Shengfeng Ye and
Shiyu Wang and
Shuiping Yu and
Shunfeng Zhou and
Shuting Pan and
S. S. Li and
Shuang Zhou and
Shaoqing Wu and
Tao Yun and
Tian Pei and
Tianyu Sun and
Tao Wang and
Wangding Zeng and
Wen Liu and
Wenfeng Liang and
Wenjun Gao and
Wenqin Yu and
Wentao Zhang and
W. L. Xiao and
Wei An and
Xiaodong Liu and
Xiaohan Wang and
Xiaokang Chen and
Xiaotao Nie and
Xin Cheng and
Xin Liu and
Xin Xie and
Xingchao Liu and
Xinyu Yang and
Xinyuan Li and
Xuecheng Su and
Xuheng Lin and
X. Q. Li and
Xiangyue Jin and
Xiaojin Shen and
Xiaosha Chen and
Xiaowen Sun and
Xiaoxiang Wang and
Xinnan Song and
Xinyi Zhou and
Xianzu Wang and
Xinxia Shan and
Y. K. Li and
Y. Q. Wang and
Y. X. Wei and
Yang Zhang and
Yanhong Xu and
Yao Li and
Yao Zhao and
Yaofeng Sun and
Yaohui Wang and
Yi Yu and
Yichao Zhang and
Yifan Shi and
Yiliang Xiong and
Ying He and
Yishi Piao and
Yisong Wang and
Yixuan Tan and
Yiyang Ma and
Yiyuan Liu and
Yongqiang Guo and
Yuan Ou and
Yuduan Wang and
Yue Gong and
Yuheng Zou and
Yujia He and
Yunfan Xiong and
Yuxiang Luo and
Yuxiang You and
Yuxuan Liu and
Yuyang Zhou and
Y. X. Zhu and
Yanping Huang and
Yaohui Li and
Yi Zheng and
Yuchen Zhu and
Yunxian Ma and
Ying Tang and
Yukun Zha and
Yuting Yan and
Z. Z. Ren and
Zehui Ren and
Zhangli Sha and
Zhe Fu and
Zhean Xu and
Zhenda Xie and
Zhengyan Zhang and
Zhewen Hao and
Zhicheng Ma and
Zhigang Yan and
Zhiyu Wu and
Zihui Gu and
Zijia Zhu and
Zijun Liu and
Zilin Li and
Ziwei Xie and
Ziyang Song and
Zizheng Pan and
Zhen Huang and
Zhipeng Xu and
Zhongyu Zhang and
Zhen Zhang},
title = {DeepSeek-R1 incentivizes reasoning in LLMs through reinforcement learning},
journal = {Nat.},
volume = {645},
number = {8081},
pages = {633--638},
year = {2025},
url = {https://doi.org/10.1038/s41586-025-09422-z},
doi = {10.1038/S41586-025-09422-Z},
timestamp = {Sun, 02 Nov 2025 21:29:27 +0100},
biburl = {https://dblp.org/rec/journals/nature/GuoYZSWZXZMBZY025.bib},
bibsource = {dblp computer science bibliography, https://dblp.org}
}
@inproceedings{yang-etal:2024loopedTransformer,
title={Looped Transformers are Better at Learning Learning Algorithms},
author={Liu Yang and Kangwook Lee and Robert D Nowak and Dimitris Papailiopoulos},
booktitle={The Twelfth International Conference on Learning Representations},
year={2024},
url={https://openreview.net/forum?id=HHbRxoDTxE}
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
# Overleaf 使用说明
# Overleaf 使用说明
本文件夹可整体上传到 Overleaf,包含:
- 21 个可独立编译的 `figure-*.tex`
- `figure_preamble.tex`:文档类、中文支持及 TikZ/PGFPlots 依赖
- `figure_style.tex`:统一浅蓝色绘图样式
## 编译设置
在 Overleaf 的 **Menu / Settings** 中将编译器设置为 **XeLaTeX**
需要编译某一幅图时,将对应的 `figure-*.tex` 设置为主文档。例如:
```text
figure-autoencoder.tex
```
所有文件必须保持在同一目录,因为每幅图通过以下命令载入公共配置:
```latex
\input{figure_preamble.tex}
```
`figure_preamble.tex` 会继续载入:
```latex
\input{figure_style.tex}
```
把课程讲义中需要使用的图片放在这个目录中。
示例:
- `cover.jpg`:封面图片,模板会自动读取。
- `example.png`:正文图片示例,模板会自动读取。
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssep{1.3cm}
\def\bsep{2.5cm}
\tikzstyle{lnode} = [fig/module,minimum width=3.0cm,minimum height=0.6cm,inner sep=2pt]
\begin{scope}
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,2.5);
\node [anchor=north,align=left] (xlabel) at (13.3,-0.1) {\footnotesize{时间 $t$}};
\draw [Round Cap->,blue,thick,shorten > = 4pt,shorten < = 4pt,very thick,dashed,domain=1.5:12.0,samples=100] plot (\x,{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))});
\def\x{1.5}
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\node [circle,minimum size=5pt,inner sep=0,fill=blue] (point0) at (\x,\p) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px0) at (\x,0) {};
\node [anchor=north] (t0label) at (\x,-0.1) {\footnotesize{$t_0$}};
\draw [-,dashed] (point0) -- (px0);
\def\x{12.0}
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\node [circle,minimum size=5pt,inner sep=0,fill=blue] (point1) at (\x,\p) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px1) at (\x,0) {};
\node [anchor=north] (t1plabel) at (\x,-0.1) {\footnotesize{$t_1$}};
\draw [-,dashed] (point1) -- (px1);
\node [anchor=south,align=left] (odelabel) at (3.5,1.4) {\footnotesize{ODE 轨迹:}\\[-1pt] \footnotesize{$\frac{d\mathbf{x}(t)}{dt} = f(\mathbf{x}(t), \theta, t)$}};
\draw [->] (odelabel.south) -- ([yshift=-0.3cm]odelabel.south);
\node [anchor=south,align=left] (solutionlabel) at (11.5,1.5) {\footnotesize{得到终态 $\mathbf{x}(t_1)$}\\[-1pt] \footnotesize{并计算损失 $\mathcal{L}$}};
\draw [->] (12.0,1.5) -- (12.0,1.0);
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(a) 前向传播(从 $t_0$$t_1$ 积分 ODE 动力学)}};
\end{scope}
\begin{scope}[yshift=-10cm]
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,2.5);
\node [anchor=north,align=left] (xlabel) at (13.3,-0.1) {\footnotesize{时间 $t$}};
\draw [<-Round Cap,blue,thick,shorten > = 4pt,shorten < = 4pt,very thick,domain=1.5:12.0,samples=100] plot (\x,{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))});
\draw [<-Round Cap,red,shorten > = 4pt,shorten < = 4pt,very thick,domain=1.5:12.0,samples=100] plot (\x,{ - 1/300 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5) + 3});
\def\x{1.5}
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\pgfmathsetmacro{\a}{-1/300 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5) + 3}
\node [circle,minimum size=5pt,inner sep=0,fill=blue] (point0) at (\x,\p) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px0) at (\x,0) {};
\node [anchor=north] (t0label) at (\x,-0.1) {\footnotesize{$t_0$}};
\draw [-,dashed] (point0) -- (px0);
\node [circle,minimum size=5pt,inner sep=0,fill=red!80] (apoint0) at (\x,\a) {};
\draw [-,dotted] (point0) -- (apoint0);
\def\x{12.0}
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\pgfmathsetmacro{\a}{-1/300 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5) + 3}
\node [circle,minimum size=5pt,inner sep=0,fill=blue] (point1) at (\x,\p) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px1) at (\x,0) {};
\node [anchor=north] (t1plabel) at (\x,-0.1) {\footnotesize{$t_1$}};
\draw [-,dashed] (point1) -- (px1);
\node [circle,minimum size=5pt,inner sep=0,fill=red!80] (apoint1) at (\x,\a) {};
\draw [-,dotted] (point1) -- (apoint1);
\foreach \x in {3.0,4.2,5.0,8.5,11.0}{
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\pgfmathsetmacro{\a}{-1/300 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5) + 3}
\node [circle,minimum size=5pt,inner sep=0,fill=blue] (point) at (\x,\p) {};
\node [circle,minimum size=5pt,inner sep=0,fill=red!80] (apoint) at (\x,\a) {};
\draw [-,dotted] (point) -- (apoint);
}
\node [anchor=south,align=left] (odelabel) at (7,4.5) {\footnotesize{增广 ODE 轨迹:$\frac{d \mathbf{s}(t)}{dt} = [ f(\mathbf{x}(t), \theta, t), - \left( \frac{\partial f}{\partial \mathbf{x}(t)} \right)^\top \mathbf{a}(t), - \left( \frac{\partial f}{\partial \theta} \right)^\top \mathbf{a}(t) ]^\top$}};
\draw [->] ([xshift=1.2cm]odelabel.south) -- ([xshift=1.8cm,yshift=-0.7cm]odelabel.south);
\node [anchor=north,align=left] (odelabel) at (4.0,-0.6) {\footnotesize{恢复的原 ODE 轨迹}};
\draw [->] ([xshift=-0.5cm]odelabel.north east) -- ([xshift=-0.1cm,yshift=0.3cm]odelabel.north east);
\node [anchor=south,align=center] (dyn1) at (7.1,5.7) {\scriptsize{原状态}\\[-2pt] \scriptsize{动力学}};
\node [anchor=south,align=center] (dyn2) at (9.2,5.7) {\scriptsize{伴随状态}\\[-2pt] \scriptsize{动力学}};
\node [anchor=south,align=center] (dyn3) at (11.3,5.7) {\scriptsize{累积参数}\\[-2pt] \scriptsize{梯度动力学}};
\draw [->] (dyn1.south) -- ([yshift=-0.3cm]dyn1.south);
\draw [->] (dyn2.south) -- ([yshift=-0.3cm]dyn2.south);
\draw [->] (dyn3.south) -- ([yshift=-0.3cm]dyn3.south);
\node [anchor=south] (adjointstate) at (5,2.9) {\scriptsize{伴随状态 $\mathbf{a}(t)$}};
\draw [->] ([yshift=2pt]adjointstate.south) -- ([yshift=-0.4cm]adjointstate.south);
\node [anchor=south west] (oristate) at (5.35,0.55) {\scriptsize{原状态 $\mathbf{x}(t)$}};
\draw [->] ([xshift=0.1cm,yshift=0.1cm]oristate.south west) -- ([xshift=-0.2cm,yshift=-0.2cm]oristate.south west);
\node [anchor=west] (initstate) at (12.1,3.75) {\scriptsize{初始状态}};
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(b) 反向传播(从 $t_1$$t_0$ 积分增广 ODE 动力学)}};
\end{scope}
\end{tikzpicture}
\end{document}
% !Mode:: "TeX:UTF-8"
% !TEX encoding = UTF-8 Unicode
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\stepsize{0.9cm}
\def\boxsize{1.3}
\def\smallsize{0.13}
\def\yslant{0.6}
\def\xslant{0.0}
\tikzstyle{snode} = [fig/module,minimum width=0.28cm,minimum height=0.28cm,inner sep=0pt]
% autoregressive generation
\begin{scope}
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s11) at (0,0) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s12) at ([xshift=\stepsize]s11) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s13) at ([xshift=\stepsize]s12) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s14) at ([xshift=\stepsize]s13) {};
\node [circle,inner sep=0,minimum size=5pt,fill=gray!20] (s15) at ([xshift=\stepsize]s14) {};
%\node [circle,inner sep=0,minimum size=5pt,fill=gray!20] (s16) at ([xshift=\stepsize]s15) {};
\node [circle,inner sep=0,minimum size=5pt,fill=gray!20] (s21) at ([yshift=1.2*\stepsize]s11) {};
\node [circle,inner sep=0,minimum size=5pt,fill=gray!20] (s22) at ([xshift=\stepsize]s21) {};
\node [circle,inner sep=0,minimum size=5pt,fill=gray!20] (s23) at ([xshift=\stepsize]s22) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s24) at ([xshift=\stepsize]s23) {};
\node [circle,inner sep=0,minimum size=5pt,fill=gray!20] (s25) at ([xshift=\stepsize]s24) {};
%\node [circle,inner sep=0,minimum size=5pt,fill=gray!20] (s26) at ([xshift=\stepsize]s25) {};
\draw [-{Stealth[length=1.3mm]}] (s11.30) -- (s24.210);
\draw [-{Stealth[length=1.3mm]}] (s12.50) -- (s24.230);
\draw [-{Stealth[length=1.3mm]}] (s13.70) -- (s24.250);
\draw [-{Stealth[length=1.3mm]}] (s14.90) -- (s24.270);
\begin{pgfonlayer}{background}
\node [fig/module,anchor=south,minimum width=6.5*\stepsize,minimum height=2.3*\stepsize] (llm) at ([xshift=0.5*\stepsize,yshift=-0.5*\stepsize]s13) {};
\node [anchor=south west] (llmlabel) at (llm.north west) {\footnotesize{Transformer}};
\end{pgfonlayer}
\node [anchor=center] (w1) at ([yshift=-1.5*\stepsize]s11) {\small{$c_1$}};
\node [anchor=center] (w2) at ([yshift=-1.5*\stepsize]s12) {\small{$c_2$}};
\node [anchor=center] (w3) at ([yshift=-1.5*\stepsize]s13) {\small{$x_1$}};
\node [anchor=center] (w4) at ([yshift=-1.5*\stepsize]s14) {\small{$x_2$}};
\node [anchor=center] (w5) at ([yshift=-1.5*\stepsize]s15) {\color{gray!20} \small{$x_3$}};
%\node [anchor=center] (w6) at ([yshift=-1.5*\stepsize]s16) {\color{gray!20} \small{$x_4$}};
\draw [->] ([yshift=0.3cm]w1.center) -- ([yshift=0.7cm]w1.center);
\draw [->] ([yshift=0.3cm]w2.center) -- ([yshift=0.7cm]w2.center);
\draw [->] ([yshift=0.3cm]w3.center) -- ([yshift=0.7cm]w3.center);
\draw [->] ([yshift=0.3cm]w4.center) -- ([yshift=0.7cm]w4.center);
\draw [->,gray!20] ([yshift=0.3cm]w5.center) -- ([yshift=0.7cm]w5.center);
%\draw [->,gray!20] ([yshift=0.3cm]w6.center) -- ([yshift=0.7cm]w6.center);
\node [anchor=center] (x4) at ([yshift=1.6*\stepsize]s24) {\scriptsize{$\Pr(x_3|\mathbf{x}_{<3},\mathbf{c})$}};
\draw [<-] ([yshift=-0.3cm]x4.center) -- ([yshift=-0.7cm]x4.center);
\node [fig/caption,anchor=north] (caption) at ([yshift=-1.5cm]llm.south) {\footnotesize{(a) 自回归生成(单步)}};
\end{scope}
% non-autoregressive generation
\begin{scope}[xshift=7.8cm]
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s11) at (0,0) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s11) at (0,0) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s12) at ([xshift=\stepsize]s11) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s13) at ([xshift=\stepsize]s12) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s14) at ([xshift=\stepsize]s13) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s15) at ([xshift=\stepsize]s14) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s16) at ([xshift=\stepsize]s15) {};
\node [circle,inner sep=0,minimum size=5pt,fill=gray!20] (s21) at ([yshift=1.2*\stepsize]s11) {};
\node [circle,inner sep=0,minimum size=5pt,fill=gray!20] (s22) at ([xshift=\stepsize]s21) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s23) at ([xshift=\stepsize]s22) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s24) at ([xshift=\stepsize]s23) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s25) at ([xshift=\stepsize]s24) {};
\node [circle,inner sep=0,minimum size=5pt,fill=black] (s26) at ([xshift=\stepsize]s25) {};
\draw [-{Stealth[length=1.3mm]}] (s11.50) -- (s23.230);
\draw [-{Stealth[length=1.3mm]}] (s12.70) -- (s23.250);
\draw [-{Stealth[length=1.3mm]}] (s13.90) -- (s23.270);
\draw [-{Stealth[length=1.3mm]}] (s14.110) -- (s23.290);
\draw [-{Stealth[length=1.3mm]}] (s15.130) -- (s23.310);
\draw [-{Stealth[length=1.3mm]}] (s16.150) -- (s23.330);
\draw [-{Stealth[length=1.3mm]}] (s11.30) -- (s24.210);
\draw [-{Stealth[length=1.3mm]}] (s12.50) -- (s24.230);
\draw [-{Stealth[length=1.3mm]}] (s13.70) -- (s24.250);
\draw [-{Stealth[length=1.3mm]}] (s14.90) -- (s24.270);
\draw [-{Stealth[length=1.3mm]}] (s15.110) -- (s24.290);
\draw [-{Stealth[length=1.3mm]}] (s16.130) -- (s24.310);
\draw [-{Stealth[length=1.3mm]}] (s11.20) -- (s25.190);
\draw [-{Stealth[length=1.3mm]}] (s12.40) -- (s25.210);
\draw [-{Stealth[length=1.3mm]}] (s13.60) -- (s25.230);
\draw [-{Stealth[length=1.3mm]}] (s14.80) -- (s25.250);
\draw [-{Stealth[length=1.3mm]}] (s15.90) -- (s25.270);
\draw [-{Stealth[length=1.3mm]}] (s16.110) -- (s25.290);
\draw [-{Stealth[length=1.3mm]}] (s11.10) -- (s26.170);
\draw [-{Stealth[length=1.3mm]}] (s12.30) -- (s26.190);
\draw [-{Stealth[length=1.3mm]}] (s13.50) -- (s26.210);
\draw [-{Stealth[length=1.3mm]}] (s14.70) -- (s26.230);
\draw [-{Stealth[length=1.3mm]}] (s15.80) -- (s26.250);
\draw [-{Stealth[length=1.3mm]}] (s16.90) -- (s26.270);
\begin{pgfonlayer}{background}
\node [fig/module,anchor=south,minimum width=6.5*\stepsize,minimum height=2.3*\stepsize] (llm) at ([xshift=0.5*\stepsize,yshift=-0.5*\stepsize]s13) {};
\node [anchor=south west] (llmlabel) at (llm.north west) {\footnotesize{Transformer}};
\end{pgfonlayer}
\node [anchor=center] (w1) at ([yshift=-1.5*\stepsize]s11) {\small{$c_1$}};
\node [anchor=center] (w2) at ([yshift=-1.5*\stepsize]s12) {\small{$c_2$}};
\node [anchor=center] (w3) at ([yshift=-1.5*\stepsize]s13) {\small{\uline{\hspace{0.3cm}}}};
\node [anchor=center] (w4) at ([yshift=-1.5*\stepsize]s14) {\small{\uline{\hspace{0.3cm}}}};
\node [anchor=center] (w5) at ([yshift=-1.5*\stepsize]s15) {\small{\uline{\hspace{0.3cm}}}};
\node [anchor=center] (w6) at ([yshift=-1.5*\stepsize]s16) {\small{\uline{\hspace{0.3cm}}}};
\draw [->] ([yshift=0.3cm]w1.center) -- ([yshift=0.7cm]w1.center);
\draw [->] ([yshift=0.3cm]w2.center) -- ([yshift=0.7cm]w2.center);
\draw [->] ([yshift=0.3cm]w3.center) -- ([yshift=0.7cm]w3.center);
\draw [->] ([yshift=0.3cm]w4.center) -- ([yshift=0.7cm]w4.center);
\draw [->] ([yshift=0.3cm]w5.center) -- ([yshift=0.7cm]w5.center);
\draw [->] ([yshift=0.3cm]w6.center) -- ([yshift=0.7cm]w6.center);
\node [anchor=center] (x3) at ([yshift=1.6*\stepsize,xshift=-0.3cm]s23) {\tiny{$\Pr(x_1|\mathbf{c})$}};
\node [anchor=center] (x4) at ([yshift=1.6*\stepsize,xshift=-0.1cm]s24) {\tiny{$\Pr(x_2|\mathbf{c})$}};
\node [anchor=center] (x5) at ([yshift=1.6*\stepsize,xshift=0.1cm]s25) {\tiny{$\Pr(x_3|\mathbf{c})$}};
\node [anchor=center] (x6) at ([yshift=1.6*\stepsize,xshift=0.3cm]s26) {\tiny{$\Pr(x_4|\mathbf{c})$}};
\draw [->] ([yshift=0.7cm]s23.center) -- ([yshift=1.1cm]s23.center);
\draw [->] ([yshift=0.7cm]s24.center) -- ([yshift=1.1cm]s24.center);
\draw [->] ([yshift=0.7cm]s25.center) -- ([yshift=1.1cm]s25.center);
\draw [->] ([yshift=0.7cm]s26.center) -- ([yshift=1.1cm]s26.center);
\node [fig/caption,anchor=north] (caption) at ([yshift=-1.5cm]llm.south) {\footnotesize{(b) 非自回归生成(单步)}};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssize{1}
\def\boxsize{1.3}
\def\smallsize{0.13}
\tikzstyle{snode} = [fig/module,minimum width=0.28cm,minimum height=0.28cm,inner sep=0pt]
\begin{scope}[scale=0.75]
\draw [-,thick,draw=red,fill=red!24] (0,0) -- (0,6) -- (6,5) -- (6,1) -- cycle;
\draw [-,thick,fill=FigPaper] (6.5,1) rectangle (7.5,5);
\draw [-,thick,draw=blue,fill=blue!24] (8,5) -- (14,6) -- (14,0) -- (8,1) -- cycle;
\node [anchor=center,rotate=90] (aeinput) at (-0.5,3) {\small{输入}};
\node [anchor=center,rotate=90] (hidden) at (7,3) {\small{隐变量}};
\node [anchor=center,rotate=90] (aeoutput) at (14.5,3) {\small{重构输入}};
\node [anchor=center] (encoderlabel) at (3,3) {\Large{编码器}};
\node [anchor=center] (decoderlabel) at (11,3) {\Large{解码器}};
\node [anchor=west,font=\bfseries\sffamily] (autoencoderlabel) at (-2,7) {\small{自动编码器}};
\node [anchor=north west,font=\bfseries\sffamily] (dmlabel) at (-2.0,-1.2) {\small{扩散模型}};
\def\xbase{-0.2}
\def\ybase{-2.5}
\draw [-] (\xbase,\ybase) rectangle (\xbase-\boxsize,\ybase-\boxsize) node [midway] (image) {\color{green!50!black} \huge{$\mathcal{X}$}};
\node [anchor=north,align=center] (imagelabel) at ([yshift=-0.2cm]image.south) {\footnotesize{自然图像 $\mathbf{x}$(即 $\mathbf{x}(0)$}\\[-3pt] \footnotesize{$\mathbf{x} \sim p_0$}};
\def\xbase{7+\boxsize/2}
\def\ybase{-2.5}
\draw [-] (\xbase,\ybase) rectangle (\xbase-\boxsize,\ybase-\boxsize) node [midway] (noise) {\color{green!50!black} \huge{$\mathcal{X}$}};
\node [anchor=north,align=center] (noiselabel) at ([yshift=-0.22cm]noise.south) {\footnotesize{噪声 $\mathbf{z}$(即 $\mathbf{x}(T)$}\\[-3pt] \footnotesize{$\mathbf{z} \sim p_T$}};
\pgfmathsetseed{42}
\foreach \x in {0,1,...,9}{
\foreach \y in {0,1,...,9}{
\pgfmathtruncatemacro{\myint}{100*rnd}
\draw [fill=black!\myint,draw=white] ({\xbase-\x*\smallsize-\smallsize},{\ybase-\y*\smallsize-\smallsize}) rectangle ({\xbase-\x*\smallsize},{\ybase-\y*\smallsize});
}
}
\def\xbase{14+0.2+\boxsize}
\def\ybase{-2.5}
\draw [-] (\xbase,\ybase) rectangle ({\xbase-\boxsize},{\ybase-\boxsize}) node [midway] (image2) {\color{green!50!black} \huge{$\mathcal{X}$}};
\node [anchor=north,align=center] (image2label) at ([yshift=-0.2cm]image2.south) {\footnotesize{自然图像 $\mathbf{x}$(即 $\mathbf{x}(0)$}\\[-3pt] \footnotesize{$\mathbf{x} \sim p_0$}};
\draw [fig/flow secondary] ([xshift=4pt]image.east) -- ([xshift=-4pt]noise.west) node [pos=0.5,below,yshift=-0.1cm] (forwardpass) {\footnotesize{前向过程}};
\draw [fig/flow primary] ([xshift=4pt]noise.east) -- ([xshift=-4pt]image2.west) node [pos=0.5,below,yshift=-0.1cm] (backwardpass) {\footnotesize{反向过程}};
\node [anchor=south] (forwardpush) at ([yshift=0.2cm]forwardpass.north) {\footnotesize{$p_T = (\Phi_{0 \to T})_{\#} p_0$}};
\node [anchor=south] (backwardpush) at ([yshift=0.2cm]backwardpass.north) {\footnotesize{$p_0 = (\Phi_{T \to 0})_{\#} p_T$}};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssep{1cm}
\def\vsep{1.5cm}
\def\yslant{0.5}
\def\xslant{-0.8}
\tikzstyle{snode} = [fig/module,minimum width=0.28cm,minimum height=0.28cm,inner sep=0pt]
\begin{scope}[
every node/.append style={yslant=\yslant,xslant=\xslant},
yslant=\yslant,xslant=\xslant]
\coordinate (a00) at (-2.5,-2.5);
\coordinate (a10) at (2.5,-2.5);
\coordinate (a01) at (-2.5,2.5);
\coordinate (a11) at (2.5,2.5);
\coordinate (p1) at (-1.1,1.0);
\draw [-,thick] (-2.5,-2.5) rectangle (2.5,2.5);
\fill [white,fill opacity=.75] (-2.5,-2.5) rectangle (2.5,2.5);
\draw [black,fill=orange!20] plot [smooth cycle, tension=0.5] coordinates {(0,1.9) (-0.9,1.3) (-2.0,0.5) (-2.1,-0.3) (-1.4,-1.6) (0,-2.0) (1.2,-1.3) (1.4,-0.7) (1.6,-0.3) (2.0,0.4) (0.8,1.7) };
\draw [black,fill=orange!40] plot [smooth cycle, tension=0.5] coordinates {(0,1.5) (-0.7,1.0) (-1.5,0.4) (-1.6,-0.2) (-1.0,-1.2) (0,-1.5) (0.9,-1.0) (1.1,-0.5) (1.3,-0.2) (1.5,0.3) (0.6,1.3) };
\draw [black,fill=orange!60] plot [smooth cycle, tension=0.5] coordinates {(0,1.0) (-0.5,0.7) (-1.0,0.3) (-1.1,-0.1) (-0.7,-0.9) (0,-1.0) (0.6,-0.7) (0.8,-0.3) (0.9,-0.1) (1.0,0.2) (0.5,0.8) };
\draw [black,fill=orange!80] plot [smooth cycle, tension=0.5] coordinates {(0,0.4) (-0.3,0.3) (-0.5,0.2) (-0.5,-0.0) (-0.4,-0.4) (0,-0.5) (0.3,-0.4) (0.5,-0.1) (0.4,0.1) (0.2,0.4) };
\node [circle,fill=blue,inner sep=0,minimum size=4pt] (point1) at (p1) {};
\end{scope}
\begin{scope}[yshift=4cm,
every node/.append style={yslant=\yslant,xslant=\xslant},
yslant=\yslant,xslant=\xslant]
\coordinate (b00) at (-2.5,-2.5);
\coordinate (b10) at (2.5,-2.5);
\coordinate (b01) at (-2.5,2.5);
\coordinate (b11) at (2.5,2.5);
\coordinate (p2) at (-1.1,1.0);
\draw [-,dashed] (p1) -- (p2);
\draw [-,thick] (-2.5,-2.5) rectangle (2.5,2.5);
\fill [white,fill opacity=.75] (-2.5,-2.5) rectangle (2.5,2.5);
\draw [black,fill=orange!20,fill opacity=.55] plot [smooth cycle, tension=0.5] coordinates {(0,2.2) (-0.9,1.6) (-1.8,0.3) (-2.0,-0.5) (-1.2,-1.8) (0,-2.1) (0.9,-2.0) (1.7,-0.2) (1.9,0.7) (0.8,1.9) };
\draw [black,fill=orange!40,fill opacity=.55] plot [smooth cycle, tension=0.5] coordinates {(0,1.6) (-0.6,1.2) (-1.4,-0.2) (-0.7,-1.2) (0,-1.5) (0.6,-1.2) (1.2,0) (1.3,0.6) (0.5,1.4)};
\draw [black,fill=orange!60,fill opacity=.55] plot [smooth cycle, tension=0.5] coordinates {(0,0.8) (-0.3,0.7) (-0.8,-0.1) (-0.4,-0.7) (0,-0.8) (0.4,-0.7) (0.7,0) (0.6,0.3) (0.4,0.7)};
\draw [black,fill=orange!80,fill opacity=.55] plot [smooth cycle, tension=0.5] coordinates {(0,0.4) (-0.2,0.3) (-0.4,-0.0) (-0.2,-0.3) (0,-0.4) (0.2,-0.3) (0.4,0.1) (0.2,0.3)};
\node [circle,fill=blue,inner sep=0,minimum size=4pt] (point1) at (p2) {};
\end{scope}
\draw [->,very thick] ([xshift=-0.5cm,yshift=-2cm]a01) -- ([xshift=-0.5cm,yshift=2cm]b01);
\node [anchor=east] (timelabel) at ([xshift=-0.7cm,yshift=1.8cm]b01) {\small{时间}};
\node [circle,fill=black,inner sep=0,minimum size=4pt] (t1point) at ([xshift=-0.5cm]a01) {};
\node [anchor=east] (t1label) at ([xshift=-0.2cm]t1point) {\small{$0$}};
\node [circle,fill=black,inner sep=0,minimum size=4pt] (t2point) at ([xshift=-0.5cm]b01) {};
\node [anchor=east] (t2label) at ([xshift=-0.2cm]t2point) {\small{$t$}};
\node [anchor=east] (p1label) at ([xshift=-0.2cm,yshift=0.0cm]p1) {\footnotesize{$p_0(\mathbf{z})$}};
\node [anchor=east] (p2label) at ([xshift=-0.2cm,yshift=0.0cm]p2) {\footnotesize{$p_t(\mathbf{x})$}};
\node [anchor=north west,align=left] (d2) at ([yshift=2.8cm,xshift=-1cm]b10) {\footnotesize{$p_t(\cdot)$$t$ 时刻的分布}\\ \footnotesize{总概率:}\\ \footnotesize{\hspace{0.6cm} $\int_{\mathcal{S}_t} p_t(\mathbf{x}) d\mathbf{x}$}\\ \footnotesize{\hspace{0.2cm} $= \int_{\mathcal{S}_0} p_t(\Phi_t(\mathbf{z})) \left| \det \frac{\partial \Phi_t(\mathbf{z})}{\partial \mathbf{z}} \right| d\mathbf{z} $}};
\node [anchor=north west,align=left] (d1) at ([yshift=-1.0cm,xshift=-1cm]a10) {\footnotesize{$p_0(\cdot)$$0$ 时刻的分布}\\ \footnotesize{总概率:}\\ \footnotesize{\hspace{0.8cm} $\int_{\mathcal{S}_0} p_0(\mathbf{z}) d\mathbf{z}$}};
\node [anchor=west] (area1) at ([xshift=-1.8cm]a10) {\footnotesize{区域 $S_0$}};
\draw [-] (area1.west) -- ([yshift=-0.2cm,xshift=-0.8cm]area1.west);
\node [anchor=west] (area2) at ([xshift=-1.8cm]b10) {\footnotesize{区域 $S_t$}};
\draw [-] (area2.west) -- ([yshift=-0.2cm,xshift=-0.8cm]area2.west);
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\sep{0.6}
\def\stepsize{0.4cm}
\def\ycoordp{4}
\def\vectorsize{1cm}
\def\smallsize{0.13}
\tikzstyle{snode} = [fig/module,minimum width=2.5cm,minimum height=0.6cm]
\tikzstyle{cnode} = [minimum width=0.3cm,minimum height=\vectorsize]
\begin{scope}
\node [anchor=center] (x1) at (0,0) {\texttt{A}};
\node [anchor=center] (x2) at ([xshift=\stepsize]x1) {\texttt{D}};
\node [anchor=center] (x3) at ([xshift=\stepsize]x2) {\texttt{B}};
\node [anchor=center] (x4) at ([xshift=\stepsize]x3) {\texttt{C}};
\node [anchor=center] (x5) at ([xshift=\stepsize]x4) {\texttt{C}};
\node [anchor=center] (y21) at ([yshift=-2cm]x1) {\texttt{B}};
\node [anchor=center] (y22) at ([xshift=\stepsize]y21) {\texttt{D}};
\node [anchor=center] (y23) at ([xshift=\stepsize]y22) {\texttt{B}};
\node [anchor=center] (y24) at ([xshift=\stepsize]y23) {\texttt{C}};
\node [anchor=center] (y25) at ([xshift=\stepsize]y24) {\texttt{C}};
\node [anchor=west] (y31) at ([xshift=0.8cm]y25.east) {\texttt{A}};
\node [anchor=center] (y32) at ([xshift=\stepsize]y31) {\texttt{D}};
\node [anchor=center] (y33) at ([xshift=\stepsize]y32) {\texttt{B}};
\node [anchor=center] (y34) at ([xshift=\stepsize]y33) {\texttt{D}};
\node [anchor=center] (y35) at ([xshift=\stepsize]y34) {\texttt{C}};
\node [anchor=east] (y15) at ([xshift=-0.8cm]y21.west) {\texttt{C}};
\node [anchor=center] (y14) at ([xshift=-\stepsize]y15) {\texttt{C}};
\node [anchor=center] (y13) at ([xshift=-\stepsize]y14) {\texttt{B}};
\node [anchor=center] (y12) at ([xshift=-\stepsize]y13) {\texttt{C}};
\node [anchor=center] (y11) at ([xshift=-\stepsize]y12) {\texttt{A}};
\begin{pgfonlayer}{background}
\node [snode] (xbox) at (x3) {};
\node [snode] (ybox1) at (y13) {};
\node [snode] (ybox2) at (y23) {};
\node [snode] (ybox3) at (y33) {};
\node [anchor=east] (xlabel) at ([xshift=-0.2cm]xbox.west) {$\mathbf{x}$};
\node [anchor=north] (y1label) at ([yshift=-0.1cm]ybox1.south) {$\mathbf{y}_1$};
\node [anchor=north] (y2label) at ([yshift=-0.1cm]ybox2.south) {$\mathbf{y}_2$};
\node [anchor=north] (y3label) at ([yshift=-0.1cm]ybox3.south) {$\mathbf{y}_3$};
\end{pgfonlayer}
\node [anchor=east] (y0) at ([xshift=-0.2cm]ybox1.west) {$\cdots$};
\node [anchor=west] (y4) at ([xshift=0.2cm]ybox3.east) {$\cdots$};
\draw [->] ([yshift=1pt]ybox1.north) -- ([xshift=-0.5cm,yshift=-2pt]xbox.south);
\draw [->] ([yshift=1pt]ybox2.north) -- ([xshift=0cm,yshift=-1pt]xbox.south);
\draw [->] ([yshift=1pt]ybox3.north) -- ([xshift=0.5cm,yshift=-2pt]xbox.south);
\draw [->,thick] ([xshift=-4.5cm,yshift=-3.0cm]x1.west) -- ([xshift=-4.5cm,yshift=1.4cm]x1.west);
\node [anchor=east] (timelabel) at ([xshift=-4.6cm,yshift=1.2cm]x1.west) {\footnotesize{时间}};
\node [anchor=center,circle,fill=black,inner sep=0,minimum size=4pt] (tpoint) at ([xshift=-4.5cm]x1.west) {};
\node [anchor=east] (tlabel) at ([xshift=-0.1cm]tpoint) {\footnotesize{$t$}};
\draw [-,dotted] ([xshift=2pt]tpoint) -- (xlabel.west);
\node [anchor=south] (CTMC) at ([yshift=2pt]xbox.north) {\footnotesize{CTMC: $\frac{d p_t(\mathbf{x})}{dt} = \sum_{\mathbf{y} \in V^n} p_t(\mathbf{y}) {\color{blue} [\mathbf{Q}^{\mathrm{seq}}(t)]_{\mathbf{y}\mathbf{x}}}$}};
\node [anchor=center] (sx1) at ([yshift=4cm,xshift=2cm]xbox.north) {\footnotesize{\texttt{A}}};
\node [anchor=center] (sx2) at ([xshift=1.2*\stepsize]sx1) {\footnotesize{\texttt{D}}};
\node [anchor=center] (sx3) at ([xshift=1.2*\stepsize]sx2) {\footnotesize{\texttt{B}}};
\node [anchor=center] (sx4) at ([xshift=1.2*\stepsize]sx3) {\footnotesize{\texttt{C}}};
\node [anchor=center] (sx5) at ([xshift=1.2*\stepsize]sx4) {\footnotesize{\texttt{C}}};
\node [anchor=center] (sy1) at ([yshift=-1.2cm]sx1.south) {\footnotesize{\texttt{A}}};
\node [anchor=center] (sy2) at ([xshift=1.2*\stepsize]sy1) {\footnotesize{\texttt{D}}};
\node [anchor=center] (sy3) at ([xshift=1.2*\stepsize]sy2) {\footnotesize{\texttt{B}}};
\node [anchor=center] (sy4) at ([xshift=1.2*\stepsize]sy3) {\footnotesize{\texttt{D}}};
\node [anchor=center] (sy5) at ([xshift=1.2*\stepsize]sy4) {\footnotesize{\texttt{C}}};
\draw [->,thin] (sy1.north) -- (sx1.south);
\draw [->,thin] (sy2.north) -- (sx2.south);
\draw [->,thin] (sy3.north) -- (sx3.south);
\draw [->,thin] (sy4.north) -- (sx4.south);
\draw [->,thin] (sy5.north) -- (sx5.south);
\node [anchor=north west] (qtokenlabel) at (sy4.south) {\scriptsize{$Q^{\mathrm{tok}}_{\texttt{DC}}(t)$}};
\begin{pgfonlayer}{background}
\node [draw=blue] [fit = (sx1) (sy5) (qtokenlabel) ] (qbox) {};
\node [fill=orange!20,inner sep=1pt] [fit = (sx4) (sy4)] (qttokenbox) {};
\end{pgfonlayer}
\draw [->,blue] ([yshift=-2pt]qbox.south) .. controls +(south:0.7cm) and +(north:0.7cm) .. ([xshift=-1.0cm,yshift=-1pt]CTMC.north east);
\node [anchor=north east,align=left] (qlabel) at ([yshift=0]qbox.north west) {\footnotesize{生成矩阵中 $(\mathbf{x},\mathbf{y}_3)$ 对应的}\\[-2pt] \footnotesize{矩阵元,记为 $[\mathbf{Q}^{\mathrm{seq}}(t)]_{\mathbf{y}\mathbf{x}}$}\\[-2pt] \footnotesize{其中}\\[-2pt] \footnotesize{$\mathbf{x} = \texttt{ADBCC}$}\\[-2pt] \footnotesize{$\mathbf{y}_3 = \texttt{ADBDC}$}};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\sep{0.6}
\def\stepsize{0.6cm}
\def\ycoordp{4}
\def\vectorsize{1cm}
\def\smallsize{0.13}
\tikzstyle{snode} = [fig/module,minimum width=4.4cm,minimum height=0.8cm]
\tikzstyle{cnode} = [minimum width=0.3cm,minimum height=\vectorsize]
\begin{scope}
\node [anchor=center] (x11) at (0,0) {\small{\texttt{X}}};
\node [anchor=center] (x12) at ([xshift=\stepsize]x11) {\small{\texttt{Y}}};
\node [anchor=center] (x13) at ([xshift=\stepsize]x12) {\small{\texttt{A}}};
\node [anchor=center] (x14) at ([xshift=\stepsize]x13) {\small{\texttt{B}}};
\node [anchor=center] (x15) at ([xshift=\stepsize]x14) {\small{\texttt{C}}};
\node [anchor=center] (x16) at ([xshift=\stepsize]x15) {\small{\texttt{D}}};
\node [anchor=center] (x17) at ([xshift=\stepsize]x16) {\small{\texttt{E}}};
\begin{pgfonlayer}{background}
\node [fill=gray!30,inner sep=1pt] [fit = (x11) (x12) ] (ccolorbox) {};
\node [fill=orange!30,inner sep=1pt] [fit = (x13) (x17) ] (xcolorbox) {};
\node [anchor=center,snode] (x1box) at (x14) {};
\end{pgfonlayer}
\node [anchor=center] (x21) at ([yshift=-3.5cm]x11) {\small{\texttt{X}}};
\node [anchor=center] (x22) at ([xshift=\stepsize]x21) {\small{\texttt{Y}}};
\node [anchor=center] (x23) at ([xshift=\stepsize]x22) {\small{\texttt{B}}};
\node [anchor=center] (x24) at ([xshift=\stepsize]x23) {\small{\texttt{C}}};
\node [anchor=center] (x25) at ([xshift=\stepsize]x24) {\small{\texttt{A}}};
\node [anchor=center] (x26) at ([xshift=\stepsize]x25) {\small{\texttt{A}}};
\node [anchor=center] (x27) at ([xshift=\stepsize]x26) {\small{\texttt{D}}};
\begin{pgfonlayer}{background}
\node [fill=gray!30,inner sep=1pt] [fit = (x21) (x22) ] {};
\node [fill=orange!30,inner sep=1pt] [fit = (x23) (x27) ] {};
\node [anchor=center,snode] (x2box) at (x24) {};
\end{pgfonlayer}
\node [anchor=center] (x31) at ([yshift=-2.5cm]x21) {\small{\texttt{X}}};
\node [anchor=center] (x32) at ([xshift=\stepsize]x31) {\small{\texttt{Y}}};
\node [anchor=center] (x33) at ([xshift=\stepsize]x32) {\small{\texttt{B}}};
\node [anchor=center] (x34) at ([xshift=\stepsize]x33) {\small{\texttt{C}}};
\node [anchor=center] (x35) at ([xshift=\stepsize]x34) {\small{\texttt{A}}};
\node [anchor=center] (x36) at ([xshift=\stepsize]x35) {\small{\texttt{A}}};
\node [anchor=center] (x37) at ([xshift=\stepsize]x36) {\small{\texttt{B}}};
\begin{pgfonlayer}{background}
\node [fill=gray!30,inner sep=1pt] [fit = (x31) (x32) ] {};
\node [fill=orange!30,inner sep=1pt] [fit = (x33) (x37) ] {};
\node [anchor=center,snode] (x3box) at (x34) {};
\end{pgfonlayer}
\node [anchor=center] (x41) at ([yshift=-2.5cm]x31) {\small{\texttt{X}}};
\node [anchor=center] (x42) at ([xshift=\stepsize]x41) {\small{\texttt{Y}}};
\node [anchor=center] (x43) at ([xshift=\stepsize]x42) {\small{\texttt{B}}};
\node [anchor=center] (x44) at ([xshift=\stepsize]x43) {\small{\texttt{D}}};
\node [anchor=center] (x45) at ([xshift=\stepsize]x44) {\small{\texttt{A}}};
\node [anchor=center] (x46) at ([xshift=\stepsize]x45) {\small{\texttt{A}}};
\node [anchor=center] (x47) at ([xshift=\stepsize]x46) {\small{\texttt{Z}}};
\begin{pgfonlayer}{background}
\node [fill=gray!30,inner sep=1pt] [fit = (x41) (x42) ] {};
\node [fill=orange!30,inner sep=1pt] [fit = (x43) (x47) ] {};
\node [anchor=center,snode] (x4box) at (x44) {};
\end{pgfonlayer}
\node [anchor=center] (x51) at ([yshift=-3.5cm]x41) {\small{\texttt{X}}};
\node [anchor=center] (x52) at ([xshift=\stepsize]x51) {\small{\texttt{Y}}};
\node [anchor=center] (x53) at ([xshift=\stepsize]x52) {\small{\texttt{D}}};
\node [anchor=center] (x54) at ([xshift=\stepsize]x53) {\small{\texttt{X}}};
\node [anchor=center] (x55) at ([xshift=\stepsize]x54) {\small{\texttt{C}}};
\node [anchor=center] (x56) at ([xshift=\stepsize]x55) {\small{\texttt{Z}}};
\node [anchor=center] (x57) at ([xshift=\stepsize]x56) {\small{\texttt{E}}};
\begin{pgfonlayer}{background}
\node [fill=gray!30,inner sep=1pt] [fit = (x51) (x52) ] {};
\node [fill=orange!30,inner sep=1pt] [fit = (x53) (x57) ] {};
\node [anchor=center,snode] (x5box) at (x54) {};
\end{pgfonlayer}
\draw [->,dotted,thick,red] ([xshift=0.5cm,yshift=-2pt]x1box.south west) -- ([xshift=0.5cm,yshift=2pt]x2box.north west) node [pos=0.5,xshift=-0.3cm,rotate=90] {\scriptsize{前向过程}};
\draw [->,thick,red] ([xshift=0.5cm,yshift=-2pt]x2box.south west) -- ([xshift=0.5cm,yshift=2pt]x3box.north west);
\draw [->,thick,red] ([xshift=0.5cm,yshift=-2pt]x3box.south west) -- ([xshift=0.5cm,yshift=2pt]x4box.north west);
\draw [->,dotted,thick,red] ([xshift=0.5cm,yshift=-2pt]x4box.south west) -- ([xshift=0.5cm,yshift=2pt]x5box.north west);
\draw [<-,dotted,thick,blue] ([xshift=-0.5cm,yshift=-2pt]x1box.south east) -- ([xshift=-0.5cm,yshift=2pt]x2box.north east) node [pos=0.5,xshift=0.3cm,rotate=90] {\scriptsize{反向过程}};
\draw [<-,thick,blue] ([xshift=-0.5cm,yshift=-2pt]x2box.south east) -- ([xshift=-0.5cm,yshift=2pt]x3box.north east);
\draw [<-,thick,blue] ([xshift=-0.5cm,yshift=-2pt]x3box.south east) -- ([xshift=-0.5cm,yshift=2pt]x4box.north east);
\draw [<-,dotted,thick,blue] ([xshift=-0.5cm,yshift=-2pt]x4box.south east) -- ([xshift=-0.5cm,yshift=2pt]x5box.north east);
\node [anchor=north] (x1label) at (x1box.south) {\small{$(\mathbf{x}(0), \mathbf{c})$}};
\node [anchor=north] (x2label) at (x2box.south) {\small{$(\mathbf{x}(t - 1), \mathbf{c})$}};
\node [anchor=north] (x3label) at (x3box.south) {\small{$(\mathbf{x}(t), \mathbf{c})$}};
\node [anchor=north] (x4label) at (x4box.south) {\small{$(\mathbf{x}(t + 1), \mathbf{c})$}};
\node [anchor=north] (x5label) at (x5box.south) {\small{$(\mathbf{x}(T), \mathbf{c})$}};
\node [anchor=east] (clabel) at ([xshift=-1.5cm,yshift=0.8cm]x1box.north) {\footnotesize{上下文 $\mathbf{c}$(固定)}};
\node [anchor=west] (xlabel) at ([xshift=1cm,yshift=0.8cm]x1box.north) {\footnotesize{目标序列 $\mathbf{x}(0)$}};
\draw [->,thin] ([xshift=-0.0cm]clabel.south) .. controls +(south:0.6cm) and +(north:0.6cm) .. ([yshift=0pt]ccolorbox.north);
\draw [->,thin] ([xshift=-0.3cm]xlabel.south) .. controls +(south:0.6cm) and +(north:0.6cm) .. ([xshift=0.3cm]xcolorbox.north);
\node [anchor=north east,align=left] (forwardlabel) at ([xshift=-0.3cm,yshift=-0.1cm]x2box.south west) {\footnotesize{计算前向转移概率}\\[-2pt] \footnotesize{$q(\mathbf{x}(t) | \mathbf{x}(t - 1), \mathbf{c})$}\\[-2pt] \footnotesize{与反向后验}\\[-2pt] \footnotesize{$q(\mathbf{x}(t-1)| \mathbf{x}(t),\mathbf{x}(0),\mathbf{c})$}\\[-2pt] \footnotesize{(均有闭式解)}};
\node [anchor=north west,align=left] (backwardlabel) at ([xshift=0.1cm,yshift=-0.1cm]x2box.south east) {\footnotesize{使用网络}\\[-2pt] \footnotesize{$p_\theta(\mathbf{x}(t-1)| \mathbf{x}(t),\mathbf{c})$}\\[-2pt] \footnotesize{预测 $\mathbf{x}(t-1)$}};
\node [fig/module,anchor=north west,align=left] (trainlabel) at ([yshift=-1cm]backwardlabel.south) {\footnotesize{训练 $\theta$ 时,最小化}\\[-2pt] \footnotesize{$q(\mathbf{x}(t-1)| \mathbf{x}(t),\mathbf{x}(0),\mathbf{c})$}\\[-2pt] \footnotesize{}\\[-2pt] \footnotesize{$p_\theta(\mathbf{x}(t-1)| \mathbf{x}(t),\mathbf{c})$}\\[-2pt] \footnotesize{之间的 KL 散度}};
\draw [->] ([xshift=-0.5cm]trainlabel.north east) .. controls +(north:1cm) and +(east:0.8cm) .. (backwardlabel.east);
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\stepsize{0.8cm}
\def\boxsize{1.3}
\def\smallsize{0.13}
\def\yslant{0.6}
\def\xslant{0.0}
\tikzstyle{snode} = [fig/module,minimum width=3cm,minimum height=0.7cm]
\begin{scope}
\node [snode,anchor=south west] (sT) at (0,0) {\small{$\mathbf{s}(T)$}};
\node [snode,anchor=south] (stm) at ([yshift=2cm]sT.north) {\small{$\mathbf{s}(t)$}};
\draw [->] ([yshift=0.1cm]sT.north) -- ([yshift=0.7cm]sT.north);
\draw [<-] ([yshift=-0.1cm]stm.south) -- ([yshift=-0.7cm]stm.south);
\node [anchor=center] (tlabel1) at ([yshift=1.0cm]sT.north) {$\cdots$};
\node [snode,anchor=south] (s0) at ([yshift=2cm]stm.north) {\small{$\mathbf{s}(0)$}};
\draw [->] ([yshift=0.1cm]stm.north) -- ([yshift=0.7cm]stm.north);
\draw [<-] ([yshift=-0.1cm]s0.south) -- ([yshift=-0.7cm]s0.south);
\node [anchor=center] (tlabel2) at ([yshift=1.0cm]stm.north) {$\cdots$};
\node [snode,anchor=east] (c) at ([xshift=-1.5cm]sT.west) {\small{$\mathbf{c}$}};
\draw [->,dotted] ([xshift=2pt]c.east) -- ([xshift=-2pt]sT.west);
\draw [->,dotted] ([xshift=-0.3cm,yshift=2pt]c.north east) .. controls +(north:1cm) and +(west:1cm) .. ([xshift=-2pt]stm.west);
\draw [->,dotted] ([xshift=-0.6cm,yshift=2pt]c.north east) .. controls +(north:3cm) and +(west:1.1cm) .. ([xshift=-2pt]s0.west);
\node [snode,anchor=south] (x) at ([yshift=2cm,xshift=2cm]s0.north) {\small{$\mathbf{x}$}};
\draw [->] ([yshift=2pt]s0.north) .. controls +(80:3cm) and +(-100:3cm) .. ([yshift=-2pt]x.south) node [pos=0.95,right] {\footnotesize{映射为词元}};
\draw [->,thick] ([xshift=2cm,yshift=-0.2cm]sT.south east) -- ([xshift=2cm,yshift=0.5cm]s0.north east);
\node [anchor=center,circle,inner sep=0,minimum size=4pt,fill=black] (pT) at ([xshift=2cm]sT.east) {};
\node [anchor=west] (pTlabel) at ([xshift=3pt]pT.east) {\footnotesize{$t=T$}};
\node [anchor=center,circle,inner sep=0,minimum size=4pt,fill=black] (p0) at ([xshift=2cm]s0.east) {};
\node [anchor=west] (p0label) at ([xshift=3pt]p0.east) {\footnotesize{$t=0$}};
\node [anchor=west] (timeline) at ([xshift=3pt,yshift=0.7cm]p0.east) {\footnotesize{时间轴}};
\node [anchor=center] (clabel) at ([yshift=-0.3cm]c.south) {\footnotesize{上下文}};
\node [anchor=center] (sTlabel) at ([yshift=-0.3cm]sT.south) {\footnotesize{先验}};
\node [anchor=south] (s0label) at ([yshift=2pt,xshift=0.3cm]s0.north west) {\footnotesize{去噪信号}};
\node [anchor=south] (xlabel) at ([yshift=2pt]x.north) {\footnotesize{词元序列}};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssep{1.3cm}
\def\bsep{2.5cm}
\tikzstyle{lnode} = [fig/module,minimum width=3.0cm,minimum height=0.6cm,inner sep=2pt]
\begin{scope}
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,2.5);
\node [anchor=north east,align=right] (ylabel) at (0-0.1,2.5) {\footnotesize{状态 $x(t)$}};
\node [anchor=north,align=left] (xlabel) at (13.3,-0.1) {\footnotesize{时间 $t$}};
\draw [Round Cap-Round Cap,very thick,dotted,gray!60,domain=0:12.7,samples=100] plot (\x,{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))});
\foreach \x in {2.4,2.80,...,10.0}{
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\node [circle,minimum size=4pt,inner sep=0,fill=blue] (point) at (\x,\p) {};
}
\foreach \x in {2.0}{
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\node [circle,minimum size=4pt,inner sep=0,fill=blue] (point) at (\x,\p) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px) at (\x,0) {};
\node [anchor=north] (plabel) at (\x,-0.1) {\footnotesize{$t_0=0$}};
\draw [-,dashed] (point) -- (px);
}
\foreach \x in {10.0}{
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\node [circle,minimum size=4pt,inner sep=0,fill=blue] (point) at (\x,\p) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px) at (\x,0) {};
\node [anchor=south] (plabel) at (\x,0.1) {\footnotesize{$t$}};
\draw [-,dashed] (point) -- (px);
}
\node [anchor=south,align=left] (pp) at (7,1) {\footnotesize{离散时刻的近似状态}};
\draw [->] ([xshift=0.2cm]pp.south west) -- ([xshift=-0.05cm,yshift=-0.5cm]pp.south west);
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(a) 状态 $x(t)$ 随时间 $t$ 的变化}};
\end{scope}
\begin{scope}[yshift=-6.0cm]
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,2.5);
\node [anchor=north east,align=right] (ylabel) at (0-0.1,2.5) {\footnotesize{动力学}\\[-2pt] \footnotesize{$f(x(t),t)$}};
\node [anchor=north,align=left] (xlabel) at (13.3,-0.1) {\footnotesize{时间 $t$}};
\draw [Round Cap-Round Cap,very thick,gray!60,domain=0:12.7,samples=100] plot (\x,{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5)});
\foreach \x in {2.0}{
\pgfmathsetmacro{\p}{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5)}
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px) at (\x,0) {};
\node [anchor=north] (plabel) at (\x,-0.1) {\footnotesize{$t_0=0$}};
}
\foreach \x in {10.0}{
\pgfmathsetmacro{\p}{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5)}
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px) at (\x,0) {};
\node [anchor=north] (plabel) at (\x,-0.1) {\footnotesize{$t$}};
}
\foreach \x in {2.0,2.4,...,9.8}{
\pgfmathsetmacro{\p}{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5)}
\draw [thick] (\x,0) rectangle (\x+0.4,\p);
}
\node [anchor=south,align=left] (integral) at (7,1) {\footnotesize{区间 $[t_k,t_{k+1}]$ 上的积分}\\[-2pt] \footnotesize{以矩形面积近似}};
\draw [->] ([xshift=0.3cm,yshift=0.3cm]integral.south) .. controls +(-45:1.5cm) and +(135:1.5cm) .. (8.98,0.45);
\node [anchor=north] (tk) at (8.8,-0.1) {\footnotesize{$t_{k}$}};
\node [anchor=north] (tkp1) at (9.3,-0.1) {\footnotesize{$t_{k+1}$}};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (tpx) at (8.8,0) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (tpxp1) at (9.2,0) {};
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(b) 动力学 $f(x(t),t)$ 随时间 $t$ 的变化}};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssep{1.3cm}
\def\bsep{2.5cm}
\def\stepsize{1.0}
\tikzstyle{lnode} = [fig/module,minimum width=3.0cm,minimum height=0.6cm,inner sep=2pt]
\begin{scope}
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,2.5);
\node [anchor=north east,align=right] (ylabel) at (0-0.1,2.5) {\footnotesize{状态 $\mathbf{x}(t)$}};
\node [anchor=north,align=left] (xlabel) at (13.3,-0.1) {\footnotesize{时间 $t$}};
\foreach \x in {1.0,2.0,...,13.0}{
\pgfmathsetmacro{\plast}{-0.001*(((\x-\stepsize)/1.15)^4) * (\x-\stepsize) + 0.029375*(((\x-\stepsize)/1.15)^4) + 1/(1.15)^4*(- (323/1200)*((\x-\stepsize)^3) + 0.75*((\x-\stepsize)^2))}
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\draw [->,thick,shorten > = 3pt,shorten < = 3pt] (\x-\stepsize,\plast) -- (\x,\p) node [pos=1,circle,minimum size=4pt,fill=blue,inner sep=0] {};
}
\def\x{8}
\pgfmathsetmacro{\leftp}{-0.001*(((\x-\stepsize)/1.15)^4) * (\x-\stepsize) + 0.029375*(((\x-\stepsize)/1.15)^4) + 1/(1.15)^4*(- (323/1200)*((\x-\stepsize)^3) + 0.75*((\x-\stepsize)^2))}
\def\x{9}
\pgfmathsetmacro{\rightp}{-0.001*(((\x-\stepsize)/1.15)^4) * (\x-\stepsize) + 0.029375*(((\x-\stepsize)/1.15)^4) + 1/(1.15)^4*(- (323/1200)*((\x-\stepsize)^3) + 0.75*((\x-\stepsize)^2))}
\node [circle,minimum size=3pt,inner sep=0,fill=black] (lpx) at (8,0) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (rpx) at (9,0) {};
\draw [-,dashed] (8,0) -- (8,\leftp);
\draw [-,dashed] (9,0) -- (9,\rightp);
\draw [-] (8,0.1) -- (8,0.4);
\draw [-] (9,0.1) -- (9,0.4);
\draw [->] (7.6,0.25) -- (7.9,0.25);
\draw [<-] (9.1,0.25) -- (9.4,0.25);
\node [anchor=south] (steplabel) at (8.5,0.6) {\scriptsize{$\Delta t$ 为常数}};
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(a) 以固定时间步长演化的系统(如残差网络)}};
\end{scope}
\begin{scope}[yshift=-6.5cm]
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,2.5);
\node [anchor=north east,align=right] (ylabel) at (0-0.1,2.5) {\footnotesize{状态 $\mathbf{x}(t)$}};
\node [anchor=north,align=left] (xlabel) at (13.3,-0.1) {\footnotesize{时间 $t$}};
\def\x{0}
\foreach \smallstep in {1.2,0.8,0.5,0.4,0.4,0.4,1.0,1.5,0.7,0.5,0.5,0.6,0.6,0.9,1.4,0.9,0.7}{
\pgfmathparse{\x + \smallstep}
\xdef\x{\pgfmathresult}
\pgfmathsetmacro{\plast}{-0.001*(((\x-\smallstep)/1.15)^4) * (\x-\smallstep) + 0.029375*(((\x-\smallstep)/1.15)^4) + 1/(1.15)^4*(- (323/1200)*((\x-\smallstep)^3) + 0.75*((\x-\smallstep)^2))}
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\draw [->,thick,shorten > = 3pt,shorten < = 3pt] (\x-\smallstep,\plast) -- (\x,\p) node [pos=1,circle,minimum size=4pt,fill=blue,inner sep=0] {};
}
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(b) 连续演化或采用动态时间步长的系统(如 Neural ODE)}};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssize{1}
\def\boxsize{1.3}
\def\smallsize{0.13}
\tikzstyle{snode} = [fig/module,minimum width=0.28cm,minimum height=0.28cm,inner sep=0pt]
\begin{scope}
\begin{scope}
\coordinate (a00) at (-2.5,-2.5);
\coordinate (a10) at (2.5,-2.5);
\coordinate (a01) at (-2.5,2.5);
\coordinate (a11) at (2.5,2.5);
\coordinate (abase) at (0,-3);
\coordinate (p1) at (-1.1,1.0);
\draw[fill=orange!15] (0,0) circle (2.0cm);
\draw[fill=orange!30] (0,0) circle (1.5cm);
\draw[fill=orange!45] (0,0) circle (1.0cm);
\draw[fill=orange!60] (0,0) circle (0.5cm);
\node [anchor=south] (t0label) at (0,2.6) {\small{$p_T$:先验分布}};
\node [anchor=center,circle,fill=blue,minimum size=4pt,inner sep=0] (point12) at (0.7,-0.5) {};
\end{scope}
\begin{scope}[xshift=8cm]
\coordinate (b00) at (-2.5,-2.5);
\coordinate (b10) at (2.5,-2.5);
\coordinate (b01) at (-2.5,2.5);
\coordinate (b11) at (2.5,2.5);
\coordinate (bbase) at (0,-3);
\coordinate (p2) at (-1.1,1.0);
\draw [black,fill=orange!15] plot [smooth cycle, tension=0.5] coordinates {(0,1.9) (-0.9,1.3) (-2.0,0.5) (-2.1,-0.3) (-1.4,-1.6) (0,-2.0) (1.2,-1.3) (1.4,-0.7) (1.6,-0.3) (2.0,0.4) (0.8,1.7) };
\draw [black,fill=orange!30] plot [smooth cycle, tension=0.5] coordinates {(0,1.5) (-0.7,1.0) (-1.5,0.4) (-1.6,-0.2) (-1.0,-1.2) (0,-1.5) (0.9,-1.0) (1.1,-0.5) (1.3,-0.2) (1.5,0.3) (0.6,1.3) };
\draw [black,fill=orange!45] plot [smooth cycle, tension=0.5] coordinates {(0,1.0) (-0.5,0.7) (-1.0,0.3) (-1.1,-0.1) (-0.7,-0.9) (0,-1.0) (0.6,-0.7) (0.8,-0.3) (0.9,-0.1) (1.0,0.2) (0.5,0.8) };
\draw [black,fill=orange!60] plot [smooth cycle, tension=0.5] coordinates {(0,0.4) (-0.3,0.3) (-0.5,0.2) (-0.5,-0.0) (-0.4,-0.4) (0,-0.5) (0.3,-0.4) (0.5,-0.1) (0.4,0.1) (0.2,0.4) };
\node [anchor=south] (t0label) at (0,2.6) {\small{$p_0$:数据分布}};
\node [anchor=center,circle,fill=blue,minimum size=4pt,inner sep=0] (point22) at (-0.2,1.2) {};
\end{scope}
\draw [-stealth,thick,shorten > = 1pt,shorten < = 1pt] (point12) -- (1.0,-0.1) -- (1.1,-0.2) -- (1.3,0.3) -- (1.35,0.2) -- (1.5,0.25) -- (1.8,0.5) -- (1.85,0.45) -- (1.9,0.48) -- (2.1,0.5) -- (2.04,0.45) -- (2.1,0.4) -- (2.2,0.5) -- (2.3,0.4) -- (2.5,0.3) -- (2.7,0.6) -- (2.75,0.45) -- (2.9,0.40) -- (3.0,0.5) -- (3.1,0.6) -- (3.3,0.1) -- (3.4,0.2) -- (3.5,0.7) -- (3.6,0.4) -- (3.8,0.6) -- (4.2,0.8) -- (4.3,1.0) -- (4.4,0.8) -- (4.5,1.0) -- (4.6,0.8) -- (4.7,1.0) -- (4.8,1.3) -- (5.0,0.6) -- (5.3,0.5) -- (5.4,0.7) -- (5.5,0.8) -- (5.6,0.9) -- (5.7,1.1) -- (5.9,1.0) -- (6.1,0.8) -- (6.3,1.0) -- (6.2,1.2) -- (6.4,1.4) -- (6.6,1.5) -- (6.7,1.5) -- (6.9,1.3) -- (7.1,1.0) -- (7.2,1.2) -- (7.3,1.1) -- (7.4,1.2) -- (point22);
\node [fig/caption,anchor=north] (caption) at (4.0,-3.0) {\small{(a) 基于得分的方法所产生的轨迹}};
\end{scope}
\begin{scope}[yshift=-8cm]
\begin{scope}
\coordinate (a00) at (-2.5,-2.5);
\coordinate (a10) at (2.5,-2.5);
\coordinate (a01) at (-2.5,2.5);
\coordinate (a11) at (2.5,2.5);
\coordinate (abase) at (0,-3);
\coordinate (p1) at (-1.1,1.0);
\draw[fill=orange!15] (0,0) circle (2.0cm);
\draw[fill=orange!30] (0,0) circle (1.5cm);
\draw[fill=orange!45] (0,0) circle (1.0cm);
\draw[fill=orange!60] (0,0) circle (0.5cm);
\node [anchor=south] (t0label) at (0,2.6) {\small{$p_T$:先验分布}};
\node [anchor=center,circle,fill=blue,minimum size=4pt,inner sep=0] (point12) at (0.7,-0.5) {};
\end{scope}
\begin{scope}[xshift=8cm]
\coordinate (b00) at (-2.5,-2.5);
\coordinate (b10) at (2.5,-2.5);
\coordinate (b01) at (-2.5,2.5);
\coordinate (b11) at (2.5,2.5);
\coordinate (bbase) at (0,-3);
\coordinate (p2) at (-1.1,1.0);
\draw [black,fill=orange!15] plot [smooth cycle, tension=0.5] coordinates {(0,1.9) (-0.9,1.3) (-2.0,0.5) (-2.1,-0.3) (-1.4,-1.6) (0,-2.0) (1.2,-1.3) (1.4,-0.7) (1.6,-0.3) (2.0,0.4) (0.8,1.7) };
\draw [black,fill=orange!30] plot [smooth cycle, tension=0.5] coordinates {(0,1.5) (-0.7,1.0) (-1.5,0.4) (-1.6,-0.2) (-1.0,-1.2) (0,-1.5) (0.9,-1.0) (1.1,-0.5) (1.3,-0.2) (1.5,0.3) (0.6,1.3) };
\draw [black,fill=orange!45] plot [smooth cycle, tension=0.5] coordinates {(0,1.0) (-0.5,0.7) (-1.0,0.3) (-1.1,-0.1) (-0.7,-0.9) (0,-1.0) (0.6,-0.7) (0.8,-0.3) (0.9,-0.1) (1.0,0.2) (0.5,0.8) };
\draw [black,fill=orange!60] plot [smooth cycle, tension=0.5] coordinates {(0,0.4) (-0.3,0.3) (-0.5,0.2) (-0.5,-0.0) (-0.4,-0.4) (0,-0.5) (0.3,-0.4) (0.5,-0.1) (0.4,0.1) (0.2,0.4) };
\node [anchor=south] (t0label) at (0,2.6) {\small{$p_0$:数据分布}};
\node [anchor=center,circle,fill=blue,minimum size=4pt,inner sep=0] (point22) at (-0.2,1.2) {};
\end{scope}
\draw [-stealth,thick,shorten > = 1pt,shorten < = 1pt] (point12) -- (point22);
\node [fig/caption,anchor=north] (caption) at (4.0,-3.0) {\small{(b) 流匹配方法的轨迹(直线路径传输)}};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssize{1}
\def\boxsize{1.3}
\def\smallsize{0.13}
\tikzstyle{snode} = [fig/module,minimum width=0.28cm,minimum height=0.28cm,inner sep=0pt]
\begin{scope}
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,2.5);
\node [anchor=north east,align=right] (ylabel) at (0-0.1,2.5) {\footnotesize{$\mathbf{x}(t)$}};
\node [anchor=north,align=left] (xlabel) at (13.3,-0.1) {\footnotesize{时间 $t$}};
\draw [Round Cap-Round Cap,blue,very thick,domain=0:12.7,samples=100] plot (\x,{0.3-0.001*(((\x-1)/1.15)^4) * (\x-1) + 0.029375*(((\x-1)/1.15)^4) + 1/(1.15)^4*(- (323/1200)*((\x-1)^3) + 0.75*((\x-1)^2))});
\node [anchor=west] (legend1) at (1.5,2.4) {\footnotesize{确定性轨迹}};
\draw [-,blue,very thick] ([xshift=-0.2cm]legend1.west) -- ([xshift=-0.8cm]legend1.west);
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(a) ODE(确定性轨迹)}};
\end{scope}
\begin{scope}[yshift=-6.5cm]
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,2.5);
\node [anchor=north east,align=right] (ylabel) at (0-0.1,2.5) {\footnotesize{$\mathbf{x}(t)$}};
\node [anchor=north,align=left] (xlabel) at (13.3,-0.1) {\footnotesize{时间 $t$}};
\draw [Round Cap-Round Cap,blue,very thick,domain=0:12.7,samples=100] plot (\x,{(0.3-0.001*(((\x-1)/1.15)^4) * (\x-1) + 0.029375*(((\x-1)/1.15)^4) + 1/(1.15)^4*(- (323/1200)*((\x-1)^3) + 0.75*((\x-1)^2))) * ((-\x/12.7) + 1)});
\begin{pgfonlayer}{background}
\draw [Round Cap-Round Cap,name path=linetop,dotted,domain=0:12.7,samples=100] plot (\x,{(0.3-0.001*(((\x-1)/1.15)^4) * (\x-1) + 0.029375*(((\x-1)/1.15)^4) + 1/(1.15)^4*(- (323/1200)*((\x-1)^3) + 0.75*((\x-1)^2))) * ((-\x/12.7) + 1) + 1 * (\x/12.7) });
\draw [Round Cap-Round Cap,name path=linebottom,dotted,domain=0:12.7,samples=100] plot (\x,{(0.3-0.001*(((\x-1)/1.15)^4) * (\x-1) + 0.029375*(((\x-1)/1.15)^4) + 1/(1.15)^4*(- (323/1200)*((\x-1)^3) + 0.75*((\x-1)^2))) * ((-\x/12.7) + 1) - 1 * (\x/12.7) });
\tikzfillbetween[of=linetop and linebottom,on layer=background]{orange!20}
\end{pgfonlayer}
\foreach \x in {10}{
\pgfmathsetmacro{\p}{(0.3-0.001*(((\x-1)/1.15)^4) * (\x-1) + 0.029375*(((\x-1)/1.15)^4) + 1/(1.15)^4*(- (323/1200)*((\x-1)^3) + 0.75*((\x-1)^2))) * ((-\x/12.7) + 1) + 1 * (\x/12.7) }
\pgfmathsetmacro{\q}{(0.3-0.001*(((\x-1)/1.15)^4) * (\x-1) + 0.029375*(((\x-1)/1.15)^4) + 1/(1.15)^4*(- (323/1200)*((\x-1)^3) + 0.75*((\x-1)^2))) * ((-\x/12.7) + 1) - 1 * (\x/12.7) }
\node [circle,minimum size=2pt,inner sep=0,fill=blue] (pointp) at (\x,\p) {};
\node [circle,minimum size=2pt,inner sep=0,fill=blue] (pointq) at (\x,\q) {};
\draw [<->,blue] ([yshift=-1pt]pointp.south) -- ([yshift=1pt]pointq.north);
}
\node [anchor=west] (legend1) at (1.5,2.4) {\footnotesize{确定性轨迹}};
\draw [-,blue,very thick] ([xshift=-0.2cm]legend1.west) -- ([xshift=-0.8cm]legend1.west);
\node [anchor=west] (legend2) at (1.5,1.9) {\footnotesize{随机噪声}};
\node [anchor=east,inner sep=0,draw,fill=orange!20,thin,dotted,minimum width=0.6cm,minimum height=0.2cm] (legend2label) at ([xshift=-0.2cm]legend2.west) {};
\node [anchor=south east] (noiselabel) at (10.7,0.6) {\footnotesize{噪声幅度}};
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(b) SDE(含随机噪声)}};
\end{scope}
\end{tikzpicture}
\end{document}
figure/figure-forward-sde-example.tex
前向 SDE 将双峰非高斯分布逐渐变换为标准高斯分布。
彩色曲线表示随机轨迹,灰色曲线表示确定性的概率流 ODE 轨迹。
figure/figure-forward-sde-example.tex
说明:
1. 左侧为双峰非高斯分布 p_0(x)
2. 右侧为单峰高斯先验 p_T(x)
3. 灰色曲线表示光滑的 probability flow ODE
4. 彩色曲线表示更粗糙、更曲折的 SDE 样本轨迹
使用方式:
\begin{figure}[!t]
\centering
\AMLFigure{figure-forward-sde-example}
\caption{...}
\label{fig:forward-sde-example}
\end{figure}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssize{1}
\def\boxsize{1.3}
\def\smallsize{0.13}
\def\yslant{0.6}
\def\xslant{0.0}
\tikzstyle{snode} = [fig/module,minimum width=0.28cm,minimum height=0.28cm,inner sep=0pt]
\begin{scope}
\begin{scope}[scale=0.8,
every node/.append style={yslant=\yslant,xslant=\xslant},
yslant=\yslant,xslant=\xslant]
\coordinate (a00) at (-2.5,-2.5);
\coordinate (a10) at (2.5,-2.5);
\coordinate (a01) at (-2.5,2.5);
\coordinate (a11) at (2.5,2.5);
\coordinate (abase) at (0,-3);
\coordinate (p1) at (-1.1,1.0);
\draw [-,thick] (-2.5,-2.5) rectangle (2.5,2.5);
\fill [white,fill opacity=.75] (-2.5,-2.5) rectangle (2.5,2.5);
\draw[fill=orange!15,fill opacity=.2] (0,0) circle (2.0cm);
\draw[fill=orange!30,fill opacity=.2] (0,0) circle (1.5cm);
\draw[fill=orange!45,fill opacity=.2] (0,0) circle (1.0cm);
\draw[fill=orange!60,fill opacity=.2] (0,0) circle (0.5cm);
\pgfmathsetseed{42}
\foreach \i in {0,1,...,100}{
\pgfmathsetmacro{\myval}{360*rnd}
\pgfmathsetmacro{\myr}{1.9*rnd}
\node [circle,inner sep=0,minimum size=2pt,fill=black!50] (point1\i) at ({\myr*cos(\myval)},{\myr*sin(\myval)}) {};
}
\end{scope}
\begin{scope}[xshift=8cm,scale=0.8,
every node/.append style={yslant=\yslant,xslant=\xslant},
yslant=\yslant,xslant=\xslant]
\coordinate (b00) at (-2.5,-2.5);
\coordinate (b10) at (2.5,-2.5);
\coordinate (b01) at (-2.5,2.5);
\coordinate (b11) at (2.5,2.5);
\coordinate (bbase) at (0,-3);
\coordinate (p2) at (-1.1,1.0);
\foreach \i in {0,1,...,100}{
\pgfmathsetmacro{\myval}{360*rnd}
\pgfmathsetmacro{\myr}{1.9*rnd}
\node [circle,inner sep=0,minimum size=2pt,fill=black!80] (point2\i) at ({\myr*cos(\myval)},{\myr*sin(\myval)}) {};
}
\foreach \i in {0,1,...,100}{
\draw [-{Latex[scale=1.0]},thin,black!20] (point1\i) -- (point2\i);
}
\draw [-,thick,fill=white,fill opacity=.3] (-2.5,-2.5) rectangle (2.5,2.5);
\fill [white,fill opacity=.75] (-2.5,-2.5) rectangle (2.5,2.5);
\draw [black,fill=orange!15,fill opacity=.2] plot [smooth cycle, tension=0.5] coordinates {(0,1.9) (-1.5,1.6) (-2.0,0.6) (-2.1,-0.3) (-1.4,-1.6) (0,-2.0) (1.2,-1.3) (1.8,-0.8) (2.1,-0.4) (2.0,0.4) (1.2,1.6) (0.9,2.0) };
\draw [black,fill=orange!30,fill opacity=.2] plot [smooth cycle, tension=0.5] coordinates {(0,1.5) (-0.7,1.2) (-1.5,0.4) (-1.6,-0.2) (-1.0,-1.2) (0,-1.5) (0.9,-1.0) (1.1,-0.5) (1.3,-0.2) (1.5,0.3) (0.6,1.3) };
\draw [black,fill=orange!45,fill opacity=.2] plot [smooth cycle, tension=0.5] coordinates {(0,1.0) (-0.5,0.7) (-1.0,0.3) (-1.1,-0.1) (-0.7,-0.9) (0,-1.0) (0.6,-0.7) (0.8,-0.3) (0.9,-0.1) (1.0,0.2) (0.5,0.8) };
\draw [black,fill=orange!60,fill opacity=.2] plot [smooth cycle, tension=0.5] coordinates {(0,0.4) (-0.3,0.3) (-0.5,0.2) (-0.5,-0.0) (-0.4,-0.4) (0,-0.5) (0.4,-0.4) (0.6,-0.1) (0.4,0.1) (0.2,0.4) };
\end{scope}
\node [circle,fill=blue,minimum size=4pt,inner sep=0] (point) at (3.5,-0.1) {};
\coordinate (c0) at ([xshift=0.4cm,yshift=0.3cm]point);
\coordinate (c1) at ([xshift=0.5cm,yshift=0.0cm]point);
\coordinate (c2) at ([xshift=0.4cm,yshift=-0.3cm]point);
\draw [-{Latex[scale=1.0]},shorten <= -0.5cm,black] (point) -- (c0);
\draw [-{Latex[scale=1.0]},shorten <= -0.5cm,red,thick] (point) -- (c1);
\draw [-{Latex[scale=1.0]},shorten <= -0.5cm,black] (point) -- (c2);
\node [circle,fill=blue,minimum size=4pt,inner sep=0] (point2) at (3.5,-0.1) {};
\node [anchor=north,align=center] (pointlabel) at ([yshift=-1.1cm]point.south) {\scriptsize{回归任意位置的}\\[-2pt] \scriptsize{速度期望}};
\node [anchor=north] (ptlabel) at (0,-3.5) {\footnotesize{先验分布 $p_T$}};
\node [anchor=north] (p0label) at (8,-3.5) {\footnotesize{数据分布 $p_0$}};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\sep{0.6}
\def\stepsize{1.3cm}
\def\ycoordp{4}
\def\vectorsize{1cm}
\def\smallsize{0.13}
\tikzstyle{snode} = [fig/module,minimum width=4.4cm,minimum height=0.8cm]
\tikzstyle{cnode} = [minimum width=0.3cm,minimum height=\vectorsize]
\begin{scope}
\node [anchor=center] (x11) at (0,0) {\small{}};
\node [anchor=center] (x12) at ([xshift=\stepsize]x11) {\small{}};
\node [anchor=center] (x13) at ([xshift=2*\stepsize]x11) {\small{}};
\node [anchor=center] (x14) at ([xshift=3*\stepsize]x11) {\small{}};
\node [anchor=center] (x15) at ([xshift=4*\stepsize,yshift=-1pt]x11) {\small{}};
\node [anchor=center] (x16) at ([xshift=5*\stepsize]x11) {\small{}};
\node [anchor=center] (x17) at ([xshift=6*\stepsize,yshift=-2pt]x11) {\small{}};
\node [anchor=center] (x1m1) at ([xshift=-\stepsize]x11) {\small{...}};
\node [anchor=east] (x1label) at ([xshift=-0.4cm]x1m1) {\small{$(\mathbf{c},\mathbf{x}(0))$}};
\node [anchor=center] (x21) at ([yshift=-2cm]x11) {\small{}};
\node [anchor=center] (x22) at ([xshift=\stepsize]x21) {\small{\texttt{\color{blue} [MASK]}}};
\node [anchor=center] (x23) at ([xshift=2*\stepsize]x21) {\small{}};
\node [anchor=center] (x24) at ([xshift=3*\stepsize]x21) {\small{}};
\node [anchor=center] (x25) at ([xshift=4*\stepsize]x21) {\small{\texttt{\color{blue} [MASK]}}};
\node [anchor=center] (x26) at ([xshift=5*\stepsize]x21) {\small{}};
\node [anchor=center] (x27) at ([xshift=6*\stepsize,yshift=-2pt]x21) {\small{}};
\node [anchor=center] (x2m1) at ([xshift=-\stepsize]x21) {\small{...}};
\node [anchor=east] (x2label) at ([xshift=-0.4cm]x2m1) {\small{$(\mathbf{c},\mathbf{x}(1))$}};
\node [anchor=center] (x31) at ([yshift=-2cm]x21) {\small{\texttt{\color{blue} [MASK]}}};
\node [anchor=center] (x32) at ([xshift=\stepsize]x31) {\small{\texttt{[MASK]}}};
\node [anchor=center] (x33) at ([xshift=2*\stepsize]x31) {\small{}};
\node [anchor=center] (x34) at ([xshift=3*\stepsize]x31) {\small{}};
\node [anchor=center] (x35) at ([xshift=4*\stepsize]x31) {\small{\texttt{[MASK]}}};
\node [anchor=center] (x36) at ([xshift=5*\stepsize]x31) {\small{\texttt{\color{blue} [MASK]}}};
\node [anchor=center] (x37) at ([xshift=6*\stepsize,yshift=-2pt]x31) {\small{}};
\node [anchor=center] (x3m1) at ([xshift=-\stepsize]x31) {\small{...}};
\node [anchor=east] (x3label) at ([xshift=-0.4cm]x3m1) {\small{$(\mathbf{c},\mathbf{x}(2))$}};
\node [anchor=center] (x41) at ([yshift=-2cm]x31) {\small{\texttt{[MASK]}}};
\node [anchor=center] (x42) at ([xshift=\stepsize]x41) {\small{\texttt{[MASK]}}};
\node [anchor=center] (x43) at ([xshift=2*\stepsize]x41) {\small{\texttt{\color{blue} [MASK]}}};
\node [anchor=center] (x44) at ([xshift=3*\stepsize]x41) {\small{}};
\node [anchor=center] (x45) at ([xshift=4*\stepsize]x41) {\small{\texttt{[MASK]}}};
\node [anchor=center] (x46) at ([xshift=5*\stepsize]x41) {\small{\texttt{[MASK]}}};
\node [anchor=center] (x47) at ([xshift=6*\stepsize]x41) {\small{\texttt{\color{blue} [MASK]}}};
\node [anchor=center] (x4m1) at ([xshift=-\stepsize]x41) {\small{...}};
\node [anchor=east] (x4label) at ([xshift=-0.4cm]x4m1) {\small{$(\mathbf{c},\mathbf{x}(3))$}};
\node [anchor=center] (x51) at ([yshift=-2cm]x41) {\small{\texttt{[MASK]}}};
\node [anchor=center] (x52) at ([xshift=\stepsize]x51) {\small{\texttt{[MASK]}}};
\node [anchor=center] (x53) at ([xshift=2*\stepsize]x51) {\small{\texttt{[MASK]}}};
\node [anchor=center] (x54) at ([xshift=3*\stepsize]x51) {\small{\texttt{\color{blue} [MASK]}}};
\node [anchor=center] (x55) at ([xshift=4*\stepsize]x51) {\small{\texttt{[MASK]}}};
\node [anchor=center] (x56) at ([xshift=5*\stepsize]x51) {\small{\texttt{[MASK]}}};
\node [anchor=center] (x57) at ([xshift=6*\stepsize]x51) {\small{\texttt{[MASK]}}};
\node [anchor=center] (x5m1) at ([xshift=-\stepsize]x51) {\small{...}};
\node [anchor=east] (x5label) at ([xshift=-0.4cm]x5m1) {\small{$(\mathbf{c},\mathbf{x}(4))$}};
\draw [->,line width=1.2pt] ([yshift=-0.2cm]x14.south) -- ([yshift=0.2cm]x24.north) node [pos=0.5,xshift=-0.2cm,left,align=right] {\footnotesize{将词元替换为}\\[-2pt] \footnotesize{\texttt{[MASK]}}};
\draw [->,line width=1.2pt] ([yshift=-0.2cm]x24.south) -- ([yshift=0.2cm]x34.north);
\draw [->,line width=1.2pt] ([yshift=-0.2cm]x34.south) -- ([yshift=0.2cm]x44.north);
\draw [->,line width=1.2pt] ([yshift=-0.2cm]x44.south) -- ([yshift=0.2cm]x54.north);
\draw [decorate,decoration={brace,amplitude=5pt,mirror,raise=0}] ([xshift=0.1cm,yshift=0.6cm]x17.east) -- ([xshift=-0.1cm,yshift=0.6cm]x11.west) node[midway,yshift=0.6cm,align=left] (x0labelabove) {\footnotesize{原始序列 $\mathbf{x}(0)$}};
\draw [->] ([yshift=0.2cm]x1m1.north) -- ([yshift=0.7cm]x1m1.north) node [pos=1,above,yshift=0.15cm] {\footnotesize{上下文 $\mathbf{c}$}};
\draw [decorate,decoration={brace,amplitude=5pt,mirror,raise=0}] ([xshift=-0.1cm,yshift=-0.6cm]x51.west) -- ([xshift=0.1cm,yshift=-0.6cm]x57.east) node[midway,yshift=-0.6cm,align=left] (x5labelbelow) {\footnotesize{全掩码序列 $\mathbf{x}(T)\ (T=4)$}};
\draw [->] (x36.north east) .. controls +(east:0.7cm) and +(east:0.7cm) .. (x26.south east) node [pos=0.5,right,align=left] {\scriptsize{训练时,模型学习}\\[-2pt] \scriptsize{恢复原始词元}};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssep{1.3cm}
\def\bsep{2.5cm}
\tikzstyle{lnode} = [fig/module,minimum width=3.0cm,minimum height=0.6cm,inner sep=2pt]
\begin{scope}
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,2.5);
\node [anchor=north east,align=right] (ylabel) at (0-0.1,2.5) {\footnotesize{位置 $x(t)$}};
\node [anchor=north,align=left] (xlabel) at (13.3,-0.1) {\footnotesize{时间 $t$}};
\draw [Round Cap-Round Cap,blue,very thick,domain=0:12.7,samples=100] plot (\x,{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))});
\foreach \x in {2.5}{
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\node [circle,minimum size=6pt,inner sep=0,fill=blue] (point) at (\x,\p) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px) at (\x,0) {};
\node [anchor=north] (plabel) at (\x,-0.1) {\footnotesize{$t_1$}};
\draw [-,dashed] (point) -- (px);
}
\draw [-,dotted,thick] (2.4,1.38) -- (2.6,0.37);
\draw [->,blue] (2.5,0.88) -- (4,1.17);
\node [anchor=south,xshift=1.3cm] (derivativelabel) at (4,1.3) {\footnotesize{$t_1$$x(t)$ 的导数 = $t_1$ 时的速度}};
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(a) 位置 $x(t)$ 随时间 $t$ 的变化}};
\end{scope}
\begin{scope}[yshift=-6.0cm]
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,2.5);
\node [anchor=north east,align=right] (ylabel) at (0-0.1,2.5) {\footnotesize{速度}\\[-2pt] \footnotesize{$f(x(t),t)$}\\[-2pt] \footnotesize{(即 $\frac{d x(t)}{d t}$}};
\node [anchor=north,align=left] (xlabel) at (13.3,-0.1) {\footnotesize{时间 $t$}};
\draw [Round Cap-Round Cap,blue,very thick,domain=0:12.7,samples=100] plot (\x,{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5)});
\foreach \x in {2.5}{
\pgfmathsetmacro{\p}{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5)}
\node [circle,minimum size=6pt,inner sep=0,fill=blue] (point) at (\x,\p) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px) at (\x,0) {};
\node [anchor=north] (plabel) at (\x,-0.1) {\footnotesize{$t_1$}};
\draw [-,dashed] (point) -- (px);
}
\node [anchor=south west] (velocity) at (2.5,0.5) {\footnotesize{$t_1$ 时的速度}};
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(b) 速度 $f(x(t),t)$(即 $\frac{d x(t)}{d t}$)随时间 $t$ 的变化}};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssep{1.3cm}
\def\bsep{2.5cm}
\tikzstyle{lnode} = [fig/module,minimum width=3.0cm,minimum height=0.6cm,inner sep=2pt]
\begin{scope}
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,2.5);
\node [anchor=north east,align=right] (ylabel) at (0-0.1,2.5) {\footnotesize{状态 $x(t)$}};
\node [anchor=north,align=left] (xlabel) at (13.3,-0.1) {\footnotesize{时间 $t$}};
\draw [Round Cap-Round Cap,blue,very thick,domain=0:12.7,samples=100] plot (\x,{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))});
\foreach \x in {2.0}{
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\node [circle,minimum size=6pt,inner sep=0,fill=blue] (point) at (\x,\p) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px) at (\x,0) {};
\node [anchor=north] (plabel) at (\x,-0.1) {\footnotesize{$t_0=0$}};
\node [anchor=south west] (t0label) at (\x-1,\p+0.2) {\footnotesize{初始状态 $x(0)$}};
\draw [-,dashed] (point) -- (px);
}
\foreach \x in {10.0}{
\pgfmathsetmacro{\p}{-0.001*((\x/1.15)^4) * \x + 0.029375*((\x/1.15)^4) + 1/(1.15)^4*(- (323/1200)*(\x^3) + 0.75*(\x^2))}
\node [circle,minimum size=6pt,inner sep=0,fill=blue] (point) at (\x,\p) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px) at (\x,0) {};
\node [anchor=south] (plabel) at (\x,0.1) {\footnotesize{$t$}};
\node [anchor=north west,align=left] (tnlabel) at (\x-0.5,\p-0.3) {\footnotesize{ODE 的解}\\[-2pt] \scriptsize{$x(t) = x(0) + \int_{0}^{t} f(x(\tau), \tau) d\tau$}};
\draw [-,dashed] (point) -- (px);
}
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(a) 状态 $x(t)$ 随时间 $t$ 的变化}};
\end{scope}
\begin{scope}[yshift=-6.0cm]
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,2.5);
\node [anchor=north east,align=right] (ylabel) at (0-0.1,2.5) {\footnotesize{动力学}\\[-2pt] \footnotesize{$f(x(t),t)$}};
\node [anchor=north,align=left] (xlabel) at (13.3,-0.1) {\footnotesize{时间 $t$}};
\draw [Round Cap-Round Cap,blue,very thick,domain=0:12.7,samples=100] plot (\x,{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5)});
\foreach \x in {2.0}{
\pgfmathsetmacro{\p}{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5)}
\node [circle,minimum size=6pt,inner sep=0,fill=blue] (point) at (\x,\p) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px) at (\x,0) {};
\node [anchor=north] (plabel) at (\x,-0.1) {\footnotesize{$t_0=0$}};
\draw [-,very thick,blue] (point) -- (px);
}
\foreach \x in {10.0}{
\pgfmathsetmacro{\p}{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5)}
\node [circle,minimum size=6pt,inner sep=0,fill=blue] (point) at (\x,\p) {};
\node [circle,minimum size=3pt,inner sep=0,fill=black] (px) at (\x,0) {};
\node [anchor=north] (plabel) at (\x,-0.1) {\footnotesize{$t$}};
\draw [-,very thick,blue] (point) -- (px);
}
\foreach \x in {2.1,2.2,...,9.9}{
\pgfmathsetmacro{\p}{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5)}
\draw [-,blue] (\x,0) rectangle (\x,\p);
}
\node [anchor=south] (integral) at (7,1) {\footnotesize{积分 $\int_{0}^{t} f(x(\tau), \tau) d\tau$}};
\draw [->] ([xshift=0.3cm,yshift=0.0cm]integral.south) .. controls +(-45:1.1cm) and +(135:1.1cm) .. (8.95,0.5);
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(b) 动力学 $f(x(t),t)$ 随时间 $t$ 的变化}};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssep{1cm}
\def\vsep{1.5cm}
\def\yslant{0.5}
\def\xslant{-0.8}
\tikzstyle{snode} = [fig/module,minimum width=0.28cm,minimum height=0.28cm,inner sep=0pt]
\begin{scope}
\coordinate (a00) at (-2.5,-2.5);
\coordinate (a10) at (2.5,-2.5);
\coordinate (a01) at (-2.5,2.5);
\coordinate (a11) at (2.5,2.5);
\coordinate (abase) at (0,-3);
\coordinate (p1) at (-1.1,1.0);
\draw [black,fill=orange!15] plot [smooth cycle, tension=0.5] coordinates {(0,1.9) (-0.9,1.3) (-2.0,0.5) (-2.1,-0.3) (-1.4,-1.6) (0,-2.0) (1.2,-1.3) (1.4,-0.7) (1.6,-0.3) (2.0,0.4) (0.8,1.7) };
\draw [black,fill=orange!30] plot [smooth cycle, tension=0.5] coordinates {(0,1.5) (-0.7,1.0) (-1.5,0.4) (-1.6,-0.2) (-1.0,-1.2) (0,-1.5) (0.9,-1.0) (1.1,-0.5) (1.3,-0.2) (1.5,0.3) (0.6,1.3) };
\draw [black,fill=orange!45] plot [smooth cycle, tension=0.5] coordinates {(0,1.0) (-0.5,0.7) (-1.0,0.3) (-1.1,-0.1) (-0.7,-0.9) (0,-1.0) (0.6,-0.7) (0.8,-0.3) (0.9,-0.1) (1.0,0.2) (0.5,0.8) };
\draw [black,fill=orange!60] plot [smooth cycle, tension=0.5] coordinates {(0,0.4) (-0.3,0.3) (-0.5,0.2) (-0.5,-0.0) (-0.4,-0.4) (0,-0.5) (0.3,-0.4) (0.5,-0.1) (0.4,0.1) (0.2,0.4) };
\node [anchor=south] (t0label) at (0,2.8) {\small{$p_0$$0$ 时刻的分布}};
\node [anchor=center,circle,fill=blue,minimum size=4pt,inner sep=0] (point11) at (-0.1,1.8) {};
\node [anchor=center,circle,fill=blue,minimum size=4pt,inner sep=0] (point12) at (-0.8,0.3) {};
\node [anchor=center,circle,fill=blue,minimum size=4pt,inner sep=0] (point13) at (-0.6,-0.8) {};
\node [anchor=center,circle,fill=blue,minimum size=4pt,inner sep=0] (point14) at (0.7,-1.5) {};
\end{scope}
\begin{scope}[xshift=8cm]
\coordinate (b00) at (-2.5,-2.5);
\coordinate (b10) at (2.5,-2.5);
\coordinate (b01) at (-2.5,2.5);
\coordinate (b11) at (2.5,2.5);
\coordinate (bbase) at (0,-3);
\coordinate (p2) at (-1.1,1.0);
\draw [black,fill=orange!15] plot [smooth cycle, tension=0.5] coordinates {(0,2.2) (-0.9,1.6) (-1.8,0.3) (-2.0,-0.5) (-1.2,-1.8) (0,-2.1) (0.9,-2.0) (1.7,-0.2) (1.9,0.7) (0.8,1.9) };
\draw [black,fill=orange!30] plot [smooth cycle, tension=0.5] coordinates {(0,1.6) (-0.6,1.2) (-1.4,-0.2) (-0.7,-1.2) (0,-1.5) (0.6,-1.2) (1.2,0) (1.3,0.6) (0.5,1.4)};
\draw [black,fill=orange!45] plot [smooth cycle, tension=0.5] coordinates {(0,0.8) (-0.3,0.7) (-0.8,-0.1) (-0.4,-0.7) (0,-0.8) (0.4,-0.7) (0.7,0) (0.6,0.3) (0.4,0.7)};
\draw [black,fill=orange!60] plot [smooth cycle, tension=0.5] coordinates {(0,0.4) (-0.2,0.3) (-0.4,-0.0) (-0.2,-0.3) (0,-0.4) (0.2,-0.3) (0.4,0.1) (0.2,0.3)};
\node [anchor=south] (t0label) at (0,2.8) {\small{$p_t$$t$ 时刻的分布}};
\node [anchor=center,circle,fill=blue,minimum size=4pt,inner sep=0] (point21) at (0.2,1.7) {};
\node [anchor=center,circle,fill=blue,minimum size=4pt,inner sep=0] (point22) at (-0.1,0.1) {};
\node [anchor=center,circle,fill=blue,minimum size=4pt,inner sep=0] (point23) at (-1.3,-0.7) {};
\node [anchor=center,circle,fill=blue,minimum size=4pt,inner sep=0] (point24) at (0.1,-1.6) {};
\end{scope}
\draw [-stealth,dotted,shorten > = 1pt,shorten < = 1pt] (point11) .. controls +(60:1.3cm) and +(120:1.3cm) .. (point21);
\draw [-stealth,dotted,shorten > = 1pt,shorten < = 1pt] (point12) .. controls +(45:1.5cm) and +(110:1.5cm) .. (point22) node [pos=0.5,above,xshift=0.5cm] (sampletrajectory) {\scriptsize{$\Phi_{0 \to t}$ 定义的样本轨迹}};
\draw [-stealth,dotted,shorten > = 1pt,shorten < = 1pt] (point13) .. controls +(-30:1.2cm) and +(-160:1.2cm) .. (point23);
\draw [-stealth,dotted,shorten > = 1pt,shorten < = 1pt] (point14) .. controls +(-45:1.0cm) and +(-160:1.0cm) .. (point24);
\draw [->,thick,align=left] ([xshift=-0.5cm]abase) -- ([xshift=0.5cm]bbase) node [pos=0.5,below,yshift=-0.1cm] (pushforwardlabel) {\footnotesize{$\Phi_{0 \to t}$ 诱导的前推操作:$p_t = (\Phi_{0 \to t})_{\#} p_0$}\\[-2pt] \footnotesize{分布 $p_0$ 被变换为分布 $p_t$}};
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssep{1.3cm}
\def\bsep{2.5cm}
\tikzstyle{lnode} = [fig/module,minimum width=3.0cm,minimum height=0.6cm,inner sep=2pt]
\begin{scope}
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,3.3);
\node [anchor=north,align=right] (xlabel) at (12.8,-0.1) {\footnotesize{时间 $t$}\\[-2pt] \footnotesize{(或深度)}};
\node [anchor=west,align=right] (legend1) at (0.7,3.0) {\footnotesize{动力学 $f(\mathbf{x}(t),\theta(t))$}};
\draw [Round Cap-Round Cap,very thick,blue] (legend1.west) -- ([xshift=-0.4cm]legend1.west);
\node [anchor=north west,align=right] (legend2) at ([yshift=0.1cm]legend1.south west) {\footnotesize{参数 $\theta(t)$}};
\draw [Round Cap-Round Cap,very thick,red!80] (legend2.west) -- ([xshift=-0.4cm]legend2.west);
\draw [Round Cap-Round Cap,very thick,blue,domain=0:12.1,samples=100] plot (\x,{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.0)});
\draw [Round Cap-Round Cap,very thick,red!80,domain=0:12.1,samples=100] plot (\x,{ 1/120 * (\x +3) * (\x - 5) * (\x - 11.0)});
\foreach \x in {2.5}{
\pgfmathsetmacro{\p}{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5)}
}
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(a) 系统动力学与参数随时间平滑演化}};
\end{scope}
\begin{scope}[yshift=-7cm]
\draw [-{Latex[scale=1.2]},thick] (0,0) -- (13.5,0);
\draw [-{Latex[scale=1.2]},thick] (0,-1.5) -- (0,3.3);
\node [anchor=north,align=right] (xlabel) at (12.8,-0.1) {\footnotesize{时间 $t$}\\[-2pt] \footnotesize{(或深度)}};
\node [anchor=west,align=right] (legend1) at (0.7,3.0) {\footnotesize{动力学 $f(\mathbf{x}(t),\theta(t))$}};
\draw [Round Cap-Round Cap,very thick,blue] (legend1.west) -- ([xshift=-0.4cm]legend1.west);
\node [anchor=north west,align=right] (legend2) at ([yshift=0.1cm]legend1.south west) {\footnotesize{参数 $\theta(t)$}};
\draw [Round Cap-Round Cap,very thick,red!80] (legend2.west) -- ([xshift=-0.4cm]legend2.west);
\draw [Round Cap-Round Cap,very thick,red!80] (0,1.5) -- (3,1.5);
\draw [Round Cap-Round Cap,very thick,red!80] (3,0.2) -- (6,0.2);
\draw [Round Cap-Round Cap,very thick,red!80] (6,-0.8) -- (9,-0.8);
\draw [Round Cap-Round Cap,very thick,red!80] (9,-0.3) -- (12.1,-0.3);
\draw [-,dashed,red!80] (3,1.5) -- (3,0.2);
\draw [-,dashed,red!80] (6,0.2) -- (6,-0.8);
\draw [-,dashed,red!80] (9,-0.8) -- (9,-0.3);
\node [circle,minimum size=5pt,inner sep=0,fill=red!80] (ppoint1) at (3,0.2) {};
\node [circle,minimum size=5pt,inner sep=0,fill=red!80] (ppoint2) at (3,1.5) {};
\node [circle,minimum size=5pt,inner sep=0,fill=red!80] (ppoint3) at (6,0.2) {};
\node [circle,minimum size=5pt,inner sep=0,fill=red!80] (ppoint4) at (6,-0.8) {};
\node [circle,minimum size=5pt,inner sep=0,fill=red!80] (ppoint5) at (9,-0.8) {};
\node [circle,minimum size=5pt,inner sep=0,fill=red!80] (ppoint6) at (9,-0.3) {};
\draw [Round Cap-Round Cap,very thick,blue] (0,0) .. controls +(60:2cm) and +(130:2cm) .. (3,-0.3);
\draw [Round Cap-,very thick,blue] (3,-1.1) .. controls +(30:0.5cm) and +(140:0.5cm) .. (4.0,-1.2);
\draw [-Round Cap,very thick,blue] (4.0,-1.2) .. controls +(-40:1.0cm) and +(-100:0.2cm) .. (6,0.7);
\draw [Round Cap-Round Cap,very thick,blue] (6,1.2) .. controls +(70:3cm) and +(130:0.8cm) .. (9,2.2);
\draw [Round Cap-Round Cap,very thick,blue] (9,0.9) .. controls +(-60:1cm) and +(180:0.5cm) .. (12.1,0.2);
\draw [-,dashed,blue] (3,-0.3) -- (3,-1.1);
\draw [-,dashed,blue] (6,0.7) -- (6,1.2);
\draw [-,dashed,blue] (9,2.2) -- (9,0.9);
\node [circle,minimum size=5pt,inner sep=0,fill=blue] (dpoint1) at (3,-0.3) {};
\node [circle,minimum size=5pt,inner sep=0,fill=blue] (dpoint2) at (3,-1.1) {};
\node [circle,minimum size=5pt,inner sep=0,fill=blue] (dpoint3) at (6,0.7) {};
\node [circle,minimum size=5pt,inner sep=0,fill=blue] (dpoint4) at (6,1.2) {};
\node [circle,minimum size=5pt,inner sep=0,fill=blue] (dpoint5) at (9,2.2) {};
\node [circle,minimum size=5pt,inner sep=0,fill=blue] (dpoint6) at (9,0.9) {};
\node [anchor=north west,align=left] (changeinparam) at (3.5,1.8) {\scriptsize{参数突变}};
\draw [->] ([yshift=0.2cm]changeinparam.south west) -- (3.1,1.0);
\node [anchor=north east,align=right] (changeindyn) at (2.4,-0.6) {\scriptsize{动力学突变}};
\draw [->] ([yshift=0.2cm]changeindyn.east) -- (2.9,-0.6);
\foreach \x in {2.5}{
\pgfmathsetmacro{\p}{ - 1/200 * (\x -0) * (\x - 3) * (\x - 8) * (\x - 12.5)}
}
\node [fig/caption,anchor=north] (caption) at (6.5,-2.0) {\small{(b) 参数突变导致系统动力学发生跃迁}};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\sep{0.6}
\def\ycoordp{4}
\def\vectorsize{1cm}
\def\smallsize{0.13}
\def\yslant{0.6}
\def\xslant{0.0}
\tikzstyle{snode} = [fig/module,minimum width=3.8cm,minimum height=0.8cm]
\tikzstyle{cnode} = [minimum width=0.3cm,minimum height=\vectorsize]
\begin{scope}
\coordinate (s11) at (0,0);
\coordinate (s12) at ({0+\sep},0);
\coordinate (s13) at ({0+2*\sep},0);
\coordinate (s14) at ({0+3*\sep},0);
\coordinate (s15) at ({0+4*\sep},0);
\coordinate (s16) at ({0+5*\sep},0);
\coordinate (s17) at ({0+6*\sep},0);
\node [cnode,anchor=north,fill=orange!30] (v11) at (s11) {};
\node [cnode,anchor=north,fill=orange!30] (v12) at (s12) {};
\node [cnode,anchor=north,fill=orange!30] (v13) at (s13) {};
\node [cnode,anchor=north,fill=ugreen!30] (v14) at (s14) {};
\node [cnode,anchor=north,fill=ugreen!30] (v15) at (s15) {};
\node [cnode,anchor=north,fill=ugreen!30] (v16) at (s16) {};
\node [cnode,anchor=north,fill=ugreen!30] (v17) at (s17) {};
\draw [->] ([yshift=0.3cm]s11) -- ([yshift=0.8cm]s11);
\draw [->] ([yshift=0.3cm]s12) -- ([yshift=0.8cm]s12);
\draw [->] ([yshift=0.3cm]s13) -- ([yshift=0.8cm]s13);
\draw [->] ([yshift=0.3cm]s14) -- ([yshift=0.8cm]s14);
\draw [->] ([yshift=0.3cm]s15) -- ([yshift=0.8cm]s15);
\draw [->] ([yshift=0.3cm]s16) -- ([yshift=0.8cm]s16);
\draw [->] ([yshift=0.3cm]s17) -- ([yshift=0.8cm]s17);
\draw [decorate,decoration={brace,amplitude=5pt,mirror,raise=0}] ([xshift=-0.2cm,yshift=-\vectorsize-0.3cm]s11) -- ([xshift=0.2cm,yshift=-\vectorsize-0.3cm]s13) node[midway,yshift=-0.8cm,xshift=-0.2cm,align=center]{\scriptsize{上下文 $\mathbf{c}$ 的嵌入}\\[-2pt] \scriptsize{(即 $\mathbf{e}_c$}};
\draw [decorate,decoration={brace,amplitude=5pt,mirror,raise=0}] ([xshift=-0.2cm,yshift=-\vectorsize-0.3cm]s14) -- ([xshift=0.2cm,yshift=-\vectorsize-0.3cm]s17) node[midway,yshift=-0.8cm,xshift=0.2cm,align=center]{\scriptsize{目标序列的}\\[-2pt] \scriptsize{中间状态}};
\draw [-] ([yshift=0.1cm,xshift=-0.2cm]s11) -- ([yshift=0.1cm,xshift=-0.4cm]s11) -- ([yshift=0.1cm-\vectorsize-0.2cm,xshift=-0.4cm]s11) -- ([yshift=0.1cm-\vectorsize-0.2cm,xshift=-0.4cm]s11) -- ([yshift=0.1cm-\vectorsize-0.2cm,xshift=-0.2cm]s11);
\draw [-] ([yshift=0.1cm,xshift=0.2cm]s17) -- ([yshift=0.1cm,xshift=0.4cm]s17) -- ([yshift=0.1cm-\vectorsize-0.2cm,xshift=0.4cm]s17) -- ([yshift=0.1cm-\vectorsize-0.2cm,xshift=0.4cm]s17) -- ([yshift=0.1cm-\vectorsize-0.2cm,xshift=0.2cm]s17);
\coordinate (s21) at (0,\ycoordp);
\coordinate (s22) at ({0+\sep},\ycoordp);
\coordinate (s23) at ({0+2*\sep},\ycoordp);
\coordinate (s24) at ({0+3*\sep},\ycoordp);
\coordinate (s25) at ({0+4*\sep},\ycoordp);
\coordinate (s26) at ({0+5*\sep},\ycoordp);
\coordinate (s27) at ({0+6*\sep},\ycoordp);
\node [cnode,anchor=south,fill=orange!30] (v21) at (s21) {};
\node [cnode,anchor=south,fill=orange!30] (v22) at (s22) {};
\node [cnode,anchor=south,fill=orange!30] (v23) at (s23) {};
\node [cnode,anchor=south,fill=ugreen!30] (v24) at (s24) {};
\node [cnode,anchor=south,fill=ugreen!30] (v25) at (s25) {};
\node [cnode,anchor=south,fill=ugreen!30] (v26) at (s26) {};
\node [cnode,anchor=south,fill=ugreen!30] (v27) at (s27) {};
\draw [<-] ([yshift=-0.3cm]s21) -- ([yshift=-0.8cm]s21);
\draw [<-] ([yshift=-0.3cm]s22) -- ([yshift=-0.8cm]s22);
\draw [<-] ([yshift=-0.3cm]s23) -- ([yshift=-0.8cm]s23);
\draw [<-] ([yshift=-0.3cm]s24) -- ([yshift=-0.8cm]s24);
\draw [<-] ([yshift=-0.3cm]s25) -- ([yshift=-0.8cm]s25);
\draw [<-] ([yshift=-0.3cm]s26) -- ([yshift=-0.8cm]s26);
\draw [<-] ([yshift=-0.3cm]s27) -- ([yshift=-0.8cm]s27);
\draw [decorate,decoration={brace,amplitude=5pt,mirror,raise=0}] ([xshift=0.2cm,yshift=\vectorsize+0.3cm]s27) -- ([xshift=-0.2cm,yshift=\vectorsize+0.3cm]s24) node[midway,yshift=0.6cm,align=center] (scorelabel) {\scriptsize{得分函数}\\[-2pt] \scriptsize{或向量场}};
\draw [-] ([yshift=-0.1cm,xshift=-0.2cm]s21) -- ([yshift=-0.1cm,xshift=-0.4cm]s21) -- ([yshift=-0.1cm+\vectorsize+0.2cm,xshift=-0.4cm]s21) -- ([yshift=-0.1cm+\vectorsize+0.2cm,xshift=-0.4cm]s21) -- ([yshift=-0.1cm+\vectorsize+0.2cm,xshift=-0.2cm]s21);
\draw [-] ([yshift=-0.1cm,xshift=0.2cm]s27) -- ([yshift=-0.1cm,xshift=0.4cm]s27) -- ([yshift=-0.1cm+\vectorsize+0.2cm,xshift=0.4cm]s27) -- ([yshift=-0.1cm+\vectorsize+0.2cm,xshift=0.4cm]s27) -- ([yshift=-0.1cm+\vectorsize+0.2cm,xshift=0.2cm]s27);
\node [anchor=west] (sp1label) at ([xshift=1.7cm]scorelabel) {\small{$\bar{\mathbf{s}}(t-\Delta t) \in \mathbb{R}^{d \times (n_c + n)}$}};
\draw [->] (scorelabel.north) .. controls +(north:0.8cm) and +(north:0.8cm) .. (sp1label.north) node [pos=0.5,above] {\small{更新 $\bar{\mathbf{s}}(t)$}};
\node [fig/module primary,anchor=south,minimum width=5cm,minimum height=2cm] (transformer) at ([yshift=1cm]s14) {\large{Transformer}};
\node [anchor=west] (stlabel) at ([xshift=0.8cm,yshift=-0.5*\vectorsize]s17) {\small{$\bar{\mathbf{s}}(t) \in \mathbb{R}^{d \times (n_c + n)}$}};
\node [snode,anchor=west,draw=blue] (st) at ([xshift=5.5cm,yshift=0.6cm]transformer.east) {\footnotesize{$t$ 时刻的演化}};
\node [snode,anchor=south] (stp1) at ([yshift=2cm]st.north) {\footnotesize{$t-\Delta t$ 时刻的演化}};
\node [anchor=center] (pdots) at ([yshift=1cm]stp1.north) {$\cdots$};
\node [anchor=center] (mdots) at ([yshift=-2.0cm]st.south) {$\cdots$};
\node [anchor=center,align=center] (sinitial) at ([yshift=-1cm]mdots.south) {\footnotesize{初始状态 $\bar{\mathbf{s}}(T)$}};
\node [snode,anchor=center,draw=white] (final) at ([yshift=1cm]pdots.north) {\footnotesize{终态 $\bar{\mathbf{s}}(0)$}};
\node [snode,anchor=center] (mapping) at ([yshift=1cm]final.north) {\footnotesize{映射为词元}};
\node [anchor=center] (tokens) at ([yshift=1cm]mapping.north) {\footnotesize{词元序列 $\mathbf{x}$}};
\draw [->,thick] (sinitial.north) -- (mdots.south);
\draw [->,thick] (mdots.north) -- ([yshift=-3pt]st.south);
\draw [->,thick] ([yshift=3pt]st.north) -- ([yshift=-3pt]stp1.south);
\draw [->,thick] ([yshift=3pt]stp1.north) -- (pdots.south);
\draw [->,thick] (pdots.north) -- ([yshift=0]final.south);
\draw [->,thick] (final.north) -- ([yshift=-3pt]mapping.south);
\draw [->,thick] ([yshift=3pt]mapping.north) -- (tokens.south);
\draw [-,blue] ([xshift=-2pt,yshift=0.2cm]st.west) -- ([xshift=-1.5cm,yshift=3.5cm]st.west);
\draw [-,blue] ([xshift=-2pt,yshift=-0.2cm]st.west) -- ([xshift=-1.5cm,yshift=-3.5cm]st.west);
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\tikzstyle{input} = [rectangle, minimum width = 0.6cm, minimum height = 0.3cm, text centered]
\tikzstyle{output} = [rectangle, minimum width = 0.6cm, minimum height = 0.3cm, text centered]
\tikzstyle{function} = [fig/module primary,rectangle,minimum width = 0.65cm, minimum height = 0.34cm, text centered]
\tikzstyle{addition} = [circle, thick, minimum size = 0.2cm, text centered, draw = black]
\tikzstyle{arrow} = [fig/flow]
\tikzstyle{line} = [thick, -]
\tikzstyle{point}=[coordinate]
\def\wsepf{1.4cm}
\def\wseps{0.8cm}
\def\wsept{0.6cm}
\begin{tikzpicture}[node distance = 0cm]
\begin{scope}
\node(output)[output]{\small{$\mathbf{x}_{k+1}$}};
\node(addition1)[addition,below of = output,yshift = -2.6cm]{};
\node(f1)[function,below of = addition1,xshift = \wsepf,yshift = -1.3cm]{\tiny $F$};
\node(input)[input,below of = addition1,yshift = -2.6cm]{\small{$\mathbf{x}_k$}};
\node(point1)[point,below of = addition1,xshift = 0.1cm, yshift = 0cm,align=center]{};
\node(point2)[point,above of = addition1,xshift = -0.1cm, yshift = 0cm,align=center]{};
\node(point3)[point,right of = addition1,xshift = 0cm, yshift = 0.1cm,align=center]{};
\node(point4)[point,left of = addition1,xshift = 0cm, yshift = -0.1cm,align=center]{};
\draw [arrow] (input) -- (addition1);
\draw [arrow, rounded corners=3pt] (input.east) -| (f1.south);
\draw [arrow, rounded corners=3pt] (f1.north) |- (addition1);
\draw [arrow] (addition1) -- (output);
\draw [line] (point1) -- (point2);
\draw [line] (point3) -- (point4);
\node[fig/caption,font=\footnotesize] at ([xshift=1.4em,yshift=-1em]input.south) {(a) 预归一化};
\end{scope}
\begin{scope}[xshift=1.7in]
\node(output)[output]{\small{$\mathbf{x}_{k+1}$}};
\node(addition1)[addition,below of = output,yshift = -1.6cm]{};
\node(addition2)[addition,below of = addition1,yshift = -1.5cm]{};
\node(f1)[function,below of = addition2,xshift = \wseps,yshift = -0.9cm]{\tiny $F$};
\node(addition3)[addition,right of = f1,xshift = \wseps]{};
\node(f2)[function,above of = addition3,yshift = 0.9cm]{\tiny $F$};
\node(input)[input,below of = addition2,yshift = -2.1cm]{\small{$\mathbf{x}_k$}};
\node(point1)[point,below of = addition1,xshift = 0.1cm, yshift = 0cm,align=center]{};
\node(point2)[point,above of = addition1,xshift = -0.1cm, yshift = 0cm,align=center]{};
\node(point3)[point,right of = addition1,xshift = 0cm, yshift = 0.1cm,align=center]{};
\node(point4)[point,left of = addition1,xshift = 0cm, yshift = -0.1cm,align=center]{};
\node(point5)[point,below of = addition2,xshift = 0.1cm, yshift = 0cm,align=center]{};
\node(point6)[point,above of = addition2,xshift = -0.1cm, yshift = 0cm,align=center]{};
\node(point7)[point,right of = addition2,xshift = 0cm, yshift = 0.1cm,align=center]{};
\node(point8)[point,left of = addition2,xshift = 0cm, yshift = -0.1cm,align=center]{};
\node(point9)[point,below of = addition3,xshift = 0.1cm, yshift = 0cm,align=center]{};
\node(point10)[point,above of = addition3,xshift = -0.1cm, yshift = 0cm,align=center]{};
\node(point11)[point,right of = addition3,xshift = 0cm, yshift = 0.1cm,align=center]{};
\node(point12)[point,left of = addition3,xshift = 0cm, yshift = -0.1cm,align=center]{};
\draw [arrow] (input) -- (addition2);
\draw [arrow, rounded corners=2pt] ([xshift=-0.0cm, yshift=0.15cm]input.east) -| (f1.south);
\draw [arrow, rounded corners=2pt] (f1.north) node[above,xshift=-0.15cm,yshift=0.7cm, scale=0.9]{ $\frac {1}{2}$} |- (addition2);
\draw [arrow] (addition2) -- (addition1);
\draw [arrow] (addition1) -- (output);
\draw [arrow] (f1.east) --(f1.east)-- (addition3);
\draw [arrow, rounded corners=3pt] (input.east) -| (addition3);
\draw [arrow] (addition3) -- (f2.south);
\draw [arrow, rounded corners=3pt] (f2.north) |- node[above, xshift=-0.5cm,scale=0.9]{ $\frac {1}{2}$}(addition1);
\draw [line] (point1) -- (point2);
\draw [line] (point3) -- (point4);
\draw [line] (point5) -- (point6);
\draw [line] (point7) -- (point8);
\draw [line] (point9) -- (point10);
\draw [line] (point11) -- (point12);
\node[fig/caption,font=\footnotesize] at ([xshift=1.8em,yshift=-1em]input.south) {(b) RK2};
\end{scope}
\begin{scope}[xshift=3.4in]
\node(output)[output]{\small{$\mathbf{x}_{k+1}$}};
\node(addition1)[addition,below of = output,yshift = -0.8cm]{};
\node(addition2)[addition,below of = addition1,yshift = -1cm]{};
\node(addition3)[addition,below of = addition2,yshift = -1cm]{};
\node(addition4)[addition,below of = addition3,yshift = -1cm]{};
\node(f1)[function,below of = addition4,xshift = \wsept,yshift = -0.55cm]{\tiny $F$};
\node(addition5)[addition,right of = f1,xshift = \wsept*1.6]{};
\node(f2)[function,above of = addition5,yshift = 0.7cm]{\tiny $F$};
\node(addition6)[addition,right of = f2,xshift = \wsept*1.6]{};
\node(f3)[function,above of = addition6,yshift = 0.7cm]{\tiny $F$};
\node(addition7)[addition,right of = f3,xshift = \wsept*1.6]{};
\node(f4)[function,above of = addition7,yshift = 1cm]{\tiny $F$};
\node(input)[input,below of = addition4,yshift = -1.4cm]{\small{$\mathbf{x}_k$}};
\node(p1)[point,left of = addition6, yshift = -1.4cm,align=center]{};
\node(p2)[point,left of = addition7, yshift = -2.25cm,align=center]{};
\node(point1)[point,below of = addition1,xshift = 0.1cm, yshift = 0cm,align=center]{};
\node(point2)[point,above of = addition1,xshift = -0.1cm, yshift = 0cm,align=center]{};
\node(point3)[point,right of = addition1,xshift = 0cm, yshift = 0.1cm,align=center]{};
\node(point4)[point,left of = addition1,xshift = 0cm, yshift = -0.1cm,align=center]{};
\node(point5)[point,below of = addition2,xshift = 0.1cm, yshift = 0cm,align=center]{};
\node(point6)[point,above of = addition2,xshift = -0.1cm, yshift = 0cm,align=center]{};
\node(point7)[point,right of = addition2,xshift = 0cm, yshift = 0.1cm,align=center]{};
\node(point8)[point,left of = addition2,xshift = 0cm, yshift = -0.1cm,align=center]{};
\node(point9)[point,below of = addition5,xshift = 0.1cm, yshift = 0cm,align=center]{};
\node(point10)[point,above of = addition5,xshift = -0.1cm, yshift = 0cm,align=center]{};
\node(point11)[point,right of = addition5,xshift = 0cm, yshift = 0.1cm,align=center]{};
\node(point12)[point,left of = addition5,xshift = 0cm, yshift = -0.1cm,align=center]{};
\node(point13)[point,below of = addition3,xshift = 0.1cm, yshift = 0cm,align=center]{};
\node(point14)[point,above of = addition3,xshift = -0.1cm, yshift = 0cm,align=center]{};
\node(point15)[point,right of = addition3,xshift = 0cm, yshift = 0.1cm,align=center]{};
\node(point16)[point,left of = addition3,xshift = 0cm, yshift = -0.1cm,align=center]{};
\node(point17)[point,below of = addition4,xshift = 0.1cm, yshift = 0cm,align=center]{};
\node(point18)[point,above of = addition4,xshift = -0.1cm, yshift = 0cm,align=center]{};
\node(point19)[point,right of = addition4,xshift = 0cm, yshift = 0.1cm,align=center]{};
\node(point20)[point,left of = addition4,xshift = 0cm, yshift = -0.1cm,align=center]{};
\node(point21)[point,below of = addition6,xshift = 0.1cm, yshift = 0cm,align=center]{};
\node(point22)[point,above of = addition6,xshift = -0.1cm, yshift = 0cm,align=center]{};
\node(point23)[point,right of = addition6,xshift = 0cm, yshift = 0.1cm,align=center]{};
\node(point24)[point,left of = addition6,xshift = 0cm, yshift = -0.1cm,align=center]{};
\node(point25)[point,below of = addition7,xshift = 0.1cm, yshift = 0cm,align=center]{};
\node(point26)[point,above of = addition7,xshift = -0.1cm, yshift = 0cm,align=center]{};
\node(point27)[point,right of = addition7,xshift = 0cm, yshift = 0.1cm,align=center]{};
\node(point28)[point,left of = addition7,xshift = 0cm, yshift = -0.1cm,align=center]{};
\draw [arrow] (input) -- (addition4);
\draw [arrow, rounded corners=1pt] ([xshift=-0.1cm,yshift=0.15cm]input.east) -| (f1.south);
\draw [arrow, rounded corners=1pt] (f1.north) |- node[above,pos=0.6,scale=0.9]{$\frac {1}{6}$}(addition4);
\draw [arrow] (f1.east) -- node[above,pos=0.5,scale=0.9]{$\frac {1}{2}$}(addition5);
\draw [arrow] (addition4) -- (addition3);
\draw [arrow] (addition3) -- (addition2);
\draw [arrow] (addition2) -- (addition1);
\draw [arrow] (addition1) -- (output);
\draw [arrow, rounded corners=2pt] ([xshift=-0.1cm, yshift=0.05cm]input.east) -| (addition5);
\draw [arrow] (addition5) -- (f2.south);
\draw [arrow, rounded corners=3pt] (f2.north) |- node[above,pos=0.75,scale=0.9]{$\frac {2}{6}$}(addition3);
\draw [arrow, rounded corners=3pt] ([xshift=-0.1cm, yshift=-0.05cm]input.east) -| (addition6);
\draw [arrow, rounded corners=3pt] ([xshift=-0.1cm, yshift=-0.15cm]input.east) -| (addition7);
\draw [arrow, rounded corners=3pt] (f3.north) |- node[above,pos=0.5,scale=0.9]{$\frac {2}{6}$}(addition2);
\draw [arrow, rounded corners=3pt] (f4.north) |- node[above,scale=0.9]{$\frac {1}{6}$}(addition1);
\draw [arrow] (addition6) -- (f3.south);
\draw [arrow] (addition7) -- (f4.south);
\draw [arrow] (f2.east) -- node[above,pos=0.5,scale=0.9]{$\frac {1}{2}$}(addition6);
\draw [arrow] (f3.east) -- (addition7);
\draw [line] (point1) -- (point2);
\draw [line] (point3) -- (point4);
\draw [line] (point5) -- (point6);
\draw [line] (point7) -- (point8);
\draw [line] (point9) -- (point10);
\draw [line] (point11) -- (point12);
\draw [line] (point13) -- (point14);
\draw [line] (point15) -- (point16);
\draw [line] (point17) -- (point18);
\draw [line] (point19) -- (point20);
\draw [line] (point21) -- (point22);
\draw [line] (point23) -- (point24);
\draw [line] (point25) -- (point26);
\draw [line] (point27) -- (point28);
\node[fig/caption,font=\footnotesize] at ([xshift=4em,yshift=-1em]input.south) {(c) RK4};
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssep{1.3cm}
\def\bsep{2.5cm}
\tikzstyle{lnode} = [fig/module,minimum width=3.0cm,minimum height=0.6cm,inner sep=2pt]
\begin{scope}
\node [fig/module primary,anchor=west,align=center,minimum height=1.2cm,minimum width=3.8cm] (corefunction) at (0,0) {\small{核心函数}\\[-2pt] \small{(MHSA 或 FFN)}};
\node [fig/module,anchor=north,minimum height=0.8cm,minimum width=3.8cm] (layernorm) at ([yshift=-0.8cm]corefunction.south) {\small{层归一化}};
\node [anchor=north] (input) at ([yshift=-1.2cm]layernorm.south) {$\mathbf{x}_l$};
\node [anchor=south,circle,minimum size=0.6cm,draw,thick,inner sep=1pt] (res) at ([yshift=1.2cm]corefunction.north) {\Large{$+$}};
\node [anchor=south] (output) at ([yshift=0.8cm]res.north) {$\mathbf{x}_{l+1} = \mathbf{x}_l + F(\mathbf{x}_l,\theta_l)$};
\draw [->,thick] ([yshift=1pt]input.north) -- ([yshift=-1pt]layernorm.south);
\draw [->,thick] ([yshift=1pt]layernorm.north) -- ([yshift=-1pt]corefunction.south);
\draw [->,thick] ([yshift=1pt]corefunction.north) -- ([yshift=-1pt]res.south);
\draw [->,thick] ([yshift=1pt]res.north) -- ([yshift=-1pt]output.south);
\draw [->,thick] ([yshift=0.3cm,xshift=-1pt]input.north) -- ([yshift=0.3cm,xshift=-3.5cm]input.north) -- ([xshift=-3.5cm]res.center) -- ([xshift=-1pt]res.west);
\begin{pgfonlayer}{background}
\node [draw,thick,dotted,inner sep=0.5cm] [fit = (corefunction) (layernorm)] (bigfunction) {};
\node [anchor=center,draw,thick,dotted,fill=white,inner sep=2pt] (functionlabel) at ([xshift=0.5cm,yshift=0pt]bigfunction.north west) {\small{$F(\mathbf{x}_l,\theta_l)$}};
\end{pgfonlayer}
\node [anchor=west,align=left] (ode) at ([xshift=4cm,yshift=-0.3cm]res.east) {\small{ODE:}\\[1pt] \small{$\frac{d\mathbf{x}(t)}{dt} = f(\mathbf{x}(t), \theta(t))$}};
\node [anchor=north west,align=left] (euler) at ([yshift=-0.5cm]ode.south west) {\small{Euler 方法:}\\[1pt] \small{$\mathbf{x}(t + \Delta t) = \mathbf{x}(t) + \Delta t \cdot f(\mathbf{x}(t), \theta(t))$}};
\node [anchor=north east] (dynlabel) at ([yshift=-2.5cm,xshift=-0.3cm]euler.south east) {\footnotesize{系统动力学}};
\draw [-] ([xshift=-0.1cm,yshift=1pt]euler.south east) -- ([xshift=-2.0cm,yshift=1pt]euler.south east);
\draw [-] ([xshift=-0.5cm,yshift=0pt]euler.south east) -- ([xshift=-1.8cm,yshift=1pt]dynlabel.north east);
\draw [-] ([xshift=1pt,yshift=1.5cm]bigfunction.south east) -- ([xshift=-2.3cm,yshift=1pt]dynlabel.north east);
\node [anchor=north] (deltat) at ([yshift=-0.5cm,xshift=-0.2cm]euler.south) {\footnotesize{$\Delta t = 1$}};
\draw [-] ([xshift=1.3cm,yshift=3pt]euler.south west) -- ([xshift=-0.2cm]deltat.north);
\draw [-] ([xshift=3.2cm,yshift=3pt]euler.south west) -- ([xshift=0.2cm]deltat.north);
\end{scope}
\end{tikzpicture}
\end{document}
\documentclass[border=9pt]{standalone}
\input{figure_preamble.tex}
\begin{document}
\begin{tikzpicture}
\def\ssep{1.3cm}
\def\bsep{2.5cm}
\tikzstyle{lnode} = [fig/module,minimum width=3.0cm,minimum height=0.6cm,inner sep=2pt]
\begin{scope}[scale=0.6]
\draw (-5.5,-5.5) rectangle (5.5,5.5);
\foreach \x in {-4,-3,...,4}{
\foreach \y in {-4,-3,...,4}{
\node [circle,minimum size=1pt,inner sep=0,fill=black] (point\x\y) at (\x,\y) {};
\pgfmathparse{or(\x != 0, \y != 0) ? 1 : 0}
\ifdim\pgfmathresult pt=1pt
\draw [-{Stealth[scale=1.0]}] (\x,\y) -- ++({-0.2*\y}, {0.2*\x});
\fi
}
}
\node [fig/caption,anchor=center] (caption) at (0,-7) {\small{(a) 向量场 $f(\mathbf{x}) = \begin{bmatrix} -x_2 \\ x_1 \end{bmatrix}$}};
\end{scope}
\begin{scope}[scale=0.6,xshift=14cm]
\draw (-5.5,-5.5) rectangle (5.5,5.5);
\foreach \x in {-4,-3,...,4}{
\foreach \y in {-4,-3,...,4}{
\node [circle,minimum size=1pt,inner sep=0,fill=black] (point\x\y) at (\x,\y) {};
\pgfmathparse{or(\x != 0, \y != 0) ? 1 : 0}
\ifdim\pgfmathresult pt=1pt
\draw [-{Stealth[scale=1.0]}] (\x,\y) -- ++({0.2*(sin(\x) + sin(\y))}, {0.2*(sin(\x) - sin(\y))});
\fi
}
}
\node [fig/caption,anchor=center] (caption) at (0,-7) {\small{(b) 向量场 $f(\mathbf{x}) = \begin{bmatrix} \sin(x_1) + \sin(x_2) \\ \sin(x_1) - \sin(x_2) \end{bmatrix}$}};
\end{scope}
\end{tikzpicture}
\end{document}
\usepackage[UTF8,fontset=fandol,scheme=plain]{ctex}
\usepackage{amsmath,amssymb,bm}
\usepackage[normalem]{ulem}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{
arrows.meta,
backgrounds,
calc,
decorations.pathreplacing,
fillbetween,
fit,
intersections,
positioning,
shapes.geometric
}
\input{figure_style.tex}
% 《ODE in Vision and Language》讲义配图的轻量视觉调整。
% 保留原图的几何结构与阅读顺序,只统一纸色、语义色、线型和模块质感。
\definecolor{FigInk}{HTML}{27323A}
\definecolor{FigMuted}{HTML}{737F89}
\definecolor{FigGrid}{HTML}{D4D9DE}
\definecolor{FigCanvas}{HTML}{FBFCFD}
\definecolor{FigPaper}{HTML}{F2F4F6}
\definecolor{FigPrimary}{HTML}{496A9F}
\definecolor{FigPrimaryLight}{HTML}{E5EBF4}
\definecolor{FigSecondary}{HTML}{BE6874}
\definecolor{FigSecondaryLight}{HTML}{F4E6E9}
\definecolor{FigAccent}{HTML}{8D7AA8}
\definecolor{FigAccentLight}{HTML}{ECE8F2}
\definecolor{FigGreen}{HTML}{5C8B84}
% 原绘图使用这些基础颜色表达语义;在不改动几何代码的前提下统一换肤。
\colorlet{black}{FigInk}
\colorlet{gray}{FigMuted}
\colorlet{blue}{FigPrimary}
\colorlet{red}{FigSecondary}
\colorlet{orange}{FigAccent}
\colorlet{green}{FigGreen}
\colorlet{ugreen}{FigGreen}
\tikzset{
every picture/.append style={
>=Latex,
line cap=round,
line join=round,
show background rectangle,
inner frame sep=4.5pt
},
background rectangle/.style={
fill=FigCanvas,
draw=FigGrid,
rounded corners=1.5pt,
line width=0.42pt
},
every node/.append style={
font=\sffamily,
text=FigInk
},
every path/.append style={
draw=FigInk
},
fig/module/.style={
draw=FigInk!68,
fill=FigPaper,
rounded corners=1.8pt,
line width=0.78pt,
inner xsep=8pt,
inner ysep=5.5pt
},
fig/module primary/.style={
fig/module,
draw=FigPrimary,
fill=FigPrimaryLight
},
fig/module secondary/.style={
fig/module,
draw=FigSecondary,
fill=FigSecondaryLight
},
fig/flow/.style={
-{Latex[length=2.15mm,width=1.45mm]},
draw=FigInk,
line width=0.78pt
},
fig/flow primary/.style={
fig/flow,
draw=FigPrimary
},
fig/flow secondary/.style={
fig/flow,
draw=FigSecondary,
dash pattern=on 3pt off 1.4pt
},
fig/guide/.style={
draw=FigMuted,
dash pattern=on 1.3pt off 1.8pt,
line width=0.45pt
},
fig/caption/.style={
font=\footnotesize\sffamily,
text=FigMuted,
align=center
},
fig/point/.style={
circle,
minimum size=5.4pt,
inner sep=0,
fill=FigCanvas,
draw=FigPrimary,
line width=0.85pt
}
}
\AtBeginDocument{\pagecolor{FigCanvas}\sffamily\color{FigInk}}
This source diff could not be displayed because it is too large. You can view the blob instead.
# 中文课程讲义 LaTeX 模板
主文件是 `aml_notes.tex`,建议使用 XeLaTeX 编译:
```powershell
xelatex aml_notes.tex
xelatex aml_notes.tex
```
第二次编译用于生成完整目录和交叉引用。图片可以放在 `figures/``images/` 目录中,然后用 `\includegraphics` 插入。
常用修改位置:
- 课程信息:修改 `\coursename``\semester``\teacher``\school`
- 新增章节:使用 `\chapter{章节标题}`
- 新增小节:使用 `\section{小节标题}``\subsection{小节标题}`
- 插入表格:参考模板中的 `table``longtable` 示例。
- 插入图片:把图片放入 `figures/`,参考模板中的 `figure` 示例。
如果使用 Overleaf,也请选择 XeLaTeX 作为编译器。
...@@ -18,6 +18,10 @@ ...@@ -18,6 +18,10 @@
% 数学、表格、图片 % 数学、表格、图片
\usepackage{amsmath,amssymb} \usepackage{amsmath,amssymb}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\setlength{\emergencystretch}{3em}
\sloppy
\usepackage{graphicx} \usepackage{graphicx}
\usepackage{booktabs} \usepackage{booktabs}
\usepackage{tabularx} \usepackage{tabularx}
...@@ -26,6 +30,8 @@ ...@@ -26,6 +30,8 @@
\usepackage{multirow} \usepackage{multirow}
\usepackage{caption} \usepackage{caption}
\usepackage{subcaption} \usepackage{subcaption}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning,fit,backgrounds,calc,shapes.geometric}
\graphicspath{{figures/}{images/}} \graphicspath{{figures/}{images/}}
% 颜色与强调框 % 颜色与强调框
...@@ -119,7 +125,34 @@ ...@@ -119,7 +125,34 @@
\newcommand{\lessondate}{\today} \newcommand{\lessondate}{\today}
\newcommand{\blankline}{\par\noindent\rule{\linewidth}{0.4pt}\par} \newcommand{\blankline}{\par\noindent\rule{\linewidth}{0.4pt}\par}
\newcommand{\keyword}[1]{\textbf{\color{CourseBlue}#1}} % 术语强调:对齐讲义第三章,使用黑色加粗(第三章用 \textbf,不使用彩色强调)。
\newcommand{\keyword}[1]{\textbf{#1}}
% 首次出现的术语(斜体),与第三章 \mindex 一致。
\newcommand{\mindex}[1]{\textit{#1}}
% 统一图片占位符:例子配图先用占位框,后续可替换为真实图片(便于做 PPT)。
\newcommand{\imageplaceholder}[2][5cm]{%
\fbox{%
\begin{minipage}[c][#1][c]{0.78\linewidth}
\centering
\textbf{图片占位}\\[0.45em]
{\small #2}
\end{minipage}%
}%
}
% 直观理解框:用类比/比喻帮助学生建立直觉
\definecolor{CoursePurple}{HTML}{6D28D9}
\newtcolorbox{intuition}{
colback=CourseGray,
colframe=CoursePurple,
title=直观理解,
fonttitle=\bfseries
}
% 协作占位:标记"待陈丹补充(语音/音频部分)"的内容,便于后续合并时定位
\newcommand{\todochen}[1]{%
\par\noindent
{\color{CourseGold}\small$\blacktriangleright$\ \textbf{[待陈丹补充 ·\ 语音/音频]}\ #1}\par}
\begin{document} \begin{document}
...@@ -278,6 +311,14 @@ ...@@ -278,6 +311,14 @@
\bottomrule \bottomrule
\end{longtable} \end{longtable}
% ============================================================
% 第五部分 多模态机器学习与生成式人工智能(润松负责:框架 + 图像内容)
% 内容本身定位为课程"第五部分",因此把章节计数器对齐到"第五讲"。
% 语音/音频小节由陈丹补充(见文中 \todochen 占位标记)。
% ============================================================
\setcounter{chapter}{4}
\include{chapters/ch5_multimodal}
\appendix \appendix
\chapter{附录} \chapter{附录}
...@@ -310,12 +351,62 @@ ...@@ -310,12 +351,62 @@
这里可以列出教材、论文、网站或其他阅读材料。 这里可以列出教材、论文、网站或其他阅读材料。
\begin{thebibliography}{9} \begin{thebibliography}{99}
\bibitem{sample-book} % —— 第五部分 多模态机器学习方法与模型 ——
作者. \emph{书名}. 出版社, 年份. \bibitem{alexnet} Krizhevsky A, Sutskever I, Hinton G E. ImageNet Classification with Deep Convolutional Neural Networks. NeurIPS, 2012.
\bibitem{vgg} Simonyan K, Zisserman A. Very Deep Convolutional Networks for Large-Scale Image Recognition (VGG). ICLR, 2015.
\bibitem{resnet} He K, Zhang X, Ren S, Sun J. Deep Residual Learning for Image Recognition. CVPR, 2016.
\bibitem{vit} Dosovitskiy A, et al. An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale. ICLR, 2021.
\bibitem{simclr} Chen T, et al. A Simple Framework for Contrastive Learning of Visual Representations (SimCLR). ICML, 2020.
\bibitem{moco} He K, et al. Momentum Contrast for Unsupervised Visual Representation Learning (MoCo). CVPR, 2020.
\bibitem{dino} Caron M, et al. Emerging Properties in Self-Supervised Vision Transformers (DINO). ICCV, 2021.
\bibitem{cpc} van den Oord A, Li Y, Vinyals O. Representation Learning with Contrastive Predictive Coding (CPC). arXiv:1807.03748, 2018.
\bibitem{wav2vec2} Baevski A, Zhou H, Mohamed A, Auli M. wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations. NeurIPS, 2020.
\bibitem{hubert} Hsu W N, et al. HuBERT: Self-Supervised Speech Representation Learning by Masked Prediction of Hidden Units. IEEE/ACM Transactions on Audio, Speech, and Language Processing, 2021.
\bibitem{data2vec} Baevski A, et al. data2vec: A General Framework for Self-Supervised Learning in Speech, Vision and Language. ICML, 2022.
\bibitem{mae} He K, et al. Masked Autoencoders Are Scalable Vision Learners (MAE). CVPR, 2022.
\bibitem{beit} Bao H, Dong L, Piao S, Wei F. BEiT: BERT Pre-Training of Image Transformers. ICLR, 2022.
\bibitem{clip} Radford A, et al. Learning Transferable Visual Models From Natural Language Supervision (CLIP). ICML, 2021.
\bibitem{align} Jia C, et al. Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision (ALIGN). ICML, 2021.
\bibitem{siglip} Zhai X, et al. Sigmoid Loss for Language Image Pre-Training (SigLIP). ICCV, 2023.
\bibitem{waco} Ouyang S, Ye R, Li L. WACO: Word-Aligned Contrastive Learning for Speech Translation. ACL, 2023.
\bibitem{clap} Elizalde B, Deshmukh S, Al Ismail M, Wang H. CLAP: Learning Audio Concepts From Natural Language Supervision. ICASSP, 2023.
\bibitem{flamingo} Alayrac J B, et al. Flamingo: a Visual Language Model for Few-Shot Learning. NeurIPS, 2022.
\bibitem{blip2} Li J, et al. BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models. ICML, 2023.
\bibitem{llava} Liu H, et al. Visual Instruction Tuning (LLaVA). NeurIPS, 2023.
\bibitem{instructblip} Dai W, et al. InstructBLIP: Towards General-purpose Vision-Language Models with Instruction Tuning. NeurIPS, 2023.
\bibitem{qwenvl} Bai J, et al. Qwen-VL: A Versatile Vision-Language Model for Understanding, Localization, Text Reading, and Beyond. arXiv:2308.12966, 2023.
\bibitem{whisper} Radford A, et al. Robust Speech Recognition via Large-Scale Weak Supervision (Whisper). ICML, 2023.
\bibitem{speechgpt} Zhang D, et al. SpeechGPT: Empowering Large Language Models with Intrinsic Cross-Modal Conversational Abilities. Findings of EMNLP, 2023.
\bibitem{qwenaudio} Chu Y, et al. Qwen-Audio: Advancing Universal Audio Understanding via Unified Large-Scale Audio-Language Models. arXiv:2311.07919, 2023.
\bibitem{salmonn} Tang C, et al. SALMONN: Towards Generic Hearing Abilities for Large Language Models. ICLR, 2024.
\bibitem{emu} Sun Q, et al. Emu: Generative Pretraining in Multimodality. ICLR, 2024.
\bibitem{gan} Goodfellow I, et al. Generative Adversarial Nets. NeurIPS, 2014.
\bibitem{vae} Kingma D P, Welling M. Auto-Encoding Variational Bayes (VAE). ICLR, 2014.
\bibitem{ddpm} Ho J, Jain A, Abbeel P. Denoising Diffusion Probabilistic Models (DDPM). NeurIPS, 2020.
\bibitem{cfg} Ho J, Salimans T. Classifier-Free Diffusion Guidance. arXiv:2207.12598, 2022.
\bibitem{dalle} Ramesh A, et al. Zero-Shot Text-to-Image Generation (DALL·E). ICML, 2021.
\bibitem{glide} Nichol A, et al. GLIDE: Towards Photorealistic Image Generation and Editing with Text-Guided Diffusion Models. ICML, 2022.
\bibitem{dalle2} Ramesh A, et al. Hierarchical Text-Conditional Image Generation with CLIP Latents (DALL·E 2). arXiv:2204.06125, 2022.
\bibitem{imagen} Saharia C, et al. Photorealistic Text-to-Image Diffusion Models with Deep Language Understanding (Imagen). NeurIPS, 2022.
\bibitem{ldm} Rombach R, et al. High-Resolution Image Synthesis with Latent Diffusion Models (Stable Diffusion). CVPR, 2022.
\bibitem{sdedit} Meng C, et al. SDEdit: Guided Image Synthesis and Editing with Stochastic Differential Equations. ICLR, 2022.
\bibitem{p2p} Hertz A, et al. Prompt-to-Prompt Image Editing with Cross Attention Control. ICLR, 2023.
\bibitem{ip2p} Brooks T, Holynski A, Efros A A. InstructPix2Pix: Learning to Follow Image Editing Instructions. CVPR, 2023.
\bibitem{controlnet} Zhang L, Rao A, Agrawala M. Adding Conditional Control to Text-to-Image Diffusion Models (ControlNet). ICCV, 2023.
\bibitem{tacotron2} Shen J, et al. Natural TTS Synthesis by Conditioning WaveNet on Mel Spectrogram Predictions (Tacotron 2). ICASSP, 2018.
\bibitem{fastspeech2} Ren Y, et al. FastSpeech 2: Fast and High-Quality End-to-End Text to Speech. ICLR, 2021.
\bibitem{vits} Kim J, Kong J, Son J. Conditional Variational Autoencoder with Adversarial Learning for End-to-End Text-to-Speech (VITS). ICML, 2021.
\bibitem{valle} Wang C, et al. Neural Codec Language Models are Zero-Shot Text to Speech Synthesizers (VALL-E). arXiv:2301.02111, 2023.
\bibitem{nist-synthetic} National Institute of Standards and Technology. Reducing Risks Posed by Synthetic Content. NIST AI 100-4, 2024.
\bibitem{dreambooth} Ruiz N, et al. DreamBooth: Fine Tuning Text-to-Image Diffusion Models for Subject-Driven Generation. CVPR, 2023.
\bibitem{audiogen} Kreuk F, et al. AudioGen: Textually Guided Audio Generation. ICLR, 2023.
\bibitem{audioldm} Liu H, et al. AudioLDM: Text-to-Audio Generation with Latent Diffusion Models. ICML, 2023.
\bibitem{makeanaudio} Huang R, et al. Make-An-Audio: Text-To-Audio Generation with Prompt-Enhanced Diffusion Models. ICML, 2023.
\bibitem{makeanaudio2} Huang J, et al. Make-An-Audio 2: Temporal-Enhanced Text-to-Audio Generation. arXiv:2305.18474, 2023.
\bibitem{audit} Wang Y, et al. AUDIT: Audio Editing by Following Instructions with Latent Diffusion Models. NeurIPS, 2023.
\bibitem{codi} Tang Z, et al. Any-to-Any Generation via Composable Diffusion (CoDi). NeurIPS, 2023.
\bibitem{sample-web}
网站名称. 文章标题. \url{https://example.com}
\end{thebibliography} \end{thebibliography}
\end{document} \end{document}
% !TeX root = ../main.tex
% 第五章正文入口。
\input{chapters/ch5_multimodal}
This source diff could not be displayed because it is too large. You can view the blob instead.
把课程讲义中需要使用的图片放在这个目录中。
示例:
- `cover.jpg`:封面图片,模板会自动读取。
- `example.png`:正文图片示例,模板会自动读取。
% Converted from the original thebibliography block in section5/aml_notes.tex.
@misc{alexnet,
note = {Krizhevsky A, Sutskever I, Hinton G E. ImageNet Classification with Deep Convolutional Neural Networks. NeurIPS, 2012.}
}
@misc{vgg,
note = {Simonyan K, Zisserman A. Very Deep Convolutional Networks for Large-Scale Image Recognition (VGG). ICLR, 2015.}
}
@misc{resnet,
note = {He K, Zhang X, Ren S, Sun J. Deep Residual Learning for Image Recognition. CVPR, 2016.}
}
@misc{vit,
note = {Dosovitskiy A, et al. An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale. ICLR, 2021.}
}
@misc{simclr,
note = {Chen T, et al. A Simple Framework for Contrastive Learning of Visual Representations (SimCLR). ICML, 2020.}
}
@misc{moco,
note = {He K, et al. Momentum Contrast for Unsupervised Visual Representation Learning (MoCo). CVPR, 2020.}
}
@misc{dino,
note = {Caron M, et al. Emerging Properties in Self-Supervised Vision Transformers (DINO). ICCV, 2021.}
}
@misc{cpc,
note = {van den Oord A, Li Y, Vinyals O. Representation Learning with Contrastive Predictive Coding (CPC). arXiv:1807.03748, 2018.}
}
@misc{wav2vec2,
note = {Baevski A, Zhou H, Mohamed A, Auli M. wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations. NeurIPS, 2020.}
}
@misc{hubert,
note = {Hsu W N, et al. HuBERT: Self-Supervised Speech Representation Learning by Masked Prediction of Hidden Units. IEEE/ACM Transactions on Audio, Speech, and Language Processing, 2021.}
}
@misc{data2vec,
note = {Baevski A, et al. data2vec: A General Framework for Self-Supervised Learning in Speech, Vision and Language. ICML, 2022.}
}
@misc{mae,
note = {He K, et al. Masked Autoencoders Are Scalable Vision Learners (MAE). CVPR, 2022.}
}
@misc{beit,
note = {Bao H, Dong L, Piao S, Wei F. BEiT: BERT Pre-Training of Image Transformers. ICLR, 2022.}
}
@misc{clip,
note = {Radford A, et al. Learning Transferable Visual Models From Natural Language Supervision (CLIP). ICML, 2021.}
}
@misc{align,
note = {Jia C, et al. Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision (ALIGN). ICML, 2021.}
}
@misc{siglip,
note = {Zhai X, et al. Sigmoid Loss for Language Image Pre-Training (SigLIP). ICCV, 2023.}
}
@misc{waco,
note = {Ouyang S, Ye R, Li L. WACO: Word-Aligned Contrastive Learning for Speech Translation. ACL, 2023.}
}
@misc{clap,
note = {Elizalde B, Deshmukh S, Al Ismail M, Wang H. CLAP: Learning Audio Concepts From Natural Language Supervision. ICASSP, 2023.}
}
@misc{flamingo,
note = {Alayrac J B, et al. Flamingo: a Visual Language Model for Few-Shot Learning. NeurIPS, 2022.}
}
@misc{blip2,
note = {Li J, et al. BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models. ICML, 2023.}
}
@misc{llava,
note = {Liu H, et al. Visual Instruction Tuning (LLaVA). NeurIPS, 2023.}
}
@misc{instructblip,
note = {Dai W, et al. InstructBLIP: Towards General-purpose Vision-Language Models with Instruction Tuning. NeurIPS, 2023.}
}
@misc{qwenvl,
note = {Bai J, et al. Qwen-VL: A Versatile Vision-Language Model for Understanding, Localization, Text Reading, and Beyond. arXiv:2308.12966, 2023.}
}
@misc{whisper,
note = {Radford A, et al. Robust Speech Recognition via Large-Scale Weak Supervision (Whisper). ICML, 2023.}
}
@misc{speechgpt,
note = {Zhang D, et al. SpeechGPT: Empowering Large Language Models with Intrinsic Cross-Modal Conversational Abilities. Findings of EMNLP, 2023.}
}
@misc{qwenaudio,
note = {Chu Y, et al. Qwen-Audio: Advancing Universal Audio Understanding via Unified Large-Scale Audio-Language Models. arXiv:2311.07919, 2023.}
}
@misc{salmonn,
note = {Tang C, et al. SALMONN: Towards Generic Hearing Abilities for Large Language Models. ICLR, 2024.}
}
@misc{emu,
note = {Sun Q, et al. Emu: Generative Pretraining in Multimodality. ICLR, 2024.}
}
@misc{gan,
note = {Goodfellow I, et al. Generative Adversarial Nets. NeurIPS, 2014.}
}
@misc{vae,
note = {Kingma D P, Welling M. Auto-Encoding Variational Bayes (VAE). ICLR, 2014.}
}
@misc{ddpm,
note = {Ho J, Jain A, Abbeel P. Denoising Diffusion Probabilistic Models (DDPM). NeurIPS, 2020.}
}
@misc{cfg,
note = {Ho J, Salimans T. Classifier-Free Diffusion Guidance. arXiv:2207.12598, 2022.}
}
@misc{dalle,
note = {Ramesh A, et al. Zero-Shot Text-to-Image Generation (DALL·E). ICML, 2021.}
}
@misc{glide,
note = {Nichol A, et al. GLIDE: Towards Photorealistic Image Generation and Editing with Text-Guided Diffusion Models. ICML, 2022.}
}
@misc{dalle2,
note = {Ramesh A, et al. Hierarchical Text-Conditional Image Generation with CLIP Latents (DALL·E 2). arXiv:2204.06125, 2022.}
}
@misc{imagen,
note = {Saharia C, et al. Photorealistic Text-to-Image Diffusion Models with Deep Language Understanding (Imagen). NeurIPS, 2022.}
}
@misc{ldm,
note = {Rombach R, et al. High-Resolution Image Synthesis with Latent Diffusion Models (Stable Diffusion). CVPR, 2022.}
}
@misc{sdedit,
note = {Meng C, et al. SDEdit: Guided Image Synthesis and Editing with Stochastic Differential Equations. ICLR, 2022.}
}
@misc{p2p,
note = {Hertz A, et al. Prompt-to-Prompt Image Editing with Cross Attention Control. ICLR, 2023.}
}
@misc{ip2p,
note = {Brooks T, Holynski A, Efros A A. InstructPix2Pix: Learning to Follow Image Editing Instructions. CVPR, 2023.}
}
@misc{controlnet,
note = {Zhang L, Rao A, Agrawala M. Adding Conditional Control to Text-to-Image Diffusion Models (ControlNet). ICCV, 2023.}
}
@misc{tacotron2,
note = {Shen J, et al. Natural TTS Synthesis by Conditioning WaveNet on Mel Spectrogram Predictions (Tacotron 2). ICASSP, 2018.}
}
@misc{fastspeech2,
note = {Ren Y, et al. FastSpeech 2: Fast and High-Quality End-to-End Text to Speech. ICLR, 2021.}
}
@misc{vits,
note = {Kim J, Kong J, Son J. Conditional Variational Autoencoder with Adversarial Learning for End-to-End Text-to-Speech (VITS). ICML, 2021.}
}
@misc{valle,
note = {Wang C, et al. Neural Codec Language Models are Zero-Shot Text to Speech Synthesizers (VALL-E). arXiv:2301.02111, 2023.}
}
@misc{nist-synthetic,
note = {National Institute of Standards and Technology. Reducing Risks Posed by Synthetic Content. NIST AI 100-4, 2024.}
}
@misc{dreambooth,
note = {Ruiz N, et al. DreamBooth: Fine Tuning Text-to-Image Diffusion Models for Subject-Driven Generation. CVPR, 2023.}
}
@misc{audiogen,
note = {Kreuk F, et al. AudioGen: Textually Guided Audio Generation. ICLR, 2023.}
}
@misc{audioldm,
note = {Liu H, et al. AudioLDM: Text-to-Audio Generation with Latent Diffusion Models. ICML, 2023.}
}
@misc{makeanaudio,
note = {Huang R, et al. Make-An-Audio: Text-To-Audio Generation with Prompt-Enhanced Diffusion Models. ICML, 2023.}
}
@misc{makeanaudio2,
note = {Huang J, et al. Make-An-Audio 2: Temporal-Enhanced Text-to-Audio Generation. arXiv:2305.18474, 2023.}
}
@misc{audit,
note = {Wang Y, et al. AUDIT: Audio Editing by Following Instructions with Latent Diffusion Models. NeurIPS, 2023.}
}
@misc{codi,
note = {Tang Z, et al. Any-to-Any Generation via Composable Diffusion (CoDi). NeurIPS, 2023.}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论