保存Keras模型后,为什么类名称会更改?

我使用tensorflow(2.0.0)编写了一个基本的keras模型(tf.keras .__ version = 2.2.4-tf):

import tensorflow as tf

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1,activation='linear',input_shape=(1,),name='equation'))
model.compile(optimizer='RMSprop',loss='mean_squared_error')
model.save('c:\\tmp\\oneneuron')
print("Model saved type : ",type(model))
loaded_model = tf.keras.models.load_model('c:\\tmp\\oneneuron')
print("Model loaded type : ",type(loaded_model))
print("compare object model with loaded_model type : ",isinstance(model,type(loaded_model)))
print("compare object loaded_model with model type : ",isinstance(loaded_model,type(model)))
print("compare sublclass loaded_model and model type : ",issubclass(type(loaded_model),type(model)))

结果是

Python 3.6.6 (v3.6.6:4cf1f54eb7,Jun 27 2018,03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help","copyright","credits" or "license" for more information.
>>> exec(open(r'C:\tmp\myPython\test_type_model.py').read())
2019-11-26 18:49:39.071088: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-11-26 18:49:39.574113: W tensorflow/python/util/util.cc:299] Sets are not currently considered sequences,but this may change in the future,so consider avoiding using them.
WARNING: Logging before flag parsing goes to stderr.
W1126 18:49:39.627490 11772 deprecation.py:506] From F:\Program Files\Python\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py:1781: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
Model saved type :  <class 'tensorflow.python.keras.engine.sequential.Sequential'>
Model loaded type :  <class 'tensorflow.python.keras.saving.saved_model.load.Sequential'>
compare object model with loaded_model type :  False
compare object loaded_model with model type :  True
compare sublclass loaded_model and model type :  True

在哪里可以找到tensorflow或keras文档中的tensorflow.python.keras.saving.saved_model.load.Sequential和tensorflow.python.keras.engine.sequential.Sequential之间的区别?

pengsijing 回答:保存Keras模型后,为什么类名称会更改?

这不是一个正确的答案,但我想补充一下我在这个问题上的发现(这让我很困惑):

  • 我找不到关于它的任何文档
  • 使用 h5 格式为我解决了问题:model.save('c:\tmp\oneneuron.h5')
  • 改变的对象类型确实给我带来了问题,例如model.layers[i].filters 可以在保存和重新加载之前工作,但不能在保存和重新加载之后工作。

我的 tensorflow 版本是 2.1

本文链接:https://www.f2er.com/3026866.html

大家都在问