- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试为时间序列预测实现 LSTM 模型。下面是我的试用代码。此代码运行无误。您也可以在没有依赖的情况下尝试。
import numpy as np, pandas as pd, matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import LSTM, Dense, TimeDistributed, Bidirectional
from sklearn.metrics import mean_squared_error, accuracy_score
from scipy.stats import linregress
from sklearn.utils import shuffle
fi = 'pollution.csv'
raw = pd.read_csv(fi, delimiter=',')
raw = raw.drop('Dates', axis=1)
print (raw.shape)
scaler = MinMaxScaler(feature_range=(-1, 1))
raw = scaler.fit_transform(raw)
time_steps = 7
def create_ds(data, t_steps):
data = pd.DataFrame(data)
data_s = data.copy()
for i in range(time_steps):
data = pd.concat([data, data_s.shift(-(i+1))], axis = 1)
data.dropna(axis=0, inplace=True)
return data.values
ds = create_ds(raw, time_steps)
print (ds.shape)
n_feats = raw.shape[1]
n_obs = time_steps * n_feats
n_rows = ds.shape[0]
train_size = int(n_rows * 0.8)
train_data = ds[:train_size, :]
train_data = shuffle(train_data)
test_data = ds[train_size:, :]
x_train = train_data[:, :n_obs]
y_train = train_data[:, n_obs:]
x_test = test_data[:, :n_obs]
y_test = test_data[:, n_obs:]
x_train = x_train.reshape(1, x_train.shape[0], x_train.shape[1])
y_train = y_train.reshape(1, y_train.shape[0], y_train.shape[1])
x_test = x_test.reshape(1, x_test.shape[0], x_test.shape[1])
print (x_train.shape)
print (y_train.shape)
print (x_test.shape)
print (y_test.shape)
model = Sequential()
model.add(LSTM(64, return_sequences=True, input_shape=(None, x_train.shape[2]), stateful=True, batch_size=1))
model.add(LSTM(32, return_sequences=True, stateful=True))
model.add(LSTM(n_feats, return_sequences=True, stateful=True))
model.compile(loss='mse', optimizer='rmsprop')
model.fit(x_train, y_train, epochs=10, batch_size=1, verbose=2)
y_predict = model.predict(x_test)
y_predict = y_predict.reshape(y_predict.shape[1], y_predict.shape[2])
y_predict = scaler.inverse_transform(y_predict)
y_test = scaler.inverse_transform(y_test)
y_test = y_test[:,0]
y_predict = y_predict[:,0]
print (y_test.shape)
print (y_predict.shape)
plt.plot(y_test, label='True')
plt.plot(y_predict, label='Predict')
plt.legend()
plt.show()
然而,预测非常糟糕。如何提高预测素?你有什么改进的想法吗?
有没有通过重新设计架构和/或层来改进预测的想法?
最佳答案
如果你想在我的代码中使用模型(你传递的链接),你需要正确塑造数据:(1 个序列,total_time_steps,5 个特征)
重要提示:我不知道这是执行此操作的最佳方法或最佳模型,但该模型预测输入之前的 7 个时间步长 (time_shift=7
)
fi = 'pollution.csv'
raw = pd.read_csv(fi, delimiter=',')
raw = raw.drop('Dates', axis=1)
print("raw shape:")
print (raw.shape)
#(1789,5) - 1789 time steps / 5 features
scaler = MinMaxScaler(feature_range=(-1, 1))
raw = scaler.fit_transform(raw)
time_shift = 7 #shift is the number of steps we are predicting ahead
n_rows = raw.shape[0] #n_rows is the number of time steps of our sequence
n_feats = raw.shape[1]
train_size = int(n_rows * 0.8)
#I couldn't understand how "ds" worked, so I simply removed it because in the code below it's not necessary
#getting the train part of the sequence
train_data = raw[:train_size, :] #first train_size steps, all 5 features
test_data = raw[train_size:, :] #I'll use the beginning of the data as state adjuster
#train_data = shuffle(train_data) !!!!!! we cannot shuffle time steps!!! we lose the sequence doing this
x_train = train_data[:-time_shift, :] #the entire train data, except the last shift steps
x_test = test_data[:-time_shift,:] #the entire test data, except the last shift steps
x_predict = raw[:-time_shift,:] #the entire raw data, except the last shift steps
y_train = train_data[time_shift:, :]
y_test = test_data[time_shift:,:]
y_predict_true = raw[time_shift:,:]
x_train = x_train.reshape(1, x_train.shape[0], x_train.shape[1]) #ok shape (1,steps,5) - 1 sequence, many steps, 5 features
y_train = y_train.reshape(1, y_train.shape[0], y_train.shape[1])
x_test = x_test.reshape(1, x_test.shape[0], x_test.shape[1])
y_test = y_test.reshape(1, y_test.shape[0], y_test.shape[1])
x_predict = x_predict.reshape(1, x_predict.shape[0], x_predict.shape[1])
y_predict_true = y_predict_true.reshape(1, y_predict_true.shape[0], y_predict_true.shape[1])
print("\nx_train:")
print (x_train.shape)
print("y_train")
print (y_train.shape)
print("x_test")
print (x_test.shape)
print("y_test")
print (y_test.shape)
你的模型对于这个任务不是很强大,所以我尝试了一个更大的模型(另一方面,这个太强大了)
model = Sequential()
model.add(LSTM(64, return_sequences=True, input_shape=(None, x_train.shape[2])))
model.add(LSTM(128, return_sequences=True))
model.add(LSTM(256, return_sequences=True))
model.add(LSTM(128, return_sequences=True))
model.add(LSTM(64, return_sequences=True))
model.add(LSTM(n_feats, return_sequences=True))
model.compile(loss='mse', optimizer='adam')
请注意,我必须训练 2000 多个时期才能使模型获得良好的结果。
我添加了验证数据,以便我们可以比较训练和测试的损失。
#notice that I'm predicting from the ENTIRE sequence, including x_train
#is important for the model to adjust its states before predicting the end
model.fit(x_train, y_train, epochs=1000, batch_size=1, verbose=2, validation_data=(x_test,y_test))
重要:至于根据开头预测序列的结尾,模型看到开头以调整内部状态很重要,所以我预测整个数据 (x_predict
) ,不仅是测试数据。
y_predict_model = model.predict(x_predict)
print("\ny_predict_true:")
print (y_predict_true.shape)
print("y_predict_model: ")
print (y_predict_model.shape)
def plot(true, predicted, divider):
predict_plot = scaler.inverse_transform(predicted[0])
true_plot = scaler.inverse_transform(true[0])
predict_plot = predict_plot[:,0]
true_plot = true_plot[:,0]
plt.figure(figsize=(16,6))
plt.plot(true_plot, label='True',linewidth=5)
plt.plot(predict_plot, label='Predict',color='y')
if divider > 0:
maxVal = max(true_plot.max(),predict_plot.max())
minVal = min(true_plot.min(),predict_plot.min())
plt.plot([divider,divider],[minVal,maxVal],label='train/test limit',color='k')
plt.legend()
plt.show()
test_size = n_rows - train_size
print("test length: " + str(test_size))
plot(y_predict_true,y_predict_model,train_size)
plot(y_predict_true[:,-2*test_size:],y_predict_model[:,-2*test_size:],test_size)
请注意该模型过度拟合,这意味着它可以学习训练数据并在测试数据中得到不好的结果。
要解决这个问题,您必须通过实验尝试较小的模型,使用 dropout 层和其他技术来防止过度拟合。
另请注意,此数据很可能包含大量随机因素,这意味着模型无法从中学到任何有用的东西。当您制作较小的模型以避免过度拟合时,您可能还会发现该模型会对训练数据做出更差的预测。
找到完美的模型不是一件容易的事,这是一个悬而未决的问题,您必须进行试验。也许 LSTM 模型根本不是解决方案。也许您的数据根本无法预测,等等。对此没有明确的答案。
利用训练中的验证数据,您可以比较训练数据和测试数据的损失。
Train on 1 samples, validate on 1 samples
Epoch 1/1000
9s - loss: 0.4040 - val_loss: 0.3348
Epoch 2/1000
4s - loss: 0.3332 - val_loss: 0.2651
Epoch 3/1000
4s - loss: 0.2656 - val_loss: 0.2035
Epoch 4/1000
4s - loss: 0.2061 - val_loss: 0.1696
Epoch 5/1000
4s - loss: 0.1761 - val_loss: 0.1601
Epoch 6/1000
4s - loss: 0.1697 - val_loss: 0.1476
Epoch 7/1000
4s - loss: 0.1536 - val_loss: 0.1287
Epoch 8/1000
.....
两者应该一起下降。当测试数据停止下降,但训练数据继续改善时,您的模型开始过度拟合。
我能做的最好的(但我并没有真正尝试太多)是使用这个模型:
model = Sequential()
model.add(LSTM(64, return_sequences=True, input_shape=(None, x_train.shape[2])))
model.add(LSTM(128, return_sequences=True))
model.add(LSTM(128, return_sequences=True))
model.add(LSTM(64, return_sequences=True))
model.add(LSTM(n_feats, return_sequences=True))
model.compile(loss='mse', optimizer='adam')
当损失约为:
loss: 0.0389 - val_loss: 0.0437
在这一点之后,验证损失开始上升(因此超过这一点的训练完全没有用)
结果:
这表明该模型可以学习的是非常全面的行为,例如具有较高值的区域。
但是高频太随机或者模型不够好...
关于python - 极差的预测 : LSTM time-series,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50054419/
我无法准确理解 LSTM 单元的范围——它如何映射到网络层。来自格雷夫斯 (2014): 在我看来,在单层网络中,layer = lstm 单元。这实际上如何在多层 rnn 中工作? 三层RNN LS
这是代码 model = Sequential() model.add(LSTM(256, input_shape=(None, 1), return_sequences=True)) model.a
为什么我们需要在pytorch中初始化LSTM中的隐藏状态h0。由于 h0 无论如何都会被计算并被覆盖?是不是很像 整合一个一 = 0 一个= 4 即使我们不做a=0,也应该没问题.. 最佳答案 重点
我正在尝试使用 LSTM 在 Deeplearning4j 中进行一些简单的时间序列预测,但我很难让它工作。 我有一个简单的文本文件,其中包含如下所示的数字列表,并希望网络学习预测下一个数字。 有没有
在大量阅读和绘制图表之后,我想我已经提出了一个模型,我可以将其用作更多测试我需要调整哪些参数和功能的基础。但是,我对如何实现以下测试用例感到困惑(所有数字都比最终模型小几个数量级,但我想从小处着手):
我正在尝试实现“Livelinet:用于预测教育视频中的活力的多模式深度循环神经网络”中的结构。 为了简单说明,我将 10 秒音频剪辑分成 10 个 1 秒音频剪辑,并从该 1 秒音频剪辑中获取频谱图
我正在 Tensorflow 中制作 LSTM 神经网络。 输入张量大小为 92。 import tensorflow as tf from tensorflow.contrib import rnn
我正在尝试 keras IMDB 数据的示例,数据形状是这样的: x_train shape: (25000, 80) 我只是把keras例子的原始代码改成了这样的代码: model = Sequen
我需要了解如何使用 torch.nn 的不同组件正确准备批量训练的输入。模块。具体来说,我希望为 seq2seq 模型创建一个编码器-解码器网络。 假设我有一个包含这三层的模块,按顺序: nn.Emb
我很难概念化 Keras 中有状态 LSTM 和无状态 LSTM 之间的区别。我的理解是,在每个批处理结束时,在无状态情况下“网络状态被重置”,而对于有状态情况,网络状态会为每个批处理保留,然后必须在
nn.Embedding() 是学习 LSTM 所必需的吗? 我在 PyTorch 中使用 LSTM 来预测 NER - 此处是类似任务的示例 - https://pytorch.org/tutori
我正在尝试找出适合我想要拟合的模型的正确语法。这是一个时间序列预测问题,我想在将时间序列输入 LSTM 之前使用一些密集层来改进时间序列的表示。 这是我正在使用的虚拟系列: import pandas
我在理解堆叠式 LSTM 网络中各层的输入-输出流时遇到了一些困难。假设我已经创建了一个如下所示的堆叠式 LSTM 网络: # parameters time_steps = 10 features
LSTM 类中的默认非线性激活函数是 tanh。我希望在我的项目中使用 ReLU。浏览文档和其他资源,我无法找到一种简单的方法来做到这一点。我能找到的唯一方法是定义我自己的自定义 LSTMCell,但
在 PyTorch 中,有一个 LSTM 模块,除了输入序列、隐藏状态和单元状态之外,它还接受 num_layers 参数,该参数指定我们的 LSTM 有多少层。 然而,还有另一个模块 LSTMCel
没什么好说的作为介绍:我想在 TensorFlow 中将 LSTM 堆叠在另一个 LSTM 上,但一直被错误阻止,我不太明白,更不用说单独解决了。 代码如下: def RNN(_X, _istate,
有人可以解释一下吗?我知道双向 LSTM 具有前向和反向传递,但是与单向 LSTM 相比,它有什么优势? 它们各自更适合什么? 最佳答案 LSTM 的核心是使用隐藏状态保留已经通过它的输入信息。 单向
我想构建一个带有特殊词嵌入的 LSTM,但我对它的工作原理有一些疑问。 您可能知道,一些 LSTM 对字符进行操作,因此它是字符输入,字符输出。我想做同样的事情,通过对单词的抽象来学习使用嵌套的 LS
我编写了一个LSTM回归模型。它是最后一个LSTM层的BATCH_SIZE=1和RETURN_Sequence=True的模型。我还设置了VERIFICATION_DATA和耐心进行培训。但似乎存在一
给定一个训练有素的 LSTM 模型,我想对单个时间步执行推理,即以下示例中的 seq_length = 1。在每个时间步之后,需要为下一个“批处理”记住内部 LSTM(内存和隐藏)状态。在推理的最开始
我是一名优秀的程序员,十分优秀!