I am using tensorflow
and keras
to build a simple MNIST classification model, and I want to fine tune my model, so I choose sklearn.model_selection.GridSearchCV
.
However, when I call the fit
function, it said:
AttributeError: 'Sequential' object has no attribute 'loss'
I compared my code to others', but still can't figure out why. The only difference is that I use tensorflow.keras
instead of keras
.
Here is my code:
from tensorflow.keras.models import Sequential, Model from tensorflow.keras.layers import Input, Dense, Activation, Dropout, BatchNormalization from tensorflow.keras.datasets import mnist from tensorflow.keras.wrappers.scikit_learn import KerasClassifier from sklearn.model_selection import GridSearchCV ... ... ... def get_model(dropout_rate=0.2, hidden_units=512): model = Sequential() model.add(Dropout(dropout_rate, input_shape=(28*28,))) model.add(Dense(hidden_units, activation='relu')) model.add(BatchNormalization()) model.add(Dropout(dropout_rate)) model.add(Dense(hidden_units, activation='relu')) model.add(BatchNormalization()) model.add(Dropout(dropout_rate)) model.add(Dense(hidden_units, activation='relu')) model.add(BatchNormalization()) model.add(Dropout(dropout_rate)) model.add(Dense(10, activation='softmax')) return model model = KerasClassifier(build_fn=get_model, batch_size=128, epochs=10) para_dict = {'dropout_rate':[0.2,0.5,0.8], 'hidden_units':[128,256,512,1024]} clf = GridSearchCV(model, para_dict, cv=5, scoring='accuracy') clf.fit(x_train, y_train)
Thank you!
model.compile(optimizer = optimizer, loss = 'binary_crossentropy', metrics = ['accuracy'])
– Nihal Sangeeth