首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IndexError:带有"stateful=True“的LSTM

IndexError:带有"stateful=True“的LSTM
EN

Stack Overflow用户
提问于 2018-04-15 22:25:52
回答 1查看 201关注 0票数 0

我尝试使用LSTM网络,为预期的未来预测使用重置回调,如下所示:

代码语言:javascript
复制
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()

但我得到了以下错误。我想不出如何解决它的预期预测。

代码语言:javascript
复制
    currentStep = predictions[:,-1:,:]
IndexError: too many indices for array

这个代码是从https://github.com/danmoller/TestRepo/blob/master/testing%20the%20blog%20code%20-%20train%20and%20pred.ipynb改编而来的

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-16 12:08:46

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

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

因此,您不能像以前那样使用三个索引从predictions中获取元素。

可能性:

  • 使用return_sequences=False,每个预测都只是最后一步。
  • 使用return_sequences=True,每个预测都将包含步骤,即使只有一个步骤。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49847379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档