首页 > 解决方案 > 为多元预测创建 LSTM 模型时遇到问题

问题描述

数据天气数据集

提供的数据以小时为单位。 在此处输入图像描述

到目前为止,我只在数据框中选择了三个参数。

输入:

df.info()

输出:

    <class 'pandas.core.frame.DataFrame'>
    DatetimeIndex: 78888 entries, 2006-01-01 00:00:00 to 2014-12-31 23:00:00
    Freq: H
    Data columns (total 3 columns):
     #   Column           Non-Null Count  Dtype  
    ---  ------           --------------  -----  
     0   Temperature (C)  78888 non-null  float64
     1   Humidity         78888 non-null  float64
     2   Visibility (km)  78888 non-null  float64
    dtypes: float64(3)
    memory usage: 2.4 MB

参数图之一如下。

    df['Temperature (C)'].plot(figsize=(30,8))

以小时为单位的温度

首先,我想将 Dataframe 从每小时数据更改为每月数据,这样训练起来会更容易。

    df = df.resample('MS').mean()

像这样,

每月数据框

几个月的温度变化是,

df['Temperature (C)'].plot(figsize=(30,8))

几个月的温度

测试和训练:

    rows_per_month=1
    test_months = 18 #number of months we want to predict in the future.
    
    test_indices = test_months*rows_per_month
    test_indices
    
    # train and test split:
    train = df_final.iloc[:-test_indices]
    
    # Choose the variable/parameter you want to predict
    test = df_final.iloc[-test_indices:]

MinMaxScaler我使用from sci-kit learn对数据进行了缩放

发电机参数:

    length =  12*rows_per_month #Length of output sequences (in number of timesteps)
    batch_size = 1 #Number of timeseries sample in batch
    generator = tf.keras.preprocessing.sequence.TimeseriesGenerator(scaled_train,scaled_train,length=length,batch_size=batch_size)

模型df(以月为单位):

    # define model
    model = Sequential()
    
    model.add(tf.keras.layers.LSTM(50, input_shape=(length,scaled_train.shape[1]),return_sequences=True))
    model.add(tf.keras.layers.LSTM(50))
    
    
    #NOTE: Do not specify the activation function for LSTM layers, this is because it will not run on GPU.
    model.add(Dense(scaled_train.shape[1]))
    
    model.compile(optimizer='adam', loss='mse')

该模型训练了 24 个 epoch,并且在预测以下三个参数方面做得相当好。

月模型的损失

模型预测(何时df为每月):

月度模型预测 这是一个相当不错的预测。

问题是当我增加数据的密度并将其设置为每天而不是每月时。

我使用了原始数据并执行了以下操作:

    df = df.resample('D').mean()

日内气温变化:

    df['Temperature (C)'].plot(figsize=(30,8))

天温度

测试和训练:

这里唯一改变的是rows_per_month = 30,其余一切都一样。

发电机参数:

也和上面一样。

型号df(天):

相同的模型(就像我df在几个月内使用的那样)

    model = Sequential()
    
    model.add(tf.keras.layers.LSTM(50, input_shape=(length,scaled_train.shape[1]),return_sequences=True))
    model.add(tf.keras.layers.LSTM(50))
    # model.add(tf.keras.layers.LSTM(50)) #add this layer if df is in 'days'
    
    #NOTE: Do not specify the activation function for LSTM layers, this is because it will not run on GPU.
    model.add(Dense(scaled_train.shape[1]))
    
    model.compile(optimizer='adam', loss='mse')

该模型训练了 24 个 epoch,但模型没有正确预测。

损失:

每日模型损失

模型预测(df每天何时)

每日模型预测

我尝试再添加一层50 个 LSTM单元。

    model = Sequential()
    
    model.add(tf.keras.layers.LSTM(50, input_shape=(length,scaled_train.shape[1]),return_sequences=True))
    model.add(tf.keras.layers.LSTM(50, return_sequences=True))
    model.add(tf.keras.layers.LSTM(50)) #add this layer if df is in 'days'
    
    #NOTE: Do not specify the activation function for LSTM layers, this is because it will not run on GPU.
    model.add(Dense(scaled_train.shape[1]))
    
    model.compile(optimizer='adam', loss='mse')

但结果相似。

我还尝试将模型训练更多的时期(〜100),但没有得到结果。

我想我错过了一个关键点,数据的周期性保持不变,只是点的密度发生了变化,为什么这会影响模型的准确性?

标签: pythontime-serieslstmtensorflow2.0forecasting

解决方案


推荐阅读