Skip to content

[TF2]'Tensor' object has no attribute '_keras_history' #33215

@SmileTM

Description

@SmileTM

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow):
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): MacOS
  • TensorFlow installed from (source or binary): source
  • TensorFlow version (use command below): 2.0
  • Python version: 3.7

Problem

in TF2.0 use Keras to save model.h5 ,then load model.h5.

I have saved model.h5 from official.nlp.bert_models.py by use model.save("model.h5") .
everything is ok , but when I load the model.h5 there have some problems.

model =tf.keras.models.load_model('./my_model1.h5', custom_objects={'BertModel':bert_modeling.BertModel})

emmmmm, how to solve this problem????
I am not sure this is BUG, but in this link someone say this maybe is a bug.
tensorflow/models#7643

Traceback

Traceback (most recent call last):
File "/Users/lollipop/Documents/tf2g/false_news/test.py", line 30, in
model =tf.keras.models.load_model('./my_model1.h5', custom_objects={'BertModel':bert_modeling.BertModel})
File "/Users/lollipop/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py", line 146, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File "/Users/lollipop/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 168, in load_model_from_hdf5
custom_objects=custom_objects)
File "/Users/lollipop/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/model_config.py", line 55, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/Users/lollipop/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/serialization.py", line 102, in deserialize
printable_module_name='layer')
File "/Users/lollipop/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py", line 191, in deserialize_keras_object
list(custom_objects.items())))
File "/Users/lollipop/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py", line 906, in from_config
config, custom_objects)
File "/Users/lollipop/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py", line 1852, in reconstruct_from_config
process_node(layer, node_data)
File "/Users/lollipop/.conda/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py", line 1802, in process_node
output_index = nest.flatten(output_tensors)[0]._keras_history.node_index
AttributeError: 'Tensor' object has no attribute '_keras_history'

Process finished with exit code 1

Metadata

Metadata

Assignees

Labels

TF 2.0Issues relating to TensorFlow 2.0comp:kerasKeras related issuesstat:awaiting tensorflowerStatus - Awaiting response from tensorflowertype:bugBug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions