【网络代码】1DCNN算法
1. 什么是一维卷积神经网络(1D-CNN)

1.1 定义与核心思想
1D-CNN 是专门用于处理一维序列数据的卷积网络结构。
卷积核只沿着一个方向滑动(通常是时间轴),从而实现:
- 捕捉局部时间模式(local temporal pattern)
- 建立多尺度感受野(multi-scale receptive field)
- 增加时间维度上的特征组合能力
典型输入格式为:
(batch_size, seq_len, input_dim) 为了使用 PyTorch 的 Conv1d,需要转换为:
(batch_size, channels=input_dim, seq_len) 其中:
- seq_len:时间步长度(如过去10天)
- input_dim:每个时间步的特征维度(如传感器数量、指标数量)
2. 1D-CNN 的适用场景
1D-CNN 特别适合以下任务:
✔ 时间序列预测(能源、电力、气象、金融等)
捕捉短期局部模式,如周期、趋势、小范围波动。
✔ 信号处理(语音、振动、加速度)
通过卷积核识别局部频率模式和形态变化。
✔ 多传感器/表格时间序列建模
在工业场景中常用于压力、温度、震动等实时监控数据。
✔ CNN + RNN 的混合架构
CNN 用于特征提取,后接 LSTM/GRU 用于长序列建模。
3. 1D-CNN 的结构解析
1D 卷积结构通常由以下组件组成:
输入 → Conv1d → ReLU → BatchNorm → Pooling → 全连接层输出 多个卷积层堆叠可以扩大网络的有效感受野,使模型能够捕捉更长时间跨度的模式。
核心优势对比 LSTM
| 指标 | 1D-CNN | LSTM/GRU |
|---|---|---|
| 并行计算 | ✔ 强并行 | ✘ 序列依赖 |
| 训练速度 | 快 | 较慢 |
| 捕捉局部模式 | ✔ 强 | 中等 |
| 长期依赖建模 | 需要堆叠 dilated CNN | ✔ 天生优势 |
| 参数量 | 少 | 多 |
| 工业部署 | 简单高效 | 相对复杂 |
4. 1D-CNN 多步预测深度实现(含代码)
- 输入跨度:10
- 输出跨度:5
- 输入维度:100
- 输出维度:10
我们将提供四类模型:
- 基础版 CNN
- 深度堆叠卷积版
- 残差 CNN(Res-CNN)
- 时序卷积网络(TCN,dilation 卷积)
4.1 基础版 1D-CNN(轻量快速)
import torch import torch.nn as nn import torch.nn.functional as F classTabularCNN1D(nn.Module):def__init__(self, input_dim=100, seq_len=10, num_filters=32, kernel_size=3, output_steps=5, output_dim=10):super().__init__() self.conv1 = nn.Conv1d(in_channels=input_dim, out_channels=num_filters, kernel_size=kernel_size, padding=1) self.bn1 = nn.BatchNorm1d(num_filters) self.pool = nn.AdaptiveMaxPool1d(1) self.fc = nn.Linear(num_filters, output_steps * output_dim) self.output_steps = output_steps self.output_dim = output_dim defforward(self, x): x = x.permute(0,2,1) x = F.relu(self.bn1(self.conv1(x))) x = self.pool(x).squeeze(-1) out = self.fc(x)return out.view(-1, self.output_steps, self.output_dim)适用于轻量部署和快速迭代。
4.2 堆叠卷积(深度 CNN)
增加感受野,提升非线性表达能力。
classDeepCNN1D(nn.Module):def__init__(self, input_dim=100, seq_len=10, output_steps=5, output_dim=10):super().__init__() self.conv_layers = nn.Sequential( nn.Conv1d(input_dim,128,3, padding=1), nn.BatchNorm1d(128), nn.ReLU(), nn.Conv1d(128,64,3, padding=1), nn.BatchNorm1d(64), nn.ReLU(), nn.AdaptiveAvgPool1d(1)) self.fc = nn.Linear(64, output_steps * output_dim)defforward(self, x): x = x.permute(0,2,1) x = self.conv_layers(x).squeeze(-1) out = self.fc(x)return out.view(-1, output_steps, output_dim)适合复杂模式、非线性更强的场景。
4.3 残差 CNN(ResNet 风格)
通过 shortcut 解决深层网络梯度衰减。
classResBlock(nn.Module):def__init__(self, in_channels, out_channels):super().__init__() self.conv1 = nn.Conv1d(in_channels, out_channels,3, padding=1) self.bn1 = nn.BatchNorm1d(out_channels) self.conv2 = nn.Conv1d(out_channels, out_channels,3, padding=1) self.bn2 = nn.BatchNorm1d(out_channels) self.shortcut = nn.Conv1d(in_channels, out_channels,1) \ if in_channels != out_channels else nn.Identity()defforward(self, x): residual = self.shortcut(x) out = F.relu(self.bn1(self.conv1(x))) out = self.bn2(self.conv2(out))return F.relu(out + residual)classResCNN1D(nn.Module):def__init__(self, input_dim=100, output_steps=5, output_dim=10):super().__init__() self.conv_in = nn.Conv1d(input_dim,64,3, padding=1) self.resblock1 = ResBlock(64,128) self.resblock2 = ResBlock(128,128) self.pool = nn.AdaptiveAvgPool1d(1) self.fc = nn.Linear(128, output_steps * output_dim)defforward(self, x): x = x.permute(0,2,1) x = F.relu(self.conv_in(x)) x = self.resblock1(x) x = self.resblock2(x) x = self.pool(x).squeeze(-1) out = self.fc(x)return out.view(-1, output_steps, output_dim)适合深度网络、表达能力强的预测任务。
4.4 时序卷积网络(TCN)
通过**膨胀卷积(dilated convolution)**实现超大感受野,不需要递归即可建模长期依赖。
classTemporalBlock(nn.Module):def__init__(self, in_channels, out_channels, kernel_size, dilation):super().__init__() padding =(kernel_size -1)* dilation self.conv1 = nn.Conv1d(in_channels, out_channels, kernel_size, padding=padding, dilation=dilation) self.bn1 = nn.BatchNorm1d(out_channels) self.conv2 = nn.Conv1d(out_channels, out_channels, kernel_size, padding=padding, dilation=dilation) self.bn2 = nn.BatchNorm1d(out_channels) self.shortcut = nn.Conv1d(in_channels, out_channels,1) \ if in_channels != out_channels else nn.Identity() self.padding = padding defforward(self, x): res = self.shortcut(x) out = F.relu(self.bn1(self.conv1(x))) out = F.relu(self.bn2(self.conv2(out))) out = out[:,:,:-self.padding]return F.relu(out + res)classTCN1D(nn.Module):def__init__(self, input_dim=100, output_steps=5, output_dim=10):super().__init__() self.tblock1 = TemporalBlock(input_dim,128,3, dilation=1) self.tblock2 = TemporalBlock(128,128,3, dilation=2) self.pool = nn.AdaptiveAvgPool1d(1) self.fc = nn.Linear(128, output_steps * output_dim)defforward(self, x): x = x.permute(0,2,1) x = self.tblock1(x) x = self.tblock2(x) x = self.pool(x).squeeze(-1) out = self.fc(x)return out.view(-1, output_steps, output_dim)这是目前工业界最强的 CNN 时序结构之一。
5. 1D-CNN 在时间序列预测中的优势总结
- 快:全卷积结构支持 GPU 并行,远超 RNN/LSTM 的逐步计算。
- 轻:参数数量少,模型易部署。
- 稳定:无梯度爆炸/梯度消失问题。
- 适配多维输入:天然适合传感器数据、表格时序。
- 易扩展为深度结构:残差、dilation、TCN 都是 CNN 体系内部升级。
6. 最佳实践建议
- 短序列用 CNN,长序列用 TCN 或 CNN+LSTM
- 卷积核大小一般取 3 或 5
- 多特征输入时设置 input_dim = 特征数量
- 多步输出建议使用 Dense 直接映射(如本例)
- 工业部署中 Res-CNN 通常表现最稳