如何搭建LSTM网络?
How to set up the LSTM network?
我正在学习如何设置 RNN-LSTM 网络进行预测。我用单变量创建了数据集。
x y
1 2.5
2 6
3 8.6
4 11.2
5 13.8
6 16.4
...
和y(t) = 2.5x(t) + x(t-1) -0.9*x(t-2)
的关系。我正在尝试设置 RNN-LSTM 来学习模式,但它发生了我的程序错误。我的程序如下:
df= pd.read_excel('dataset.xlsx')
def split_dataset(data):
# split into standard weeks
train, test = data[:-328], data[-328:-6]
# restructure into windows of weekly data
train = np.array(np.split(train, len(train)/1))
test = np.array(np.split(test, len(test)/1))
return train, test
verbose, epochs, batch_size = 0, 20, 16
train, test = split_dataset(df.values)
train_x, train_y = train[:,:,0], train[:,:,1]
model = Sequential()
model.add(LSTM(200, return_sequences=True, input_shape = train_x.shape))
model.compile(loss='mse', optimizer='adam')
发生了 ValueError
:
ValueError: Error when checking input: expected lstm_35_input to have 3 dimensions, but got array with shape (8766, 1)
哪位有经验的DS或者pythoner可以教我如何设置网络?
谢谢
对于基于 LSTM 的 RNN,输入应为 3 维(批次、时间、data_point)。我假设您的 x
变量的索引是它的时间。在这种情况下,您必须将您的输入转换为一些 window 的批次,比如 window 为 3,那么您的输入是:
批次 # 输入 目标
0 x[0:3] y[3]
1 x[1:4] y[4]
2 x[2:5] y[5]
注意:您的 y 从 t=3 开始,因为您使用最后 3 个时间步来预测下一个第 4 个值。如果你的 y 已经按照你所说的从最后三个时间步计算出来,那么 y 应该从 0 索引开始,即在第 0 批次你有 y[0] 作为目标
UPDATE 根据以下评论
如果你想有多个序列,那么你可以将它建模为一个序列到序列的问题,并且将是一个 N to M
映射,你需要五个 x 值来预测三个 y:
批次 # 输入 目标
0 x[0:5] y[3:6]
1 x[1:6] y[4:7]
2 x[2:7] y[5:8]
目前,我已经创建了数据 window,它看起来适用于我提到的案例。
下面是我的代码:
df= pd.read_excel('dataset.xlsx')
# split a univariate dataset into train/test sets
def split_dataset(data):
train, test = data[:-328], data[-328:-6]
return train, test
train, test = split_dataset(df.values)
# scale train and test data to [-1, 1]
def scale(train, test):
# fit scaler
scaler = MinMaxScaler(feature_range=(0,1))
scaler = scaler.fit(train)
# transform train
#train = train.reshape(train.shape[0], train.shape[1])
train_scaled = scaler.transform(train)
# transform test
#test = test.reshape(test.shape[0], test.shape[1])
test_scaled = scaler.transform(test)
return scaler, train_scaled, test_scaled
scaler, train_scaled, test_scaled = scale(train, test)
def to_supervised(train, n_input, n_out=7):
# flatten data
data = train
X, y = list(), list()
in_start = 0
# step over the entire history one time step at a time
for _ in range(len(data)):
# define the end of the input sequence
in_end = in_start + n_input
out_end = in_end + n_out
# ensure we have enough data for this instance
if out_end <= len(data):
x_input = data[in_start:in_end, 0]
x_input = x_input.reshape((len(x_input), 1))
X.append(x_input)
y.append(data[in_end:out_end, 0])
# move along one time step
in_start += 1
return np.array(X), np.array(y)
train_x, train_y = to_supervised(train_scaled, n_input = 3, n_out = 1)
test_x, test_y = to_supervised(test_scaled, n_input = 3, n_out = 1)
verbose, epochs, batch_size = 0, 20, 16
n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1]
model = Sequential()
model.add(LSTM(200, return_sequences= False, input_shape = (train_x.shape[1],train_x.shape[2])))
model.add(Dense(1))
model.compile(loss = 'mse', optimizer = 'adam')
history = model.fit(train_x, train_y, epochs=epochs, verbose=verbose, validation_data = (test_x, test_y))
但是,我对此还有其他问题:
Q1:LSTM中的单位是什么意思? [model.add(LSTM(units
, ...))]
(我已经为模型尝试了不同的单位,随着单位的增加它会更准确。)
Q2:我应该设置多少层?
Q3:如何进行多步预测?例如基于 (x(t),x(t-1)) 来预测 y(t), y(t+1)
我试图在 to_supervised
函数中设置 n_out = 2
,但是当我应用相同的方法时,它返回了错误
train_x, train_y = to_supervised(train_scaled, n_input = 3, n_out = 2)
test_x, test_y = to_supervised(test_scaled, n_input = 3, n_out = 2)
verbose, epochs, batch_size = 0, 20, 16
n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1]
model = Sequential()
model.add(LSTM(200, return_sequences= False, input_shape = (train_x.shape[1],train_x.shape[2])))
model.add(Dense(1))
model.compile(loss = 'mse', optimizer = 'adam')
history = model.fit(train_x, train_y, epochs=epochs, verbose=verbose, validation_data = (test_x, test_y))
ValueError: Error when checking target: expected dense_27 to have shape (1,) but got array with shape (2,)
Q3(续):我应该在模型设置中添加或更改什么?
Q3(续):return_sequences
是什么?我应该什么时候设置True
?
我正在学习如何设置 RNN-LSTM 网络进行预测。我用单变量创建了数据集。
x y
1 2.5
2 6
3 8.6
4 11.2
5 13.8
6 16.4
...
和y(t) = 2.5x(t) + x(t-1) -0.9*x(t-2)
的关系。我正在尝试设置 RNN-LSTM 来学习模式,但它发生了我的程序错误。我的程序如下:
df= pd.read_excel('dataset.xlsx')
def split_dataset(data):
# split into standard weeks
train, test = data[:-328], data[-328:-6]
# restructure into windows of weekly data
train = np.array(np.split(train, len(train)/1))
test = np.array(np.split(test, len(test)/1))
return train, test
verbose, epochs, batch_size = 0, 20, 16
train, test = split_dataset(df.values)
train_x, train_y = train[:,:,0], train[:,:,1]
model = Sequential()
model.add(LSTM(200, return_sequences=True, input_shape = train_x.shape))
model.compile(loss='mse', optimizer='adam')
发生了 ValueError
:
ValueError: Error when checking input: expected lstm_35_input to have 3 dimensions, but got array with shape (8766, 1)
哪位有经验的DS或者pythoner可以教我如何设置网络?
谢谢
对于基于 LSTM 的 RNN,输入应为 3 维(批次、时间、data_point)。我假设您的 x
变量的索引是它的时间。在这种情况下,您必须将您的输入转换为一些 window 的批次,比如 window 为 3,那么您的输入是:
批次 # 输入 目标
0 x[0:3] y[3]
1 x[1:4] y[4]
2 x[2:5] y[5]
注意:您的 y 从 t=3 开始,因为您使用最后 3 个时间步来预测下一个第 4 个值。如果你的 y 已经按照你所说的从最后三个时间步计算出来,那么 y 应该从 0 索引开始,即在第 0 批次你有 y[0] 作为目标
UPDATE 根据以下评论
如果你想有多个序列,那么你可以将它建模为一个序列到序列的问题,并且将是一个 N to M
映射,你需要五个 x 值来预测三个 y:
批次 # 输入 目标
0 x[0:5] y[3:6]
1 x[1:6] y[4:7]
2 x[2:7] y[5:8]
目前,我已经创建了数据 window,它看起来适用于我提到的案例。
下面是我的代码:
df= pd.read_excel('dataset.xlsx')
# split a univariate dataset into train/test sets
def split_dataset(data):
train, test = data[:-328], data[-328:-6]
return train, test
train, test = split_dataset(df.values)
# scale train and test data to [-1, 1]
def scale(train, test):
# fit scaler
scaler = MinMaxScaler(feature_range=(0,1))
scaler = scaler.fit(train)
# transform train
#train = train.reshape(train.shape[0], train.shape[1])
train_scaled = scaler.transform(train)
# transform test
#test = test.reshape(test.shape[0], test.shape[1])
test_scaled = scaler.transform(test)
return scaler, train_scaled, test_scaled
scaler, train_scaled, test_scaled = scale(train, test)
def to_supervised(train, n_input, n_out=7):
# flatten data
data = train
X, y = list(), list()
in_start = 0
# step over the entire history one time step at a time
for _ in range(len(data)):
# define the end of the input sequence
in_end = in_start + n_input
out_end = in_end + n_out
# ensure we have enough data for this instance
if out_end <= len(data):
x_input = data[in_start:in_end, 0]
x_input = x_input.reshape((len(x_input), 1))
X.append(x_input)
y.append(data[in_end:out_end, 0])
# move along one time step
in_start += 1
return np.array(X), np.array(y)
train_x, train_y = to_supervised(train_scaled, n_input = 3, n_out = 1)
test_x, test_y = to_supervised(test_scaled, n_input = 3, n_out = 1)
verbose, epochs, batch_size = 0, 20, 16
n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1]
model = Sequential()
model.add(LSTM(200, return_sequences= False, input_shape = (train_x.shape[1],train_x.shape[2])))
model.add(Dense(1))
model.compile(loss = 'mse', optimizer = 'adam')
history = model.fit(train_x, train_y, epochs=epochs, verbose=verbose, validation_data = (test_x, test_y))
但是,我对此还有其他问题:
Q1:LSTM中的单位是什么意思? [model.add(LSTM(units
, ...))]
(我已经为模型尝试了不同的单位,随着单位的增加它会更准确。)
Q2:我应该设置多少层?
Q3:如何进行多步预测?例如基于 (x(t),x(t-1)) 来预测 y(t), y(t+1)
我试图在 to_supervised
函数中设置 n_out = 2
,但是当我应用相同的方法时,它返回了错误
train_x, train_y = to_supervised(train_scaled, n_input = 3, n_out = 2)
test_x, test_y = to_supervised(test_scaled, n_input = 3, n_out = 2)
verbose, epochs, batch_size = 0, 20, 16
n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1]
model = Sequential()
model.add(LSTM(200, return_sequences= False, input_shape = (train_x.shape[1],train_x.shape[2])))
model.add(Dense(1))
model.compile(loss = 'mse', optimizer = 'adam')
history = model.fit(train_x, train_y, epochs=epochs, verbose=verbose, validation_data = (test_x, test_y))
ValueError: Error when checking target: expected dense_27 to have shape (1,) but got array with shape (2,)
Q3(续):我应该在模型设置中添加或更改什么?
Q3(续):return_sequences
是什么?我应该什么时候设置True
?