# 2.5 模型保存与加载

主要有三种方法：

* model.save\_weights() : 该方法可保存模型权重；
* model.save() :该方法可保存HDF5模型文件，或者pb格式文件；
* tf.keras.models.save() : 该方法可保存HDF5模型文件，或者pb格式文件；
* tf.saved\_model.save() : 该方法可保存pb格式文件，主要用于模型服务。
* model.load\_weights() : 加载模型权重，和model.save\_weights配合使用。
* tf.keras.models.load\_model() : 加载HDF5或pb模型文件，和model.save()、tf.keras.models.save\_models()配合使用
* tf.saved\_model.load() :加载模型文件，和tf.saved\_model.save()配合使用

## 小知识

tf.keras.models.save、model.save 和 tf.saved\_model.save()保存的pb文件有何区别？

tf.saved\_model格式的模型可以直接用来预测(predict)，但是 tf.saved\_model没有保存优化器配置。 反之，tf.keras.models.save()保存模型的所有参数，包含优化器等参数，可直接进行模型预测(model.predict())。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hecongqing.gitbook.io/tfnotes/di-er-zhang-tensorflow-ji-chu-pian-i/mo-xing-bao-cun-yu-jia-zai.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
