LSTM-一段时间后预测相同的常数值

时间:2018-11-03 07:20:37

标签: python tensorflow keras lstm

我有一个要在未来30年内预测的变量。不幸的是我没有很多样品。

df = pd.DataFrame({'FISCAL_YEAR': [1979,1980,1981,1982,1983,  1984,  
1985,  1986,  1987,  1988,  1989,  1990,  1991,  1992,  1993,  1994,  
1995,  1996,
  1997,  1998,  1999,  2000,  2001,  2002,  2003,  2004,  2005,  2006,  
2007,  2008,  2009,  2010,  2011,  2012,  2013,  2014,  2015,  2016,  
2017,  2018,  2019],
 'VALS': [1341.9,  1966.95,  2085.75,  2087.1000000000004,  2760.75,  
3461.4,  3156.3,  3061.8,  2309.8500000000004,  2320.65,  2535.3,  
2964.6000000000004,  2949.75,  2339.55,
  2327.4,  2571.75,  2299.05,  1560.6000000000001,  1370.25,  1301.4,  
1215.0,  5691.6,  6281.55,  6529.950000000001,  17666.100000000002,  
14467.95,  15205.050000000001,  14717.7,  14426.1,  12946.5,
  13000.5,  12761.550000000001,  13076.1,  13444.650000000001,  
13444.650000000001,  13321.800000000001,  13536.45,  13331.25,  
12630.6,  12741.300000000001,  12658.95]})

这是我的代码:

def build_model(n_neurons,dropout,s):
    lstm = Sequential()
    if cudnn:
        lstm.add(CuDNNLSTM(n_neurons))
        n_epochs = 200
    else:
        lstm.add(Masking(mask_value=-1,input_shape=(s[1],s[2])))
        lstm.add(LSTM(n_neurons,dropout=dropout))
        n_epochs = 500

    lstm.add(Dense(1))
    #lstm.add(Activation('softmax'))
    lstm.compile(loss='mean_squared_error',optimizer='adam')
    return lstm

def create_df(dfin,fwd,lstmws):
    ''' Input Normalization '''
    idx = dfin.FISCAL_YEAR.values[fwd:]
    dfx = dfin[[varn]].copy()
    dfy = dfin[[varn]].copy()

    # LSTM window - use last lstmws values
    for i in range(0,lstmws-1):
        dfx = dfx.join(dfin[[varn]].shift(-i-1),how='left',rsuffix='{:02d}'.format(i+1))

    dfx = (dfx-vmnx).divide(vmxx-vmnx)
    dfx.fillna(-1,inplace=True) # replace missing values with -1

    dfy = (dfy-vmnx).divide(vmxx-vmnx)
    dfy.fillna(-1,inplace=True) # replace missing values with -1
    return dfx,dfy,idx

def forecast(dfin,dfx,lstm,idx,gapyr=1):
    ''' Model Forecast '''
    xhat = dfx.values
    xhat = xhat.reshape(xhat.shape[0],lstmws,int(xhat.shape[1]/lstmws))
    yhat = lstm.predict(xhat)

    yhat = yhat*(vmxx-vmnx)+vmnx
    dfout = pd.DataFrame(list(zip(idx+gapyr,yhat.reshape(1,-1)[0])),columns=['FISCAL_YEAR',varn])
    dfout = pd.concat([dfin.head(1),dfout],axis=0).reset_index(drop=True)
    #append last prediction to X and use for prediction
    dfin = pd.concat([dfin,dfout.tail(1)],axis=0).reset_index(drop=True)
    return dfin

def lstm_training(dfin,lstmws,fwd,num_years,batchsize=4,cudnn=False,n_neurons=47,dropout=0.05,retrain=False):
    ''' LSTM Parameter '''
    seed(2018)
    set_random_seed(2018)
    gapyr = 1 # Forecast +1 Year

    dfx,dfy,idx = create_df(dfin,fwd,lstmws)

    X,y = dfx.iloc[fwd:-gapyr].values,dfy[fwd+gapyr:].values[:,0]
    X,y = X.reshape(X.shape[0],lstmws,int(X.shape[1]/lstmws)),y.reshape(len(y), 1)

    lstm = build_model(n_neurons,dropout,X.shape)
    ''' LSTM Training Start '''
    if batchsize == 1:
        history_i = 
lstm.fit(X,y,epochs=25,batch_size=batchsize,verbose=0,shuffle=False)
    else:
        history_i = lstm.fit(X,y,epochs=n_epochs,batch_size=batchsize,verbose=0,shuffle=False)

    dfin = forecast(dfin,dfx,lstm,idx)


    lstm.reset_states()
    if not retrain:
        for fwd in range(1,num_years):

            dfx,dfy,idx = create_df(dfin,fwd,lstmws)

            dfin = forecast(dfin,dfx,lstm,idx)

            lstm.reset_states()

    del dfy,X,y,lstm
    gc.collect();
clear_session();
return dfin,history_i

