代码之家  ›  专栏  ›  技术社区  ›  Roman

索引器:带“stateful=True”的LSTM

  •  0
  • Roman  · 技术社区  · 7 年前

    我尝试使用重置回调来使用LSTM网络,以实现预期的未来 预测如下:

    import numpy as np, pandas as pd, matplotlib.pyplot as plt
    from keras.models import Sequential
    from keras.layers import Dense, LSTM
    from keras.callbacks import LambdaCallback
    from sklearn.metrics import mean_squared_error
    from sklearn.preprocessing import MinMaxScaler
    from sklearn.preprocessing import StandardScaler
    
    raw = np.sin(2*np.pi*np.arange(1024)/float(1024/2)).reshape(-1,1)
    
    scaler = MinMaxScaler(feature_range=(-1, 1))
    scaled = scaler.fit_transform(raw)
    
    data = pd.DataFrame(scaled)
    
    
    window_size = 3
    data_s = data.copy()
    for i in range(window_size):
        data = pd.concat([data, data_s.shift(-(i+1))], axis = 1)   
    data.dropna(axis=0, inplace=True)
    
    ds = data.values
    
    n_rows = ds.shape[0]
    ts = int(n_rows * 0.8)
    
    train_data = ds[:ts,:]
    test_data = ds[ts:,:]
    
    train_X = train_data[:,:-1]
    train_y = train_data[:,-1]
    test_X = test_data[:,:-1]
    test_y = test_data[:,-1]
    
    print (train_X.shape)
    print (train_y.shape)
    print (test_X.shape)
    print (test_y.shape)
    
    batch_size = 3
    n_feats = 1
    
    train_X = train_X.reshape(train_X.shape[0], batch_size, n_feats)
    test_X = test_X.reshape(test_X.shape[0], batch_size, n_feats)
    print(train_X.shape, train_y.shape)
    
    regressor = Sequential()
    regressor.add(LSTM(units = 64, batch_input_shape=(1, batch_size, n_feats),
                       activation = 'sigmoid',  
                       stateful=True, return_sequences=False))
    regressor.add(Dense(units = 1))
    
    regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
    
    resetCallback = LambdaCallback(on_epoch_begin=lambda epoch,logs: regressor.reset_states())
    
    regressor.fit(train_X, train_y, batch_size=1, epochs = 1, callbacks=[resetCallback])
    
    previous_inputs = test_X                                    
    regressor.reset_states()
    
    previous_predictions = regressor.predict(previous_inputs, batch_size=1)
    previous_predictions = scaler.inverse_transform(previous_predictions).reshape(-1)
    test_y = scaler.inverse_transform(test_y.reshape(-1,1)).reshape(-1)
    
    plt.plot(test_y, color = 'blue')
    plt.plot(previous_predictions, color = 'red')
    plt.show()
    
    inputs = test_X
    future_predicitons = regressor.predict(inputs, batch_size=1)
    
    n_futures = 7
    regressor.reset_states()
    predictions = regressor.predict(previous_inputs, batch_size=1)
    print (predictions)
    
    future_predicts = []
    currentStep = predictions[:,-1:,:] 
    for i in range(n_futures):
        currentStep = regressor.predict(currentStep, batch_size=1)
        future_predicts.append(currentStep)  
    regressor.reset_states()
    
    future_predicts = np.array(future_predicts, batch_size=1).reshape(-1,1)
    future_predicts = scaler.inverse_transform(future_predicts).reshape(-1)
    
    all_predicts = np.concatenate([predicts, future_predicts])
    
    plt.plot(all_predicts, color='red')
    plt.show()
    

    但我有以下错误。我想不出如何解决预期预测的问题。

        currentStep = predictions[:,-1:,:]
    IndexError: too many indices for array
    

    PS本代码改编自 https://github.com/danmoller/TestRepo/blob/master/testing%20the%20blog%20code%20-%20train%20and%20pred.ipynb

    1 回复  |  直到 7 年前
        1
  •  1
  •   Daniel Möller    7 年前

    定义回归器时,使用 return_sequences=False

    因此,回归器返回的是2D,张量(没有步骤),而不是3D。

    所以你无法从 predictions 像您一样使用三个索引。

    可能性:

    • 具有 return\u sequences=False ,每个预测都只是最后一步。
    • 具有 return_sequences=True ,每个预测将包含步骤,即使只有一个步骤。