title: Crossvalidation of Keras model with multiply inputs with scikit-learn
body: <p>I want to apply K-Fold cross-validation to my neural network model, which looks like this:</p>
  
  <pre><code>from sklearn.model_selection import StratifiedKFold 
  from numpy import *
  from keras.wrappers.scikit_learn import KerasClassifier
  from sklearn.model_selection import cross_val_score
  import numpy
  
  X = df.iloc[:,0:10165]  
  X = X.to_numpy()                      
  X = X.reshape([X.shape[0], X.shape[1],1]) 
  X_train_1 = X[:,0:10080,:]                     
  X_train_2 = X[:,10080:10165,:].reshape(921,85)      
  Y = df.iloc[:,10168:10170]
  Y = Y.to_numpy()
  
  def my_model():
  
      inputs_1 = keras.Input(shape=(10080,1))
      layer1 = Conv1D(64,14)(inputs_1)
      layer2 = layers.MaxPool1D(5)(layer1)
      layer3 = Conv1D(64, 14)(layer2)       
      layer4 = layers.GlobalMaxPooling1D()(layer3)
  
      inputs_2 = keras.Input(shape=(85,))
      layer5 = layers.concatenate([layer4, inputs_2])
      layer6 = Dense(128, activation='relu')(layer5)
      layer7 = Dense(2, activation='softmax')(layer6)
  
      model_2 = keras.models.Model(inputs = [inputs_1, inputs_2], output = [layer7])
      model_2.summary()    
      adam = keras.optimizers.Adam(lr = 0.0001)
      model_2.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['acc'])
      return model_2    
  model_2 = KerasClassifier(build_fn=my_model, epochs=150, batch_size=10, verbose=0)
  kfold = StratifiedKFold(n_splits=10, shuffle=True)
  results = cross_val_score(model_2, [X_train_1,X_train_2], Y, cv=kfold)
  print(results.mean())
  </code></pre>
  
  <p>and got this error</p>
  
  <pre><code>ValueError                                Traceback (most recent call last)
  &lt;ipython-input-44-297145425a53&gt; in &lt;module&gt;()
       42 # evaluate using 10-fold cross validation
       43 kfold = StratifiedKFold(n_splits=10, shuffle=True)
  ---&gt; 44 results = cross_val_score(model_2, [X_train_1,X_train_2], Y, cv=kfold)
       45 print(results.mean())
  
  3 frames
  /usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
      203     if len(uniques) &gt; 1:
      204         raise ValueError("Found input variables with inconsistent numbers of"
  --&gt; 205                          " samples: %r" % [int(l) for l in lengths])
      206 
      207 
  
  ValueError: Found input variables with inconsistent numbers of samples: [2, 921]
  </code></pre>
  
  <p>The shape and type of each variable are shown below:</p>
  
  <pre><code>X (921, 10165, 1) numpy.ndarray; 
  Y (921, 2) numpy.ndarray; 
  X_train_1 (921, 10080, 1) numpy.ndarray; 
  X_train_2(921, 85) numpy.ndarray
  </code></pre>
  
  <p>The model could run perfectly when I am not doing K-Fold cross-validation. ie. if I simply fit with:</p>
  
  <pre><code>model_2.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['acc']) 
  history = model_2.fit([X_train_1,X_train_2], y_train, epochs = 120, batch_size = 256, validation_split = 0.2, callbacks = [keras.callbacks.EarlyStopping(monitor='val_loss', patience=20)])
  </code></pre>
  
  <p>Hence, I am not sure what is wrong by the error message. Any help is appreciated. Thanks</p>
  
  <hr>
  
  <p>Edit:
  Here is the original model:</p>
  
  <pre><code>inputs_1 = keras.Input(shape=(10081,1))
  
  layer1 = Conv1D(64,14)(inputs_1)
  layer2 = layers.MaxPool1D(5)(layer1)
  layer3 = Conv1D(64, 14)(layer2)
  layer4 = layers.GlobalMaxPooling1D()(layer3)
  
  
  inputs_2 = keras.Input(shape=(85,))            
  layer5 = layers.concatenate([layer4, inputs_2])
  layer6 = Dense(128, activation='relu')(layer5)
  layer7 = Dense(2, activation='softmax')(layer6)
  
  
  model_2 = keras.models.Model(inputs = [inputs_1, inputs_2], output = [layer7])
  model_2.summary()
  
  
  X_train, X_test, y_train, y_test = train_test_split(df.iloc[:,0:10166], df[['Result1','Result2']].values, test_size=0.2)     
  
  X_train = X_train.to_numpy()
  X_train = X_train.reshape([X_train.shape[0], X_train.shape[1], 1])
  X_train_1 = X_train[:,0:10081,:]
  X_train_2 = X_train[:,10081:10166,:].reshape(736,85)  
  
  
  X_test = X_test.to_numpy()
  X_test = X_test.reshape([X_test.shape[0], X_test.shape[1], 1])
  X_test_1 = X_test[:,0:10081,:]
  X_test_2 = X_test[:,10081:10166,:].reshape(185,85)    
  
  
  adam = keras.optimizers.Adam(lr = 0.0005) 
  model_2.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['acc']) 
  history = model_2.fit([X_train_1,X_train_2], y_train, epochs = 120, batch_size = 256, validation_split = 0.2, callbacks = [keras.callbacks.EarlyStopping(monitor='val_loss', patience=20)])
  </code></pre>