使用Keras时间分布层进行预测

时间:2017-03-14 08:36:24

标签: time-series keras panel-data regularized

我正在尝试使用带有LSTM和时间分配层的Keras一次模拟两个时间序列。我正在考虑的数据是回归设置,有两个余弦波,加上一些高斯噪声。然而,该模型在预测样本时有很多麻烦。这是预期的,还是我的层数太少了?

在下面的输出中,蓝色圆圈是训练数据,绿色方块是训练数据的预测值,红色方块是测试数据,蓝色三角形是测试数据的预测值。很明显,该模型过度拟合了训练数据,并没有找到数据中的基础结构。我也试过使用正则化,没有任何成功。

# Import models
from keras.wrappers.scikit_learn import KerasClassifier as KC
from keras.layers import Dense, LSTM, TimeDistributed
from keras.models import Sequential
from keras.regularizers import l1
import numpy as np
import matplotlib.pyplot as plt

# Generate some data
t = 100
n = 2
xtrain = np.zeros((n,t,1))
xtest = np.zeros((n,t,1))
ytrain = np.zeros((n,t,1))
ytest = np.zeros((n,t,1))+np.random.normal(0,1,(n,t,1))

change_point = 3.14*2

for i in range(xtrain.shape[0]):
    xtrain[i,:,0] = np.random.uniform(0,change_point,t)
    xtest[i,:,0] = np.random.uniform(change_point,2*change_point,t)

ytrain[0,:,0] = np.cos(xtrain[0,:,0])+np.random.normal(0,0.2,t)
ytrain[1,:,0] = np.cos(xtrain[1,:,0])+np.random.normal(0,0.2,t)
ytest[0,:,0] = np.cos(xtest[0,:,0])+np.random.normal(0,0.2,t)
ytest[1,:,0] = np.cos(xtest[1,:,0])+np.random.normal(0,0.2,t)

m = Sequential()
m.add(LSTM(90,input_shape=(t,1), return_sequences = True))
m.add(LSTM(90, return_sequences = True))
m.add(LSTM(90, return_sequences = True))
#m.add(LSTM(1, return_sequences = True))
m.add(TimeDistributed(Dense(1)))


m.compile(loss = 'mean_squared_error', optimizer='rmsprop')

m.fit(xtrain,ytrain,nb_epoch=400, batch_size=2)
ypred = m.predict(xtest)
ytrain_pred = m.predict(xtrain)

ps = 0
plt.scatter(xtrain[ps,:,0],ytrain[ps,:,0])
plt.scatter(xtest[ps,:,0],ytest[ps,:,0], marker='s', c='r')
plt.scatter(xtrain[ps,:,0],ytrain_pred[ps,:,0],marker = 's',c='g')
plt.scatter(xtest[ps,:,0],ypred[ps,:,0], marker='^', c='b')

First plot Second plot

0 个答案:

没有答案