\chapter{相关理论与技术基础} 本章系统阐述研究涉及的核心理论与技术,包括循环神经网络的数学基础、LSTM与BiLSTM的原理、注意力机制的完整推导、XGBoost算法细节、体感温度计算方法以及Focal Loss的理论分析。 \section{循环神经网络与长短期记忆} \subsection{循环神经网络(RNN)} 循环神经网络(Recurrent Neural Network, RNN)是处理序列数据的经典深度学习架构。给定输入序列$\{\mathbf{x}_1, \mathbf{x}_2, \dots, \mathbf{x}_T\}$,RNN在每个时间步$t$计算隐藏状态$\mathbf{h}_t$: \begin{equation} \mathbf{h}_t = \sigma(\mathbf{W}_{xh}\mathbf{x}_t + \mathbf{W}_{hh}\mathbf{h}_{t-1} + \mathbf{b}_h) \end{equation} \begin{equation} \mathbf{y}_t = \mathbf{W}_{hy}\mathbf{h}_t + \mathbf{b}_y \end{equation} 其中$\mathbf{W}_{xh}$、$\mathbf{W}_{hh}$、$\mathbf{W}_{hy}$为权重矩阵,$\mathbf{b}_h$、$\mathbf{b}_y$为偏置,$\sigma$为非线性激活函数(通常为tanh或ReLU)。 \subsection{梯度消失与梯度爆炸} RNN通过时间反向传播(Backpropagation Through Time, BPTT)进行训练。对于长度为$T$的序列,损失函数$\mathcal{L}$对参数$\mathbf{W}_{hh}$的梯度为: \begin{equation} \frac{\partial \mathcal{L}}{\partial \mathbf{W}_{hh}} = \sum_{t=1}^{T} \frac{\partial \mathcal{L}_t}{\partial \mathbf{W}_{hh}} \end{equation} \begin{equation} \frac{\partial \mathcal{L}_t}{\partial \mathbf{W}_{hh}} = \sum_{k=1}^{t} \frac{\partial \mathcal{L}_t}{\partial \mathbf{h}_t} \cdot \frac{\partial \mathbf{h}_t}{\partial \mathbf{h}_k} \cdot \frac{\partial \mathbf{h}_k}{\partial \mathbf{W}_{hh}} \end{equation} 其中雅可比矩阵的连乘$\frac{\partial \mathbf{h}_t}{\partial \mathbf{h}_k} = \prod_{j=k+1}^{t} \frac{\partial \mathbf{h}_j}{\partial \mathbf{h}_{j-1}}$。若雅可比矩阵的谱半径$\rho < 1$,连乘后梯度指数衰减至零(梯度消失);若$\rho > 1$,则梯度指数膨胀(梯度爆炸)。梯度消失使RNN难以学习长距离依赖,这正是LSTM的提出动机。 \subsection{LSTM单元结构} 长短期记忆网络(LSTM)由Hochreiter和Schmidhuber\cite{hochreiter1997lstm}提出,通过引入细胞状态(Cell State)$\mathbf{C}_t$和三个门控结构,有效解决了RNN的梯度消失问题。 \textbf{遗忘门(Forget Gate)}控制前一时刻细胞状态的保留比例: \begin{equation} \mathbf{f}_t = \sigma(\mathbf{W}_f \cdot [\mathbf{h}_{t-1}, \mathbf{x}_t] + \mathbf{b}_f) \end{equation} \textbf{输入门(Input Gate)}决定当前输入信息中有多少写入细胞状态: \begin{equation} \mathbf{i}_t = \sigma(\mathbf{W}_i \cdot [\mathbf{h}_{t-1}, \mathbf{x}_t] + \mathbf{b}_i) \end{equation} \begin{equation} \tilde{\mathbf{C}}_t = \tanh(\mathbf{W}_C \cdot [\mathbf{h}_{t-1}, \mathbf{x}_t] + \mathbf{b}_C) \end{equation} \textbf{细胞状态更新}: \begin{equation} \mathbf{C}_t = \mathbf{f}_t \odot \mathbf{C}_{t-1} + \mathbf{i}_t \odot \tilde{\mathbf{C}}_t \end{equation} \textbf{输出门(Output Gate)}调节细胞状态对当前隐藏状态的贡献: \begin{equation} \mathbf{o}_t = \sigma(\mathbf{W}_o \cdot [\mathbf{h}_{t-1}, \mathbf{x}_t] + \mathbf{b}_o) \end{equation} \begin{equation} \mathbf{h}_t = \mathbf{o}_t \odot \tanh(\mathbf{C}_t) \end{equation} 其中$\sigma(\cdot)$为sigmoid函数,$\odot$为逐元素乘积(Hadamard积)。遗忘门的sigmoid输出范围(0,1)允许网络自适应地决定信息保留程度,这是LSTM缓解梯度消失的关键——当$\mathbf{f}_t \approx 1$时,梯度可通过细胞状态近乎无损地回传。 \subsection{双向LSTM(BiLSTM)} 双向LSTM由前向LSTM和后向LSTM组成,分别从序列的正向和反向处理输入: \begin{equation} \overrightarrow{\mathbf{h}}_t = \text{LSTM}_{\text{fwd}}(\mathbf{x}_t, \overrightarrow{\mathbf{h}}_{t-1}) \end{equation} \begin{equation} \overleftarrow{\mathbf{h}}_t = \text{LSTM}_{\text{bwd}}(\mathbf{x}_t, \overleftarrow{\mathbf{h}}_{t+1}) \end{equation} \begin{equation} \mathbf{h}_t^{\text{bi}} = [\overrightarrow{\mathbf{h}}_t; \overleftarrow{\mathbf{h}}_t] \end{equation} 在气象时序预测中,BiLSTM的优势在于每个时间步的表示同时融合了前后文信息——某一天的温度既受前期天气累积影响(前向),也与即将到来的天气系统演变有关(后向)。 \section{注意力机制} \subsection{注意力机制的起源} 注意力机制的核心思想源于人类视觉系统中的选择性注意——在面对大量信息时,大脑会自动筛选出对当前任务最重要的部分进行深度加工。Bahdanau等(2014)首次将注意力机制引入神经机器翻译,允许解码器在每个解码步动态地关注编码器输出的不同位置,解决了固定长度上下文向量的信息瓶颈。 Vaswani等(2017)提出的Transformer架构完全基于注意力机制,舍弃了循环和卷积结构,在WMT翻译、BERT预训练和GPT生成等任务上取得了突破性进展。 \subsection{缩放点积注意力} 缩放点积注意力(Scaled Dot-Product Attention)是自注意力的基础计算单元: \begin{equation} \text{Attention}(\mathbf{Q}, \mathbf{K}, \mathbf{V}) = \text{softmax}\left(\frac{\mathbf{Q}\mathbf{K}^T}{\sqrt{d_k}}\right)\mathbf{V} \end{equation} 其中$\mathbf{Q} \in \mathbb{R}^{n \times d_k}$(Query)、$\mathbf{K} \in \mathbb{R}^{n \times d_k}$(Key)、$\mathbf{V} \in \mathbb{R}^{n \times d_v}$(Value)分别为查询、键和值矩阵,$n$为序列长度,$d_k$为键向量维度。 除以$\sqrt{d_k}$的缩放操作是关键的工程实践——当$d_k$较大时,点积$\mathbf{Q}\mathbf{K}^T$的元素值可能很大,导致softmax落入梯度极小的饱和区。缩放使点积的方差稳定在1,加速训练收敛。 \subsection{多头自注意力} 多头自注意力将Query、Key、Value分别通过$h$个独立的线性投影映射到不同的表示子空间: \begin{equation} \text{head}_i = \text{Attention}(\mathbf{Q}\mathbf{W}_i^Q, \mathbf{K}\mathbf{W}_i^K, \mathbf{V}\mathbf{W}_i^V), \quad i = 1, 2, \dots, h \end{equation} \begin{equation} \text{MultiHead}(\mathbf{Q}, \mathbf{K}, \mathbf{V}) = \text{Concat}(\text{head}_1, \dots, \text{head}_h)\mathbf{W}^O \end{equation} 其中$\mathbf{W}_i^Q \in \mathbb{R}^{d_{\text{model}} \times d_k}$、$\mathbf{W}_i^K \in \mathbb{R}^{d_{\text{model}} \times d_k}$、$\mathbf{W}_i^V \in \mathbb{R}^{d_{\text{model}} \times d_v}$和$\mathbf{W}^O \in \mathbb{R}^{h d_v \times d_{\text{model}}}$为可学习参数。取$d_k = d_v = d_{\text{model}} / h$以控制每个头的计算量。 在自注意力中,$\mathbf{Q} = \mathbf{K} = \mathbf{V} = \mathbf{X}$,即序列中每个位置同时作为查询、键和值。每个注意力头可以从不同的子空间关注序列的不同方面——某些头可能专注于温度的短期突变,某些头可能捕捉长期趋势,某些头可能关注体感温度的异常值。 \subsection{注意力权重分析} 注意力权重矩阵$\mathbf{A} \in \mathbb{R}^{n \times n}$提供了模型决策的可解释性: \begin{equation} A_{ij} = \text{softmax}\left(\frac{\mathbf{Q}_i \cdot \mathbf{K}_j}{\sqrt{d_k}}\right) \end{equation} $A_{ij}$表示第$i$个时间步对第$j$个时间步的关注程度。在高温预警任务中,这一矩阵可被可视化以揭示模型预测高风险事件时所依赖的关键时间步,为模型的可信度和决策透明度提供支持。 \section{XGBoost算法} \subsection{梯度提升框架} XGBoost(eXtreme Gradient Boosting)由Chen和Guestrin于2016年提出,是梯度提升决策树(GBDT)的高效实现。给定训练集$\{(\mathbf{x}_i, y_i)\}_{i=1}^n$,梯度提升以加法模型方式逐步集成$K$棵决策树: \begin{equation} \hat{y}_i = \phi(\mathbf{x}_i) = \sum_{k=1}^{K} f_k(\mathbf{x}_i), \quad f_k \in \mathcal{F} \end{equation} 其中$\mathcal{F} = \{f(\mathbf{x}) = w_{q(\mathbf{x})}\}$是回归树函数空间($q$将输入映射到叶节点索引,$w$为叶节点权重向量)。 \subsection{正则化目标函数} 第$t$轮迭代的目标函数为: \begin{equation} \mathcal{L}^{(t)} = \sum_{i=1}^{n} l(y_i, \hat{y}_i^{(t-1)} + f_t(\mathbf{x}_i)) + \Omega(f_t) \end{equation} 使用二阶泰勒展开近似: \begin{equation} \mathcal{L}^{(t)} \simeq \sum_{i=1}^{n} \left[l(y_i, \hat{y}^{(t-1)}) + g_i f_t(\mathbf{x}_i) + \frac{1}{2} h_i f_t^2(\mathbf{x}_i)\right] + \Omega(f_t) \end{equation} 其中$g_i = \partial_{\hat{y}^{(t-1)}} l(y_i, \hat{y}^{(t-1)})$为一阶梯度,$h_i = \partial^2_{\hat{y}^{(t-1)}} l(y_i, \hat{y}^{(t-1)})$为二阶梯度。移除常数项后: \begin{equation} \tilde{\mathcal{L}}^{(t)} = \sum_{i=1}^{n} \left[g_i f_t(\mathbf{x}_i) + \frac{1}{2} h_i f_t^2(\mathbf{x}_i)\right] + \gamma T + \frac{1}{2}\lambda \sum_{j=1}^{T} w_j^2 \end{equation} 其中$T$为叶节点数,$\gamma$和$\lambda$为正则化系数。 \subsection{节点分裂增益} 定义叶节点$j$的样本集合为$I_j = \{i \mid q(\mathbf{x}_i) = j\}$,则最优叶节点权重和对应的最小损失为: \begin{equation} w_j^* = -\frac{\sum_{i \in I_j} g_i}{\sum_{i \in I_j} h_i + \lambda} \end{equation} \begin{equation} \tilde{\mathcal{L}}^{(t)}(q) = -\frac{1}{2} \sum_{j=1}^{T} \frac{(\sum_{i \in I_j} g_i)^2}{\sum_{i \in I_j} h_i + \lambda} + \gamma T \end{equation} 节点分裂的增益为: \begin{equation} \text{Gain} = \frac{1}{2}\left[\frac{G_L^2}{H_L + \lambda} + \frac{G_R^2}{H_R + \lambda} - \frac{(G_L + G_R)^2}{H_L + H_R + \lambda}\right] - \gamma \end{equation} 其中$G_L = \sum_{i \in I_L} g_i$、$H_L = \sum_{i \in I_L} h_i$。XGBoost遍历所有候选分裂点,选择使Gain最大的分裂。Gain为负时停止分裂,实现自动剪枝。 \section{体感温度计算方法} \subsection{Magnus公式——相对湿度} 从ERA5-Land获取的2m温度($T$, °C)和2m露点温度($T_d$, °C)出发,使用Magnus公式计算相对湿度: 饱和水汽压(hPa): \begin{equation} e_s(T) = 6.112 \times \exp\left(\frac{17.67 \times T}{T + 243.5}\right) \end{equation} 实际水汽压(hPa): \begin{equation} e_a(T_d) = 6.112 \times \exp\left(\frac{17.67 \times T_d}{T_d + 243.5}\right) \end{equation} 相对湿度(\%): \begin{equation} RH = 100 \times \frac{e_a}{e_s} \end{equation} \subsection{NOAA Rothfusz公式——体感温度} 体感温度(Heat Index, HI)由NOAA的Rothfusz回归公式计算。首先将温度转换为华氏度:$T_F = T_C \times 1.8 + 32$。 简化公式($T_F \leq 80$°F): \begin{equation} HI_F = 0.5 \times \left[T_F + 61.0 + (T_F - 68.0) \times 1.2 + RH \times 0.094\right] \end{equation} 完整Rothfusz回归($T_F > 80$°F): \begin{equation} \begin{aligned} HI_F = &-42.379 + 2.04901523 \times T_F + 10.14333127 \times RH \\ &- 0.22475541 \times T_F \times RH - 0.00683783 \times T_F^2 \\ &- 0.05481717 \times RH^2 + 0.00122874 \times T_F^2 \times RH \\ &+ 0.00085282 \times T_F \times RH^2 - 0.00000199 \times T_F^2 \times RH^2 \end{aligned} \end{equation} NOAA标准修正(RH < 13\%且80°F < T < 112°F时): \begin{equation} \text{Adjustment} = \left(\frac{13 - RH}{4}\right) \times \sqrt{\frac{17 - |T_F - 95|}{17}} \end{equation} 最终转回摄氏温度:$HI_C = (HI_F - 32) / 1.8$。 \textbf{物理学含义:}体感温度考虑了高湿度环境对汗液蒸发的抑制效应。在相对湿度50\%以上时,汗液蒸发效率下降,人体通过出汗散热的机制受阻,导致体感温度显著高于实际气温。这一效应在高温高湿的夏季尤为突出,是高温健康风险评估中将气象数据转化为生理意义指标的关键步骤。 \section{高温健康风险等级划分} 参考WMO和WHO的高温健康预警标准,结合NOAA体感温度阈值和中国老年人的生理特征,本研究定义四级风险等级: \begin{table}[H] \centering \caption{高温健康风险等级划分标准} \begin{tabular}{cccp{5cm}} \toprule \textbf{风险等级} & \textbf{标签} & \textbf{体感温度(°C)} & \textbf{公共卫生建议} \\ \midrule 0级(低风险) & 低 & HI < 32 & 正常户外活动,注意饮水和休息 \\ 1级(中风险) & 中 & 32 ≤ HI < 35 & 减少午后户外活动,保持室内通风 \\ 2级(高风险) & 高 & 35 ≤ HI < 38 & 避免户外活动,开启空调/风扇降温 \\ 3级(严重风险)& 严重 & HI ≥ 38 & 停止一切户外活动,社区入户巡查高危老人 \\ \bottomrule \end{tabular} \end{table} 32°C阈值对应NOAA定义的"Exercise Caution"水平;35°C对应"Extreme Caution";38°C对应"Danger"水平,在此水平下持续暴露可能导致热痉挛、热衰竭甚至热射病。 \section{Focal Loss损失函数} \subsection{交叉熵损失的局限性} 标准多分类交叉熵损失定义为: \begin{equation} \text{CE}(p_t) = -\log(p_t) \end{equation} 其中$p_t$为模型对正确类别的预测概率。当类别极度不平衡时(如本研究低风险类占96.6\%),模型通过始终预测多数类即可获得低损失:$\text{CE}(0.96) \approx 0.041$。虽然损失数值上很低,但模型在少数类(高风险事件)上的预测能力几乎为零。 \subsection{Focal Loss的调制机制} Focal Loss由Lin等(2017)在目标检测领域提出,通过引入调制因子降低已正确分类样本的损失贡献: \begin{equation} \text{FL}(p_t) = -\alpha_t (1 - p_t)^\gamma \log(p_t) \end{equation} 其中$\alpha_t$为类别平衡因子,$\gamma \geq 0$为聚焦参数。调制因子$(1 - p_t)^\gamma$的作用: \begin{itemize} \item 当$p_t \to 1$(易分类样本):$(1 - p_t)^\gamma \to 0$,损失被大幅衰减 \item 当$p_t \to 0$(难分类样本):$(1 - p_t)^\gamma \to 1$,损失几乎不变 \end{itemize} $\gamma$控制衰减速率:$\gamma=0$退化为加权交叉熵;$\gamma=2$时,预测概率为0.9的样本的损失衰减100倍($(0.1)^2$),而预测概率为0.1的难样本则保持原始损失的81\%($(0.9)^2$)。 \subsection{Focal Loss的梯度分析} 对logits $\mathbf{z}$的梯度为: \begin{equation} \frac{\partial \text{FL}}{\partial z_k} = \alpha_t \cdot (1 - p_t)^\gamma \cdot \left[\gamma \cdot p_t \cdot \log(p_t) + (1 - p_t)\right] \cdot (p_k - \mathbb{1}[k = y]) \end{equation} 与交叉熵的梯度$\frac{\partial \text{CE}}{\partial z_k} = p_k - \mathbb{1}[k = y]$相比,Focal Loss对每个样本的梯度贡献由$(1-p_t)^\gamma$加权。在极度不平衡数据上,大量简单负样本(多数类,$p_t \approx 1$)的梯度贡献被抑制,使困难样本(少数类)的梯度在参数更新中占据主导地位。 \section{ERA5-Land数据同化系统} ERA5-Land是ECMWF开发的全球陆地表面再分析数据集,是ERA5大气再分析的增强版本。核心特点包括: \begin{itemize} \item \textbf{4D-Var同化}:采用四维变分数据同化技术,将卫星观测、地面观测、无线电探空等多源观测数据与数值天气预报模型(IFS Cy41r2)的短期预报进行最优融合 \item \textbf{空间分辨率}:0.1°×0.1°(约9 km),相比ERA5的0.25°(约31 km)提升约3.5倍 \item \textbf{HTESSEL地表模型}:采用Tiled ECMWF Scheme for Surface Exchanges over Land,显式模拟植被、裸土、积雪等不同地表覆盖类型的水热交换 \item \textbf{时间覆盖}:1950年至今,逐小时输出(本研究使用6小时间隔的月平均存档) \end{itemize} ERA5-Land通过CDS API以NetCDF4格式分发。2024年CDS基础设施迁移至CDS-Beta(cds-beta.climate.copernicus.eu),数据格式从直接NetCDF变为ZIP封装的NetCDF。本研究通过ZIP解压预处理步骤适配了这一格式变更。 \section{Flask框架与ECharts可视化} Flask是Python生态中最广泛使用的轻量级Web框架(WSGI微框架),核心特性包括路由装饰器、Jinja2模板引擎和丰富的扩展生态。ECharts是Apache基金会旗下的声明式JavaScript可视化库,支持37种图表类型和丰富的交互组件。两者通过RESTful JSON API实现前后端解耦,是数据科学Web应用开发的成熟技术栈。