Reputation: 145
I'm working on a deep learning project and I tried following a tutorial to evaluate my model with Cross-Validation.
I was looking at this tutorial: https://machinelearningmastery.com/use-keras-deep-learning-models-scikit-learn-python/
I started by first splitting my dataset into features and labels:
labels = dataset['Label']
features = dataset.loc[:, dataset.columns != 'Label'].astype('float64')
I have the following shapes:
features.shape ,labels.shape
((2425727, 78), (2425727,))
I used RobustScalar to scale my data and how I have
features
array([[ 1.40474359e+02, -1.08800488e-02, 0.00000000e+00, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 1.40958974e+02, -1.08609909e-02, -2.50000000e-01, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 1.40961538e+02, -1.08712390e-02, -2.50000000e-01, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
...,
[ 1.48589744e+02, -1.08658453e-02, 0.00000000e+00, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[-6.92307692e-02, 1.77654485e-01, 1.00000000e+00, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[-6.92307692e-02, 6.18858398e-03, 5.00000000e-01, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00]])
labels
array([0, 0, 0, ..., 0, 0, 0])
Now the data is ready to perform cross-validation.
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
# Function to create model, required for KerasClassifier
def create_model():
# create model
model= Sequential()
model.add(Dense(128, activation='relu',input_shape = (78,1)))
model.add(Dropout(0.01))
model.add(Dense(15, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
#fix random seed for reproducibility
seed = 7
np.random.seed(seed)
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed)
# create model
model = KerasClassifier(build_fn=create_model(), epochs=30, batch_size=64, verbose=0)
# evaluate using 5-fold cross-validation
results = cross_val_score(model, features, labels, cv=kfold,scoring='accuracy', error_score="raise")
print(results.mean())
After doing this I get this error thrown: "ValueError: The first argument to Layer.call
must always be passed. "
I also check scikit learn documentation to check if I'm doing something wrong: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.cross_val_score.html
I also tried to look tryed looking for others that could have had this issue such as: https://github.com/scikit-learn/scikit-learn/issues/18944
But I couldn't manage to fix this. Can someone help me with this please ?
Upvotes: 0
Views: 694
Reputation: 113
model = KerasClassifier(build_fn=create_model(), ...)
Try removing the braces of function create_model since the Argument expects a callback function which will be called when needed. So it will be
model = KerasClassifier(build_fn=create_model, ... )
Upvotes: 2