PyTorch搭建ANN实现时间序列预测(风速预测)

39 篇文章 204 订阅
36 篇文章 147 订阅


时间序列预测系列文章:

  1. 深入理解PyTorch中LSTM的输入和输出(从input输入到Linear输出)
  2. PyTorch搭建LSTM实现时间序列预测(负荷预测)
  3. PyTorch中利用LSTMCell搭建多层LSTM实现时间序列预测
  4. PyTorch搭建LSTM实现多变量时间序列预测(负荷预测)
  5. PyTorch搭建双向LSTM实现时间序列预测(负荷预测)
  6. PyTorch搭建LSTM实现多变量多步长时间序列预测(一):直接多输出
  7. PyTorch搭建LSTM实现多变量多步长时间序列预测(二):单步滚动预测
  8. PyTorch搭建LSTM实现多变量多步长时间序列预测(三):多模型单步预测
  9. PyTorch搭建LSTM实现多变量多步长时间序列预测(四):多模型滚动预测
  10. PyTorch搭建LSTM实现多变量多步长时间序列预测(五):seq2seq
  11. PyTorch中实现LSTM多步长时间序列预测的几种方法总结(负荷预测)
  12. PyTorch-LSTM时间序列预测中如何预测真正的未来值
  13. PyTorch搭建LSTM实现多变量输入多变量输出时间序列预测(多任务学习)
  14. PyTorch搭建ANN实现时间序列预测(风速预测)
  15. PyTorch搭建CNN实现时间序列预测(风速预测)
  16. PyTorch搭建CNN-LSTM混合模型实现多变量多步长时间序列预测(负荷预测)
  17. PyTorch搭建Transformer实现多变量多步长时间序列预测(负荷预测)
  18. PyTorch时间序列预测系列文章总结(代码使用方法)
  19. TensorFlow搭建LSTM实现时间序列预测(负荷预测)
  20. TensorFlow搭建LSTM实现多变量时间序列预测(负荷预测)
  21. TensorFlow搭建双向LSTM实现时间序列预测(负荷预测)
  22. TensorFlow搭建LSTM实现多变量多步长时间序列预测(一):直接多输出
  23. TensorFlow搭建LSTM实现多变量多步长时间序列预测(二):单步滚动预测
  24. TensorFlow搭建LSTM实现多变量多步长时间序列预测(三):多模型单步预测
  25. TensorFlow搭建LSTM实现多变量多步长时间序列预测(四):多模型滚动预测
  26. TensorFlow搭建LSTM实现多变量多步长时间序列预测(五):seq2seq
  27. TensorFlow搭建LSTM实现多变量输入多变量输出时间序列预测(多任务学习)
  28. TensorFlow搭建ANN实现时间序列预测(风速预测)
  29. TensorFlow搭建CNN实现时间序列预测(风速预测)
  30. TensorFlow搭建CNN-LSTM混合模型实现多变量多步长时间序列预测(负荷预测)
  31. PyG搭建图神经网络实现多变量输入多变量输出时间序列预测
  32. PyTorch搭建GNN-LSTM和LSTM-GNN模型实现多变量输入多变量输出时间序列预测
  33. PyG Temporal搭建STGCN实现多变量输入多变量输出时间序列预测
  34. 时序预测中Attention机制是否真的有效?盘点LSTM/RNN中24种Attention机制+效果对比
  35. 详解Transformer在时序预测中的Encoder和Decoder过程:以负荷预测为例
  36. (PyTorch)TCN和RNN/LSTM/GRU结合实现时间序列预测
  37. PyTorch搭建Informer实现长序列时间序列预测
  38. PyTorch搭建Autoformer实现长序列时间序列预测

I. 数据集

在这里插入图片描述
数据集为Barcelona某段时间内的气象数据,其中包括温度、湿度以及风速等。本文将简单搭建来对风速进行预测。

II. 特征构造

对于风速的预测,除了考虑历史风速数据外,还应该充分考虑其余气象因素的影响。因此,我们根据前24个时刻的风速+其余气象数据来预测下一时刻的风速。

III. 数据处理

1.数据预处理

