sync(latex): 从 Word 同步第二章和附录内容

- ch02: 第5节重写为"环境-智能体框架",新增表2.1/2.2和图2.1
- ch01: 补充 Garrity 引用来源
- 全部附录(ap01-ap09)从 Word 重新转换,修复 \sub 等转换错误
- 新增 ch13 结语占位、上下篇分隔页

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-29 15:13:27 +08:00
parent a90f7adfa1
commit d5a6208e89
16 changed files with 1414 additions and 1273 deletions
+197 -207
View File
@@ -1,18 +1,18 @@
\chapter{附录1计算机基础与编程环境}
\chapter{计算机基础与编程环境}
本附录介绍计算机的基本构成、操作系统常见操作、程序设计语言的概念、Python编程语言的系统学习指南,以及机器学习与深度学习的实践入门。
\subsubsection{计算机的基本构成与操作系统常见操作}
\subsection{计算机的基本构成与操作系统常见操作}
\paragraph{计算机基本构成}
\subsubsection{计算机基本构成}
了解计算机的基本组成有助于理解AI程序运行时的资源需求。
{\def\LTcaptype{none} % do not increment counter
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.2194}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.2599}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.3778}}@{}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.2368}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.3026}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.4342}}@{}}
\toprule\noalign{}
\begin{minipage}[b]{\linewidth}\raggedright
组件
@@ -33,11 +33,12 @@ GPU & 图形处理器,并行计算 & 深度学习训练与推理的核心硬
\end{longtable}
}
\paragraph{操作系统常见操作}
\subsubsection{操作系统常见操作}
本书以 macOS/Linux 为主要环境,Windows 用户推荐使用 WSL2Windows Subsystem for Linux)。
本书以 macOS/Linux 为主要环境,Windows 用户推荐使用 WSL2Windows
Subsystem for Linux)。
\subparagraph{文件与目录操作:}
\paragraph{文件与目录操作:}
\emph{\# 查看当前路径}\\
pwd\\
@@ -56,7 +57,7 @@ cp file.txt backup.txt\\
mv old.txt new.txt\\
rm unwanted.txt
\subparagraph{环境与进程管理:}
\paragraph{环境与进程管理:}
\emph{\# 查看系统资源}\\
top \emph{\# CPU和内存使用}\\
@@ -67,17 +68,18 @@ nvidia-smi \emph{\# GPU状态(NVIDIA显卡)}\\
brew install xxx \emph{\# macOS Homebrew}\\
apt install xxx \emph{\# Ubuntu/Debian}
\paragraph{硬件资源推荐}
\subsubsection{硬件资源推荐}
\textbf{本地GPU配置} - GPURTX 3060 (12GB) 或更高 - 内存:16GB+ - 存储:至少100GB SSD
\textbf{本地GPU配置} - GPURTX 3060 (12GB) 或更高 - 内存:16GB+ -
存储:至少100GB SSD
\textbf{云平台}
{\def\LTcaptype{none} % do not increment counter
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.2003}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1245}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1185}}@{}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.2639}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1528}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1528}}@{}}
\toprule\noalign{}
\begin{minipage}[b]{\linewidth}\raggedright
平台
@@ -97,9 +99,11 @@ AutoDL & 按时计费 & 中期项目 \\
\end{longtable}
}
\paragraph{软件安装与运行}
\subsubsection{软件安装与运行}
\textbf{“安装”的本质就是}:把编译好的二进制文件放到 PATH 某个目录下,让 shell 能找到它。例如git的安装和使用: \textbf{总结:三层抽象}
\textbf{``安装''的本质就是}:把编译好的二进制文件放到 PATH
某个目录下,让 shell 能找到它。例如git的安装和使用:
\textbf{总结:三层抽象}
┌─────────────────────────────────────────────┐\\
│ 用户层:brew install git / git clone │ ← 你看到的\\
@@ -111,23 +115,26 @@ AutoDL & 按时计费 & 中期项目 \\
│ 硬件层:CPU 执行指令、网卡收发数据、磁盘写入 │ ← 物理上发生了什么\\
└─────────────────────────────────────────────┘
所以整个链条是:\textbf{包管理器下载编译好的二进制 → 放到 PATH 目录 → shell 通过 PATH 找到它 → execve 加载到内存 →}
所以整个链条是:\textbf{包管理器下载编译好的二进制 → 放到 PATH 目录 →
shell 通过 PATH 找到它 → execve 加载到内存 →}
\textbf{二进制内部调用 OS API 完成实际工作}。没有任何“魔法”,本质上就是文件操作和进程管理的组合。
\textbf{二进制内部调用 OS API
完成实际工作}。没有任何“魔法”,本质上就是文件操作和进程管理的组合。
\subsubsection{程序设计语言与软件开发}
\subsection{程序设计语言与软件开发}
\paragraph{什么是程序设计语言}
\subsubsection{什么是程序设计语言}
计算机只能执行由0和1组成的\textbf{机器码}machine code),但人类直接阅读和编写机器码极其困难。程序设计语言就是人与计算机之间的桥梁------用人类可读的语法表达逻辑,再通过特定工具转换为机器可执行的指令。
计算机只能执行由0和1组成的\textbf{机器码}machine
code),但人类直接阅读和编写机器码极其困难。程序设计语言就是人与计算机之间的桥梁——用人类可读的语法表达逻辑,再通过特定工具转换为机器可执行的指令。
从底层到高层的演进:
{\def\LTcaptype{none} % do not increment counter
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1185}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.2548}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.4249}}@{}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1429}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.3506}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.4805}}@{}}
\toprule\noalign{}
\begin{minipage}[b]{\linewidth}\raggedright
层级
@@ -147,7 +154,7 @@ AutoDL & 按时计费 & 中期项目 \\
\end{longtable}
}
\paragraph{编译型与解释型语言}
\subsubsection{编译型与解释型语言}
高级语言需要转换为机器码才能运行,根据转换方式的不同,分为两大类:
@@ -175,16 +182,17 @@ RubyWeb开发(Ruby on Rails
特点:开发灵活、调试方便,但运行速度通常慢于编译型语言。
\textbf{混合模式}Java 采用“编译为字节码 → 虚拟机解释执行”的混合方式,兼顾了跨平台和性能。
\textbf{混合模式}Java 采用“编译为字节码 →
虚拟机解释执行”的混合方式,兼顾了跨平台和性能。
\paragraph{常见编程语言概览}
\subsubsection{常见编程语言概览}
{\def\LTcaptype{none} % do not increment counter
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1266}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.0951}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.3546}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.3258}}@{}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1512}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1047}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.3605}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.3605}}@{}}
\toprule\noalign{}
\begin{minipage}[b]{\linewidth}\raggedright
语言
@@ -200,7 +208,8 @@ RubyWeb开发(Ruby on Rails
\bottomrule\noalign{}
\endlastfoot
Python & 解释型 & AI、数据科学、自动化 & 本书主要编程语言 \\
C/C++ & 编译型 & 操作系统、嵌入式、高性能计算 & 深度学习框架的底层实现 \\
C/C++ & 编译型 & 操作系统、嵌入式、高性能计算 &
深度学习框架的底层实现 \\
Java & 混合型 & 企业应用、Android开发 & 大数据处理(Hadoop/Spark \\
JavaScript & 解释型 & 网页前端、Node.js后端 & Web可视化、交互设计 \\
Shell/Bash & 解释型 & 命令行脚本、系统管理 & 自动化任务、环境管理 \\
@@ -208,15 +217,19 @@ SQL & 声明式 & 数据库查询 & 数据管理与提取 \\
\end{longtable}
}
\textbf{命令行界面(CLI}CLICommand Line Interface)是通过文本命令与计算机交互的方式。终端中输入的每一条命令(如 ls、git commit)本质上都是调用某个程序。掌握 CLI 是进行AI开发的基础技能,许多工具(如 conda、pip、git)主要通过命令行操作。
\textbf{命令行界面(CLI}CLICommand Line
Interface)是通过文本命令与计算机交互的方式。终端中输入的每一条命令(如
ls、git commit)本质上都是调用某个程序。掌握 CLI
是进行AI开发的基础技能,许多工具(如
conda、pip、git)主要通过命令行操作。
\paragraph{软件开发基本概念}
\subsubsection{软件开发基本概念}
{\def\LTcaptype{none} % do not increment counter
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1379}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.3042}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.5579}}@{}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1327}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.3186}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.5310}}@{}}
\toprule\noalign{}
\begin{minipage}[b]{\linewidth}\raggedright
概念
@@ -229,18 +242,21 @@ SQL & 声明式 & 数据库查询 & 数据管理与提取 \\
\endhead
\bottomrule\noalign{}
\endlastfoot
集成开发环境 & IDE & 集成代码编辑、调试、运行的开发工具(如 VS Code、PyCharm \\
集成开发环境 & IDE & 集成代码编辑、调试、运行的开发工具(如 VS
Code、PyCharm \\
编译器 & Compiler & 将源代码翻译为机器码的程序 \\
解释器 & Interpreter & 逐行读取并执行源代码的程序 \\
调试器 & Debugger & 帮助定位和修复代码错误的工具 \\
包管理器 & Package Manager & 管理第三方库的安装和更新(如 pip、conda、npm \\
API & Application Programming Interface & 程序之间交互的接口(如调用AI模型的API \\
包管理器 & Package Manager & 管理第三方库的安装和更新(如
pip、conda、npm \\
API & Application Programming Interface &
程序之间交互的接口(如调用AI模型的API) \\
开源 & Open Source & 源代码公开,可自由使用和修改 \\
版本控制 & Version Control & 管理代码的修改历史(如 Git \\
\end{longtable}
}
\paragraph{Python环境配置}\label{pythonux73afux5883ux914dux7f6e}
\subsubsection{Python环境配置}
Python是本书使用的核心编程语言,以下介绍环境搭建方法。
@@ -248,10 +264,10 @@ Anaconda vs Miniconda
{\def\LTcaptype{none} % do not increment counter
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1265}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1092}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1422}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1599}}@{}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1165}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.0971}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1262}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.6408}}@{}}
\toprule\noalign{}
\begin{minipage}[b]{\linewidth}\raggedright
工具
@@ -266,8 +282,10 @@ Anaconda vs Miniconda
\endhead
\bottomrule\noalign{}
\endlastfoot
Anaconda & \textasciitilde500MB & 预装常用库 & \href{https://www.anaconda.com/download}{anaconda.com} \\
Miniconda & \textasciitilde50MB & 精简安装 & \href{https://docs.conda.io/en/latest/miniconda.html}{docs.conda.io} \\
Anaconda & \textasciitilde500MB & 预装常用库 &
\href{https://www.anaconda.com/download}{anaconda.com} \\
Miniconda & \textasciitilde50MB & 精简安装 &
\href{https://docs.conda.io/en/latest/miniconda.html}{docs.conda.io} \\
\end{longtable}
}
@@ -285,15 +303,18 @@ conda install numpy pandas scipy\\
\# 4. 安装深度学习框架\\
pip install torch torchvision
\subsubsection{Python编程语言}\label{pythonux7f16ux7a0bux8bedux8a00}
\subsection{Python编程语言}
\subparagraph{Python概述与特点}\label{pythonux6982ux8ff0ux4e0eux7279ux70b9}
\paragraph{Python概述与特点}
Python由Guido van Rossum于1991年发布,以”优雅”\,”简洁”\,”可读性强”为设计哲学。它是当前AI和数据科学领域使用最广泛的编程语言。
Python由Guido van
Rossum于1991年发布,以“优雅”\,``简洁“``可读性强”为设计哲学。它是当前AI和数据科学领域使用最广泛的编程语言。
\textbf{核心特点}: - 语法简洁,接近自然语言,入门门槛低 - 丰富的第三方库生态(AI、数据处理、Web等) - 跨平台运行(Windows、macOS、Linux - 活跃的开源社区支持
\textbf{核心特点}: - 语法简洁,接近自然语言,入门门槛低 -
丰富的第三方库生态(AI、数据处理、Web等) -
跨平台运行(Windows、macOS、Linux - 活跃的开源社区支持
\subparagraph{基础语法}
\paragraph{基础语法}
变量与数据类型
@@ -304,7 +325,8 @@ chapter\_count = 26 \emph{\# 整数 int}\\
is\_published = True \emph{\# 布尔值 bool}\\
\strut \\
\emph{\# 查看类型}\\
print(type(name)) \emph{\# \textless class \textquotesingle str\textquotesingle\textgreater{}}
print(type(name)) \emph{\# \textless class
\textquotesingle str\textquotesingle\textgreater{}}
字符串操作
@@ -332,7 +354,7 @@ print(len(title)) \emph{\# 长度}
\strut \\
\emph{\# 好的注释解释"为什么",而不是"做什么"}
\subparagraph{数据结构}
\paragraph{数据结构}
列表(List
@@ -387,10 +409,9 @@ rgb = (255, 128, 0)
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
\begin{verbatim}
g, b = rgb
\end{verbatim}
g, b = rgb
\end{enumerate}
集合(Set
@@ -404,7 +425,7 @@ tools\_b = \{"Figma", "Blender", "Rhino"\}\\
print(tools\_a \& tools\_b) \emph{\# 交集: \{"Figma"\}}\\
print(tools\_a \textbar{} tools\_b) \emph{\# 并集}
\subparagraph{控制流}
\paragraph{控制流}
条件判断
@@ -446,9 +467,10 @@ squares.append(x ** 2)\\
squares = {[}x ** 2 \textbf{for} x \textbf{in} range(10){]}\\
\strut \\
\emph{\# 带条件过滤}\\
even\_squares = {[}x ** 2 \textbf{for} x \textbf{in} range(10) \textbf{if} x \% 2 == 0{]}
even\_squares = {[}x ** 2 \textbf{for} x \textbf{in} range(10)
\textbf{if} x \% 2 == 0{]}
\subparagraph{函数与模块}
\paragraph{函数与模块}
定义函数
@@ -484,25 +506,19 @@ train\_model(100, optimizer="SGD", lr=0.1)
\textbf{import} pandas \textbf{as} pd
\begin{verbatim}
\end{verbatim}
\emph{\# 从模块中导入特定功能}
\textbf{from} pathlib \textbf{import} Path
\textbf{from} collections \textbf{import} Counter
\begin{verbatim}
\end{verbatim}
\emph{\# 安装第三方库(在终端中执行)}
\begin{verbatim}
\begin{lstlisting}
# pip install numpy pandas matplotlib
\end{verbatim}
\end{lstlisting}
\subparagraph{3.6 文件读写}
\paragraph{3.6 文件读写}
\emph{\# 读取文件}
@@ -518,9 +534,6 @@ content = f.read()
print(line.strip())
\begin{verbatim}
\end{verbatim}
\emph{\# 写入文件}
\textbf{with} open("output.txt", "w", encoding="utf-8") \textbf{as} f:
@@ -529,46 +542,35 @@ f.write("分析结果\textbackslash n")
f.write(f"准确率: \{acc:.4f\}\textbackslash n")
\begin{verbatim}
\end{verbatim}
\emph{\# 读写JSON(AI应用中常用的数据格式)}
\textbf{import} json
\begin{verbatim}
\end{verbatim}
data = \{"model": "ResNet", "accuracy": 0.96\}
\begin{verbatim}
\end{verbatim}
\emph{\# 写入JSON}
\textbf{with} open("result.json", "w") \textbf{as} f:
json.dump(data, f, indent=2)
\begin{verbatim}
\end{verbatim}
\emph{\# 读取JSON}
\textbf{with} open("result.json", "r") \textbf{as} f:
\begin{verbatim}
\begin{lstlisting}
loaded = json.load(f)
\end{verbatim}
\end{lstlisting}
\subparagraph{面向对象编程基础}
\paragraph{面向对象编程基础}
面向对象编程(OOP)是Python的重要范式,许多AI库都基于OOP设计。
\textbf{class} NeuralNetwork:\\
\emph{"""简单的神经网络类"""}\\
\strut \\
\textbf{def} \_\_init\_\_(self, input\_size, hidden\_size, output\_size):\\
\textbf{def} \_\_init\_\_(self, input\_size, hidden\_size,
output\_size):\\
\emph{"""初始化网络结构"""}\\
self.input\_size = input\_size\\
self.hidden\_size = hidden\_size\\
@@ -578,7 +580,8 @@ self.loss\_history = {[}{]}\\
\textbf{def} forward(self, x):\\
\emph{"""前向传播"""}\\
\emph{\# 这里简化为概念演示}\\
\textbf{return} f"输出: 输入\{x\}经过\{self.hidden\_size\}个隐藏层神经元"\\
\textbf{return} f"输出:
输入\{x\}经过\{self.hidden\_size\}个隐藏层神经元"\\
\strut \\
\textbf{def} train(self, data, epochs=10):\\
\emph{"""训练网络"""}\\
@@ -588,13 +591,14 @@ self.loss\_history.append(loss)\\
print(f"Epoch \{epoch+1\}, Loss: \{loss:.4f\}")\\
\strut \\
\emph{\# 创建实例}\\
model = NeuralNetwork(input\_size=784, hidden\_size=128, output\_size=10)\\
model = NeuralNetwork(input\_size=784, hidden\_size=128,
output\_size=10)\\
\strut \\
\emph{\# 使用}\\
output = model.forward({[}0.5, 0.3, 0.8{]})\\
model.train(data=None, epochs=5)
\paragraph{核心科学计算库}
\subsubsection{核心科学计算库}
NumPy:数值计算基础
@@ -615,7 +619,7 @@ print(np.mean(x)) \emph{\# 均值}\\
print(np.std(x)) \emph{\# 标准差}\\
print(np.max(x)) \emph{\# 最大值}
\subparagraph{Pandas:数据处理}\label{pandasux6570ux636eux5904ux7406}
\paragraph{Pandas:数据处理}
\textbf{import} pandas \textbf{as} pd\\
\strut \\
@@ -634,9 +638,10 @@ df = df.dropna() \emph{\# 删除缺失值}\\
print(df.describe()) \emph{\# 描述性统计}\\
\strut \\
\emph{\# 筛选数据}\\
filtered = df{[}df{[}\textquotesingle accuracy\textquotesingle{]} \textgreater{} 0.9{]}
filtered = df{[}df{[}\textquotesingle accuracy\textquotesingle{]}
\textgreater{} 0.9{]}
\subparagraph{Matplotlib:数据可视化}\label{matplotlibux6570ux636eux53efux89c6ux5316}
\paragraph{Matplotlib:数据可视化}
\textbf{import} matplotlib.pyplot \textbf{as} plt\\
\strut \\
@@ -651,16 +656,16 @@ plt.title(\textquotesingle Training Loss Curve\textquotesingle)\\
plt.savefig(\textquotesingle loss\_curve.png\textquotesingle, dpi=150)\\
plt.show()
\subsubsection{AI工具库速查与模型资源}\label{aiux5de5ux5177ux5e93ux901fux67e5ux4e0eux6a21ux578bux8d44ux6e90}
\subsection{AI工具库速查与模型资源}
\paragraph{工具库速查}
\subsubsection{工具库速查}
{\def\LTcaptype{none} % do not increment counter
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1422}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1383}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.3840}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.3081}}@{}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1340}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.1443}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.4227}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.2784}}@{}}
\toprule\noalign{}
\begin{minipage}[b]{\linewidth}\raggedright
类别
@@ -676,17 +681,22 @@ plt.show()
\bottomrule\noalign{}
\endlastfoot
计算机视觉 & OpenCV & pip install opencv-python & 图像处理、视频分析 \\
目标检测 & Ultralytics & pip install ultralytics & YOLO目标检测、实例分割 \\
图像生成 & Diffusers & pip install diffusers & Stable Diffusion模型调用 \\
Agent开发 & LangChain & pip install langchain langchain-openai & LLM应用开发框架 \\
目标检测 & Ultralytics & pip install ultralytics &
YOLO目标检测、实例分割 \\
图像生成 & Diffusers & pip install diffusers & Stable
Diffusion模型调用 \\
Agent开发 & LangChain & pip install langchain langchain-openai &
LLM应用开发框架 \\
Agent开发 & LangGraph & pip install langgraph & 状态机式Agent开发 \\
数据检索 & LlamaIndex & pip install llama-index & 数据索引与检索(RAG \\
数据检索 & LlamaIndex & pip install llama-index &
数据索引与检索(RAG \\
\end{longtable}
}
\paragraph{模型资源}
\subsubsection{模型资源}
\subparagraph{\texorpdfstring{Hugging Face\href{https://huggingface.co/}{huggingface.co}}{Hugging Facehuggingface.co}}\label{hugging-facehuggingface.co}
\paragraph{\texorpdfstring{Hugging
Face\href{https://huggingface.co/}{huggingface.co}}{Hugging Facehuggingface.co}}\label{hugging-facehuggingface.co}
功能:模型仓库、数据集、Spaces在线演示
@@ -694,9 +704,9 @@ Agent开发 & LangGraph & pip install langgraph & 状态机式Agent开发 \\
{\def\LTcaptype{none} % do not increment counter
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1421}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.2163}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.3845}}@{}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1625}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.2750}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.5375}}@{}}
\toprule\noalign{}
\begin{minipage}[b]{\linewidth}\raggedright
任务
@@ -709,24 +719,25 @@ Hugging Face ID
\endhead
\bottomrule\noalign{}
\endlastfoot
文生图 & Stable Diffusion XL & stabilityai/stable-diffusion-xl-base-1.0 \\
文生图 & Stable Diffusion XL &
stabilityai/stable-diffusion-xl-base-1.0 \\
目标检测 & YOLOv8 & Ultralytics \\
语义分割 & SAM & segment-anything \\
大语言模型 & Llama 3 & meta-llama/Meta-Llama-3-8B \\
\end{longtable}
}
\subsubsection{4 机器学习与深度学习入门教程}
\section{机器学习与深度学习入门教程}
本节通过一个完整的实践流程,带领读者从数据准备到模型训练,体验机器学习和深度学习的核心步骤。
\paragraph{机器学习 vs 深度学习}
\subsubsection{机器学习 vs 深度学习}
{\def\LTcaptype{none} % do not increment counter
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1185}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.4054}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.3778}}@{}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1358}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.4198}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.4198}}@{}}
\toprule\noalign{}
\begin{minipage}[b]{\linewidth}\raggedright
维度
@@ -747,15 +758,15 @@ Hugging Face ID
\end{longtable}
}
\paragraph{scikit-learn:机器学习实践}\label{scikit-learnux673aux5668ux5b66ux4e60ux5b9eux8df5}
\subsubsection{scikit-learn:机器学习实践}
scikit-learn是Python最经典的机器学习库,提供了丰富的算法和工具。
\subparagraph{安装}
\paragraph{安装}
pip install scikit-learn
\subparagraph{完整示例:鸢尾花分类}
\paragraph{完整示例:鸢尾花分类}
\textbf{import} numpy \textbf{as} np
@@ -763,7 +774,8 @@ pip install scikit-learn
\textbf{from} sklearn \textbf{import} datasets
\textbf{from} sklearn.model\_selection \textbf{import} train\_test\_split
\textbf{from} sklearn.model\_selection \textbf{import}
train\_test\_split
\textbf{from} sklearn.preprocessing \textbf{import} StandardScaler
@@ -773,9 +785,10 @@ pip install scikit-learn
\textbf{from} sklearn.ensemble \textbf{import} RandomForestClassifier
\textbf{from} sklearn.metrics \textbf{import} accuracy\_score, classification\_report
\textbf{from} sklearn.metrics \textbf{import} accuracy\_score,
classification\_report
\paragraph{}\label{section-6}
\subsubsection{}\label{section}
\emph{\# 1. 加载数据}
@@ -787,22 +800,16 @@ y = iris.target \emph{\# 标签:三种鸢尾花}
print(f"数据集大小: \{X.shape\}, 类别数: \{len(np.unique(y))\}")
\begin{verbatim}
\end{verbatim}
\emph{\# 2. 划分训练集和测试集}
X\_train, X\_test, y\_train, y\_test = train\_test\_split(
\begin{verbatim}
\begin{lstlisting}
X, y, test_size=0.3, random_state=42
\end{verbatim}
\end{lstlisting}
)
\begin{verbatim}
\end{verbatim}
\emph{\# 3. 数据标准化}
scaler = StandardScaler()
@@ -811,9 +818,6 @@ X\_train = scaler.fit\_transform(X\_train)
X\_test = scaler.transform(X\_test)
\begin{verbatim}
\end{verbatim}
\emph{\# 4. 训练多个模型并比较}
models = \{
@@ -826,9 +830,9 @@ models = \{
"随机森林": RandomForestClassifier(n\_estimaors=100),
\end{quote}
\begin{verbatim}
\begin{lstlisting}
}
\end{verbatim}
\end{lstlisting}
\textbf{for} name, model \textbf{in} models.items():
@@ -848,20 +852,21 @@ y\_pred = best\_model.predict(X\_test)
print("\textbackslash n分类报告:")
\begin{verbatim}
\begin{lstlisting}
print(classification_report(y_test, y_pred, target_names=iris.target_names))
\end{verbatim}
\end{lstlisting}
\subparagraph{机器学习工作流总结}
\paragraph{机器学习工作流总结}
数据收集 → 数据预处理 → 特征工程 → 划分训练/测试集\\
→ 选择模型 → 训练 → 评估 → 调优 → 部署
\paragraph{PyTorch:深度学习实践}\label{pytorchux6df1ux5ea6ux5b66ux4e60ux5b9eux8df5}
\subsubsection{PyTorch:深度学习实践}
PyTorch是当前研究和实验中最流行的深度学习框架,以动态计算图和Pythonic API著称。
PyTorch是当前研究和实验中最流行的深度学习框架,以动态计算图和Pythonic
API著称。
\subparagraph{张量(Tensor)基础}
\paragraph{张量(Tensor)基础}
张量是PyTorch的核心数据结构,类似于NumPy数组,但可以在GPU上运算。
@@ -884,7 +889,7 @@ arr = np.array({[}1, 2, 3{]})\\
tensor = torch.from\_numpy(arr) \emph{\# NumPy → Tensor}\\
back = tensor.numpy() \emph{\# Tensor → NumPy}
\subparagraph{自动求导(Autograd}
\paragraph{自动求导(Autograd}
PyTorch的autograd模块可以自动计算梯度,是训练神经网络的核心。
@@ -898,7 +903,7 @@ y = x ** 2 + 3 * x + 1 \emph{\# y = x² + 3x + 1}\\
y.backward()\\
print(f"dy/dx = \{x.grad\}") \emph{\# 应为 2x + 3 = 7x=2时)}
\subparagraph{完整示例:手写数字识别}
\paragraph{完整示例:手写数字识别}
以下是一个完整的神经网络训练流程,使用经典的MNIST数据集。
@@ -910,33 +915,31 @@ print(f"dy/dx = \{x.grad\}") \emph{\# 应为 2x + 3 = 7x=2时)}
\textbf{from} torchvision \textbf{import} datasets, transforms
\subparagraph{}\label{section-7}
\paragraph{}\label{section-4}
\emph{\# 1. 数据准备}
transform = transforms.Compose({[}
\begin{verbatim}
\begin{lstlisting}
transforms.ToTensor(), # 转为张量
\end{verbatim}
\end{lstlisting}
transforms.Normalize((0.1307,), (0.3081,)) \emph{\# 标准化}
{]})
train\_dataset = datasets.MNIST(\textquotesingle./data\textquotesingle, train=True, download=True, transform=transform)
train\_dataset = datasets.MNIST(\textquotesingle./data\textquotesingle,
train=True, download=True, transform=transform)
test\_dataset = datasets.MNIST(\textquotesingle./data\textquotesingle, train=False, transform=transform)
test\_dataset = datasets.MNIST(\textquotesingle./data\textquotesingle,
train=False, transform=transform)
\begin{verbatim}
\end{verbatim}
train\_loader = torch.utils.data.DataLoader(train\_dataset,
batch\_size=64, shuffle=True)
train\_loader = torch.utils.data.DataLoader(train\_dataset, batch\_size=64, shuffle=True)
test\_loader = torch.utils.data.DataLoader(test\_dataset, batch\_size=1000)
\begin{verbatim}
\end{verbatim}
test\_loader = torch.utils.data.DataLoader(test\_dataset,
batch\_size=1000)
\emph{\# 2. 定义模型}
@@ -952,42 +955,30 @@ self.fc2 = nn.Linear(128, 64) \emph{\# 隐藏层 → 隐藏层}
self.fc3 = nn.Linear(64, 10) \emph{\# 隐藏层 → 输出层(10个数字)}
\begin{verbatim}
\end{verbatim}
\textbf{def} forward(self, x):
\begin{verbatim}
\begin{lstlisting}
x = x.view(-1, 28 * 28) # 展平图像
\end{verbatim}
\end{lstlisting}
x = torch.relu(self.fc1(x)) \emph{\# ReLU激活}
x = torch.relu(self.fc2(x))
\begin{verbatim}
\begin{lstlisting}
x = self.fc3(x) # 输出层不加激活
\end{verbatim}
\end{lstlisting}
\textbf{return} x
\begin{verbatim}
\end{verbatim}
model = Net()
\begin{verbatim}
\end{verbatim}
\emph{\# 3. 定义损失函数和优化器}
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
\begin{verbatim}
\end{verbatim}
\emph{\# 4. 训练}
\textbf{for} epoch \textbf{in} range(5):
@@ -998,25 +989,26 @@ total\_loss = 0
\textbf{for} batch\_x, batch\_y \textbf{in} train\_loader:
\begin{verbatim}
\begin{lstlisting}
optimizer.zero_grad() # 清零梯度
output = model(batch_x) # 前向传播
\end{verbatim}
\end{lstlisting}
loss = criterion(output, batch\_y) \emph{\# 计算损失}
\begin{verbatim}
\begin{lstlisting}
loss.backward() # 反向传播
optimizer.step() # 更新参数
\end{verbatim}
\end{lstlisting}
total\_loss += loss.item()
print(f"Epoch \{epoch+1\}, Loss: \{total\_loss/len(train\_loader):.4f\}")
print(f"Epoch \{epoch+1\}, Loss:
\{total\_loss/len(train\_loader):.4f\}")
\begin{verbatim}
\begin{lstlisting}
# 5. 测试
\end{verbatim}
\end{lstlisting}
model.eval()
@@ -1036,12 +1028,11 @@ total += batch\_y.size(0)
correct += (predicted == batch\_y).sum()
\begin{verbatim}
\begin{lstlisting}
print(f"\n测试准确率: {correct/total:.2%}")
\end{verbatim}
\end{lstlisting}
\paragraph{使用预训练模型}
\subsubsection{使用预训练模型}
在实际应用中,通常不需要从头训练模型,而是使用预训练模型进行微调或直接推理。
@@ -1055,37 +1046,32 @@ print(f"\n测试准确率: {correct/total:.2%}")
classifier = pipeline("sentiment-analysis")\\
result = classifier("This design is amazing!")\\
print(result)\\
\emph{\# {[}\{\textquotesingle label\textquotesingle: \textquotesingle POSITIVE\textquotesingle, \textquotesingle score\textquotesingle: 0.9998\}{]}}\\
\emph{\# {[}\{\textquotesingle label\textquotesingle:
\textquotesingle POSITIVE\textquotesingle,
\textquotesingle score\textquotesingle: 0.9998\}{]}}\\
\strut \\
\emph{\# 图像分类}\\
image\_classifier = pipeline("image-classification")\\
result = image\_classifier("building.jpg")\\
print(result)\\
\emph{\# {[}\{\textquotesingle score\textquotesingle: 0.92, \textquotesingle label\textquotesingle: \textquotesingle palace\textquotesingle\}, ...{]}}
\emph{\# {[}\{\textquotesingle score\textquotesingle: 0.92,
\textquotesingle label\textquotesingle:
\textquotesingle palace\textquotesingle\}, ...{]}}
\subparagraph{使用Ultralytics YOLO}
\paragraph{使用Ultralytics YOLO}
\emph{\# 安装: pip install ultralytics}
\textbf{from} ultralytics \textbf{import} YOLO
\begin{verbatim}
\end{verbatim}
\emph{\# 加载预训练模型}
model = YOLO("yolov8n.pt")
\begin{verbatim}
\end{verbatim}
\emph{\# 目标检测}
results = model("street\_photo.jpg")
\begin{verbatim}
\end{verbatim}
\emph{\# 查看结果}
\textbf{for} result \textbf{in} results:
@@ -1100,16 +1086,17 @@ conf = float(box.conf{[}0{]})
label = model.names{[}cls{]}
\begin{verbatim}
\begin{lstlisting}
print(f"检测到: {label}, 置信度: {conf:.2f}")
\end{verbatim}
\end{lstlisting}
\paragraph{GPU加速}\label{gpuux52a0ux901f}
\subsubsection{GPU加速}
深度学习训练在GPU上可以快数十倍。PyTorch的GPU使用非常简洁:
\emph{\# 检查GPU是否可用}\\
device = torch.device("cuda" \textbf{if} torch.cuda.is\_available() \textbf{else} "cpu")\\
device = torch.device("cuda" \textbf{if} torch.cuda.is\_available()
\textbf{else} "cpu")\\
print(f"使用设备: \{device\}")\\
\strut \\
\emph{\# 将模型和数据移动到GPU}\\
@@ -1121,15 +1108,17 @@ batch\_x = batch\_x.to(device)\\
batch\_y = batch\_y.to(device)\\
\emph{\# ... 后续训练代码不变}
在没有本地GPU的情况下,可以使用 \href{https://colab.research.google.com/}{Google Colab} 免费使用云端GPU运行上述代码。
在没有本地GPU的情况下,可以使用
\href{https://colab.research.google.com/}{Google Colab}
免费使用云端GPU运行上述代码。
\paragraph{学习路径建议}
\subsubsection{学习路径建议}
{\def\LTcaptype{none} % do not increment counter
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1657}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.3204}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.2490}}@{}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.1327}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.2478}}
>{\raggedright\arraybackslash}p{(\linewidth - 4\tabcolsep) * \real{0.6018}}@{}}
\toprule\noalign{}
\begin{minipage}[b]{\linewidth}\raggedright
阶段
@@ -1143,12 +1132,13 @@ batch\_y = batch\_y.to(device)\\
\bottomrule\noalign{}
\endlastfoot
入门 & Python基础 + NumPy/Pandas & 本附录 §3 \\
机器学习 & scikit-learn实践 & \href{https://scikit-learn.org/stable/tutorial/}{scikit-learn官方教程} \\
深度学习基础 & PyTorch入门 + MLP & \href{https://pytorch.org/tutorials/}{PyTorch官方教程} \\
机器学习 & scikit-learn实践 &
\href{https://scikit-learn.org/stable/tutorial/}{scikit-learn官方教程} \\
深度学习基础 & PyTorch入门 + MLP &
\href{https://pytorch.org/tutorials/}{PyTorch官方教程} \\
计算机视觉 & CNN + 图像分类/检测 & 本书第三篇 + CS231n \\
自然语言处理 & Transformer + LLM & 本书第四篇 + CS224n \\
生成式AI & Diffusion + AIGC工具 & 本书第五篇 \\
前沿探索 & Agent + 具身智能 & 本书第六篇 \\
\end{longtable}
}