我正在尝试复制和改编一些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)的形状,然后确定,我重塑了{{1 }}至(679,1):
X_train
,并且出现以下错误:
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)
预先感谢
NB。我只在{min {1}}中输入了一个示例代码。如果您在错误中输入了其他数字而不是679,那是很正常的。
答案 0 :(得分:1)
由于您正在使用batch_size = batch_size调用.fit,因此该模型期望的数据形式为(batch_size,679,1)
答案 1 :(得分:1)
我将更改2件事: -LSTM input_shape =(1,1) -LSTM return_sequences = False
使用示例代码:
重塑X_train.shape =(6,1,1)=(样本,时间步长,特征)后
y_train.shape =(6,1)
因此LSTM input_shape =(1,1)而不是(6,1)。含义input_shape =(时间步长,特征)
此外,LSTM具有return_sequences = True,因此它返回3个暗淡数据,而不是Dense层的2个暗淡数据。导致另一个错误:
检查目标时出错:预期的activation_2为3 尺寸,但数组的形状为(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)