我使用tensorflow keras创建一个模型,并定义了一个回调以在每个时期后保存模型。它可以正常工作并以pb
格式保存模型,但是我无法再次将其加载到keras中,因为keras只接受h5
格式。
我有两个问题:
h5
格式保存keras模型?我的回调并保存模型:
from tensorflow.keras.callbacks import ModelCheckpoint
cp_callback = ModelCheckpoint(filepath=checkpoint_path, save_freq= 'epoch', verbose=1 )
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
regressor.fit(X_train, y_train, epochs = 10, batch_size = 32, callbacks=[cp_callback])
我保存的模型结构:
saved_trained_10_epochs
├── assets
├── saved_model.pb
└── variables
├── variables.data-00000-of-00001
└── variables.index
我尝试如下使用latest_checkpoint
,但出现以下错误:
from tensorflow.train import latest_checkpoint
loaded_model = latest_checkpoint(checkpoint_path)
loaded_model.summary()
错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-57-76a8ebe4f259> in <module>
----> 1 loaded_model.summary()
AttributeError: 'NoneType' object has no attribute 'summary'
在重新创建模型之后:
loaded_regressor = Sequential()
loaded_regressor.add(LSTM(units = 180, return_sequences = True, input_shape = (X_train.shape[1], 3)))
loaded_regressor.add(Dropout(0.2))
loaded_regressor.add(LSTM(units = 180, return_sequences = True))
loaded_regressor.add(Dropout(0.2))
loaded_regressor.add(LSTM(units = 180, return_sequences = True))
loaded_regressor.add(Dropout(0.2))
loaded_regressor.add(LSTM(units = 180, return_sequences = True))
loaded_regressor.add(Dropout(0.2))
loaded_regressor.add(LSTM(units = 180, return_sequences = True))
loaded_regressor.add(Dropout(0.2))
loaded_regressor.add(LSTM(units = 180))
loaded_regressor.add(Dropout(0.2))
loaded_regressor.add(Dense(units = 1))
loaded_regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
loaded_regressor.load_weights(latest_checkpoint(checkpoint_path))
错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-30-c344f1759d01> in <module>
22
23 loaded_regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
---> 24 loaded_regressor.load_weights(latest_checkpoint(checkpoint_path))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in load_weights(self, filepath, by_name)
160 raise ValueError('Load weights is not yet supported with TPUStrategy '
161 'with steps_per_run greater than 1.')
--> 162 return super(Model, self).load_weights(filepath, by_name)
163
164 @trackable.no_automatic_dependency_tracking
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py in load_weights(self, filepath, by_name)
1375 format.
1376 """
-> 1377 if _is_hdf5_filepath(filepath):
1378 save_format = 'h5'
1379 else:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py in _is_hdf5_filepath(filepath)
1670
1671 def _is_hdf5_filepath(filepath):
-> 1672 return (filepath.endswith('.h5') or filepath.endswith('.keras') or
1673 filepath.endswith('.hdf5'))
1674
AttributeError: 'NoneType' object has no attribute 'endswith'
答案 0 :(得分:1)
tf.keras
模型是使用tf.keras.models.load_model
加载的,这应该可以正常工作,因为tf.keras
支持读取/写入多种格式,包括张量流检查点。