[Python] GridSearch hyperparameter tuning with KerasClassifier : ValueError: Sequential model...

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I am try to do an hyperparameter tuning for an LSTM model using GridSearch and KerasClassifier.

    It's a multiclass classification problem of a text dataset. I saw in the documentation that n_outputs_expected_ should be set to the number of expected classes.

    I do not understand what the issue is.

    !pip install scikeras
    from sklearn.model_selection import GridSearchCV
    from scikeras.wrappers import KerasClassifier

    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
    from tensorflow.keras.preprocessing.text import Tokenizer
    from tensorflow.keras.preprocessing.sequence import pad_sequences

    def create_model(units, dropout, optimizer):
    model = Sequential()
    model.add(Embedding(5000, 128, input_length=100))
    model.add(LSTM(units))
    model.add(Dropout(dropout))
    model.add(Dense(3, activation='softmax'))
    model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer,metrics=['accuracy'])
    return model

    param_grid = {
    'model__units': [50, 100, 150],
    'model__dropout': [0.1, 0.2, 0.3, 0.4],
    'model__optimizer': ['adam', 'rmsprop'],
    'epochs': [10, 20, 30],
    'batch_size': [32, 64, 128]
    }


    grid_search_model = KerasClassifier(build_fn=create_model, n_outputs_expected_ = 3, error_score='raise')
    grid_search_model = GridSearchCV(estimator=grid_search_model, param_grid=param_grid, cv=3)
    grid_result = grid_search_model.fit(X_train_tf, label_encoded_y_train)


    /usr/local/lib/python3.10/dist-packages/scikeras/wrappers.py:925: UserWarning: ``build_fn`` will be renamed to ``model`` in a future release, at which point use of ``build_fn`` will raise an Error instead.
    X, y = self._initialize(X, y)
    /usr/local/lib/python3.10/dist-packages/keras/src/layers/core/embedding.py:90: UserWarning: Argument `input_length` is deprecated. Just remove it.
    warnings.warn(
    ---------------------------------------------------------------------------
    ValueError Traceback (most recent call last)
    <ipython-input-154-d57ef5578357> in <cell line: 1>()
    ----> 1 grid_result = grid_search_model.fit(X_train_tf, label_encoded_y_train)

    13 frames
    /usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py in outputs(self)
    290 if self._functional:
    291 return self._functional.outputs
    --> 292 raise ValueError(
    293 f"Sequential model '{self.name}' has no defined outputs yet."
    294 )

    ValueError: Sequential model 'sequential_2595' has no defined outputs yet.

    Continue reading...

Compartilhe esta Página