What's new

Help Help hindi na po kaya ni gpt

ItsukiNakano

Eternal Poster
Established
Joined
Dec 5, 2015
Posts
1,134
Solutions
1
Reaction
253
Points
460
rom sklearn.model_selection import GridSearchCV from keras.models import Sequential from keras.layers import Conv1D, LSTM, Dense, Dropout, MaxPooling1D, BatchNormalization from scikeras.wrappers import KerasClassifier from sklearn.model_selection import train_test_split # Function to create the Keras model def create_model(input_shape, filters_1=32, kernel_size_1=3, dilation_rate_1=2, filters_2=64, kernel_size_2=5, dilation_rate_2=4, filters_3=128, kernel_size_3=5, dilation_rate_3=4, dense_units=128, lstm_units_1=128, lstm_units_2=64): model = Sequential() # Convolutional Block 1 model.add(Conv1D(filters=filters_1, kernel_size=kernel_size_1, activation='relu', dilation_rate=dilation_rate_1, input_shape=input_shape)) model.add(MaxPooling1D(pool_size=2)) model.add(Dropout(0.25)) model.add(BatchNormalization()) # Convolutional Block 2 model.add(Conv1D(filters=filters_2, kernel_size=kernel_size_2, activation='relu', dilation_rate=dilation_rate_2)) model.add(MaxPooling1D(pool_size=2)) model.add(Dropout(0.25)) model.add(BatchNormalization()) # Convolutional Block 3 model.add(Conv1D(filters=filters_3, kernel_size=kernel_size_3, activation='relu', dilation_rate=dilation_rate_3)) model.add(MaxPooling1D(pool_size=2)) model.add(Dropout(0.25)) model.add(BatchNormalization()) # Fully Connected Layers model.add(Dense(dense_units, activation='relu')) model.add(Dropout(0.25)) model.add(BatchNormalization()) # LSTM Block model.add(LSTM(lstm_units_1, return_sequences=True)) model.add(Dropout(0.25)) model.add(BatchNormalization()) # LSTM Block 2 model.add(LSTM(lstm_units_2)) model.add(Dropout(0.25)) model.add(BatchNormalization()) # Fully Connected Layers model.add(Dense(64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) return model # Assuming you have your data (X_train, y_train) prepared # Assuming you have defined input_shape input_shape = (X_train.shape[1], 1) # Create KerasClassifier model = KerasClassifier(build_fn=create_model, input_shape=input_shape, epochs=50, batch_size=32, verbose=0) param_grid = { 'build_fn__dense_units': [64, 128], 'build_fn__filters_1': [32, 64], 'build_fn__kernel_size_1': [3, 5], 'build_fn__dilation_rate_1': [2, 4], 'build_fn__filters_2': [64, 128], 'build_fn__kernel_size_2': [3, 5], 'build_fn__dilation_rate_2': [2, 4], 'build_fn__filters_3': [128, 256], 'build_fn__kernel_size_3': [3, 5], 'build_fn__dilation_rate_3': [2, 4], 'build_fn__lstm_units_1': [64, 128], 'build_fn__lstm_units_2': [32, 64] } # Create and fit the grid search grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring='accuracy', cv=3) grid_result = grid.fit(X_train, y_train) # Print the best parameters and results print(f"Best parameters: {grid_result.best_params_}") print(f"Best accuracy: {grid_result.best_score_}") # Evaluate the best model on the test set best_model = grid_result.best_estimator_ test_loss, test_accuracy = best_model.evaluate(x_test, y_test) print(f"Test Loss: {test_loss}") print(f"Test Accuracy: {test_accuracy}")

tapos eto po error

---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-59-8e36360d3ff4> in <cell line: 78>() 76 # Create and fit the grid search 77 grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring='accuracy', cv=3)---> 78 grid_result = grid.fit(X_train, y_train) 79 # Print the best parameters and results 80 print(f"Best parameters: {grid_result.best_params_}")9 frames/usr/local/lib/python3.10/dist-packages/sklearn/base.py in set_params(self, **params) 234 ) 235 key = "estimator"--> 236 valid_params[key].set_params(**sub_params) 237 238 return selfAttributeError: 'function' object has no attribute 'set_params'
 
The error you're encountering is due to a mismatch in the estimator object being used in the GridSearchCV. The KerasClassifier from scikeras.wrappers is not compatible with the standard GridSearchCV from sklearn.model_selection.

To resolve this issue, you can modify the code as follows:

1. Import the GridSearchCV class from the scikeras.wrappers module instead of sklearn.model_selection:

Code:
python
   from scikeras.wrappers import KerasClassifier, GridSearchCV

2. Replace the line where you create the model object with the following code:

Code:
python
   model = KerasClassifier(model=create_model, input_shape=input_shape, epochs=50, batch_size=32, verbose=0)

This change is necessary because the KerasClassifier wrapper from scikeras expects the model parameter instead of build_fn.

Here's the modified code:

Python:
from scikeras.wrappers import KerasClassifier, GridSearchCV
from keras.models import Sequential
from keras.layers import Conv1D, LSTM, Dense, Dropout, MaxPooling1D, BatchNormalization
from sklearn.model_selection import train_test_split

# Rest of the code...

# Assuming you have your data (X_train, y_train) prepared
# Assuming you have defined input_shape
input_shape = (X_train.shape[1], 1)

# Create KerasClassifier
model = KerasClassifier(model=create_model, input_shape=input_shape, epochs=50, batch_size=32, verbose=0)

param_grid = {
    'model__dense_units': [64, 128],
    'model__filters_1': [32, 64],
    'model__kernel_size_1': [3, 5],
    'model__dilation_rate_1': [2, 4],
    'model__filters_2': [64, 128],
    'model__kernel_size_2': [3, 5],
    'model__dilation_rate_2': [2, 4],
    'model__filters_3': [128, 256],
    'model__kernel_size_3': [3, 5],
    'model__dilation_rate_3': [2, 4],
    'model__lstm_units_1': [64, 128],
    'model__lstm_units_2': [32, 64]
}

# Create and fit the grid search
grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring='accuracy', cv=3)
grid_result = grid.fit(X_train, y_train)
# Rest of the code...

This should resolve the AttributeError and allow you to perform the grid search.
 
Back
Top