bhd*_*rkn 5 python keras tensorflow
我正在使用Colab 笔记本学习 Tensorflow入门教程。
成功之后,我决定尝试一下保存模型。我试着保存它并且它起作用了。(感谢这个答案)
# Saving Model to Drive
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Authenticate and create the PyDrive
client.auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
# Save Keras Model or weights on google drive
model.save('getting-started-wtih-tf.h5')
model_file = drive.CreateFile({'title' : 'getting-started-wtih-tf.h5'})
model_file.SetContentFile('getting-started-wtih-tf.h5')
model_file.Upload()
# download to google drive
drive.CreateFile({'id': model_file.get('id')})
Run Code Online (Sandbox Code Playgroud)
但几周后,它开始因这个异常而失败。
NotImplementedErrorTraceback (most recent call last)
<ipython-input-28-9fcdb77fc96c> in <module>()
15 # Save Keras Model or weights on google drive
16 # tf.keras.models.save_model(model, 'getting-started-wtih-tf.h5')
---> 17 model.save('getting-started-wtih-tf.h5')
18 model_file = drive.CreateFile({'title' : 'getting-started-wtih-tf.h5'})
19 model_file.SetContentFile('getting-started-wtih-tf.h5')
/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/network.pyc in save(self, filepath, overwrite, include_optimizer)
1356 """
1357 if not self._is_graph_network:
-> 1358 raise NotImplementedError
1359
1360 from tensorflow.python.keras.models import save_model #pylint: disable=g-import-not-at-top
NotImplementedError:
Run Code Online (Sandbox Code Playgroud)
我试图通过改变来克服这个问题:
model.save('getting-started-wtih-tf.h5')
Run Code Online (Sandbox Code Playgroud)
面向 TF 的保存语句的行:
tf.keras.models.save_model(model, 'getting-started-wtih-tf.h5')
Run Code Online (Sandbox Code Playgroud)
但现在,我无法使用这些加载相同的模型:
# Loading Model and Reavaluating
new_model = tf.keras.models.load_model('getting-started-wtih-tf.h5')
new_model.summary()
new_model.evaluate(x_test_gray_scale, y_test)
Run Code Online (Sandbox Code Playgroud)
因为我得到:
AttributeErrorTraceback (most recent call last)
<ipython-input-43-f7b789f2dd31> in <module>()
----> 1 new_model = tf.keras.models.load_model('getting-started-wtih-tf.h5')
2 new_model.summary()
3
4 new_model.evaluate(x_test_gray_scale, y_test)
/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/saving.pyc in load_model(filepath, custom_objects, compile)
260 weighted_metrics=weighted_metrics,
261 loss_weights=loss_weights,
--> 262 sample_weight_mode=sample_weight_mode)
263
264 # Set optimizer weights.
/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/checkpointable/base.py in _method_wrapper(self, *args, **kwargs)
424 self._setattr_tracking = False # pylint: disable=protected-access
425 try:
--> 426 method(self, *args, **kwargs)
427 finally:
428 self._setattr_tracking = previous_value # pylint: disable=protected-access
/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/training.pyc in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, distribute, **kwargs)
523 # One list per output of the model.
524 self.nested_metrics = training_utils.collect_metrics(
--> 525 metrics, self.output_names)
526 self.nested_weighted_metrics = training_utils.collect_metrics(
527 weighted_metrics, self.output_names)
AttributeError: 'Sequential' object has no attribute 'output_names'
Run Code Online (Sandbox Code Playgroud)
简而言之,我的问题是,如何在 Tensorflow 中保存/加载 keras 模型?
另外,这是我正在制作的笔记本。
| 归档时间: |
|
| 查看次数: |
973 次 |
| 最近记录: |