IndexError: LSTM with "stateful=True"

IndexError: LSTM with "stateful=True"

我尝试使用 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

当您定义回归量时,您使用了 return_sequences=False

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

所以你不能像你那样使用三个索引从 predictions 中获取元素。

可能性:

  • 使用return_sequences=False,每次预测都只是最后一步。
  • 使用 return_sequences=True,每个预测都将包含步骤,即使只有一个步骤。