1

我正在尝试使用带有 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')

第一个情节 第二个情节

4

0 回答 0