question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RuntimeError: Model-building function did not return a valid Keras Model instance

See original GitHub issue

Hi,

I followed exactly the tutorial and I got this error …

Input :

import keras
from keras import layers
from kerastuner.tuners import RandomSearch

def build_model(hp):
    model = keras.Sequential()
    model.add(layers.Dense(units=hp.Int('units',
                                        min_value=32,
                                        max_value=512,
                                        step=32),
                           activation='relu'))
    model.add(layers.Dense(10, activation='softmax'))
    model.compile(
        optimizer=keras.optimizers.Adam(
            hp.Choice('learning_rate',
                      values=[1e-2, 1e-3, 1e-4])),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])
    return model

tuner = RandomSearch(
    build_model,
    objective='val_accuracy',
    max_trials=5,
    executions_per_trial=3,
    directory='my_dir',
    project_name='helloworld')

Output :

RuntimeError: Model-building function did not return a valid Keras Model instance, found <keras.engine.sequential.Sequential object at 0x7f7209a72950>

Help please ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
Vaunoragecommented, Oct 31, 2019

OKay sorry I got it on my own…

Just needed to change the import from

import keras to from tensorflow import keras

Now it works !

2reactions
rao208commented, May 18, 2020

Oh! It was such a silly mistake on my end. It is working now. Thank you! @ben-arnao

can you please share it? i am getting the same error and I don’t understand where am I going wrong.

My layers and optimizers were imported from tensorflow.keras like this

from tensorflow.keras import layers
from tensorflow.keras.optimizers import Adam, Adadelta

whereas Sequential and layers like Conv2D, MaxPooling etc were imported from keras like this:

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Lambda, Flatten, Dropout, BatchNormalization

Also, doing this from tensorflow import keras doesn’t help, because I had both, tensorflow 2.0 and keras installed.

So, all I had to do was change all from keras. import ..... to from tensorflow.keras. import .... to get rid of the error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keras Tuner - Model-building function did not return a valid ...
I'm trying to search Hyperparameters for a model using Keras Tuner, but I'm getting this error when ...
Read more >
Model-building function did not return a valid Keras ... - SILVER
RuntimeError : Model-building function did not return a valid Keras Model instance, found None 케라스 튜너 에러 해결 방법 !
Read more >
A few pitfalls for Kerastuner beginner users and why I like and ...
RuntimeError : Model-building function did not return a valid Keras Model instance, found <keras.engine.sequential.Sequential object at 0x000001A7D79E3128>.
Read more >
Keras FAQ
How can I use pre-trained models in Keras? How can I use stateful RNNs? General questions. How can I train a Keras model...
Read more >
How can one apply `keras_tuner` to `xgboost`-based models?
RuntimeError : Model-building function did not return a valid Keras Model instance, found StackingRegressor(estimators=[('rnd_reg_opt', ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found