Reputation: 306
I am trying to learn CNN following Krish naik Youtube tutorial in one program i am getting this error:
RuntimeError: Model-building function did not return a valid Keras Model instance, found <keras.engine.sequential.Sequential object at 0x7fd882393b38>
my code is given below
import keras.datasets
fashion=keras.datasets.fashion_mnist
(x_train,y_train),(x_test,y_test)=fashion.load_data()
x_train=x_train/255.00
x_test=x_test/255.00
x_train=x_train.reshape(x_train.shape[0],x_train.shape[1],x_train.shape[2],1)
x_test=x_test.reshape(x_test.shape[0],x_test.shape[1],x_test.shape[2],1)
from keras.models import Sequential
from keras.layers import Conv2D,Flatten,Dropout,Dense
from keras.optimizers import Adam
def build_knn(hp):
models=Sequential()
models.add(Conv2D(filters=hp.Int('conv2d_1',min_value=32,max_value=128,step=16),
kernel_size=hp.Choice('conv1_kernal',values=[3,5]),
activation='relu',
input_shape=(28,28,1)
))
models.add(Conv2D(filters=hp.Int('conv2d_1',min_value=32,max_value=128,step=16),
kernel_size=hp.Choice('conv1_kernal',values=[3,5]),
activation='relu'
))
models.add(Flatten())
models.add(Dense(hp.Int('neural1',min_value=32,max_value=128,step=16),activation='relu'))
models.add(Dense(10,activation='softmax'))
models.compile(optimizer=Adam(hp.Choice('learning_rate',values=[1e-2,1e-3])),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
return models
from kerastuner import RandomSearch
from kerastuner.engine.hyperparameters import HyperParameters
tuner_search=RandomSearch(build_knn,objective='val_accuracy',max_trials=5,directory='jupyterfiles',project_name='krish_naik_fashion_mnist')
Upvotes: 1
Views: 1219
Reputation: 60318
It is not very clear in the project site, but is indeed mentioned that Keras Tuner is (emphasis added):
A hyperparameter tuner for Keras, specifically for
tf.keras
with TensorFlow 2.0.
In other words, it works for tf.keras
, but not for the stand-alone version of Keras, which you seem to be using here.
Indeed, adapting the basic example from the project site linked above, but changing the imports to use stand-alone Keras, replicates the error you report:
!pip install -U keras-tuner
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')
Result:
RuntimeError: Model-building function did not return a valid Keras Model instance, found <keras.engine.sequential.Sequential object at 0x7fd882393b38>
But just by changing the imports to use tf.keras
instead, i.e.:
from tensorflow import keras
from tensorflow.keras import layers
from kerastuner.tuners import RandomSearch
the same code above runs OK without error.
So, you should change the imports shown here to:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,Flatten,Dropout,Dense
from tensorflow.keras.optimizers import Adam
and you should be fine (provided of course that you use Tensorflow 2.0 indeed).
I have just opened a relevant Github issue, advising that they include this clarification explicitly in a visible position.
Upvotes: 3