% !TEX program = xelatex
% =====================================================================
%  深度学习 (WJ64007N) 作业模板
%
%  编译方式：XeLaTeX（必须）
%    - 本地：xelatex hw_template.tex   或   latexmk -xelatex hw_template.tex
%    - Overleaf：Menu -> Settings -> Compiler 选择 XeLaTeX
%  中文字体由 ctex 自动选择宋体：
%    Windows -> 中易宋体 (SimSun)，macOS -> 华文宋体 (Songti SC)，
%    Overleaf/Linux -> Fandol 宋体
%
%  使用说明：
%    1. 在下方“作业信息”处填写姓名、学号；
%    2. 每道题用 question 环境，你的解答写在紧随其后的 answer 环境中；
%    3. 中英文均可作答，提交编译后的 PDF。
% =====================================================================
\documentclass[11pt,a4paper]{article}

% ---------------- 中文支持（宋体） ----------------
% scheme=plain：只提供中文字体，不改动英文版式（标题、图表名等保持英文）
\usepackage[UTF8, scheme=plain]{ctex}
\linespread{1.25}

% ---------------- 版面 ----------------
\usepackage[a4paper, margin=2.5cm, headheight=14pt]{geometry}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0.5em}

% ---------------- 常用宏包 ----------------
\usepackage{amsmath, amssymb, amsthm}
\usepackage{bbm}
\usepackage{fixmath}
\usepackage{nicefrac}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage[shortlabels]{enumitem}
\setlist{itemsep=2pt, topsep=4pt}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{tikz}
\usepackage{listings}
\usepackage{fancyhdr}
\usepackage[most]{tcolorbox}
\usepackage{url}
\usepackage[colorlinks=true, linkcolor=blue!60!black, urlcolor=blue!60!black, citecolor=blue!60!black]{hyperref}

% =====================================================================
%  作业信息（学生填写姓名、学号）
% =====================================================================
\newcommand{\hwcourse}{Deep Learning (WJ64007N)}
\newcommand{\hwnumber}{1}
\newcommand{\hwdue}{2026-10-25 23:59:59}
\newcommand{\hwname}{张三}          % 姓名
\newcommand{\hwid}{2026000000}      % 学号

% 是否显示 answer 环境：改为 \showanswerfalse 可隐藏全部解答，
% 用于由同一份源文件生成“只含题目”的版本
\newif\ifshowanswer
\showanswertrue

% =====================================================================
%  question / answer 环境
%    \begin{question}[附加说明]  ...  \end{question}
%    \begin{answer}[附加说明]    ...  \end{answer}
%  可选参数例如 [10 pts]、[Backprop, 10 分]；
%  题号自动编号，可在 question 内 \label{q:xxx}，正文中用 \ref{q:xxx} 引用。
% =====================================================================
\newcommand{\questionname}{题目}   % 可改为 题目
\newcommand{\answername}{解答}       % 可改为 解答

\definecolor{qback}{gray}{0.94}
\definecolor{aframe}{RGB}{30,80,160}

