autokeras: Model saving doesn't work for StructuredDataRegressor

Bug Description

I can’t save the best model as h5 or even a tf file

Bug Reproduction

Code for reproducing the bug:

model = regressor.export_model()

model.save('testmodel.h5')

gives me

NotImplementedError: Save or restore weights that is not an instance of `tf.Variable` is not supported in h5, use `save_format='tf'` instead. Got a model or layer CategoricalEncoding with weights ....

It suggests I use tf instead of h5, but when I do that I get this error

model.save('testmodel', save_format='tf')
2020-04-03 14:44:31.041453: W tensorflow/python/util/util.cc:329] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
*** RuntimeError: Attempting to capture an EagerTensor without building a function.

Data used by the code:

Expected Behavior

Save the best model in either h5 or tf format

Setup Details

  • OS type and version: Ubuntu 18.04.01
  • Python: 3.6.6
  • autokeras: 1.0.2
  • scikit-learn: 0.20.3
  • numpy: 1.18.2
  • pandas: 1.0.1
  • tensorflow: 2.2.0-dev20200401 (nightly)

Additional context

I also can’t load the model json after saving it but I think that’s related to #1023

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19 (5 by maintainers)

Most upvoted comments

As I tested the latest version of AutoKeras can save the structureddataregressor fine.

@utkarshgupta137

save and load model

def load_pickle_model_from_file(name_file):
    with open(name_file, 'rb') as file:
        loaded_model = pickle.load(file)
    return loaded_model

def save_pickle_model_to_file(model, name_model):
    with open(Path(str(name_model)+".pickle"), 'wb') as file:
        pickle.dump(model, file)
    print(str(Path(str(name_model)+".pickle"))+' - model saved!')

create: model_name - you select own name; directory - you select place for model’s data

model = ak.StructuredDataRegressor(max_trials=max_trials,
                                               column_names=data_cols,
                                               column_types=data_type,
                                               project_name=model_name
                                               directory='data/models_saved_data/'
                                               )

save after creating: model_name - your selected name on previouse stage

save_pickle_model_to_file(model, model_name)

Issue solved in the latest version, thanks.