TCN 一维预测の笔记

这篇博客记录了使用Tensorflow.Keras而非纯Keras实现一维时间序列预测任务的过程。内容包括数据集介绍,如美国1962年至1975年牛奶产量数据,模型构建、优化器设置、模型训练及结果可视化。示例代码简洁明了,未进行数据预处理或归一化操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来源

这个例子Support of tensorflow.keras instead of keras
https://github.com/philipperemy/keras-tcn/tree/master/tasks

数据

month milk_production_pounds
1962-01 589
1962-02 561
1962-03 640
1962-04 656
1962-05 727
1962-06 697
1962-07 640
1962-08 599
。。。。。。。。。。
1975-09 817
1975-10 827
1975-11 797
1975-12 843

代码

加载包

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import Dense
from tcn import TCN

说明

这个例子非常简单,
为了简单起见一切都是训练集
输入输入没有normalization正则化。

读取数据

milk = pd.read_csv('monthly-milk-production-pounds-p.csv', index_col=
### 使用TCN模型预测二维时间序列数据 对于二维时间序列数据的预测,可以采用扩展的时间卷积网络(TCN)。TCN是一种基于卷积神经网络(CNN)架构设计的模型,特别适用于处理顺序数据。该方法不仅能够捕捉局部特征,还能通过堆叠多层膨胀卷积有效扩大感受野范围,从而更好地理解长期依赖关系[^1]。 为了适应二维输入,通常的做法是在原有的一维卷积基础上增加额外的空间维度。具体来说: - 输入张量形状应调整为 `[batch_size, channels, height, width]` 或者 `[batch_size, time_steps, features]` 形式的三维数组; - 卷积操作需改为两维形式 `Conv2D` 来替代标准的一维版本; - 如果原始问题是单变量时间序列,则可能需要先将其转换成适合二维表示的形式;如果是多变量情况则可以直接作为矩阵输入给模型训练过程。 下面给出一段简化版Python代码片段展示如何构建并训练这样一个改进后的TCN模型来进行二维时间序列预测任务: ```python import torch from torch import nn class TCNLayer(nn.Module): def __init__(self, c_in, c_out, kernel_size=3, dilation=1): super().__init__() padding = (kernel_size - 1) * dilation self.conv = nn.Conv2d(c_in, c_out, kernel_size=(kernel_size, 1), stride=1, padding=(padding, 0), dilation=dilation) def forward(self, x): return self.conv(x) class TemporalBlock(nn.Module): def __init__(self, n_inputs, n_outputs, kernel_size, stride, dilation, dropout=0.2): super(TemporalBlock, self).__init__() conv_layer_1 = TCNLayer(n_inputs, n_outputs, kernel_size, dilation) relu_1 = nn.ReLU() dropout_1 = nn.Dropout(dropout) conv_layer_2 = TCNLayer(n_outputs, n_outputs, kernel_size, dilation*2) relu_2 = nn.ReLU() dropout_2 = nn.Dropout(dropout) net = nn.Sequential(conv_layer_1, relu_1, dropout_1, conv_layer_2, relu_2, dropout_2) downsample = nn.Conv2d( n_inputs, n_outputs, 1) if n_inputs != n_outputs else None self.net = nn.Sequential(net) self.downsample = downsample self.relu = nn.ReLU() def forward(self, x): out = self.net(x) res = x if self.downsample is None else self.downsample(x) return self.relu(out + res) class TCNPredictor(nn.Module): def __init__(self, input_channels, output_channels, num_layers=8, hidden_units=64, kernel_size=7, dropout=0.2): super(TCNPredictor, self).__init__() layers = [] for i in range(num_layers): dilations = 2 ** i layer = TemporalBlock(input_channels if i == 0 else hidden_units, hidden_units, kernel_size, stride=1, dilation=dilations, dropout=dropout) layers.append(layer) final_conv = nn.Conv2d(hidden_units, output_channels, 1) seq_model = [*layers, final_conv] model = nn.Sequential(*seq_model) model.apply(init_weights) def init_weights(m): if isinstance(m, nn.Conv2d): nn.init.xavier_uniform_(m.weight.data) model.apply(init_weights) sequence_length = 100 # 假设的时间步数长度 feature_dim = 5 # 特征数量 batch_size = 32 # 批次大小 dummy_input = torch.randn(batch_size, feature_dim, sequence_length, 1) print(model(dummy_input).shape) tcn_predictor = TCNPredictor(5, 1) print(tcn_predictor) ``` 此段代码定义了一个基本框架用于创建一个针对二维时间序列数据优化过的TCN预测器实例,并打印出了模型结构及其预期输出尺寸。需要注意的是这只是一个非常基础的例子,在实际应用中还需要考虑更多因素如正则化技术的选择、超参数调优等[^2]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值