ValueError: Error when checking input: expected lstm_10_input to have shape (679, 1) but got array with shape (1, 1)
ValueError: Error when checking input: expected lstm_10_input to have shape (679, 1) but got array with shape (1, 1)
我正在尝试重现和改编一些 LSTM 代码。
在模型的拟合部分model.fit
,出现了标题中提到的错误。
这是错误的完整回溯:
Traceback (most recent call last):
File "<ipython-input-61-fc0772e5553b>", line 4, in <module>
show_inner=False)])
File "C:\Users\fassi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\training.py", line 952, in fit
batch_size=batch_size)
File "C:\Users\fassi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\training.py", line 751, in _standardize_user_data
exception_prefix='input')
File "C:\Users\fassi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\training_utils.py", line 138, in standardize_input_data
str(data_shape))
ValueError: Error when checking input: expected lstm_10_input to have shape (679, 1) but got array with shape (1, 1)
这是命令行:
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=epochs,
validation_split=0.05, verbose=verbose, callbacks=
[lstm_progress(), TQDMCallback(outer_description="%-16s"%"Training LSTM",
show_inner=False)])
我检查了 X_train
: (679,1,1) 和 y_train
:(679,1) 的形状,然后,为了确定,我重塑了 X_train
到 (679,1):
X_train = X_train.reshape(-1,1)
并且出现以下错误:
ValueError: Error when checking input: expected lstm_10_input to have 3 dimensions, but got array with shape (679, 1)
我也试过这样做:X_train = X_train.reshape(1,-1,1)
为了得到这样的形状 (1, 679,1)
但我得到了这个错误:
ValueError: Error when checking target: expected activation_2 to have 3 dimensions, but got array with shape (679, 1)
最小可重现代码(以备不时之需):
O = [0.701733664614, 0.699495411782, 0.572129320819, 0.613315597684, 0.58079660603, 0.596638918579, 0.48453382119]
Ab = [datetime.datetime(2018, 12, 11, 14, 0), datetime.datetime(2018, 12, 21, 10, 0), datetime.datetime(2018, 12, 21, 14, 0), datetime.datetime(2019, 1, 1, 10, 0), datetime.datetime(2019, 1, 1, 14, 0), datetime.datetime(2019, 1, 11, 10, 0), datetime.datetime(2019, 1, 11, 14, 0)]
data = DataFrame(np.column_stack([O,Ab]),
columns=['ndvi', 'datetime'])
decade_end = len(data)-1
decade_begin = int(decade_end - len(data)*90/100)
data2 = data.loc[decade_begin:decade_end]
def load_data(time_series, train_perc, normalise_window,
decade_begin, decade_end, points=None):
data2 = time_series.loc[decade_begin:decade_end]
scaler = None
train = data2['ndvi'].values[0:-10].reshape(-1,1)
shift_steps = 1
train_targets = data2['ndvi'].shift(-shift_steps).values[0:-10].reshape(-1,1)
train_reshaped = train[:, 0].reshape(len(train), 1, 1)
x_train, y_train = train_reshaped, train_targets
x_test = data2['ndvi'].values[:-10].reshape(-1,1)
y_test = data2['ndvi'].values[-10:].reshape(-1,1)
return [x_train, y_train, scaler, x_test, y_test]
def build(layers):
model = Sequential()
model.add(LSTM(
input_shape=(int(layers[1]), int(layers[0])),
output_dim=int(layers[1]),
return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(
output_dim=layers[2]))
model.add(Activation("linear"))
model.compile(loss="mse", optimizer="rmsprop")
return model
X_train, y_train, scaler = load_data(data, 1, False,
decade_begin, decade_end, points=None)[0:3]
seq_len_in = len(X_train)
batch_size = 40
nb_hidden = int((2/3)*len(data))
model = build([1, seq_len_in, nb_hidden])
X_train, y_train, scaler = load_data(data, 1, False,
decade_begin, decade_end, points=None)[0:3]
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=epochs,
validation_split=0.05, verbose=verbose, callbacks=
[lstm_progress(), TQDMCallback(outer_description="%-16s"%"Training LSTM",
show_inner=False)])
提前致谢
注意。我在最小代码中只放了一个 data
的样本。如果您在错误中得到其他数字而不是 679,那是很正常的。
由于您使用 batch_size=batch_size
调用 .fit,因此模型需要 (batch_size,679,1) 形式的数据
我会改变两件事:
- LSTM input_shape=(1,1)
- LSTM return_sequences = False
使用您的示例代码:
重塑 X_train.shape = (6,1,1) = (samples, timesteps, features)
y_train.shape = (6,1)
所以 LSTM input_shape =(1,1) 而不是 (6,1)。含义 input_shape=(时间步长,特征)
此外,LSTM 具有 return_sequences=True,因此它 returns 3 dim 数据而不是 Dense 层的 2 dim。这会导致另一个错误:
Error when checking target: expected activation_2 to have 3
dimensions, but got array with shape (6, 1)
意味着 return_sequences=True 在这种情况下输出 y_train 应该有 3 dim 而不是 2: (6,1)
如果你设置 return_sequences=False 它有效。
使用您的示例代码:
import pandas as pd
import keras
from keras.models import Sequential
from keras.layers import LSTM, Dropout, Dense, Activation
from time import daylight
import datetime
import numpy as np
O = [0.701733664614, 0.699495411782, 0.572129320819, 0.613315597684, 0.58079660603, 0.596638918579, 0.48453382119]
Ab = [datetime.datetime(2018, 12, 11, 14, 0), datetime.datetime(2018, 12, 21, 10, 0), datetime.datetime(2018, 12, 21, 14, 0), datetime.datetime(2019, 1, 1, 10, 0), datetime.datetime(2019, 1, 1, 14, 0), datetime.datetime(2019, 1, 11, 10, 0), datetime.datetime(2019, 1, 11, 14, 0)]
data = pd.DataFrame(np.column_stack([O,Ab]),
columns=['ndvi', 'datetime'])
decade_end = len(data)-1
decade_begin = int(decade_end - len(data)*90/100)
data2 = data.loc[decade_begin:decade_end]
def load_data(time_series, train_perc, normalise_window,
decade_begin, decade_end, points=None):
data2 = time_series.loc[decade_begin:decade_end]
print("data2: ",data2)
scaler = None
train = data2['ndvi'].values[0:-1]#.reshape(-1,1)
print("train: ",train)
shift_steps = 1
train_targets = data2['ndvi'].shift(-shift_steps).values[0:-1].reshape(-1,1)
train_reshaped = train.reshape(len(train), 1, 1)
x_train, y_train = train_reshaped, train_targets
x_test = data2['ndvi'].values[:-1].reshape(-1,1)
y_test = data2['ndvi'].values[-1:].reshape(-1,1)
return [x_train, y_train, scaler, x_test, y_test]
def build(layers):
model = Sequential()
print("layers: ", layers)
model.add(LSTM(
input_shape=(1, 1),
output_dim=int(layers[0]),
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(
output_dim=layers[0]))
model.add(Activation("linear"))
model.compile(loss="mse", optimizer="rmsprop")
return model
X_train, y_train, scaler = load_data(data, 1, False,
decade_begin, decade_end, points=None)[0:3]
print(X_train)
seq_len_in = len(X_train)
batch_size = 1
nb_hidden = int((2/3)*len(data))
X_train.shape
y_train.shape
model = build([1, seq_len_in, nb_hidden])
model.summary()
X_train, y_train, scaler = load_data(data, 2, False,
decade_begin, decade_end, points=None)[0:3]
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=10,
validation_split=0.05, verbose=1)
我正在尝试重现和改编一些 LSTM 代码。
在模型的拟合部分model.fit
,出现了标题中提到的错误。
这是错误的完整回溯:
Traceback (most recent call last):
File "<ipython-input-61-fc0772e5553b>", line 4, in <module>
show_inner=False)])
File "C:\Users\fassi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\training.py", line 952, in fit
batch_size=batch_size)
File "C:\Users\fassi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\training.py", line 751, in _standardize_user_data
exception_prefix='input')
File "C:\Users\fassi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\training_utils.py", line 138, in standardize_input_data
str(data_shape))
ValueError: Error when checking input: expected lstm_10_input to have shape (679, 1) but got array with shape (1, 1)
这是命令行:
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=epochs,
validation_split=0.05, verbose=verbose, callbacks=
[lstm_progress(), TQDMCallback(outer_description="%-16s"%"Training LSTM",
show_inner=False)])
我检查了 X_train
: (679,1,1) 和 y_train
:(679,1) 的形状,然后,为了确定,我重塑了 X_train
到 (679,1):
X_train = X_train.reshape(-1,1)
并且出现以下错误:
ValueError: Error when checking input: expected lstm_10_input to have 3 dimensions, but got array with shape (679, 1)
我也试过这样做:X_train = X_train.reshape(1,-1,1)
为了得到这样的形状 (1, 679,1)
但我得到了这个错误:
ValueError: Error when checking target: expected activation_2 to have 3 dimensions, but got array with shape (679, 1)
最小可重现代码(以备不时之需):
O = [0.701733664614, 0.699495411782, 0.572129320819, 0.613315597684, 0.58079660603, 0.596638918579, 0.48453382119]
Ab = [datetime.datetime(2018, 12, 11, 14, 0), datetime.datetime(2018, 12, 21, 10, 0), datetime.datetime(2018, 12, 21, 14, 0), datetime.datetime(2019, 1, 1, 10, 0), datetime.datetime(2019, 1, 1, 14, 0), datetime.datetime(2019, 1, 11, 10, 0), datetime.datetime(2019, 1, 11, 14, 0)]
data = DataFrame(np.column_stack([O,Ab]),
columns=['ndvi', 'datetime'])
decade_end = len(data)-1
decade_begin = int(decade_end - len(data)*90/100)
data2 = data.loc[decade_begin:decade_end]
def load_data(time_series, train_perc, normalise_window,
decade_begin, decade_end, points=None):
data2 = time_series.loc[decade_begin:decade_end]
scaler = None
train = data2['ndvi'].values[0:-10].reshape(-1,1)
shift_steps = 1
train_targets = data2['ndvi'].shift(-shift_steps).values[0:-10].reshape(-1,1)
train_reshaped = train[:, 0].reshape(len(train), 1, 1)
x_train, y_train = train_reshaped, train_targets
x_test = data2['ndvi'].values[:-10].reshape(-1,1)
y_test = data2['ndvi'].values[-10:].reshape(-1,1)
return [x_train, y_train, scaler, x_test, y_test]
def build(layers):
model = Sequential()
model.add(LSTM(
input_shape=(int(layers[1]), int(layers[0])),
output_dim=int(layers[1]),
return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(
output_dim=layers[2]))
model.add(Activation("linear"))
model.compile(loss="mse", optimizer="rmsprop")
return model
X_train, y_train, scaler = load_data(data, 1, False,
decade_begin, decade_end, points=None)[0:3]
seq_len_in = len(X_train)
batch_size = 40
nb_hidden = int((2/3)*len(data))
model = build([1, seq_len_in, nb_hidden])
X_train, y_train, scaler = load_data(data, 1, False,
decade_begin, decade_end, points=None)[0:3]
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=epochs,
validation_split=0.05, verbose=verbose, callbacks=
[lstm_progress(), TQDMCallback(outer_description="%-16s"%"Training LSTM",
show_inner=False)])
提前致谢
注意。我在最小代码中只放了一个 data
的样本。如果您在错误中得到其他数字而不是 679,那是很正常的。
由于您使用 batch_size=batch_size
调用 .fit,因此模型需要 (batch_size,679,1) 形式的数据我会改变两件事: - LSTM input_shape=(1,1) - LSTM return_sequences = False
使用您的示例代码:
重塑 X_train.shape = (6,1,1) = (samples, timesteps, features)
y_train.shape = (6,1)
所以 LSTM input_shape =(1,1) 而不是 (6,1)。含义 input_shape=(时间步长,特征)
此外,LSTM 具有 return_sequences=True,因此它 returns 3 dim 数据而不是 Dense 层的 2 dim。这会导致另一个错误:
Error when checking target: expected activation_2 to have 3 dimensions, but got array with shape (6, 1)
意味着 return_sequences=True 在这种情况下输出 y_train 应该有 3 dim 而不是 2: (6,1) 如果你设置 return_sequences=False 它有效。
使用您的示例代码:
import pandas as pd
import keras
from keras.models import Sequential
from keras.layers import LSTM, Dropout, Dense, Activation
from time import daylight
import datetime
import numpy as np
O = [0.701733664614, 0.699495411782, 0.572129320819, 0.613315597684, 0.58079660603, 0.596638918579, 0.48453382119]
Ab = [datetime.datetime(2018, 12, 11, 14, 0), datetime.datetime(2018, 12, 21, 10, 0), datetime.datetime(2018, 12, 21, 14, 0), datetime.datetime(2019, 1, 1, 10, 0), datetime.datetime(2019, 1, 1, 14, 0), datetime.datetime(2019, 1, 11, 10, 0), datetime.datetime(2019, 1, 11, 14, 0)]
data = pd.DataFrame(np.column_stack([O,Ab]),
columns=['ndvi', 'datetime'])
decade_end = len(data)-1
decade_begin = int(decade_end - len(data)*90/100)
data2 = data.loc[decade_begin:decade_end]
def load_data(time_series, train_perc, normalise_window,
decade_begin, decade_end, points=None):
data2 = time_series.loc[decade_begin:decade_end]
print("data2: ",data2)
scaler = None
train = data2['ndvi'].values[0:-1]#.reshape(-1,1)
print("train: ",train)
shift_steps = 1
train_targets = data2['ndvi'].shift(-shift_steps).values[0:-1].reshape(-1,1)
train_reshaped = train.reshape(len(train), 1, 1)
x_train, y_train = train_reshaped, train_targets
x_test = data2['ndvi'].values[:-1].reshape(-1,1)
y_test = data2['ndvi'].values[-1:].reshape(-1,1)
return [x_train, y_train, scaler, x_test, y_test]
def build(layers):
model = Sequential()
print("layers: ", layers)
model.add(LSTM(
input_shape=(1, 1),
output_dim=int(layers[0]),
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(
output_dim=layers[0]))
model.add(Activation("linear"))
model.compile(loss="mse", optimizer="rmsprop")
return model
X_train, y_train, scaler = load_data(data, 1, False,
decade_begin, decade_end, points=None)[0:3]
print(X_train)
seq_len_in = len(X_train)
batch_size = 1
nb_hidden = int((2/3)*len(data))
X_train.shape
y_train.shape
model = build([1, seq_len_in, nb_hidden])
model.summary()
X_train, y_train, scaler = load_data(data, 2, False,
decade_begin, decade_end, points=None)[0:3]
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=10,
validation_split=0.05, verbose=1)