Python Keras LSTM 时间序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32514704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:42:47  来源:igfitidea点击:

Keras LSTM Time Series

machine-learningpython

提问by Ryan Allen

I have a problem and at this point I'm completely lost as to how to solve it. I'm using Keras with an LSTM layer to project a time series. I'm trying to use the previous 10 data points to predict the 11th.

我有一个问题,此时我完全不知道如何解决它。我正在使用带有 LSTM 层的 Keras 来投影时间序列。我正在尝试使用前 10 个数据点来预测第 11 个。

Here's the code:

这是代码:

from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM

def _load_data(data):
"""
data should be pd.DataFrame()
"""
n_prev = 10
docX, docY = [], []
for i in range(len(data)-n_prev):
    docX.append(data.iloc[i:i+n_prev].as_matrix())
    docY.append(data.iloc[i+n_prev].as_matrix())
if not docX:
    pass
else:
    alsX = np.array(docX)
    alsY = np.array(docY)
    return alsX, alsY

X, y = _load_data(df_test)

X_train = X[:25]
X_test = X[25:]

y_train = y[:25]
y_test = y[25:]

in_out_neurons = 2
hidden_neurons = 300
model = Sequential()
model.add(LSTM(in_out_neurons, hidden_neurons, return_sequences=False))
model.add(Dense(hidden_neurons, in_out_neurons))
model.add(Activation("linear"))
model.compile(loss="mean_squared_error", optimizer="rmsprop")
model.fit(X_train, y_train, nb_epoch=10, validation_split=0.05)

predicted = model.predict(X_test)

So I'm taking the input data (a two column dataframe), creating X which is an n by 10 by 2 array, and y which is an n by 2 array which is one step ahead of the last row in each array of X (labeling the data with the point directly ahead of it.

所以我正在获取输入数据(一个两列数据框),创建 X 是一个 n x 10 x 2 数组,y 是一个 n x 2 数组,它比每个 X 数组中的最后一行提前一步(用直接在数据前面的点标记数据。

predicted is returning

预测正在返回

[[ 7.56940445,  5.61719704],
[ 7.57328357,  5.62709032],
[ 7.56728049,  5.61216415],
[ 7.55060187,  5.60573629],
[ 7.56717342,  5.61548522],
[ 7.55866942,  5.59696181],
[ 7.57325984,  5.63150951]]

but I should be getting

但我应该得到

[[ 73,  48],
[ 74,  42],
[ 91,  51],
[102,  64],
[109,  63],
[ 93,  65],
[ 92,  58]]

The original data set only has 42 rows, so I'm wondering if there just isn't enough there to work with? Or am I missing a key step in the modeling process maybe? I've seen some examples using Embedding layers etc, is that something I should be looking at?

原始数据集只有 42 行,所以我想知道是否没有足够的数据可以使用?或者我是否错过了建模过程中的关键步骤?我看过一些使用嵌入层等的例子,这是我应该看的东西吗?

Thanks in advance for any help!

在此先感谢您的帮助!

回答by ted

Hey Ryan!

嘿瑞恩!

I know it's late but I just came across your question hope it's not too late or that you still find some knowledge here.

我知道已经晚了,但我刚刚遇到了您的问题,希望还为时不晚,或者您仍然可以在这里找到一些知识。

Firstof all, Stackoverflow may not be the best place for this kind of question. First reason to that is you have a conceptual question that is not this site's purpose. Moreover your code runs so it's not even a matter of general programming. Have a look at stats.

首先,Stackoverflow 可能不是此类问题的最佳场所。第一个原因是您有一个概念性问题,这不是本网站的目的。此外,您的代码可以运行,因此它甚至不是一般编程的问题。看看stats

Secondfrom what I see there is no conceptual error. You're using everything necessary that is:

其次,我所看到的没有概念错误。您正在使用所有必要的东西:

  • lstm with propper dimensions
  • return_sequences=falsejust before your Denselayer
  • linear activation for your output
  • msecost/loss/objective function
  • 具有适当尺寸的 lstm
  • return_sequences=false就在你的Dense图层之前
  • 输出的线性激活
  • mse成本/损失/目标函数

ThirdI however find it extremely unlikely that your network learns anything with so few pieces of data. You have to understand that you have less data than parameters here! For the great majority of supervised learning algorithm, the first thing you need is not a good model, it's good data. You can not learn from so few examples, especially not with a complex model such as LSTM networks.

第三,我发现您的网络极不可能用这么少的数据来学习任何东西。你必须明白,你这里的数据比参数少!对于绝大多数监督学习算法,首先需要的不是好的模型,而是好的数据。你不能从这么少的例子中学习,尤其是不能学习像 LSTM 网络这样的复杂模型。

FourthIt seems like your target data is made of relatively high values. First step of pre-processing here could be to standardize the data : center it around zero - that is translate your data by its mean - and rescale by ists standard deviation. This really helps learning!

第四,您的目标数据似乎由相对较高的值组成。这里预处理的第一步可能是对数据进行标准化:以零为中心 - 即按平均值转换您的数据 - 并按 IST 标准偏差重新缩放。这真的有助于学习!

FifthIn general here are a few things you should look into to improve learning and reduce overfitting :

第五一般而言,您应该考虑以下几点来改进学习并减少过度拟合:

Last but NOT leastI suggest you look at this tutorial on Github, especially the recurrent tutorial for time series with keras.

最后但并非最不重要的是,我建议您在 Github 上查看本教程,尤其是使用 keras 进行时间序列循环教程

PS: Daniel Hnyk updated his post;)

PS:Daniel Hnyk 更新了他的帖子;)