Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fits failed with following error : ValueError: Sequential model 'sequential_XXX' has no defined outputs yet. #328

Open
DaoFof opened this issue Oct 6, 2024 · 0 comments

Comments

@DaoFof
Copy link

DaoFof commented Oct 6, 2024

I am trying an hyperparameter tuning 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.

The code


!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_ = num_class)
grid_search_model = GridSearchCV(estimator=grid_search_model, param_grid=param_grid, cv=3, error_score='raise')
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>](https://localhost:8080/#) 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](https://localhost:8080/#) 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant