refactor: 附录/致谢/参考文献拆分为独立 chapter 文件,main.tex 保持清爽
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
\chapter*{致谢}
|
||||
\addcontentsline{toc}{chapter}{致谢}
|
||||
|
||||
值此论文完成之际,衷心感谢导师在选题方向确定、研究方法设计、实验方案优化和论文撰写修改等各个环节给予的悉心指导和宝贵建议。导师严谨治学的学术态度、开阔的学术视野和耐心的指导风格,使我在科研能力、学术规范和工程实践等多个方面都得到了系统的训练和显著的提升。
|
||||
|
||||
感谢河南理工大学计算机科学与技术学院四年来的培养,提供了完善的实验环境、丰富的数据资源和活跃的学术氛围。感谢学院各位老师在课堂内外传授的专业知识和科研方法,为本文的研究工作奠定了坚实的理论与实践基础。
|
||||
|
||||
感谢课题组同窗在数据下载策略优化、模型训练参数调试和LaTeX排版等方面的有益讨论和技术帮助。
|
||||
|
||||
感谢家人二十余年的养育之恩,以及在学业期间始终如一的理解、支持与鼓励,使我能够心无旁骛地专注于学术研究。
|
||||
|
||||
最后,向Copernicus Climate Data Store提供的ERA5-Land开放数据,以及所有为XGBoost、PyTorch、ECharts等开源工具做出贡献的开发者致以诚挚的谢意。开放科学的基础设施是本研究得以开展的重要前提。
|
||||
@@ -0,0 +1,169 @@
|
||||
\chapter{项目代码结构}
|
||||
|
||||
本研究核心代码已开源至 Gitea 仓库 \texttt{git@lhy-git.liuhangyv.top:Serendipity/elderly-heat-warning.git}。项目采用模块化结构,总规模约28个源文件(约3,500行Python代码 + 约800行前端HTML/CSS/JS代码)。
|
||||
|
||||
\section{项目目录结构}
|
||||
|
||||
\begin{verbatim}
|
||||
src/
|
||||
├── data/
|
||||
│ ├── download_era5.py # ERA5 数据下载(CDS API)
|
||||
│ ├── extract_zips.py # NetCDF ZIP 解压
|
||||
│ ├── preprocess.py # 数据预处理管线(597行)
|
||||
│ └── collect_mortality.py # 死亡率数据整理
|
||||
├── models/
|
||||
│ ├── lstm_attention.py # LSTM-Attention 模型定义
|
||||
│ ├── xgboost_baseline.py # XGBoost 基线
|
||||
│ ├── train.py # 训练脚本(365行)
|
||||
│ └── evaluate.py # 评估脚本(295行)
|
||||
├── web/
|
||||
│ ├── app.py # Flask 后端(177行)
|
||||
│ └── static/
|
||||
│ └── index.html # ECharts 前端大屏(~800行)
|
||||
└── utils/
|
||||
└── config.py # 全局配置常量
|
||||
\end{verbatim}
|
||||
|
||||
\section{关键代码讲解}
|
||||
|
||||
\subsection{多头自注意力层}
|
||||
|
||||
\begin{lstlisting}[language=Python, caption=MultiHeadSelfAttention前向传播]
|
||||
class MultiHeadSelfAttention(nn.Module):
|
||||
def __init__(self, embed_dim, num_heads=4, dropout=0.3):
|
||||
super().__init__()
|
||||
self.num_heads = num_heads
|
||||
self.head_dim = embed_dim // num_heads
|
||||
self.qkv = nn.Linear(embed_dim, 3 * embed_dim)
|
||||
self.out_proj = nn.Linear(embed_dim, embed_dim)
|
||||
|
||||
def forward(self, x):
|
||||
B, T, D = x.shape
|
||||
qkv = self.qkv(x).reshape(B, T, 3, self.num_heads, self.head_dim)
|
||||
qkv = qkv.permute(2, 0, 3, 1, 4)
|
||||
q, k, v = qkv[0], qkv[1], qkv[2]
|
||||
scale = self.head_dim ** -0.5
|
||||
attn = (q @ k.transpose(-2, -1)) * scale
|
||||
attn = F.softmax(attn, dim=-1)
|
||||
out = attn @ v
|
||||
out = out.permute(0, 2, 1, 3).reshape(B, T, D)
|
||||
return self.out_proj(out)
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{设计要点:}
|
||||
\begin{enumerate}
|
||||
\item \texttt{qkv}将Q、K、V三次投影合并为一次矩阵乘法,计算效率提升约30\%
|
||||
\item \texttt{scale = head\_dim ** -0.5}是缩放点积注意力的核心——防止点积过大导致softmax梯度弥散
|
||||
\item \texttt{permute}操作重排维度使每个注意力头独立计算
|
||||
\end{enumerate}
|
||||
|
||||
\subsection{主模型HeatRiskPredictor}
|
||||
|
||||
\begin{lstlisting}[language=Python, caption=模型前向传播]
|
||||
class HeatRiskPredictor(nn.Module):
|
||||
def __init__(self, input_dim, hidden_dim=128):
|
||||
super().__init__()
|
||||
self.input_proj = nn.Linear(input_dim, hidden_dim)
|
||||
self.lstm = nn.LSTM(hidden_dim, hidden_dim, num_layers=2,
|
||||
batch_first=True, bidirectional=True)
|
||||
self.attention = MultiHeadSelfAttention(hidden_dim * 2)
|
||||
self.lstm_proj = nn.Linear(hidden_dim * 2, hidden_dim)
|
||||
self.head_short = self._make_head(hidden_dim, 4)
|
||||
self.head_medium = self._make_head(hidden_dim, 4)
|
||||
self.head_long = self._make_head(hidden_dim, 4)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.input_proj(x) # (B,14,19) -> (B,14,128)
|
||||
lstm_out, _ = self.lstm(x) # -> (B,14,256) bidirectional
|
||||
attn_out = self.attention(lstm_out)
|
||||
last = self.lstm_proj(attn_out[:, -1, :])
|
||||
return {
|
||||
"short": self.head_short(last),
|
||||
"medium": self.head_medium(last),
|
||||
"long": self.head_long(last),
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{设计要点:}
|
||||
\begin{enumerate}
|
||||
\item BiLSTM使每个时间步同时编码前后文,输出维从128翻倍至256
|
||||
\item \texttt{lstm\_proj}将256维投影回128维以衔接注意力层
|
||||
\item 取序列最后一个时间步的注意力输出作为序列摘要向量
|
||||
\item 三个输出头参数独立,各自学习适应不同预测窗口的判别规则
|
||||
\end{enumerate}
|
||||
|
||||
\subsection{Focal Loss损失函数}
|
||||
|
||||
\begin{lstlisting}[language=Python, caption=FocalLoss实现]
|
||||
class FocalLoss(nn.Module):
|
||||
def __init__(self, alpha=0.5, gamma=2.0):
|
||||
super().__init__()
|
||||
self.alpha = alpha; self.gamma = gamma
|
||||
|
||||
def forward(self, logits, targets):
|
||||
ce = F.cross_entropy(logits, targets, reduction="none")
|
||||
pt = torch.exp(-ce)
|
||||
focal = self.alpha * (1 - pt) ** self.gamma * ce
|
||||
return focal.mean()
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{设计要点:}
|
||||
\begin{enumerate}
|
||||
\item \texttt{reduction="none"}保留逐样本损失以施加调制因子
|
||||
\item \texttt{pt = torch.exp(-ce)}利用交叉熵定义反推预测概率,避免额外softmax计算
|
||||
\item \texttt{(1-pt)**gamma}是核心调制项——$p_t$→1时因子→0衰减简单样本,$p_t$→0时因子→1保留困难样本
|
||||
\end{enumerate}
|
||||
|
||||
\subsection{数据预处理管线}
|
||||
|
||||
\begin{lstlisting}[language=Python, caption=ERA5数据加载与拼接]
|
||||
def load_era5_city(city: str) -> xr.Dataset:
|
||||
era5_dir = Path(DATA_RAW) / "era5" / city
|
||||
nc_files = sorted(era5_dir.glob("era5_*.nc"))
|
||||
combined = xr.open_mfdataset(nc_files, combine="by_coords",
|
||||
engine="h5netcdf", chunks=None)
|
||||
combined = combined.sortby("valid_time")
|
||||
_, idx = np.unique(combined["valid_time"], return_index=True)
|
||||
return combined.isel(valid_time=sorted(idx)) # 去重
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{设计要点:}
|
||||
\begin{enumerate}
|
||||
\item \texttt{open\_mfdataset}的\texttt{combine="by\_coords"}沿已有时间坐标自动对齐拼接
|
||||
\item \texttt{engine="h5netcdf"}避免Windows下netcdf-C库依赖
|
||||
\item \texttt{chunks=None}将全部数据加载到内存(每城约100MB)
|
||||
\item 去重处理CDS跨月文件的时间重叠
|
||||
\end{enumerate}
|
||||
|
||||
\subsection{Flask API后端}
|
||||
|
||||
\begin{lstlisting}[language=Python, caption=模型延迟加载与预测推理]
|
||||
model = None # 全局变量,None表示未加载
|
||||
|
||||
def load_model():
|
||||
"""首次API请求时才加载模型,降低启动延迟"""
|
||||
global model
|
||||
if model is not None: return
|
||||
data = np.load(DATA_PROCESSED / "jiaozuo_sequences.npz")
|
||||
model = HeatRiskPredictor(input_dim=data["X"].shape[2])
|
||||
model.load_state_dict(torch.load(OUTPUT_MODELS / "best_model.pt"))
|
||||
model.eval()
|
||||
|
||||
@app.route("/api/predict")
|
||||
def predict():
|
||||
load_model()
|
||||
X = get_recent_features() # 取最近14天
|
||||
with torch.no_grad(): # 推理模式
|
||||
outputs = model(torch.FloatTensor(X).to(device))
|
||||
for key in ["short", "medium", "long"]:
|
||||
probs = torch.softmax(outputs[key], dim=-1)[0]
|
||||
level = int(probs.argmax()) # 最高概率类别
|
||||
# 封装为JSON: level+label+probabilities+suggestions
|
||||
\end{lstlisting}
|
||||
|
||||
\textbf{设计要点:}
|
||||
\begin{enumerate}
|
||||
\item 延迟加载使Flask启动从约5秒降至<1秒,避免空闲时GPU内存占用
|
||||
\item \texttt{torch.no\_grad()}禁用自动求导,推理时节省约30\%显存
|
||||
\item 模型不可用时自动降级为fallback预测以保证系统可用性
|
||||
\end{enumerate}
|
||||
@@ -0,0 +1,46 @@
|
||||
\chapter{系统运行说明}
|
||||
|
||||
\section{环境配置}
|
||||
本项目基于Python 3.13开发,使用uv进行虚拟环境和依赖管理。核心依赖及其版本号如下:
|
||||
|
||||
\begin{table}[H]
|
||||
\centering
|
||||
\caption{核心依赖环境一览}
|
||||
\begin{tabular}{lll}
|
||||
\toprule
|
||||
\textbf{软件包} & \textbf{版本} & \textbf{用途} \\
|
||||
\midrule
|
||||
Python & 3.13.13 & 编程语言 \\
|
||||
PyTorch & 2.12.0+cu126 & 深度学习框架(GPU训练) \\
|
||||
XGBoost & 2.0+ & 梯度提升模型 \\
|
||||
Scikit-learn & 1.3+ & 评估指标和数据处理 \\
|
||||
Flask & 3.0+ & Web后端框架 \\
|
||||
xarray + h5netcdf & 2023+/1.8+ & NetCDF文件处理 \\
|
||||
NumPy + Pandas & 1.26+/2.1+ & 数据处理 \\
|
||||
Matplotlib & 3.8+ & 图表生成 \\
|
||||
CDSAPI & 0.7.7 & ERA5数据下载 \\
|
||||
\bottomrule
|
||||
\end{tabular}
|
||||
\end{table}
|
||||
|
||||
\section{运行步骤}
|
||||
以下步骤在项目根目录下依次执行:
|
||||
|
||||
\begin{enumerate}
|
||||
\item \textbf{创建并激活虚拟环境}:\texttt{uv venv .venv \&\& .venv\textbackslash Scripts\textbackslash activate}
|
||||
\item \textbf{安装依赖}:\texttt{uv pip install -e .}
|
||||
\item \textbf{下载 ERA5 数据}(耗时约5天):\texttt{python -m src.data.download\_era5}
|
||||
\item \textbf{解压 ZIP 格式数据}(耗时<1秒):\texttt{python -m src.data.extract\_zips}
|
||||
\item \textbf{运行预处理}(耗时约27分钟):\texttt{python -m src.data.preprocess}
|
||||
\item \textbf{训练LSTM模型}(可选,耗时取决于epoch数):\texttt{python -m src.models.train}
|
||||
\item \textbf{评估模型(含XGBoost训练)}:\texttt{python -m src.models.evaluate}
|
||||
\item \textbf{启动可视化大屏}:\texttt{python -m src.web.app}
|
||||
\item \textbf{浏览器访问}:\texttt{http://localhost:5005}
|
||||
\end{enumerate}
|
||||
|
||||
\textbf{注意事项:}
|
||||
\begin{enumerate}
|
||||
\item 步骤3(ERA5下载)需在Copernicus CDS网站接受数据许可协议
|
||||
\item 需在用户目录配置\texttt{\textasciitilde/.cdsapirc}文件(URL + API Key)
|
||||
\item 如仅需查看大屏效果,可在无模型文件时直接启动步骤8(系统自动降级为默认预测)
|
||||
\end{enumerate}
|
||||
@@ -0,0 +1,52 @@
|
||||
\chapter{模型超参数配置}
|
||||
|
||||
\section{LSTM-Attention模型}
|
||||
\begin{table}[H]
|
||||
\centering
|
||||
\caption{LSTM-Attention模型超参数汇总}
|
||||
\begin{tabular}{ll}
|
||||
\toprule
|
||||
\textbf{参数} & \textbf{取值} \\
|
||||
\midrule
|
||||
输入维度 & 19(气象特征数) \\
|
||||
隐藏维度 & 128 \\
|
||||
LSTM层数 & 2(双向) \\
|
||||
注意力头数 & 4 \\
|
||||
每头维度 & 32 \\
|
||||
Dropout率 & 0.3 \\
|
||||
总参数量 & 983,628 \\
|
||||
Focal Loss $\alpha$ & 0.5 \\
|
||||
Focal Loss $\gamma$ & 2.0 \\
|
||||
优化器 & AdamW (lr=1e-3, weight\_decay=1e-4) \\
|
||||
学习率调度 & ReduceLROnPlateau (factor=0.5, patience=5) \\
|
||||
梯度裁剪 & max\_norm=1.0 \\
|
||||
早停耐心值 & 15 epoch \\
|
||||
Batch Size & 32 \\
|
||||
最大Epoch & 50 \\
|
||||
训练设备 & NVIDIA RTX 4060 Laptop (8GB) \\
|
||||
每epoch耗时 & 约2.5分钟 \\
|
||||
\bottomrule
|
||||
\end{tabular}
|
||||
\end{table}
|
||||
|
||||
\section{XGBoost模型}
|
||||
\begin{table}[H]
|
||||
\centering
|
||||
\caption{XGBoost模型超参数汇总}
|
||||
\begin{tabular}{ll}
|
||||
\toprule
|
||||
\textbf{参数} & \textbf{取值} \\
|
||||
\midrule
|
||||
估计器数量 & 200 \\
|
||||
最大深度 & 6 \\
|
||||
学习率 & 0.05 \\
|
||||
L2正则化($\lambda$) & 1.0 \\
|
||||
最小分裂增益($\gamma$) & 0.0 \\
|
||||
子采样率 & 1.0 \\
|
||||
目标函数 & multi:softmax(4类) \\
|
||||
训练设备 & CUDA (GPU) \\
|
||||
输入特征维度 & 266(14×19展平) \\
|
||||
每分类器训练耗时 & 约2分钟 \\
|
||||
\bottomrule
|
||||
\end{tabular}
|
||||
\end{table}
|
||||
@@ -0,0 +1,42 @@
|
||||
\begin{thebibliography}{99}
|
||||
\zihao{5}
|
||||
\bibitem{gasparrini2015mortality} Gasparrini A, Guo Y, Hashizume M, et al. Mortality risk attributable to high and low ambient temperature[J]. The Lancet, 2015, 386: 369-375.
|
||||
|
||||
\bibitem{chen2018heat} Chen R, Yin P, Wang L, et al. Association between ambient temperature and mortality risk and burden in China[J]. The Lancet Planetary Health, 2018, 2(8): e344-e352.
|
||||
|
||||
\bibitem{hochreiter1997lstm} Hochreiter S, Schmidhuber J. Long Short-Term Memory[J]. Neural Computation, 1997, 9(8): 1735-1780.
|
||||
|
||||
\bibitem{vaswani2017attention} Vaswani A, Shazeer N, Parmar N, et al. Attention Is All You Need[C]. Advances in Neural Information Processing Systems, 2017, 30.
|
||||
|
||||
\bibitem{chen2016xgboost} Chen T, Guestrin C. XGBoost: A Scalable Tree Boosting System[C]. Proceedings of the 22nd ACM SIGKDD, 2016: 785-794.
|
||||
|
||||
\bibitem{ipcc2023ar6} IPCC. Climate Change 2023: Synthesis Report. Contribution of Working Groups I, II and III to the Sixth Assessment Report[R]. Geneva: IPCC, 2023.
|
||||
|
||||
\bibitem{lin2017focal} Lin T Y, Goyal P, Girshick R, et al. Focal Loss for Dense Object Detection[J]. IEEE Transactions on Pattern Analysis and Machine Intelligence, 2020, 42(2): 318-327.
|
||||
|
||||
\bibitem{bahdanau2014attention} Bahdanau D, Cho K, Bengio Y. Neural Machine Translation by Jointly Learning to Align and Translate[J]. arXiv preprint arXiv:1409.0473, 2014.
|
||||
|
||||
\bibitem{curriero2002temperature} Curriero F C, Heiner K S, Samet J M, et al. Temperature and Mortality in 11 Cities of the Eastern United States[J]. American Journal of Epidemiology, 2002, 155(1): 80-87.
|
||||
|
||||
\bibitem{rothfusz1990heat} Rothfusz L P. The Heat Index Equation (or, More Than You Ever Wanted to Know About Heat Index)[R]. NWS Southern Region Technical Attachment, 1990, SR 90-23.
|
||||
|
||||
\bibitem{era5land} Copernicus Climate Change Service. ERA5-Land hourly data from 1950 to present[EB/OL]. https://cds.climate.copernicus.eu/, 2024.
|
||||
|
||||
\bibitem{china_census2020} 国家统计局. 第七次全国人口普查公报[EB/OL]. https://www.stats.gov.cn/, 2021.
|
||||
|
||||
\bibitem{ma2015heat} Ma W, Chen R, Kan H. Temperature-related mortality in 17 large Chinese cities[J]. Environmental Health Perspectives, 2015, 123(10): 989-994.
|
||||
|
||||
\bibitem{anderson2013heat} Anderson G B, Bell M L. Heat Waves in the United States: Mortality Risk during Heat Waves and Effect Modification by Heat Wave Characteristics[J]. Environmental Health Perspectives, 2011, 119(2): 210-218.
|
||||
|
||||
\bibitem{guo2017heat} Guo Y, Gasparrini A, Armstrong B G, et al. Heat Wave and Mortality: A Multicountry, Multicommunity Study[J]. Environmental Health Perspectives, 2017, 125(8): 087006.
|
||||
|
||||
\bibitem{wmo2024state} WMO. State of the Global Climate 2023[R]. Geneva: World Meteorological Organization, 2024.
|
||||
|
||||
\bibitem{china_climate_bluebook2024} 中国气象局. 中国气候变化蓝皮书(2024)[R]. 北京: 中国气象局气候变化中心, 2024.
|
||||
|
||||
\bibitem{zhou2021informer} Zhou H, Zhang S, Peng J, et al. Informer: Beyond Efficient Transformer for Long Sequence Time-Series Forecasting[C]. Proceedings of the AAAI Conference on Artificial Intelligence, 2021, 35(12): 11106-11115.
|
||||
|
||||
\bibitem{wu2021autoformer} Wu H, Xu J, Wang J, et al. Autoformer: Decomposition Transformers with Auto-Correlation for Long-Term Series Forecasting[C]. Advances in Neural Information Processing Systems, 2021, 34.
|
||||
|
||||
\bibitem{ke2017lightgbm} Ke G, Meng Q, Finley T, et al. LightGBM: A Highly Efficient Gradient Boosting Decision Tree[C]. Advances in Neural Information Processing Systems, 2017, 30.
|
||||
\end{thebibliography}
|
||||
Reference in New Issue
Block a user