This is the code and I\'m getting the error in the last line only which is y_pred = classifier.predict(X_test)
. The error I\'m getting is AttributeError:
You get the error because you didn´t actually train the returned model from KerasClassifier
which is a Scikit-learn Wrapper to make use of Scikit-learn functions.
You could for example do a GridSearch (as you might know since the code seems to be from the Udemy ML/DL course):
def build_classifier(optimizer):
classifier = Sequential()
classifier.add(Dense(units = 6, kernel_initializer = 'uniform',
activation = 'relu', input_dim = 11))
classifier.add(Dense(units = 6, kernel_initializer = 'uniform',
activation = 'relu'))
classifier.add(Dense(units = 1, kernel_initializer = 'uniform',
activation = 'sigmoid'))
classifier.compile(optimizer = optimizer, loss =
'binary_crossentropy', metrics = ['accuracy'])
return classifier
classifier = KerasClassifier(build_fn = build_classifier)
parameters = {'batch_size': [25, 32],
'epochs': [100, 500],
'optimizer': ['adam', 'rmsprop']}
grid_search = GridSearchCV(estimator = classifier,
param_grid = parameters,
scoring = 'accuracy',
cv = 10)
grid_search = grid_search.fit(X_train, y_train)
If you don´t need Scikit-learn functionality I suggest to avoid the wrapper and simply build your model with:
model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))
…
and then train with:
model.fit( … )
Because you haven't fitted the classifier
yet. For classifier
to have the model variable available, you need to call
classifier.fit(X_train, y_train)
Although you have used cross_val_score()
over the classifier
, and found out accuracies, but the main point to note here is that the cross_val_score
will clone the supplied model and use them for cross-validation folds. So your original estimator classifier
is untouched and untrained.
You can see the working of cross_val_score
in my other answer here
So put the above mentioned line just above y_pred = classifier.predict(X_test)
line and you are all set. Hope this makes it clear.