keras-team / autokeras

AutoML library for deep learning
http://autokeras.com/
Apache License 2.0
9.16k stars 1.4k forks source link

Bug: FatalTypeError: Expected the model-building function, or HyperModel.build() to return a valid Keras Model instance #1907

Open diramazioni opened 8 months ago

diramazioni commented 8 months ago

Bug Description

I'm just following the tutorial for image regression I've installed tf_keras and then export the environment variable TF_USE_LEGACY_KERAS=1 as suggested #1906

Bug Reproduction

Code for reproducing the bug: reg.fit(x_train, y_train, epochs=epochs)


---------------------------------------------------------------------------
FatalTypeError                            Traceback (most recent call last)
Cell In[7], [line 4](vscode-notebook-cell:?execution_count=7&line=4)
      [2](vscode-notebook-cell:?execution_count=7&line=2) reg = ak.ImageRegressor(overwrite=True, max_trials=max_trials)
      [3](vscode-notebook-cell:?execution_count=7&line=3) # Feed the image regressor with training data.
----> [4](vscode-notebook-cell:?execution_count=7&line=4) reg.fit(x_train, y_train, epochs=epochs)
      [7](vscode-notebook-cell:?execution_count=7&line=7) # Predict with the best model.
      [8](vscode-notebook-cell:?execution_count=7&line=8) predicted_y = reg.predict(x_test)

File [~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:298](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:298), in ImageRegressor.fit(self, x, y, epochs, callbacks, validation_split, validation_data, **kwargs)
    [240](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:240) def fit(
    [241](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:241)     self,
    [242](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:242)     x: Optional[types.DatasetType] = None,
   (...)
    [250](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:250)     **kwargs
    [251](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:251) ):
    [252](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:252)     """Search for the best model and hyperparameters for the AutoModel.
    [253](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:253) 
    [254](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:254)     It will search for the best model based on the performances on
   (...)
    [296](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:296)             validation loss values and validation metrics values (if applicable).
    [297](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:297)     """
--> [298](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:298)     history = super().fit(
    [299](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:299)         x=x,
    [300](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:300)         y=y,
    [301](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:301)         epochs=epochs,
    [302](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:302)         callbacks=callbacks,
    [303](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:303)         validation_split=validation_split,
    [304](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:304)         validation_data=validation_data,
    [305](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:305)         **kwargs
    [306](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:306)     )
    [307](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/tasks/image.py:307)     return history

File [~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:292](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:292), in AutoModel.fit(self, x, y, batch_size, epochs, callbacks, validation_split, validation_data, verbose, **kwargs)
    [287](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:287) if validation_data is None and validation_split:
    [288](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:288)     dataset, validation_data = data_utils.split_dataset(
    [289](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:289)         dataset, validation_split
    [290](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:290)     )
--> [292](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:292) history = self.tuner.search(
    [293](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:293)     x=dataset,
    [294](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:294)     epochs=epochs,
    [295](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:295)     callbacks=callbacks,
    [296](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:296)     validation_data=validation_data,
    [297](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:297)     validation_split=validation_split,
    [298](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:298)     verbose=verbose,
    [299](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:299)     **kwargs
    [300](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:300) )
    [302](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/auto_model.py:302) return history

File [~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/engine/tuner.py:191](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/engine/tuner.py:191), in AutoTuner.search(self, epochs, callbacks, validation_split, verbose, **fit_kwargs)
    [189](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/engine/tuner.py:189) hp = self.oracle.get_space()
    [190](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/engine/tuner.py:190) self._prepare_model_build(hp, **fit_kwargs)
--> [191](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/engine/tuner.py:191) self._try_build(hp)
    [192](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/engine/tuner.py:192) self.oracle.update_space(hp)
    [193](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/engine/tuner.py:193) super().search(
    [194](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/engine/tuner.py:194)     epochs=epochs, callbacks=new_callbacks, verbose=verbose, **fit_kwargs
    [195](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/autokeras/engine/tuner.py:195) )

File [~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/keras_tuner/src/engine/tuner.py:167](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/keras_tuner/src/engine/tuner.py:167), in Tuner._try_build(self, hp)
    [165](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/keras_tuner/src/engine/tuner.py:165) # Stop if `build()` does not return a valid model.
    [166](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/keras_tuner/src/engine/tuner.py:166) if not isinstance(model, keras.models.Model):
--> [167](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/keras_tuner/src/engine/tuner.py:167)     raise errors.FatalTypeError(
    [168](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/keras_tuner/src/engine/tuner.py:168)         "Expected the model-building function, or HyperModel.build() "
    [169](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/keras_tuner/src/engine/tuner.py:169)         "to return a valid Keras Model instance. "
    [170](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/keras_tuner/src/engine/tuner.py:170)         f"Received: {model} of type {type(model)}."
    [171](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/keras_tuner/src/engine/tuner.py:171)     )
    [172](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/keras_tuner/src/engine/tuner.py:172) # Check model size.
    [173](https://file+.vscode-resource.vscode-cdn.net/home/es/data/prj/vitali/sprint/tf/regression/~/.pyenv/versions/3.11.4/lib/python3.11/site-packages/keras_tuner/src/engine/tuner.py:173) size = maybe_compute_model_size(model)

FatalTypeError: Expected the model-building function, or HyperModel.build() to return a valid Keras Model instance. Received: <tf_keras.src.engine.functional.Functional object at 0x7fa164504810> of type <class 'tf_keras.src.engine.functional.Functional'>.```

### Setup Details
Include the details about the versions of:
 - OS type and version: linux mint 21.2
 - Python: 3.11.4
 - autokeras: 1.1.0
 - keras-tuner: 1.4.7 
 - scikit-learn: 1.4.1.post1
 - numpy: 1.26.4
 - pandas: 2.2.1
 - tensorflow==2.16.1
 - tensorflow-io-gcs-filesystem==0.36.0
 - tensorflow-text==2.16.1