shenweichen / DeepCTR

Easy-to-use,Modular and Extendible package of deep-learning based CTR models .
https://deepctr-doc.readthedocs.io/en/latest/index.html
Apache License 2.0
7.54k stars 2.21k forks source link

save/load model error #532

Open chentao169 opened 1 year ago

chentao169 commented 1 year ago

Describe the bug(问题描述)

  1. got error when saving AutoInt model by following FAQ . code:
    model.save(os.path.join(log_dir, 'model.h5'), save_format='h5')

stack trace:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[29], line 1
----> 1 model.save(os.path.join(log_dir, 'model.h5'), save_format='h5')

File /opt/conda/lib/python3.10/site-packages/tensorflow/python/keras/engine/training.py:2132, in Model.save(self, filepath, overwrite, include_optimizer, save_format, signatures, options, save_traces)
   2090 """Saves the model to Tensorflow SavedModel or a single HDF5 file.
   2091 
   2092 Please see `tf.keras.models.save_model` or the
   (...)
   2129 ```
   2130 """
   2131 # pylint: enable=line-too-long
-> 2132 save.save_model(self, filepath, overwrite, include_optimizer, save_format,
   2133                 signatures, options, save_traces)

File /opt/conda/lib/python3.10/site-packages/tensorflow/python/keras/saving/save.py:145, in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures, options, save_traces)
    136   if (not model._is_graph_network and  # pylint:disable=protected-access
    137       not isinstance(model, sequential.Sequential)):
    138     raise NotImplementedError(
    139         'Saving the model to HDF5 format requires the model to be a '
    140         'Functional model or a Sequential model. It does not work for '
   (...)
    143         'to the Tensorflow SavedModel format (by setting save_format="tf") '
    144         'or using `save_weights`.')
--> 145   hdf5_format.save_model_to_hdf5(
    146       model, filepath, overwrite, include_optimizer)
    147 else:
    148   with generic_utils.SharedObjectSavingScope():

File /opt/conda/lib/python3.10/site-packages/tensorflow/python/keras/saving/hdf5_format.py:110, in save_model_to_hdf5(model, filepath, overwrite, include_optimizer)
    107   opened_new_file = False
    109 try:
--> 110   model_metadata = saving_utils.model_metadata(model, include_optimizer)
    111   for k, v in model_metadata.items():
    112     if isinstance(v, (dict, list, tuple)):

File /opt/conda/lib/python3.10/site-packages/tensorflow/python/keras/saving/saving_utils.py:172, in model_metadata(model, include_optimizer, require_config)
    161   logging.warning(
    162       'TensorFlow optimizers do not '
    163       'make it possible to access '
   (...)
    169       'Prefer using a Keras optimizer instead '
    170       '(see keras.io/optimizers).')
    171 elif model._compile_was_called:  # pylint: disable=protected-access
--> 172   training_config = model._get_compile_args(user_metrics=False)  # pylint: disable=protected-access
    173   training_config.pop('optimizer', None)  # Handled separately.
    174   metadata['training_config'] = _serialize_nested_config(training_config)

TypeError: clone_and_build_model.<locals>.<lambda>() got an unexpected keyword argument 'user_metrics'
  1. got error too when loading checkpoint which is saved model format, not h5 format. code:
    from deepctr.layers import custom_objects
    model_path = '~/autoint/training/ckpt/epoch_31-val_loss_0.098'
    reloaded = tf.keras.models.load_model(model_path, custom_objects=custom_objects)

    stack trace

    
    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    Cell In[34], line 5
    ----> 5 reloaded = tf.keras.models.load_model(model_path, 
      6                                       custom_objects=custom_objects)

File /opt/conda/lib/python3.10/site-packages/keras/utils/traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs) 67 filtered_tb = _process_traceback_frames(e.traceback) 68 # To get the full stack trace, call: 69 # tf.debugging.disable_traceback_filtering() ---> 70 raise e.with_traceback(filtered_tb) from None 71 finally: 72 del filtered_tb

File /opt/conda/lib/python3.10/site-packages/keras/saving/legacy/saved_model/load.py:358, in KerasObjectLoader._add_children_recreated_from_config(self, obj, proto, node_id) 356 obj._maybe_initialize_trackable() 357 if isinstance(obj, base_layer.Layer) and not obj.built: --> 358 metadata = json_utils.decode(self._metadata[node_id].metadata) 359 self._try_build_layer( 360 obj, node_id, metadata.get("build_input_shape") 361 ) 363 # Create list of all possible children

KeyError: 808



**Operating environment(运行环境):**
 - python version [e.g. 3.10.12]
 - tensorflow version [e.g. 2.11.0]
 - deepctr version [e.g. 0.9.3]

**Additional context**
Add any other context about the problem here.
chentao169 commented 1 year ago

ok, I found the issue. use from tensorflow.python.keras.models import save_model,load_model instead of tensorflow.keras.models