Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

keras - Can't save and load a model

I just followed this tutorial of Keras.io https://keras.io/examples/nlp/semantic_similarity_with_bert/

I can run it and the model works. When i want to save the model with h5 format I have this error :

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-29-549810e352cb> in <module>()
----> 1 model.save('my_model.h5')

9 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in get_config(self)
   2252 
   2253   def get_config(self):
-> 2254     raise NotImplementedError
   2255 
   2256   @classmethod

NotImplementedError:
-----------------------------------------------------------------------------

when i want to do the same things with the "SavedModel" format I can save the model but when i try to load it, i have this error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in assert_same_structure(nest1, nest2, check_types, expand_composites)
    403     _pywrap_utils.AssertSameStructure(nest1, nest2, check_types,
--> 404                                       expand_composites)
    405   except (ValueError, TypeError) as e:

ValueError: The two structures don't have the same nested structure.

First structure: type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='inputs/input_ids')}

Second structure: type=TensorSpec str=TensorSpec(shape=(None, 128), dtype=tf.int32, name='inputs')

More specifically: Substructure "type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='inputs/input_ids')}" is a sequence, while substructure "type=TensorSpec str=TensorSpec(shape=(None, 128), dtype=tf.int32, name='inputs')" is not

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
7 frames
<ipython-input-33-ae06d36f12a1> in <module>()
----> 1 new_model = tf.keras.models.load_model('saved_model/my_model2', compile=False)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile, options)
    210       if isinstance(filepath, six.string_types):
    211         loader_impl.parse_saved_model(filepath)
--> 212         return saved_model_load.load(filepath, compile, options)
    213 
    214   raise IOError(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in load(path, compile, options)
    145 
    146   # Finalize the loaded layers and remove the extra tracked dependencies.
--> 147   keras_loader.finalize_objects()
    148   keras_loader.del_tracking()
    149 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in finalize_objects(self)
    594         layers_revived_from_config.append(node)
    595 
--> 596     _finalize_saved_model_layers(layers_revived_from_saved_model)
    597     _finalize_config_layers(layers_revived_from_config)
    598 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in _finalize_saved_model_layers(layers)
    783         call_fn = _get_keras_attr(layer).call_and_return_conditional_losses
    784         if call_fn.input_signature is None:
--> 785           inputs = infer_inputs_from_restored_call_function(call_fn)
    786         else:
    787           inputs = call_fn.input_signature[0]

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in infer_inputs_from_restored_call_function(fn)
   1068   for concrete in fn.concrete_functions[1:]:
   1069     spec2 = concrete.structured_input_signature[0][0]
-> 1070     spec = nest.map_structure(common_spec, spec, spec2)
   1071   return spec
   1072 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in map_structure(func, *structure, **kwargs)
    651   for other in structure[1:]:
    652     assert_same_structure(structure[0], other, check_types=check_types,
--> 653                           expand_composites=expand_composites)
    654 
    655   flat_structure = (flatten(s, expand_composites) for s in structure)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in assert_same_structure(nest1, nest2, check_types, expand_composites)
    409                   "Entire first structure:
%s
"
    410                   "Entire second structure:
%s"
--> 411                   % (str(e), str1, str2))
    412 
    413 

ValueError: The two structures don't have the same nested structure.

First structure: type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='inputs/input_ids')}

Second structure: type=TensorSpec str=TensorSpec(shape=(None, 128), dtype=tf.int32, name='inputs')

More specifically: Substructure "type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='inputs/input_ids')}" is a sequence, while substructure "type=TensorSpec str=TensorSpec(shape=(None, 128), dtype=tf.int32, name='inputs')" is not
Entire first structure:
{'input_ids': .}
Entire second structure:
.

------------------------------------------------------------------------------

Any ideas?

question from:https://stackoverflow.com/questions/65540622/cant-save-and-load-a-model

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you are using different versions of TensorFlow for training and saving. And then, you are getting errors. Please follow these threads (here and here ), some common problems regarding loading Keras models have been discussed there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...