D.S. Parihar
D.S. Parihar

Reputation: 1

AttributeError: 'Sequential' object has no attribute 'compiled' while using gridsearch.fit

All the 2880 fits failed. It is very likely that your model is misconfigured. You can try to debug the error by setting error_score='raise'.

Below are more details about the failures:

2880 fits failed with the following error: Traceback (most recent call last): File "/home/devendra/anaconda3/lib/python3.10/site-packages/sklearn/model_selection/_validation.py", line 888, in _fit_and_score estimator.fit(X_train, y_train, **fit_params) File "/home/devendra/anaconda3/lib/python3.10/site-packages/scikeras/wrappers.py", line 1501, in fit super().fit(X=X, y=y, sample_weight=sample_weight, **kwargs) File "/home/devendra/anaconda3/lib/python3.10/site-packages/scikeras/wrappers.py", line 770, in fit self._fit( File "/home/devendra/anaconda3/lib/python3.10/site-packages/scikeras/wrappers.py", line 928, in _fit self._ensure_compiled_model() File "/home/devendra/anaconda3/lib/python3.10/site-packages/scikeras/wrappers.py", line 439, in ensure_compiled_model if not self.model.compiled: AttributeError: 'Sequential' object has no attribute 'compiled'

def create_model(neurons_1,neurons_2,optimizer,learning_rate,dropout):
    
    cnn = models.Sequential([
        layers.Conv1D(filters=neurons_1, kernel_size=8, activation='relu', input_shape=(82,1)),
        layers.MaxPooling1D(4),
        layers.BatchNormalization(),
        
        layers.Conv1D(filters=neurons_2, kernel_size=8, activation='relu'),
        layers.MaxPooling1D(4),
        layers.BatchNormalization(),
        
        layers.Flatten(),
        layers.Dense(100, activation='relu'),
        layers.Dropout(dropout),
        layers.Dense(50, activation='relu'),
        layers.Dropout(dropout),
        
        layers.Dense(2, activation='softmax')
    ])
            
    
    cnn.compile(optimizer=optimizer,
                  loss= tf.keras.losses.SparseCategoricalCrossentropy(),
                  metrics=['accuracy'])
    
    
    return cnn


model = KerasClassifier(model=create_model)

params={
    'epochs':[20,50,100],
    'model__neurons_1':[16,32,64,128],
    'model__neurons_2':[16,32,64,128],
    'model__optimizer':['adam','SGD'],
    'model__dropout':[0.2,0.5],
    'model__learning_rate':[0.001, 0.01, 0.1]
    
    }

# labels
y_val = train_df.TB_status.values
# data
X_val = train_df[feat_names]

grid= GridSearchCV(estimator=model,param_grid=params,cv=5,verbose=1)

transformer = RobustScaler().fit(X_val)
X_r_scaled = transformer.transform(X_val)
grid_search = grid.fit(X_r_scaled, y_val)

Upvotes: 0

Views: 106

Answers (1)

Sagar
Sagar

Reputation: 149

To fine-tune hyperparameters using GridSearchCV, begin by installing the scikeras library. Preprocess your data and define your model. Next, define a dictionary containing hyperparameter names and their potential values. Wrap your model within the KerasClassifier class. Finally, pass the wrapped model and the hyperparameter dictionary to GridSearchCV and initiate the training process with the fit method. I have implemented GridSearchCv , refer to the provided gist.

Upvotes: 0

Related Questions