数据预处理阶段,主要将某些列上的文本数据转为数值型数据,同时对原始数据进行归一化处理。文本数据如下所示:
在这里插入图片描述
经过转换后,上述各个类别分别被赋予不同的数值,比如"sky is clear"为0,"few clouds"为1。

def load_data():
    df = pd.read_csv('Barcelona/Barcelona.csv')
    df.drop_duplicates(subset=[df.columns[0]], inplace=True)
    df.drop([df.columns[0], df.columns[1]], axis=1, inplace=True)
    # weather_main
    listType = df['weather_main'].unique()
    df.fillna(method='ffill', inplace=True)
    dic = dict.fromkeys(listType)
    for i in range(len(listType)):
        dic[listType[i]] = i
    df['weather_main'] = df['weather_main'].map(dic)
    # weather_description
    listType = df['weather_description'].unique()
    dic = dict.fromkeys(listType)
    for i in range(len(listType)):
        dic[listType[i]] = i
    df['weather_description'] = df['weather_description'].map(dic)
    # weather_icon
    listType = df['weather_icon'].unique()
    dic = dict.fromkeys(listType)
    for i in range(len(listType)):
        dic[listType[i]] = i
    df['weather_icon'] = df['weather_icon'].map(dic)
    # print(df)
    return df

2.数据集构造

利用前24个小时的风速数据+气象数据来预测下一时刻的风速,数据被划分为三部分:Dtr、Val以及Dte,Dtr用作训练集,Val用作验证集,Dte用作测试集,模型训练返回的是验证集上表现最优的模型。

IV. ANN模型

1.模型训练

ANN模型搭建如下:

class ANN(nn.Module):
    def __init__(self):
        super(ANN, self).__init__()
        self.seq_len = seq_len
        self.input_size = input_size
        self.nn = torch.nn.Sequential(
            nn.Linear(seq_len * input_size, 128),
            torch.nn.ReLU(),
            nn.Linear(128, 256),
            torch.nn.ReLU(),
            nn.Linear(256, 128),
            torch.nn.ReLU(),
            nn.Linear(128, 1)
        )

    def forward(self, x):
        # x(batch_size, seq_len, input_size)
        x = x.view(x.shape[0], -1)
        x = self.nn(x)
        return x

2.模型预测及表现

训练50轮,ANN在Dte上的表现如下表所示:

MAERMSE
0.961.28

在这里插入图片描述

V. 源码及数据

后面将陆续公开~

  • 19
    点赞
  • 90
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论
以下是使用PyTorch实现的CNN(一维卷积神经网络)用于时间序列预测的示例代码: ```python import torch import torch.nn as nn class TimeSeriesCNN(nn.Module): def __init__(self, input_dim, hidden_dim, output_dim): super(TimeSeriesCNN, self).__init__() self.conv1 = nn.Conv1d(input_dim, hidden_dim, kernel_size=3) self.relu = nn.ReLU() self.fc = nn.Linear(hidden_dim, output_dim) def forward(self, x): x = self.conv1(x) x = self.relu(x) x = torch.mean(x, dim=2) x = self.fc(x) return x # 定义输入数据和标签 input_dim = 1 # 输入维度(时间序列的特征数) hidden_dim = 16 # 隐藏层维度 output_dim = 1 # 输出维度(预测的目标) seq_length = 10 # 时间序列的长度 # 创建模型实例 model = TimeSeriesCNN(input_dim, hidden_dim, output_dim) # 创建输入数据(batch_size=1) input_data = torch.randn(1, input_dim, seq_length) # 运行模型进行预测 output = model(input_data) # 打印预测结果 print(output) ``` 在这个例子中,我们定义了一个名为`TimeSeriesCNN`的自定义模型,它由一个卷积层、ReLU激活函数和全连接层组成。输入数据是一个一维的时间序列,经过卷积和池化操作后,通过全连接层输出预测结果。 我们创建了一个模型实例并将输入数据传递给模型进行预测。最后,我们打印出模型的预测结果。 需要根据具体的时间序列数据的特点和预测任务的要求来调整模型的参数和架构。例如,可以添加更多的卷积层、池化层或全连接层来增加模型的复杂度。同时,可以使用其他的损失函数和优化算法来训练模型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cyril_KI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值