4
votes

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!

1
In your build_model you havent added a loss function. Do that like:model.compile(optimizer = optimizer, loss = 'binary_crossentropy', metrics = ['accuracy'])Nihal Sangeeth

1 Answers

6
votes

The build_model function above doesn't configure your model for training. You have added loss and other parameters.

You can compile the model by using keras sequential method compile. https://keras.io/models/sequential/

So your build_model function should be:

loss = 'binary_crossentropy' #https://keras.io/optimizers
optimizer = 'adam'           #https://keras.io/losses
metrics = ['accuracy']
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'))
    model.compile(optimizer = optimizer, loss = loss, metrics = metrics)
    return model