% 可选参数非空时输出 “#2#1#3”
\newcommand{\hwopt}[3]{\if\relax\detokenize{#1}\relax\else#2#1#3\fi}

\newcounter{question}
\newtcolorbox{question}[1][]{%
  code={\refstepcounter{question}},
  enhanced, breakable, sharp corners,
  colback=qback, colframe=qback, boxrule=0pt,
  left=8pt, right=8pt, top=6pt, bottom=6pt,
  before skip=14pt, after skip=8pt,
  fonttitle=\bfseries, coltitle=black,
  title={\questionname~\thequestion\hwopt{#1}{\ (}{)}.},
  detach title, before upper={\tcbtitle\quad},
}

\ifshowanswer
  \newtcolorbox{answer}[1][]{%
    enhanced, breakable, sharp corners,
    colback=aframe!3, colframe=aframe, boxrule=0pt, leftrule=2pt,
    left=8pt, right=8pt, top=6pt, bottom=6pt,
    before skip=6pt, after skip=14pt,
    fonttitle=\bfseries, coltitle=aframe,
    title={\answername\hwopt{#1}{\ }{}.},
    detach title, before upper={\tcbtitle\quad},
  }
\else
  \NewDocumentEnvironment{answer}{o +b}{}{}
\fi

% ---------------- 标题与页眉 ----------------
\newcommand{\makehwtitle}{%
  \thispagestyle{plain}%
  \noindent\rule{\linewidth}{3pt}\par\vspace{0.6em}%
  \begin{center}
    {\LARGE\bfseries \hwcourse: Homework \#\hwnumber\par}
  \end{center}
  \vspace{0.2em}\noindent\rule{\linewidth}{0.8pt}\par
  \noindent 姓名 Name: \hwname \hfill 学号 Student ID: \hwid \hfill Due: \hwdue\par
  \vspace{0.5em}%
}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\small \hwcourse}
\fancyhead[R]{\small Homework \#\hwnumber\quad \hwname\ (\hwid)}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}

% ---------------- 代码排版 ----------------
\lstset{
  basicstyle=\ttfamily\small,
  keywordstyle=\color{aframe}\bfseries,
  commentstyle=\color{gray},
  stringstyle=\color{red!60!black},
  numbers=left, numberstyle=\tiny\color{gray},
  frame=single, rulecolor=\color{gray!50},
  xleftmargin=2em, framexleftmargin=1.6em,
  breaklines=true, columns=fullflexible,
  showstringspaces=false, tabsize=4,
}

% ---------------- 定理类环境 ----------------
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{assume}[theorem]{Assumption}
\newtheorem{remark}[theorem]{Remark}

% ---------------- 数学符号 ----------------
\newcommand{\prob}{\mathbb{P}}
\newcommand{\bR}{\mathbb{R}}
\newcommand{\E}{\mathbb{E}}
\newcommand{\loss}{\ell}
\newcommand{\Loss}{\mathcal{L}}
\newcommand{\kl}{\mathbf{KL}}
\newcommand{\boldone}{\mathbf{1}}
\newcommand{\dd}[1]{\,\mathrm{d}#1}
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
\newcommand{\TODO}{\textbf{TODO: }}

\newcommand{\huaA}{\mathcal{A}}
\newcommand{\huaB}{\mathcal{B}}
\newcommand{\huaC}{\mathcal{C}}
\newcommand{\huaD}{\mathcal{D}}
\newcommand{\huaE}{\mathcal{E}}
\newcommand{\huaF}{\mathcal{F}}
\newcommand{\huaH}{\mathcal{H}}
\newcommand{\huaJ}{\mathcal{J}}
\newcommand{\huaK}{\mathcal{K}}
\newcommand{\huaL}{\mathcal{L}}
\newcommand{\huaN}{\mathcal{N}}
\newcommand{\huaQ}{\mathcal{Q}}
\newcommand{\huaR}{\mathcal{R}}
\newcommand{\huaS}{\mathcal{S}}
\newcommand{\huaT}{\mathcal{T}}
\newcommand{\huaU}{\mathcal{U}}
\newcommand{\huaY}{\mathcal{Y}}

% 向量 / 矩阵（粗斜体）
\renewcommand{\vec}[1]{\mathbold{#1}}
\newcommand{\mat}[1]{\mathbold{#1}}
\newcommand{\va}{\vec{a}}
\newcommand{\vb}{\vec{b}}
\newcommand{\vd}{\vec{d}}
\newcommand{\ve}{\vec{e}}
\newcommand{\vf}{\vec{f}}
\newcommand{\vg}{\vec{g}}
\newcommand{\vh}{\vec{h}}
\newcommand{\vu}{\vec{u}}
\newcommand{\vv}{\vec{v}}
\newcommand{\vw}{\vec{w}}
\newcommand{\vx}{\vec{x}}
\newcommand{\vy}{\vec{y}}
\newcommand{\vz}{\vec{z}}
\newcommand{\vo}{\vec{0}}
\newcommand{\valpha}{\mathbold{\alpha}}
\newcommand{\vbeta}{\mathbold{\beta}}
\newcommand{\vtheta}{\mathbold{\theta}}
\newcommand{\vxi}{\mathbold{\xi}}
\newcommand{\vzeta}{\mathbold{\zeta}}
\newcommand{\vomega}{\mathbold{\omega}}
\newcommand{\vepsilon}{\mathbold{\epsilon}}
\newcommand{\vSigma}{\mathbold{\Sigma}}
\newcommand{\vA}{\mat{A}}
\newcommand{\vC}{\mat{C}}
\newcommand{\vD}{\mat{D}}
\newcommand{\vI}{\mat{I}}
\newcommand{\vM}{\mat{M}}
\newcommand{\vQ}{\mat{Q}}
\newcommand{\vU}{\mat{U}}
\newcommand{\vV}{\mat{V}}
\newcommand{\vW}{\mat{W}}
\newcommand{\vX}{\mat{X}}
\newcommand{\vZ}{\mat{Z}}

\DeclareMathOperator{\diag}{diag}
\DeclareMathOperator{\trace}{Tr}
\DeclareMathOperator{\softmax}{softmax}
\DeclareMathOperator*{\argmin}{argmin}
\DeclareMathOperator*{\argmax}{argmax}

% ---------------- 题目专用符号 ----------------
\DeclareMathOperator{\onehot}{onehot}

% =====================================================================
\begin{document}

\makehwtitle

% ---------------------------------------------------------------------
\begin{question}[ST Gumbel-Softmax，20 pts]\label{q:gumbel}
设类别概率为 $\vec{\pi}=(\pi_1,\dots,\pi_K)$（$\pi_i>0$），$G_i=-\log(-\log U_i)$，$U_i\sim\mathcal{U}(0,1)$ 相互独立。ST Gumbel-Softmax 中，
\[
  \vz=\onehot\Bigl(\argmax_i\,v_i\Bigr),\qquad \vy=\softmax(\vv/\tau),\qquad \vv=\log\vec{\pi}+\vec{G},
\]
其中温度 $\tau>0$。前向使用 $\vz$，反向用 $\vy$ 的梯度代替 $\vz$ 的梯度。
\begin{enumerate}[(a)]
  \item 求 $G_i$ 的分布函数，并证明 $P\bigl(\argmax_i\,v_i=k\bigr)=\pi_k$。
  \item 证明 $\dfrac{\partial y_i}{\partial v_j}=\dfrac1\tau\,y_i(\delta_{ij}-y_j)$（$\delta_{ij}$ 为 Kronecker 符号），并证明 $\tau\to0$ 时 $\vy\to\vz$（设 $\vv$ 的最大分量唯一）。
  \item 说明 \texttt{z = y\_hard - y\_soft.detach() + y\_soft}（\texttt{y\_hard} 即 $\vz$，\texttt{y\_soft} 即 $\vy$）的前向值与反向梯度；
  写出上游梯度为 $\vec{c}$ 时 ST 给出的 $\partial\Loss/\partial v_j$。
  若 $\vec{\pi}=\softmax(\vec{s})$、$\Loss=\vec{c}^\top\vz$（$\vec{c}$ 为常向量），ST 对 $s_j$ 的梯度与上式相同，它是 $\partial\,\E[\Loss]/\partial s_j$ 的无偏估计吗？
\end{enumerate}
\end{question}

\begin{answer}
% 在此写下你的解答

\end{answer}

% ---------------------------------------------------------------------
\begin{question}[PL 条件，15 pts]\label{q:pl}
称 $\Loss$ 满足 $\mu$-PL 条件（$\mu>0$），若对所有 $\vw$ 都有
$\frac12\norm{\nabla\Loss(\vw)}^2\ge\mu\bigl(\Loss(\vw)-\Loss^*\bigr)$，其中 $\Loss^*=\min_{\vw}\Loss(\vw)$。
\begin{enumerate}[(a)]
  \item 设 $f(x)=x^2+3\sin^2x$。证明 $f$ 不是凸函数，并通过数值计算说明 $f$ 满足 PL 条件，估计使其成立的最大常数 $\mu$。
  \item 设模型有 $p$ 个参数，在 $n$ 个训练样本上的输出为 $F(\vw)\in\bR^n$，平方损失 $\Loss(\vw)=\frac12\norm{F(\vw)-\vy}^2$。
  记 $J=\partial F/\partial\vw\in\bR^{n\times p}$，$K=JJ^\top$。证明：若 $\lambda_{\min}(K(\vw))\ge\mu>0$ 对所有 $\vw$ 成立，则 $\Loss$ 满足 $\mu$-PL；
  并说明 $p<n$ 时这一条件不可能成立。
  \item 设 $\Loss\ge0$、$\Loss^*=0$，$\Loss$ 是 $L$-光滑的且满足 $\mu$-PL，GD 的步长为 $1/L$。证明对任意 $T$，
  $\norm{\vw_T-\vw_0}\le2\sqrt{2L\,\Loss(\vw_0)}/\mu$，并证明 $\{\vw_t\}$ 收敛到 $\Loss$ 的某个全局最小点。
\end{enumerate}
\end{question}

\begin{answer}
% 在此写下你的解答

\end{answer}

% ---------------------------------------------------------------------
\begin{question}[He 初始化，15 pts]\label{q:he}
考虑无偏置的 $L$ 层 ReLU 网络 $\vz^{(l)}=\vW^{(l)}\vx^{(l-1)}$，$\vx^{(l)}=\mathrm{ReLU}(\vz^{(l)})$，第 $l$ 层宽度为 $n_l$（$n_0$ 为输入维数）。
$\vW^{(l)}$ 的元素独立同分布，均值为 $0$、方差为 $\sigma_l^2$，分布关于 $0$ 对称，且与 $\vx^{(l-1)}$ 独立。
\begin{enumerate}[(a)]
  \item 如果把每一层的权重都初始化为同一个常数（例如全为 $0$），训练时会出现什么问题？
  \item 证明 $\mathrm{Var}(z_i^{(l)})=\frac12\,n_{l-1}\,\sigma_l^2\,\mathrm{Var}(z_j^{(l-1)})$（$l\ge2$），并由此说明 ReLU 网络应取 $\sigma_l^2=2/n_{l-1}$。
  比较 $L=50$、每层宽度为 $n$ 时取 $\sigma^2=1/n$ 与 $\sigma^2=2/n$ 的 $\mathrm{Var}(z^{(L)})/\mathrm{Var}(z^{(1)})$。
  \item 记 $\vec{\delta}^{(l)}=\partial\Loss/\partial\vz^{(l)}$。假设 $\vW^{(l)}$ 与 $\vec{\delta}^{(l)}$ 独立，$\delta_i^{(l)}$ 同分布且均值为 $0$，
  $\mathrm{ReLU}'(z_j^{(l-1)})$ 以各 $\frac12$ 的概率取 $0$ 或 $1$ 且与其余量独立。证明
  $\mathrm{Var}(\delta_j^{(l-1)})=\frac12\,n_l\,\sigma_l^2\,\mathrm{Var}(\delta_i^{(l)})$，并与 (b) 的条件比较。
\end{enumerate}
\end{question}

\begin{answer}
% 在此写下你的解答

\end{answer}

% ---------------------------------------------------------------------
\begin{question}[反向传播计算，15 pts]\label{q:bp}
考虑 2 个输入、2 个隐藏单元（ReLU）、1 个输出的网络与平方损失：
\[
  \vz=W_1\vx+\vb_1,\qquad \vh=\mathrm{ReLU}(\vz),\qquad \hat y=\vw_2^\top\vh+b_2,\qquad \Loss=\tfrac12(\hat y-y)^2,
\]
其中
$W_1=\begin{pmatrix}2&1\\1&3\end{pmatrix}$，$\vb_1=\begin{pmatrix}0\\1\end{pmatrix}$，$\vw_2=\begin{pmatrix}1.5\\-2\end{pmatrix}$，$b_2=0.5$，
样本为 $\vx=(1,\,-1)^\top$，$y=3$。
\begin{enumerate}[(a)]
  \item 计算 $\vz$、$\vh$、$\hat y$ 和 $\Loss$。
  \item 计算 $\partial\Loss/\partial\vw_2$、$\partial\Loss/\partial b_2$、$\partial\Loss/\partial W_1$、$\partial\Loss/\partial\vb_1$（与参数同形状）。哪些梯度为零？为什么？
  \item 取学习率 $\eta=0.1$，用 (b) 的梯度同时更新所有参数，重新计算损失。
\end{enumerate}
\end{question}

\begin{answer}
% 在此写下你的解答

\end{answer}

% ---------------------------------------------------------------------
\begin{question}[STE 与 REINFORCE，15 pts]\label{q:ste}
\begin{enumerate}[(a)]
  \item 设 $\Loss(x)=\bigl(\mathrm{round}(x)-1\bigr)^2$，当前 $x=0.3$。求真实导数 $\mathrm{d}\Loss/\mathrm{d}x$ 和 STE（前向用 $\mathrm{round}$，反向令 $\mathrm{d}\,\mathrm{round}(x)/\mathrm{d}x:=1$）给出的梯度；
  用学习率 $\eta=0.15$ 按 STE 梯度做一步梯度下降，求新的 $x$ 和损失。
  \item 设 $z\in\{0,1\}$，$P(z=1)=p=\sigma(\theta)$，$\sigma$ 为 sigmoid 函数。证明 $\frac{\partial}{\partial\theta}\log p_\theta(z)=z-p$（$p_\theta(z)$ 为 $z$ 的概率）；
  并证明对任意 $f:\{0,1\}\to\bR$ 与常数 $b$（均与 $\theta$、$z$ 无关），$\hat g_b=\bigl(f(z)-b\bigr)(z-p)$ 都是 $\frac{\mathrm{d}}{\mathrm{d}\theta}\E[f(z)]$ 的无偏估计。
  \item 取 $\theta=0$，$f(0)=99$，$f(1)=101$，分别计算 $b=0$ 与 $b=100$ 时 $\hat g_b$ 的方差。对一般的 $p$、$f(0)$、$f(1)$，求使方差最小的 $b$。
\end{enumerate}
\end{question}

\begin{answer}
% 在此写下你的解答

\end{answer}

% ---------------------------------------------------------------------
\begin{question}[SGD 的收敛性，20 pts]\label{q:sgd}
设 $\Loss$ 是 $L$-光滑的，下界为 $\Loss^*$。SGD 的迭代为 $\vw_{t+1}=\vw_t-\eta\,\vg_t$，其中随机梯度无偏（$\E[\vg_t\mid\vw_t]=\nabla\Loss(\vw_t)$），且满足
\[
 \E\bigl[\lVert \vg_t-\nabla\Loss(\vw_t)\rVert^2\mid\vw_t\bigr]\le\sigma^2 .
\]
\begin{enumerate}[(a)]
  \item 证明
  $\E[\Loss(\vw_{t+1})\mid\vw_t]\le\Loss(\vw_t)-\eta\bigl(1-\frac{L\eta}{2}\bigr)\lVert \nabla\Loss(\vw_t)\rVert^2+\frac{L\eta^2}{2}\sigma^2$。
  \item 设 $\eta\le1/L$，证明
  \[
    \frac1T\sum_{t=0}^{T-1}\E\lVert \nabla\Loss(\vw_t)\rVert^2\ \le\ \frac{2\bigl(\Loss(\vw_0)-\Loss^*\bigr)}{\eta T}+L\eta\sigma^2 .
  \]
  \item 固定 $\eta$ 而 $T\to\infty$ 时，上界趋于什么？
  若对每个 $T$ 取 $\eta=1/\sqrt T$（设 $T\ge L^2$，迭代记为 $\vw_t^{(T)}$），收敛速度是多少？
  并证明 $\lim_{T\to\infty}\min_{0\le t\le T-1}\E\lVert \nabla\Loss(\vw_t^{(T)})\rVert^2=0$。
\end{enumerate}
\end{question}

\begin{answer}
% 在此写下你的解答

\end{answer}

\end{document}
