Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crossvalidation of Keras model with multiply inputs with scikit-learn

I want to apply K-Fold cross-validation to my neural network model, which looks like this:

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())

and got this error

ValueError                                Traceback (most recent call last)
<ipython-input-44-297145425a53> in <module>()
     42 # evaluate using 10-fold cross validation
     43 kfold = StratifiedKFold(n_splits=10, shuffle=True)
---> 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) > 1:
    204         raise ValueError("Found input variables with inconsistent numbers of"
--> 205                          " samples: %r" % [int(l) for l in lengths])
    206 
    207 

ValueError: Found input variables with inconsistent numbers of samples: [2, 921]

The shape and type of each variable are shown below:

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

The model could run perfectly when I am not doing K-Fold cross-validation. ie. if I simply fit with:

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)])

Hence, I am not sure what is wrong by the error message. Any help is appreciated. Thanks


Edit: Here is the original model:

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)])
like image 294
nilsinelabore Avatar asked Oct 15 '22 08:10

nilsinelabore


1 Answers

Scikit's cross_val_score complains, because it detects, that your X and y have different length. That's because you pass:

[X_train_1,X_train_2]

where X has actually 2 false "sample" on axis 0, as it is a list with two members. In contrary y has 921 sample on axis 0.

Edit:

After some research, I found, that sklearn's split() method doesn't support neither multi-input data nor one-hot encoded labels.

Solution:

So as a workaround you can make an own crossvalidation with sklearn as follows:

Import and define everything we need first:

from sklearn.model_selection import StratifiedKFold
import numpy as np
import keras
from keras import layers
from keras.layers import Conv1D, Dense
from keras.utils.np_utils import to_categorical

# This is just for dummy data ##################################
X_train_1 = np.random.randint(0, 10000, (921, 10080, 1))
X_train_2 = np.random.randint(0, 10000, (921, 85))
Y_kat = np.random.randint(0, 2, (921))
Y = to_categorical(Y_kat, num_classes=2)
# This is just for dummy data ##################################


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    

And here we go with the actual solution:

# We need convert one_hot encoded labels to categorical labels for skf
Y_kat = np.argmax(Y, axis=1)

n_folds = 5
skf = StratifiedKFold(n_splits=n_folds, shuffle=True)
skf = skf.split(X_train_1, Y_kat)

cv_score = []

for i, (train, test) in enumerate(skf):
    # currently keras doesn't have like model.reset(), so the easiest way
    # recompiling our model in every step of the loop see below more
    # create model
    model_2 = my_model()

    print("Running Fold", i+1, "/", n_folds)
    model_2.fit([X_train_1[train], X_train_2[train]], Y[train], epochs=150, batch_size=10)
    result = model_2.evaluate([X_train_1[test], X_train_2[test]], Y[test])
    # if we want only the accuracy metric
    cv_score.append(result[1])
    # we have to clear previous model to reset weights
    # currently keras doesn't have like model.reset()
    keras.backend.clear_session()

print("\nMean accuracy of the crossvalidation: {}".format(np.mean(cv_score)))

Out:

Mean accuracy of the crossvalidation: 0.5049177408218384

Hope it helps.

like image 112
Geeocode Avatar answered Oct 30 '22 14:10

Geeocode