varn = "VALS"
#LSTM-window
lstmws = 10
vmnx,vmxx = df[varn].astype(float).min(),df[varn].astype(float).max()
dfin,history_i = lstm_training(dfin,lstmws,0,2051-2018)

在我的第一个版本中,每次添加新的预测后,我都会对模型进行重新训练,并且预测从未收敛。但是因为每次进行新的观察后都要进行培训,所以我不得不改变。

我的结果:

dfin.VALS.values
array([  1341.9       ,   1966.95      ,   2085.75      ,   2087.1       ,
     2760.75      ,   3461.4       ,   3156.3       ,   3061.8       ,
     2309.85      ,   2320.65      ,   2535.3       ,   2964.6       ,
     2949.75      ,   2339.55      ,   2327.4       ,   2571.75      ,
     2299.05      ,   1560.6       ,   1370.25      ,   1301.4       ,
     1215.        ,   5691.6       ,   6281.55      ,   6529.95      ,
    17666.1       ,  14467.95      ,  15205.05      ,  14717.7       ,
    14426.1       ,  12946.5       ,  13000.5       ,  12761.55      ,
    13076.1       ,  13444.65      ,  13444.65      ,  13321.8       ,
    13536.45      ,  13331.25      ,  12630.6       ,  12741.3       ,
    12658.95      ,  10345.97167969,  12192.12792969,  13074.4296875 ,
    13264.40917969,  12956.1796875 ,  12354.1953125 ,  11659.03125   ,
    11044.06933594,  10643.19921875,  10552.52246094,  10552.52246094,
    10552.52246094,  10552.52246094,  10552.52246094,  10552.52246094,
    10552.52246094,  10552.52246094,  10552.52246094,  10552.52246094,
    10552.52246094,  10552.52246094,  10552.52246094,  10552.52246094,
    10552.52246094,  10552.52246094,  10552.52246094,  10552.52246094,
    10552.52246094,  10552.52246094,  10552.52246094,  10552.52246094,
    10552.52246094,  10552.52246094])

如何避免在过去20多年中获得相同的预测?

编辑:

我在前面添加了更多的随机数据,以查看是否是由于样本量少所致,但是一段时间后预测仍保持不变。

df0 = pd.DataFrame([range(1900,1979),list(np.random.rand(1979-1900)*(vmxx-vmnx)+vmnx)],index=["FISCAL_YEAR","VALS"]).T
df = pd.concat([df0,df])
df["FISCAL_YEAR"] = df["FISCAL_YEAR"].astype(int)
df.index = range(1900,2020)

我观察到的一件奇怪的事是,预测在10年后是相同的,即窗口大小,但是如果我将lstmws增加到20,则预测在20年后会收敛:

lstmws = 20

结果:

{'FISCAL_YEAR': [2020,  2021,  2022,  2023,  2024,  2025,  2026,  027,  028,  2029,  2030,  2031,  2032,  2033,  2034,  2035,  2036,  2037,  2038,  039,  2040,  2041,  2042,  2043,  2044,  2045,  2046,  2047,  2048,  2049,  050,  2051,  2052],
 'VALS': [11183.32421875,  12388.28125,  13151.013671875,  12543.6796875,  2590.0888671875,  12002.583984375,  11822.8857421875,  11479.6572265625,  1423.1279296875,  11444.5751953125,  11506.60546875,  11563.3173828125,  1595.0029296875,  11599.8955078125,  11586.8037109375,  11571.337890625,  1574.541015625,  11620.7900390625,  11734.2431640625,  11934.216796875,  1934.216796875,  11934.216796875,  11934.216796875,  11934.216796875,  1934.216796875,  11934.216796875,  11934.216796875,  11934.216796875,  1934.216796875,  11934.216796875,  11934.216796875,  11934.216796875,  1934.216796875]}

2 个答案:

答案 0 :(得分:1)

我可以看到您的预测步骤仅使用默认批次大小?尝试将批次大小设置为您用于培训步骤的大小。看看是否有帮助。

yhat = lstm.predict(xhat, batch_size=batchsize)

答案 1 :(得分:1)

根据我对LSTM的经验(我一直在生成诸如this之类的舞蹈序列),我发现有两点特别有助于防止模型停滞和预测相同的输出。

首先,使用混合密度网络而不是L2损耗(如您所愿)会很有帮助。详细信息请阅读Christopher Bishop的paper on MDN layers,但是基本上L2损失试图将某些输入的误差项的条件平均值预测为y。如果对于一个值x,您有多个可能的输出y0,y1,y2,每个输出都有一定的概率(就像许多复杂系统一样),则需要考虑MDN层和对数似然损失。 Here是我正在使用的Keras实现。

现在更仔细地阅读您的情况,这可能对您的情况没有帮助,因为您似乎正在预测一个时间序列,根据定义,每个x映射到一个y。

接下来,我发现在我要预测的LSTM n序列值之前输入序列值很有帮助。 n越大,我发现的结果越好(尽管训练速度较慢)。我读过的许多论文都使用1024个先前的序列值来预测下一个序列值。

您没有很多观测值,但是您可以尝试将之前的8个观测值输入以预测下一个观测值。