简体   繁体   English

LSTM 中的交叉验证 - ValueError:层序号_3 的输入 0 与层不兼容

[英]Cross-Validation in LSTM - ValueError: Input 0 of layer sequential_3 is incompatible with the layer

I am trying to perform a 10-fold cross-validation on a LSTM, the code is the following:我正在尝试对 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

The idea is to perform a 10-fold cross-validation with an EarlyStopping based on the lack of improvements on the validation accuracy.这个想法是基于验证准确性缺乏改进,使用 EarlyStopping 执行 10 倍交叉验证。 The first fold runs perfectly, but everytime the second fold is supposed to begin, I receive the error:第一次折叠运行完美,但每次第二次折叠应该开始时,我都会收到错误消息:

    ValueError: Input 0 of layer sequential_3 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 68)  

A note about my input:关于我的输入的注释:

 X_train1.shape[1] = 1
 len(columns1) = 68

So for some reasons, when the second fold begins X_train1.shape[1] appears to be equal to None.所以由于某些原因,当第二次折叠开始时,X_train1.shape[1] 似乎等于无。 Has this ever happened to you?你曾经发生过这些事情吗? Thanks!谢谢!

I can see straight away some strange things in the cycle you aim to implement.我可以立即看到您打算实施的循环中的一些奇怪的事情。 i think you can safely get rid of the我认为你可以安全地摆脱

kfold.split(X_train1, y_train1)

before the for loop.for循环之前。

Then, you are not selecting the split istances but are just feeding the whole dataset X_train1 .然后,您没有选择分割距离,而只是提供整个数据集X_train1 This looks better:这看起来更好:

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError: 层序号_3 的输入 0 与层不兼容: - ValueError: Input 0 of layer sequential_3 is incompatible with the layer: LSTM:层序的输入0与层不兼容 - LSTM: Input 0 of layer sequential is incompatible with the layer ValueError:层 sequential_3 的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 已收到完整形状:(无、224、256) - ValueError: Input 0 of layer sequential_3 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 224, 256) ValueError:“顺序”层的输入 0 与该层不兼容 - ValueError: Input 0 of layer "sequential" is incompatible with the layer ValueError: 层序列 1 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_1 is incompatible with the layer TensorFlow ValueError:层顺序的输入0与层不兼容 - TensorFlow ValueError: Input 0 of layer sequential is incompatible with the layer ValueError: 层序号_2 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_2 is incompatible with the layer "ValueError: Input 0 of layer "sequential" is in compatible with the layer" 在预测中 - "ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction 在 Keras 中实现 LSTM。 ValueError:层顺序与层不兼容 - Implementing LSTM in Keras. ValueError: layer sequential is incompatible with the layer ValueError: 层序列 10 的输入 0 与层不兼容:预期 ndim=5,发现 ndim=4。 Alexnet(cnn) + LSTM - ValueError: Input 0 of layer sequential_10 is incompatible with the layer: expected ndim=5, found ndim=4. Alexnet(cnn) + LSTM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM