LSTM - LSTM - 未来值预测错误

时间:2017-06-20 13:21:01

标签: time future prediction lstm

经过一些研究,我能够使用下面的LSTM代码预测未来的价值。我还在我正在使用的github链接中附加了Dmd1ahr.csv文件。

https://github.com/ukeshchawal/hello-world/blob/master/Dmd1ahr.csv

正如大家所见,90个数据点是训练集,第91个到第100个是未来值预测。

enter image description here

然而,我仍然有一些问题是:

  1. 为了预测这些值,我最初需要超过一百个数据集(这里,我已经采用了500个数据集),这不是我的主要目标。有没有一种方法可以给出500个数据集,它会预测其余的10或20个样本数据点?如果是的话,请你给我一个示例代码,你可以从下面的Dmd1ahr.csv文件中获取500个数据点,并根据这500个点预测一些未来的值(比如501到520)?

    < / LI>
  2. 与博客中的人相比,预测有点远(明确表示参数调整 - 我尝试更改时期,LSTM图层,激活,优化器)。我可以采取哪些其他参数调整来使其更加健壮?

  3. 提前谢谢你。

    import numpy as np
     import matplotlib.pyplot as plt
     import pandas
    
    # By twaking the architecture it could be made more robust
     np.random.seed(7)
     numOfSamples = 500
    
    lengthTrain = 90
     lengthValidation = 100
     look_back = 1 # Can be set higher, in my experiments it made performance worse though
     transientTime = 90 # Time to "burn in" time series
    
    series = pandas.read_csv('Dmd1ahr.csv')
    
    def generateTrainData(series, i, look_back):
         return series[i:look_back+i+1]
    
    trainX = np.stack([generateTrainData(series, i, look_back) for i in range(lengthTrain)])
     testX = np.stack([generateTrainData(series, lengthTrain + i, look_back) for i in range(lengthValidation)])
    
    trainX = trainX.reshape((lengthTrain,look_back+1,1))
     testX = testX.reshape((lengthValidation, look_back + 1, 1))
    
    trainY = trainX[:,1:,:]
     trainX = trainX[:,:-1,:]
    
    testY = testX[:,1:,:]
     testX = testX[:,:-1,:]
    
    ############### Build Model ###############
    
    import keras
     from keras.models import Model
     from keras import layers
     from keras import regularizers
    
    inputs = layers.Input(batch_shape=(1,look_back,1), name="main_input")
     inputsAux = layers.Input(batch_shape=(1,look_back,1), name="aux_input")
    
    # this layer makes the actual prediction, i.e. decides if and how much it goes up or down
     x = layers.recurrent.LSTM(300,return_sequences=True, stateful=True)(inputs)
     x = layers.recurrent.LSTM(200,return_sequences=True, stateful=True)(inputs)
     x = layers.recurrent.LSTM(100,return_sequences=True, stateful=True)(inputs)
     x = layers.recurrent.LSTM(50,return_sequences=True, stateful=True)(inputs)
     x = layers.wrappers.TimeDistributed(layers.Dense(1, activation="linear",
                                                      kernel_regularizer=regularizers.l2(0.005),
                                                      activity_regularizer=regularizers.l1(0.005)))(x)
    
    # auxillary input, the current input will be feed directly to the output
     # this way the prediction from the step before will be used as a "base", and the Network just have to
     # learn if it goes a little up or down
     auxX = layers.wrappers.TimeDistributed(layers.Dense(1,
                                                         kernel_initializer=keras.initializers.Constant(value=1),
                                                         bias_initializer='zeros',
                                                         input_shape=(1,1), activation="linear", trainable=False
                                                         ))(inputsAux)
    
    outputs = layers.add([x, auxX], name="main_output")
     model = Model(inputs=[inputs, inputsAux], outputs=outputs)
     model.compile(optimizer='adam',
                   loss='mean_squared_error',
                   metrics=['mean_squared_error'])
     #model.summary()
     #model.fit({"main_input": trainX, "aux_input": trainX[look_back-1,look_back,:]},{"main_output": trainY}, epochs=4, batch_size=1, shuffle=False)
     model.fit({"main_input": trainX, "aux_input": trainX[:,look_back-1,:].reshape(lengthTrain,1,1)},{"main_output": trainY}, epochs=100, batch_size=1, shuffle=False)
    
    ############### make predictions ###############
    
    burnedInPredictions = np.zeros(transientTime)
     testPredictions = np.zeros(len(testX))
     # burn series in, here use first transitionTime number of samples from test data
     for i in range(transientTime):
         prediction = model.predict([np.array(testX[i, :, 0].reshape(1, look_back, 1)), np.array(testX[i, look_back - 1, 0].reshape(1, 1, 1))])
         testPredictions[i] = prediction[0,0,0]
    
    burnedInPredictions[:] = testPredictions[:transientTime]
     # prediction, now dont use any previous data whatsoever anymore, network just has to run on its own output
     for i in range(transientTime, len(testX)):
         prediction = model.predict([prediction, prediction])
         testPredictions[i] = prediction[0,0,0]
    
    # for plotting reasons
     testPredictions[:np.size(burnedInPredictions)-1] = np.nan
    
    ############### plot results ###############
     #import matplotlib.pyplot as plt
     plt.plot(testX[:, 0, 0])
     plt.show()
     plt.plot(burnedInPredictions, label = "training")
     plt.plot(testPredictions, label = "prediction")
     plt.legend()
     plt.show()
    

0 个答案:

没有答案