Cross-Validation in LSTM - ValueError: Input 0 of layer sequential_3 is incompatible with the layer
Cross-Validation in LSTM - ValueError: Input 0 of layer sequential_3 is incompatible with the layer
我正在尝试对 LSTM 执行 10 折交叉验证,代码如下:
# Initialising the RNN
regressor = Sequential()
# Adding the first LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 350, return_sequences = True, input_shape = (X_train1.shape[1], len(columns1))))
regressor.add(Dropout(0.5))
# Adding a second LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 350, return_sequences = True))
regressor.add(Dropout(0.5))
# Adding a third LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 350, return_sequences = True))
regressor.add(Dropout(0.5))
# Adding a fourth LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 350))
regressor.add(Dropout(0.5))
# Adding the output layer
regressor.add(Dense(units = 1))
# Compiling the RNN
regressor.compile(optimizer = 'rmsprop', loss = 'mean_squared_error',metrics=['accuracy'])
# RNN TRAINING
kfold = KFold(n_splits=10, shuffle=True, random_state=0)
val_accuracies = []
test_accuracies = []
i = 1
df_metrics = pd.DataFrame()
kfold.split(X_train1, y_train1)
#for train_index, test_index in kfold.split(disease_df):
for train_index, test_index in kfold.split(X_train1, y_train1):
#callback = EarlyStopping(monitor='val_accuracy', patience=10,restore_best_weights=True)
# Fitting the RNN to the Training set (RUN/TRAIN the model)
history = regressor.fit(X_train1, y_train1, epochs = 100, batch_size = 25, validation_split = 0.1, callbacks=[EarlyStopping('val_accuracy', mode='max',patience=5)])
i+=1
想法是基于验证准确性缺乏改进,使用 EarlyStopping 执行 10 折交叉验证。第一次折叠运行完美,但每次应该开始第二次折叠时,我都会收到错误消息:
ValueError: Input 0 of layer sequential_3 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 68)
关于我的输入的注释:
X_train1.shape[1] = 1
len(columns1) = 68
因此,出于某些原因,当第二次折叠开始时,X_train1.shape[1] 似乎等于 None。你曾经发生过这些事情吗?
谢谢!
我可以立即看到您打算实施的周期中的一些奇怪的事情。
我认为你可以安全地摆脱
kfold.split(X_train1, y_train1)
在 for 循环之前。
那么,您不是在选择分割距离,而是在提供整个数据集 X_train1。这看起来更好:
from sklearn.model_selection import KFold
kf = KFold(n_splits=2)
for train_index, test_index in kf.split(X_train1):
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X_train1[train_index], X_train1[test_index]
y_train, y_test = y_train1[train_index], y_train1[test_index]
我正在尝试对 LSTM 执行 10 折交叉验证,代码如下:
# Initialising the RNN
regressor = Sequential()
# Adding the first LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 350, return_sequences = True, input_shape = (X_train1.shape[1], len(columns1))))
regressor.add(Dropout(0.5))
# Adding a second LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 350, return_sequences = True))
regressor.add(Dropout(0.5))
# Adding a third LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 350, return_sequences = True))
regressor.add(Dropout(0.5))
# Adding a fourth LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 350))
regressor.add(Dropout(0.5))
# Adding the output layer
regressor.add(Dense(units = 1))
# Compiling the RNN
regressor.compile(optimizer = 'rmsprop', loss = 'mean_squared_error',metrics=['accuracy'])
# RNN TRAINING
kfold = KFold(n_splits=10, shuffle=True, random_state=0)
val_accuracies = []
test_accuracies = []
i = 1
df_metrics = pd.DataFrame()
kfold.split(X_train1, y_train1)
#for train_index, test_index in kfold.split(disease_df):
for train_index, test_index in kfold.split(X_train1, y_train1):
#callback = EarlyStopping(monitor='val_accuracy', patience=10,restore_best_weights=True)
# Fitting the RNN to the Training set (RUN/TRAIN the model)
history = regressor.fit(X_train1, y_train1, epochs = 100, batch_size = 25, validation_split = 0.1, callbacks=[EarlyStopping('val_accuracy', mode='max',patience=5)])
i+=1
想法是基于验证准确性缺乏改进,使用 EarlyStopping 执行 10 折交叉验证。第一次折叠运行完美,但每次应该开始第二次折叠时,我都会收到错误消息:
ValueError: Input 0 of layer sequential_3 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 68)
关于我的输入的注释:
X_train1.shape[1] = 1
len(columns1) = 68
因此,出于某些原因,当第二次折叠开始时,X_train1.shape[1] 似乎等于 None。你曾经发生过这些事情吗? 谢谢!
我可以立即看到您打算实施的周期中的一些奇怪的事情。 我认为你可以安全地摆脱
kfold.split(X_train1, y_train1)
在 for 循环之前。
那么,您不是在选择分割距离,而是在提供整个数据集 X_train1。这看起来更好:
from sklearn.model_selection import KFold
kf = KFold(n_splits=2)
for train_index, test_index in kf.split(X_train1):
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X_train1[train_index], X_train1[test_index]
y_train, y_test = y_train1[train_index], y_train1[test_index]