使用Keras,Tensorflow在多个时间序列维度上进行RNN时间序列预测 [英] RNN time series predictions with multiple time series dimension with Keras, Tensorflow

查看:878
本文介绍了使用Keras,Tensorflow在多个时间序列维度上进行RNN时间序列预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在某些时间序列集上运行RNN/LSTM网络.应该提到的是,时间序列正在分类.我有大约600个不同的时间序列,每个序列都有930个时间步长,其中包含功能.我已经将数据结构化为一个numpy 3D数组,其结构如下:

I am trying to run a RNN/LSTM network on some time series sets. It should be mentioned that the time series are being classified. I have ~600 different time series, and each of these has 930 timesteps with features in them. I have structured my data into a numpy 3D array that is structured like:

X = [666 observations/series, 930 timesteps in each observation, 15 features]
Y = [666 observations/series, 930 timesteps in each observation, 2 features]

对于训练和验证数据,我将数据拆分为70/30.因此Train_X = [466,930,15],Train_Y = [200,930,2].

For training and validation data I split the data 70/30. So Train_X = [466, 930, 15] and Train_Y = [200, 930, 2].

我的网络出现一个错误,表示它期望输入为2维,并且得到一个形状为(466,930,2)的数组.我的代码如下:

My network is getting an error that is saying that it expected the input to be 2 dimensions and that it got an array with shape (466, 930, 2). My code is as follows:

from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Bidirectional

Train_X = new_ped_data[0:466]
Test_X = new_ped_data[466:]

Train_Y = new_ped_valid_data[0:466]
Test_Y = new_ped_valid_data[466:]

model = Sequential()
model.add(Bidirectional(LSTM(20, return_sequences=True),
                        input_shape=Train_X.shape[1:]))
model.add(Bidirectional(LSTM(10)))
model.add(Dense(5))
model.compile(loss='mae', 
              optimizer='rmsprop')

model.fit(Train_X, Train_Y, epochs = 30, batch_size = 32, 
      validation_data =(Test_X, Test_Y))

我只是想让模型运行.完成后,我将调整架构并调整参数.我应该指出,分类输出之一可能不是必需的.关于如何设置体系结构的任何建议,以便如果输入时间序列,我将获得每个时间步长的网络分类值?

I am just trying to get the model running. Once I do, then I'll tweak the architecture and fit parameters. I should mention that one of the classification outputs might not be necessary. Any suggestions as to how I set up the architecture so that if a time series is fed in I will get the classification values of the network for each timestep?

Error was: ValueError: Error when checking target: expected dense_9 to
have 2 dimensions, but got array with shape (466, 930, 2)

推荐答案

您的输出也具有顺序性质.默认情况下,LSTM具有标志return_sequences=False.这使您的序列在第二个LSTM层之后被压缩为一个向量.为了改变那个尝试:

Your output has also sequential nature. LSTM by default has a flag return_sequences=False. This makes your sequence to be squashed to a vector after the second LSTM layer. In order to change that try:

model = Sequential()
model.add(Bidirectional(LSTM(20, return_sequences=True),
                    input_shape=Train_X.shape[1:]))
model.add(Bidirectional(LSTM(10, return_sequences=True)))
model.add(Dense(5))
model.compile(loss='mae', 
          optimizer='rmsprop')

这篇关于使用Keras,Tensorflow在多个时间序列维度上进行RNN时间序列预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