Python LSTM中的交叉验证-ValueError:层顺序_3的输入0与层不兼容
我试图在LSTM上执行10倍交叉验证,代码如下:Python LSTM中的交叉验证-ValueError:层顺序_3的输入0与层不兼容,python,tensorflow,keras,lstm,Python,Tensorflow,Keras,Lstm,我试图在LSTM上执行10倍交叉验证,代码如下: # Initialising the RNN regressor = Sequential() # Adding the first LSTM layer and some Dropout regularisation regressor.add(
# 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
我们的想法是,在验证准确性缺乏改进的基础上,进行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]似乎等于零。你有过这样的经历吗?
谢谢 我可以马上看到您打算实施的周期中的一些奇怪的事情。 我想你可以安全地摆脱这个问题
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]
谢谢,所以基本上第二个折叠没有什么需要处理的,因为它不是一个折叠,但是我在第一个折叠中传递了整个数据集。我明白了,现在,谢谢你的支持,这件事让我感到困惑是的,事实上你没有从sklearn应用k折叠。希望有帮助!