Objective Accurate short-term traffic flow forecasting is essential for alleviating urban congestion, optimizing transportation efficiency, and ensuring road safety. However, existing traffic prediction methods exhibit limitations in modeling complex nonlinear spatiotemporal dynamics and incorporating external factors. Traditional approaches tend to rely on linear assumptions or static structures, making them insufficient for capturing long-range temporal dependencies and integrating multifactor information such as weather conditions and traffic accidents. Therefore, this study proposes a deep learning model based on an enhanced Transformer architecture, namely LC‒Transformer, which aims to improve prediction accuracy and robustness under complex traffic scenarios. Methods The LC‒Transformer model integrated three key components: 1) Long Short‒Term Memory (LSTM) networks to extract short- and long-term dependencies in traffic time series; 2) Conditional Variational Autoencoders (CVAE) to capture complex probabilistic features from multivariate weather data; and 3) traffic accident embeddings to account for historical incident impacts. The traffic flow input was fused with learned representations from the CVAE and accident embeddings before being passed through a multi-head self-attention mechanism, which enabled the model to dynamically learn temporal relevance under multifactor conditions. The model was trained and evaluated using two real-world datasets from the California PeMS system, PEMS01 and PEMS02, which covered highway segments with rich spatiotemporal and contextual data. Four widely adopted metrics were utilized to evaluate prediction performance: Mean Absolute Error (MAE), Root Mean Square Error (RMSE), Mean Absolute Percentage Error (MAPE), and the coefficient of determination (R2). Results and Discussions Experimental results demonstrated that the LC‒Transformer outperformed both classical machine learning models (Decision Tree and Random Forest) and mainstream deep learning models (LSTM, GRU, and STGCN). On the experiment datasets, the model achieved an MAE of 17.931 and an R2 of 0.953, showing substantial improvements over baseline models such as LSTM (EMAE=23.310, R2=0.780), Random Forest (EMAE=25.103, R2=0.698), and STGCN (EMAE=19.412, R2=0.912). In addition, two state-of-the-art (SOTA) Transformer-based models, Informer and Autoformer, were included for comparative evaluation. Although both models performed competitively because of their long-range modeling capabilities, they did not explicitly incorporate external factors. Their MAE values (18.624 and 18.312) and R2 values (0.931 and 0.937) were slightly lower than those of the LC‒Transformer, confirming the importance of multifactor fusion in real-world traffic forecasting tasks. A multi-head attention visualization analysis was also conducted to improve interpretability. The attention maps revealed that different heads focused on distinct temporal features. Some emphasized adjacent time steps, reflecting short-term continuity, whereas others highlighted distant intervals, such as the influence of early morning conditions on peak-hour traffic flow. These findings demonstrated the model's ability to capture both short-term patterns and latent long-term interactions that were potentially influenced by weather and incident conditions. Ablation studies further confirmed the contribution of each component. Removing the LSTM reduced temporal learning capability, excluding the CVAE weakened the model's responsiveness to weather variations, and omitting external features significantly degraded prediction accuracy, particularly under changing environmental conditions. Conclusions The LC‒Transformer model presents an effective solution for short-term traffic flow forecasting under complex conditions. Its hybrid architecture enables robust learning of nonlinear spatiotemporal dependencies and facilitates the seamless integration of multiple external factors. Compared to existing models, it provides higher prediction accuracy, stronger generalization capability, and improved interpretability through attention visualization. This research not only contributes a novel modeling framework but also provides practical insights for real-time traffic management and intelligent transportation systems (ITS). Although this study incorporated weather and accident data as external features, additional influencing factors, such as public holidays, road events, and unexpected incidents (construction activities and protests), can further enhance the model's adaptability. Future research will focus on extending the current architecture to accommodate these context-sensitive variables through hierarchical attention or adaptive gating mechanisms.
LSTM的输出即为最后时刻的,作为Transformer模型的交通流特征输入,以更好地捕捉交通流的全局时空依赖关系[23]。其中,自注意力机制通过计算输入特征间的相关性来生成加权和。每个输入向量通过线性变换生成3个向量:查询(query,记为 Q )、键(key,记为 K )和值(value,记为 V )。输入特征序列 X 通过线性变换生成查询矩阵 Q 、键矩阵 K 和值矩阵 V,如式(7)所示:
式中,、、分别为查询矩阵、键矩阵和值矩阵的权重矩阵, X 为输入序列。
通过将查询向量与所有键向量进行点积运算并除以一个缩放因子来计算输入之间的相关性,并根据每个元素的重要性来加权值向量,即将归一化的注意力分数与 V 相乘,如式(8)所示:
式中:Attention(·)为缩放点积注意力函数;softmax(·)用于将相似度得分归一化为概率分布形式;为缩放后的注意力得分矩阵,为 K 的维度,用于维度缩放以保证梯度稳定。
选取源自美国加州交通运输部(California Department of Transportation,Caltrans)建立的高速公路性能监测系统(Performance Measurement System,PeMS)提供的统一交通数据库。PeMS数据由布设在加州高速公路网中的交通检测器实时采集,并整合Caltrans及地方交通管理机构的相关交通运行信息,主要包括交通流量、平均速度和车道占有率等。这些数据可以让用户对高速公路性能进行统一、全面的评估,基于对高速公路网络当前状态的了解做出运营决策,分析拥堵瓶颈以确定潜在的补救措施,并做出更好的整体决策。
为进一步提高交通流预测的准确性,将历史交通事故数据作为外部影响因素引入预测模型。选取的数据集来自加州全州综合交通记录系统(Statewide Integrated Traffic Records System,SWITRS),包括事故发生的时间、地点、天气条件、事故类型、涉及车辆的数量,以及伤亡情况等数据。本文重点使用了与研究路段相关的交通事故数据,覆盖2024‒01‒01—2024‒02‒01。
为进一步增强模型的可解释性,对LC‒Transformer模型中的多头注意力机制(multi-head attention)进行可视化分析。图7为各注意力头在不同时间步的注意力权重分布,展示了模型在12个时间步窗口下,不同注意力头(Head 0~3)对交通流序列的注意力权重。图7中:横轴表示关键时间步(key time step),即被关注的位置;纵轴表示查询时间步(query time step),即当前输入的位置;颜色越深表示该时间步对当前预测的影响程度越大。
SayedS A, Abdel‒HamidY, HefnyH A.Artificial intelligence-based traffic flow prediction:A comprehensive review[J].Journal of Electrical Systems and Information Technology,2023,10(1):13. doi:10.1186/s43067-023-00081-6
[2]
XuCuicui.ARIMA-based traffic flow prediction modeling at urban intersections[J].Electronic Design Engineering,2022,30(2):20‒23.
ChenKun, QuDayi, JiaYanfeng,et al.Research on short-term traffic flow forecast based on ARIMA‒SVM Model[J].Journal of Qingdao University of Technology,2022,43(5):104‒109.
HeHongjie, ChenXianlong.Short-term traffic flow forecasting model research based on exponential smoothing and residual network[J].Journal of Transportation Engineering,2023,23(3):97‒106.
HeZujie, WuXinye, LiuZhonghua.Optimized SVM model for short-term traffic flow prediction based on improved gray wolf optimizer[J].Journal of Xiamen University(Natural Science),2022,61(2):288‒297.
TianJia, WangDeyong, ShiWenxi.Short-term traffic flow forecasting based on EEMD and random forest[J].Science Technology and Engineering,2023,23(29):12612‒12619.
YinLisheng, TangShengqi, LiSheng,et al.Traffic flow prediction based on hybrid model of auto-regressive integrated moving average and genetic particle swarm optimization wavelet neural network[J].Journal of Electronics & Information Technology,2019,41(9):2273‒2279.
DaiJiajia, LiangYiwen, ChengQianling.Analysis and discussion on urban traffic flow prediction models[C]//Proceedings of the 2024 World Transport Convention(WTC2024).Qindao:WTC,2024:589‒594.
ZhaoHenghui, HuangDeqi, ZengRong,et al.Short term traffic flow prediction based on spatial-temporal attention Bi‒LSTM model[J].Computer Simulation,2022,39(9):177‒181.
ZhangYang, HuYue, ChenDewang,et al.Short-term traffic flow prediction model based on GCN‒BiLSTM[J].Journal of Wuhan University of Technology(Transportation Science & Engineering),2023,47(5):802‒806.
YinQi, DingFei, ZhuYue,et al.An urban traffic flow prediction model based on CNN and multi-scale feature fusion[J].Computer Technology and Development,2022,32(10):175‒181.
LiHaowen.Study on short-term traffic flow prediction of expressway under rain and snow conditions based on CNN‒BiLSTM‒Attention[D].Harbin:Harbin Institute of Technology,2023.
ZengAiling, ChenMuxi, ZhangLei,et al.Are transformers effective for time series forecasting[J].Proceedings of the AAAI Conference on Artificial Intelligence,2023,37(9):11121‒11128. doi:10.1609/aaai.v37i9.26317
[31]
ZhouChuhao, LinPeiqun.Traffic flow prediction method based on multi-channel Transformer[J].Application Research of Computers,2023,40(2):435‒439.
ZhangMingjian, YeBaolin, DongRui,et al.A short-term traffic flow prediction model based on temporal information enhanced Transformer[J].Software Engineer,2024,27(8):41‒45.
WangFangzhou.Forecasting the duration and spatial-temporal impact of freeway accidents based on real-time weather conditions[D].Guangzhou:South China University of Technology,2023.
VaswaniA, ShazeerN, ParmarN,et al.Attention is all you need[C]//Proceedings of the 31st International Conference on Neural Information Processing System.Red Hook:Curran Associates Inc,2017:6000‒6010.
[42]
SohnK, LeeH, YanXinchen.Learning structured output representation using deep conditional generative models[C]//Proceedings of the Neural Information Processing Systems,2015.
[43]
SuMintong, ZhengJin, ZhangZuping.Clustering mining of urban traffic flow based on CVAE[J].Journal of Traffic and Logistics Engineering,2020:34‒44. doi:10.18178/jtle.8.2.34-44
[44]
World Meteorological Organization(WMO).International Cloud Atlas[M].Geneva:WMO,2017.
[45]
LiYaguang, YuR, ShahabiC,et al.Diffusion convolutional recurrent neural network:Data-driven traffic forecasting[C]//Proceedings of the 6th International Conference on Learning Representations(ICLR).Vancouver:ICLR,2018:1‒16.
[46]
YuBing, YinHaoteng, ZhuZhanxing.Spatio-temporal graph convolutional networks:A deep learning framework for traffic forecasting[C]//Proceedings of the Twenty‒Seventh International Joint Conference on Artificial Intelligence.Stockholm:International Joint Conferences on Artificial Intelligence Organization,2018:3634‒3640. doi:10.24963/ijcai.2018/505
[47]
ZhouHaoyi, ZhangShanghang, PengJieqi,et al.Informer:Beyond efficient transformer for long sequence time-series forecasting[J].Proceedings of the AAAI Conference on Artificial Intelligence,2021,35(12):11106‒11115. doi:10.1609/aaai.v35i12.17325
[48]
WuHaixu, XuJiehui, WangJianmin, LongMingsheng.Autoformer:Decomposition transformers with auto-correlation for long-term series forecasting[J].Advances in Neural Information Processing Systems,2021,34:22419‒22430.