✨✨ 欢迎大家来访Srlua的博文(づ ̄3 ̄)づ╭❤~✨✨🌟🌟 欢迎各位亲爱的读者,感谢你们抽出宝贵的时间来阅读我的文章。
我是Srlua小谢,在这里我会分享我的知识和经验。🎥
希望在这里,我们能一起探索IT世界的奥妙,提升我们的技能。🔮
记得先点赞👍后阅读哦~ 👏👏
📘📚 所属专栏:传知代码论文复现
欢迎访问我的主页:Srlua小谢 获取更多信息和资源。✨✨🌙🌙
目录
备注
前言介绍
问题背景
复现:
一. 多维特征提取的提取框架:
二. 论文中进行性能测试的MultiTag2Vec-STLF模型:
三. 与整数编码(IE)的特征处理方法进行对比
部署方式
备注
-
需要本文的详细复现过程的项目源码、数据和预训练好的模型可从该地址处获取完整版:
地址跳转
前言介绍
-
短期电力负荷技术是对未来几小时或一天内电力系统负荷变化进行预测的技术。该技术通过收集和分析历史负荷数据及相关影响因素,运用时间序列分析、回归分析、神经网络、支持向量机等数学模型和方法,对电力负荷进行精确预测。短期电力负荷预测对于电力系统运行和调度至关重要,有助于电力企业制定合理的发电和输电计划,保障电网的安全稳定运行,降低运行成本,提高供电质量和经济效益。
问题背景
一. 基本问题
-
短期电力负荷预测(STLF),即对未来几小时到几周的电力负荷进行准确预测。
二. 本论文发现的问题
-
在电力负荷预测中,由于数据的高维性和波动性,传统的特征提取方法往往难以捕捉到负荷数据中的复杂模式和关系。
对于论文发现问题的解决方案: 本论文通过提出一个名为MultiTag2Vec的特征提取框架来解决短期电力负荷预测(STLF)中的特征工程问题。该框架包括两个主要过程:标记(tagging)和嵌入(embedding)。
-
标记过程:首先,通过从高维时间序列数据中提取关键信息,将电气负荷数据转换成紧凑形式。这一步通过聚类子序列来发现重复出现的模式,并为每个模式分配唯一的标签,从而实现数据的标记。
-
嵌入过程:接下来,通过学习标签序列中的时间和维度关系来提取特征。为了捕捉这些关系,提出了一个带有卷积层的网络模型,该模型采用数学分析设计的多输出结构。通过训练,可以从任何任意多维标签中提取特征。
复现:
一. 多维特征提取的提取框架:
-
时间序列切分,聚类,打标签
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">def</span> <span style="color:#0000ff">segment_time_series</span>(<span style="color:#000000">X</span>, <span style="color:#000000">T</span>):<span style="color:#aa1111">"""</span>
<span style="color:#aa1111"> 将时间序列 X 分段为长度为 T 的子序列。</span>
<span style="color:#aa1111"> X: 多元时间序列 (N x D), N 为时间序列长度, D 为维度数</span>
<span style="color:#aa1111"> T: 每个子序列的长度</span>
<span style="color:#aa1111"> 返回: 分段后的子序列集合,形状为 (N_segment, T, D)</span>
<span style="color:#aa1111"> """</span><span style="color:#000000">N</span>, <span style="color:#000000">D</span> <span style="color:#981a1a">=</span> <span style="color:#000000">X</span>.<span style="color:#000000">shape</span><span style="color:#000000">N_segment</span> <span style="color:#981a1a">=</span> <span style="color:#000000">N</span> <span style="color:#981a1a">//</span> <span style="color:#000000">T</span> <span style="color:#aa5500"># 计算分段后的子序列数量</span><span style="color:#000000">segments</span> <span style="color:#981a1a">=</span> <span style="color:#000000">np</span>.<span style="color:#000000">array</span>([<span style="color:#000000">X</span>[<span style="color:#000000">i</span><span style="color:#981a1a">*</span><span style="color:#000000">T</span>:(<span style="color:#000000">i</span><span style="color:#981a1a">+</span><span style="color:#116644">1</span>)<span style="color:#981a1a">*</span><span style="color:#000000">T</span>] <span style="color:#770088">for</span> <span style="color:#000000">i</span> <span style="color:#770088">in</span> <span style="color:#3300aa">range</span>(<span style="color:#000000">N_segment</span>)])<span style="color:#770088">return</span> <span style="color:#000000">segments</span>
<span style="color:#aa5500"># 2. 模式发现</span>
<span style="color:#770088">def</span> <span style="color:#0000ff">discover_patterns</span>(<span style="color:#000000">segments</span>, <span style="color:#000000">K</span>):<span style="color:#aa1111">"""</span>
<span style="color:#aa1111"> 对分段后的子序列进行聚类,提取模式。</span>
<span style="color:#aa1111"> segments: 分段后的子序列集合, 形状为 (N_segment, T, D)</span>
<span style="color:#aa1111"> K: 聚类的数量,即模式的数量</span>
<span style="color:#aa1111"> 返回: 每个维度的模式集合,形状为 (K, T, D)</span>
<span style="color:#aa1111"> """</span><span style="color:#000000">N_segment</span>, <span style="color:#000000">T</span>, <span style="color:#000000">D</span> <span style="color:#981a1a">=</span> <span style="color:#000000">segments</span>.<span style="color:#000000">shape</span><span style="color:#000000">patterns</span> <span style="color:#981a1a">=</span> []<span style="color:#aa5500"># 对每个维度单独进行聚类</span><span style="color:#770088">for</span> <span style="color:#000000">d</span> <span style="color:#770088">in</span> <span style="color:#3300aa">range</span>(<span style="color:#000000">D</span>):<span style="color:#aa5500"># 提取第 d 个维度的所有子序列</span><span style="color:#000000">data_d</span> <span style="color:#981a1a">=</span> <span style="color:#000000">segments</span>[:, :, <span style="color:#000000">d</span>] <span style="color:#aa5500"># 形状为 (N_segment, T)</span><span style="color:#aa5500"># 使用 KMeans 进行聚类</span><span style="color:#000000">kmeans</span> <span style="color:#981a1a">=</span> <span style="color:#000000">KMeans</span>(<span style="color:#000000">n_clusters</span><span style="color:#981a1a">=</span><span style="color:#000000">K</span>, <span style="color:#000000">random_state</span><span style="color:#981a1a">=</span><span style="color:#116644">42</span>)<span style="color:#000000">kmeans</span>.<span style="color:#000000">fit</span>(<span style="color:#000000">data_d</span>)<span style="color:#aa5500"># 保存聚类中心(模式)</span><span style="color:#000000">patterns</span>.<span style="color:#000000">append</span>(<span style="color:#000000">kmeans</span>.<span style="color:#000000">cluster_centers_</span>)<span style="color:#aa5500"># patterns 为 D 维的聚类中心集合,形状为 (D, K, T)</span><span style="color:#770088">return</span> <span style="color:#000000">np</span>.<span style="color:#000000">array</span>(<span style="color:#000000">patterns</span>)
<span style="color:#aa5500"># 3. 数据标记</span>
<span style="color:#770088">def</span> <span style="color:#0000ff">tag_data</span>(<span style="color:#000000">segments</span>, <span style="color:#000000">patterns</span>):<span style="color:#aa1111">"""</span>
<span style="color:#aa1111"> 对每个子序列打标签,标签为距离最近的聚类中心。</span>
<span style="color:#aa1111"> segments: 分段后的子序列集合, 形状为 (N_segment, T, D)</span>
<span style="color:#aa1111"> patterns: 每个维度的聚类中心集合,形状为 (D, K, T)</span>
<span style="color:#aa1111"> 返回: 每个子序列的标签集合,形状为 (N_segment, D)</span>
<span style="color:#aa1111"> """</span><span style="color:#000000">N_segment</span>, <span style="color:#000000">T</span>, <span style="color:#000000">D</span> <span style="color:#981a1a">=</span> <span style="color:#000000">segments</span>.<span style="color:#000000">shape</span><span style="color:#000000">K</span> <span style="color:#981a1a">=</span> <span style="color:#000000">patterns</span>.<span style="color:#000000">shape</span>[<span style="color:#116644">1</span>] <span style="color:#aa5500"># 模式的数量</span><span style="color:#000000">labels</span> <span style="color:#981a1a">=</span> <span style="color:#000000">np</span>.<span style="color:#000000">zeros</span>((<span style="color:#000000">N_segment</span>, <span style="color:#000000">D</span>), <span style="color:#000000">dtype</span><span style="color:#981a1a">=</span><span style="color:#3300aa">int</span>)<span style="color:#aa5500"># 对每个维度进行标记</span><span style="color:#770088">for</span> <span style="color:#000000">d</span> <span style="color:#770088">in</span> <span style="color:#3300aa">range</span>(<span style="color:#000000">D</span>):<span style="color:#770088">for</span> <span style="color:#000000">i</span> <span style="color:#770088">in</span> <span style="color:#3300aa">range</span>(<span style="color:#000000">N_segment</span>):<span style="color:#aa5500"># 计算当前子序列与所有聚类中心的距离</span><span style="color:#000000">distances</span> <span style="color:#981a1a">=</span> <span style="color:#000000">np</span>.<span style="color:#000000">linalg</span>.<span style="color:#000000">norm</span>(<span style="color:#000000">segments</span>[<span style="color:#000000">i</span>, :, <span style="color:#000000">d</span>] <span style="color:#981a1a">-</span> <span style="color:#000000">patterns</span>[<span style="color:#000000">d</span>], <span style="color:#000000">axis</span><span style="color:#981a1a">=</span><span style="color:#116644">1</span>)<span style="color:#aa5500"># 选择最小距离的聚类中心的标签</span><span style="color:#000000">labels</span>[<span style="color:#000000">i</span>, <span style="color:#000000">d</span>] <span style="color:#981a1a">=</span> <span style="color:#000000">np</span>.<span style="color:#000000">argmin</span>(<span style="color:#000000">distances</span>)<span style="color:#770088">return</span> <span style="color:#000000">labels</span></span></span>
-
嵌入网络定义:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">class</span> <span style="color:#0000ff">EmbeddingNetwork</span>(<span style="color:#000000">nn</span>.<span style="color:#000000">Module</span>):<span style="color:#770088">def</span> <span style="color:#0000ff">__init__</span>(<span style="color:#0055aa">self</span>, <span style="color:#000000">D</span>, <span style="color:#000000">K</span>, <span style="color:#000000">M</span>):<span style="color:#3300aa">super</span>(<span style="color:#000000">EmbeddingNetwork</span>, <span style="color:#0055aa">self</span>).<span style="color:#000000">__init__</span>()<span style="color:#aa5500"># 卷积层,用于提取输入张量的特征</span><span style="color:#0055aa">self</span>.<span style="color:#000000">conv</span> <span style="color:#981a1a">=</span> <span style="color:#000000">nn</span>.<span style="color:#000000">Conv2d</span>(<span style="color:#000000">in_channels</span><span style="color:#981a1a">=</span><span style="color:#000000">D</span>, <span style="color:#000000">out_channels</span><span style="color:#981a1a">=</span><span style="color:#000000">M</span>, <span style="color:#000000">kernel_size</span><span style="color:#981a1a">=</span>(<span style="color:#116644">1</span>, <span style="color:#000000">K</span>), <span style="color:#000000">stride</span><span style="color:#981a1a">=</span><span style="color:#116644">1</span>) <span style="color:#0055aa">self</span>.<span style="color:#000000">pool</span> <span style="color:#981a1a">=</span> <span style="color:#000000">nn</span>.<span style="color:#000000">AdaptiveAvgPool2d</span>((<span style="color:#116644">1</span>, <span style="color:#116644">1</span>))
<span style="color:#aa5500"># 两个并行的全连接层,用于预测两个维度的输出标签</span><span style="color:#0055aa">self</span>.<span style="color:#000000">fc1</span> <span style="color:#981a1a">=</span> <span style="color:#000000">nn</span>.<span style="color:#000000">Linear</span>(<span style="color:#000000">M</span>, <span style="color:#000000">K</span>)<span style="color:#0055aa">self</span>.<span style="color:#000000">fc2</span> <span style="color:#981a1a">=</span> <span style="color:#000000">nn</span>.<span style="color:#000000">Linear</span>(<span style="color:#000000">M</span>, <span style="color:#000000">K</span>)
<span style="color:#770088">def</span> <span style="color:#0000ff">forward</span>(<span style="color:#0055aa">self</span>, <span style="color:#000000">x</span>):<span style="color:#aa5500"># 卷积层</span><span style="color:#3300aa">print</span>(<span style="color:#000000">x</span>.<span style="color:#000000">shape</span>)<span style="color:#000000">x</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">self</span>.<span style="color:#000000">conv</span>(<span style="color:#000000">x</span>) <span style="color:#aa5500"># 卷积操作</span><span style="color:#3300aa">print</span>(<span style="color:#000000">x</span>.<span style="color:#000000">shape</span>)<span style="color:#000000">x</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">self</span>.<span style="color:#000000">pool</span>(<span style="color:#000000">x</span>) <span style="color:#aa5500"># 使用自适应平均池化,将每个样本缩减为大小为 (M, 1)</span><span style="color:#3300aa">print</span>(<span style="color:#000000">x</span>.<span style="color:#000000">shape</span>)<span style="color:#000000">x</span> <span style="color:#981a1a">=</span> <span style="color:#000000">x</span>.<span style="color:#000000">view</span>(<span style="color:#000000">x</span>.<span style="color:#000000">size</span>(<span style="color:#116644">0</span>), <span style="color:#981a1a">-</span><span style="color:#116644">1</span>) <span style="color:#aa5500"># 展平张量,形状变为 (batch_size, M)</span><span style="color:#aa5500"># 两个并行的全连接层</span><span style="color:#000000">output1</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">self</span>.<span style="color:#000000">fc1</span>(<span style="color:#000000">x</span>) <span style="color:#aa5500"># 维度1的输出</span><span style="color:#000000">output2</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">self</span>.<span style="color:#000000">fc2</span>(<span style="color:#000000">x</span>) <span style="color:#aa5500"># 维度2的输出</span><span style="color:#aa5500"># 将两个输出拼接在一起,形成最后的输出</span><span style="color:#000000">output</span> <span style="color:#981a1a">=</span> <span style="color:#000000">torch</span>.<span style="color:#000000">stack</span>((<span style="color:#000000">output1</span>, <span style="color:#000000">output2</span>), <span style="color:#000000">dim</span><span style="color:#981a1a">=</span><span style="color:#116644">1</span>)<span style="color:#770088">return</span> <span style="color:#000000">output</span></span></span>
二. 论文中进行性能测试的MultiTag2Vec-STLF模型:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">class</span> <span style="color:#0000ff">FeatureExtractor</span>(<span style="color:#000000">nn</span>.<span style="color:#000000">Module</span>):<span style="color:#770088">def</span> <span style="color:#0000ff">__init__</span>(<span style="color:#0055aa">self</span>, <span style="color:#000000">embedding_network</span>):<span style="color:#3300aa">super</span>(<span style="color:#000000">FeatureExtractor</span>, <span style="color:#0055aa">self</span>).<span style="color:#000000">__init__</span>()<span style="color:#0055aa">self</span>.<span style="color:#000000">conv</span> <span style="color:#981a1a">=</span> <span style="color:#000000">embedding_network</span>.<span style="color:#000000">conv</span>
<span style="color:#770088">def</span> <span style="color:#0000ff">forward</span>(<span style="color:#0055aa">self</span>, <span style="color:#000000">x</span>):<span style="color:#000000">x</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">self</span>.<span style="color:#000000">conv</span>(<span style="color:#000000">x</span>) <span style="color:#aa5500"># 卷积层</span><span style="color:#000000">x</span> <span style="color:#981a1a">=</span> <span style="color:#000000">x</span>.<span style="color:#000000">view</span>(<span style="color:#000000">x</span>.<span style="color:#000000">size</span>(<span style="color:#116644">0</span>), <span style="color:#981a1a">-</span><span style="color:#116644">1</span>) <span style="color:#aa5500"># 展平张量</span><span style="color:#770088">return</span> <span style="color:#000000">x</span>
<span style="color:#aa5500"># 初始化特征提取器</span>
<span style="color:#000000">feature_extractor</span> <span style="color:#981a1a">=</span> <span style="color:#000000">FeatureExtractor</span>(<span style="color:#000000">embedding_network</span>)
<span style="color:#aa5500"># 4. 定义 MultiTag2Vec-STLF 模型</span>
<span style="color:#770088">class</span> <span style="color:#0000ff">MultiTag2VecSTLF</span>(<span style="color:#000000">nn</span>.<span style="color:#000000">Module</span>):<span style="color:#770088">def</span> <span style="color:#0000ff">__init__</span>(<span style="color:#0055aa">self</span>, <span style="color:#000000">input_dim</span>, <span style="color:#000000">hidden_dim</span>, <span style="color:#000000">output_dim</span>, <span style="color:#000000">feature_extractor</span>):<span style="color:#3300aa">super</span>(<span style="color:#000000">MultiTag2VecSTLF</span>, <span style="color:#0055aa">self</span>).<span style="color:#000000">__init__</span>()<span style="color:#0055aa">self</span>.<span style="color:#000000">feature_extractor</span> <span style="color:#981a1a">=</span> <span style="color:#000000">feature_extractor</span><span style="color:#aa5500"># 冻结特征提取器的参数</span><span style="color:#770088">for</span> <span style="color:#000000">param</span> <span style="color:#770088">in</span> <span style="color:#0055aa">self</span>.<span style="color:#000000">feature_extractor</span>.<span style="color:#000000">parameters</span>():<span style="color:#000000">param</span>.<span style="color:#000000">requires_grad</span> <span style="color:#981a1a">=</span> <span style="color:#770088">False</span><span style="color:#aa5500"># 双向 LSTM 层</span><span style="color:#0055aa">self</span>.<span style="color:#000000">lstm</span> <span style="color:#981a1a">=</span> <span style="color:#000000">nn</span>.<span style="color:#000000">LSTM</span>(<span style="color:#000000">input_dim</span>, <span style="color:#000000">hidden_dim</span>, <span style="color:#000000">batch_first</span><span style="color:#981a1a">=</span><span style="color:#770088">True</span>, <span style="color:#000000">bidirectional</span><span style="color:#981a1a">=</span><span style="color:#770088">True</span>)<span style="color:#aa5500"># 自注意力机制</span><span style="color:#0055aa">self</span>.<span style="color:#000000">attention</span> <span style="color:#981a1a">=</span> <span style="color:#000000">nn</span>.<span style="color:#000000">MultiheadAttention</span>(<span style="color:#000000">embed_dim</span><span style="color:#981a1a">=</span><span style="color:#116644">2</span> <span style="color:#981a1a">*</span> <span style="color:#000000">hidden_dim</span>, <span style="color:#000000">num_heads</span><span style="color:#981a1a">=</span><span style="color:#116644">1</span>, <span style="color:#000000">batch_first</span><span style="color:#981a1a">=</span><span style="color:#770088">True</span>)<span style="color:#aa5500"># 全连接层用于预测下一天 24 小时的负荷</span><span style="color:#0055aa">self</span>.<span style="color:#000000">fc</span> <span style="color:#981a1a">=</span> <span style="color:#000000">nn</span>.<span style="color:#000000">Linear</span>(<span style="color:#116644">2</span> <span style="color:#981a1a">*</span> <span style="color:#000000">hidden_dim</span>, <span style="color:#000000">output_dim</span>)
<span style="color:#770088">def</span> <span style="color:#0000ff">forward</span>(<span style="color:#0055aa">self</span>, <span style="color:#000000">x</span>):<span style="color:#000000">x</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">self</span>.<span style="color:#000000">feature_extractor</span>(<span style="color:#000000">x</span>)<span style="color:#000000">x</span> <span style="color:#981a1a">=</span> <span style="color:#000000">x</span>.<span style="color:#000000">view</span>(<span style="color:#000000">x</span>.<span style="color:#000000">size</span>()[<span style="color:#116644">0</span>], <span style="color:#000000">seg_c</span>, <span style="color:#981a1a">-</span><span style="color:#116644">1</span>)<span style="color:#aa5500"># LSTM 前向传播</span><span style="color:#000000">lstm_out</span>, <span style="color:#000000">_</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">self</span>.<span style="color:#000000">lstm</span>(<span style="color:#000000">x</span>) <span style="color:#aa5500"># lstm_out 形状: (batch_size, seq_length, 2 * hidden_dim)</span><span style="color:#aa5500"># 注意力机制</span><span style="color:#000000">attn_output</span>, <span style="color:#000000">_</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">self</span>.<span style="color:#000000">attention</span>(<span style="color:#000000">lstm_out</span>, <span style="color:#000000">lstm_out</span>, <span style="color:#000000">lstm_out</span>) <span style="color:#aa5500"># 计算自注意力,形状: (batch_size, seq_length, 2 * hidden_dim)</span><span style="color:#000000">context_vector</span> <span style="color:#981a1a">=</span> <span style="color:#000000">torch</span>.<span style="color:#000000">sum</span>(<span style="color:#000000">attn_output</span>, <span style="color:#000000">dim</span><span style="color:#981a1a">=</span><span style="color:#116644">1</span>) <span style="color:#aa5500"># 计算上下文向量,形状: (batch_size, 2 * hidden_dim)</span><span style="color:#aa5500"># 全连接层预测</span><span style="color:#000000">output</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">self</span>.<span style="color:#000000">fc</span>(<span style="color:#000000">context_vector</span>) <span style="color:#aa5500"># 预测输出,形状: (batch_size, output_dim)</span><span style="color:#770088">return</span> <span style="color:#000000">output</span></span></span>
三. 与整数编码(IE)的特征处理方法进行对比
使用论文中的GEFCom2014数据集中的温度和负荷数据,训练的参数设置按照论文中最优效果的参数设置。论文中使用的温度数据来自于数据集中的哪一个气象站,论文中没有说,此处是选择w1气象站的温度数据训练的结果和论文中的RMSE指标不太一样,但是从IE和MultiTag2Vec的RMSE指标对比可以看到,论文提出的特征提取方法具有一定优势。
部署方式
-
Python 3.9.12
-
Pytorch
-
以及其他的常用python库
-
需要本文的详细复现过程的项目源码、数据和预训练好的模型可从该地址处获取完整版:
地址跳转
希望对你有帮助!加油!
若您认为本文内容有益,请不吝赐予赞同并订阅,以便持续接收有价值的信息。衷心感谢您的关注和支持!