[英]How to load TensorFlow Keras model from a saved checkpoint in Colaboratory?
[英]How to convert or load saved model into TensorFlow or Keras?
我使用 tensorflow keras 創建了一個 model 並定義了一個回調來保存每個 Z20F35E630DAF439DBFA4C3F6Depoch58CZ 之后的回調。 它工作並以pb
格式保存了 model 但我無法將其再次加載到 keras 因為 keras 只接受h5
格式。
我有兩個問題:
h5
格式保存 keras model ?我的回調並保存 model:
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])
我保存的 model 結構:
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'
在重新創建 model 之后:
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'
tf.keras
models are loaded using tf.keras.models.load_model
, this should work fine as tf.keras
supports reading/writing multiple formats, including tensorflow checkpoints.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.