GRU时间序列数据分类预测

目的:通过一段时间的数据,预测后面一段时间的类别,比如输入数据是1-50步的变量,预测的是50-60步的类别。

输入变量的数目:15

预测的类别数:0,1,2,3,4,10 (1类数目最多,数据不均衡)

GRU模型参数解释:

参考链接:[Pytorch系列-54]:循环神经网络 – torch.nn.GRU()参数详解_文火冰糖的硅基工坊的博客-CSDN博客_nn.gru参数

GRU时间序列数据分类预测

input_size: 输入序列的变量的数目。

hidden_size: 隐藏层的特征的数目。

num_layers: GRU层的数目。

bias:是否需要偏置,默认是True(需要)。

batch_first: 用于确定batch size是否需要放到输入输出数据形状的最前面。

若为True, 则输入、输出的tensor的格式为(batch, seq_len, feature)

若为False,则输入、输出的tensor的格式为(seq_len,batch,feature)

默认是False。

为什么需要该参数呢?

在CNN网络和全连接网络,batch通常位于输入数据形状的最前面。

而对于具有时间信息的序列化数据,通常需要把seq放在最前面,需要把序列数据串行地输入网络中。(那我的模型不能设置为True???)

seq_len: 输入序列的长度。在我的情形下可以为50。

搭建GRU网络:

参考链接: pytorch使用torch.nn.Sequential快速搭建神经网络 – pytorch中文网

self.gru = nn.GRU(self.input_size, self.hidden_size, self.num_layers, batch_first=True, dropout=self.dropout)
self.fc = nn.Sequential(nn.Linear(self.hidden_size, self.output_size), nn.Sigmoid())
self.gru = torch.nn.GRU(self.input_size, self.hidden_size, self.num_layers, batch_first=True)
self.fc1 = torch.nn.Linear(self.hidden_size, 4)
self.fc2 = torch.nn.Linear(self.hidden_size, 4)
self.fc3 = torch.nn.Linear(self.hidden_size, 4)
self.fc4 = torch.nn.Linear(self.hidden_size, 4)
self.fc5 = torch.nn.Linear(self.hidden_size, 4)
self.softmax = torch.nn.Softmax(dim=1)

nn.Sequential:是一个Sequential容器,模块将按照构造函数中传递的顺序添加到模块中。另外,也可以传入一个有序模块。使用 torch.nn.Sequential会自动加入激励函数。

torch.nn.Sequential与torch.nn.Module区别与选择

  • 使用 torch.nn.Module,我们可以根据自己的需求改变传播过程,如 RNN
  • 如果你需要快速构建或者不需要过多的过程,直接使用 torch.nn.Sequential即可

nn.Linear(input_dim , output_dim)

torch.nn.Softmax(dim=1)

参考链接: torch.nn.Softmax_CtrlZ1的博客-CSDN博客_torch.nn.softmax

tensor([[0.3458, 0.0596, 0.5147],
[0.3774, 0.7503, 0.3705],
[0.2768, 0.1901, 0.1148]])

dim=0表示对于第一个维度的对应下标之和是1, 即0.3458+0.3774+0.2768=1、0.0596+0.7503+0.1901=1。

tensor([[0.3381, 0.1048, 0.5572],
[0.1766, 0.6315, 0.1919],
[0.3711, 0.4586, 0.1704]])

dim=1表示对于第二维度而言,对应下标之和为1,0.3381+0.1048+0.5572=1, 0.1766+0.6315+0.1919=1,即所有列的对应下标之和为1。

一些报错记录:

1. 计算交叉熵损失使用的output必须是softmax输出的概率而不是argmax之后得到的类别。

RuntimeError: Expected floating point type for target with class probabilities, got Long

语义分割损失函数系列(1):交叉熵损失函数_spectrelwf的博客-CSDN博客_语义分割交叉熵

2. 加载生成训练数据集的时候报错。

Ran out of input

python报错Ran out of input_在上树的路上的博客-CSDN博客

因为生成的数据集太大了,要减少数据集。(The actually error is OverflowError: cannot serialize a bytes object larger than 4 GiB. You have to reduce the size of the input.)

3. 输入张量和隐藏张量不在一个device上。

h_0 = torch.zeros(self.num_layers, batch_size, self.hidden_size)
Input and hidden tensors are not at the same device, found input tensor at cuda:0 and hidden tensor at cpu
h_0 = torch.zeros(self.num_layers, batch_size, self.hidden_size).to(Train.device)
Input and hidden tensors are not at the same device, found input tensor at cpu and
and hidden tensor at cuda:0

解决方法:

output, _ = self.gru(input_seq.to(Train.device), h_0)

(input_seq后面加上to(Train.device))

4. 预测和真实标签长度不一致。

报错:Found input variables with inconsistent numbers of samples

y_true. shape
y_predict.shape

查看真实值和预测值的形状。

Original: https://blog.csdn.net/weixin_39915444/article/details/127242809
Author: 儒雅的晴天
Title: GRU时间序列数据分类预测

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/715827/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球