TF1/TF2 的模型兼容性
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
TF Hub 模型格式
TF Hub 提供了可重用的模型,可以在 TensorFlow 程序中重新加载、以之为基础进行构建以及重新训练。这些模型有两种不同的格式:
tfhub.dev 上的模型页面提供了模型格式信息。根据模型格式,TF1/2 中可能不支持模型加载/推断、微调或创建。
TF1 Hub 格式兼容性
操作 |
TF2 中的 TF1/TF1 兼容模式 [1] |
TF2 |
加载/推断 |
完全支持(完整的 TF1 Hub 格式加载指南) m = hub.Module(handle) outputs = m(inputs) |
建议使用 hub.load m = hub.load(handle)
outputs = m.signatures["sig"](inputs) 或 hub.KerasLayer m = hub.KerasLayer(handle, signature="sig")
outputs = m(inputs) |
微调 |
完全支持(完整的 TF1 Hub 格式微调指南)m = hub.Module(handle, trainable=True, tags=["train"]*is_training) outputs = m(inputs) 注:不需要单独的训练计算图的模块没有训练标签。 |
不受支持 |
创建 |
完全支持(请参阅完整的 TF1 Hub 格式创建指南) 注:TF1 Hub 格式适用于 TF1,而在 TF2 中仅部分受支持。请考虑创建 TF2 SavedModel。 |
不受支持 |
TF2 SavedModel 兼容性
TF1.15 之前的版本不支持。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-01-11。
[null,null,["最后更新时间 (UTC):2024-01-11。"],[],[],null,["# Model compatibility for TF1/TF2\n\n\u003cbr /\u003e\n\nTF Hub model formats\n--------------------\n\nTF Hub offers reusable model pieces that can be loaded back, built upon, and\npossibly be retrained in a TensorFlow program. These come in two different\nformats:\n\n- The custom [TF1 Hub format](https://www.tensorflow.org/hub/tf1_hub_module) . Its main intended use is in TF1 (or TF1 compatibility mode in TF2) via its [hub.Module API](https://www.tensorflow.org/hub/api_docs/python/hub/Module). Full compatibility details [below](#compatibility_of_hubmodule).\n- The native [TF2 SavedModel](https://www.tensorflow.org/hub/tf2_saved_model) format. Its main intended use is in TF2 via the [hub.load](https://www.tensorflow.org/hub/api_docs/python/hub/load) and [hub.KerasLayer](https://www.tensorflow.org/hub/api_docs/python/hub/KerasLayer) APIs. Full compatibility details [below](#compatibility_of_tf2_savedmodel).\n\nThe model format can be found on the model page on\n[tfhub.dev](https://tfhub.dev). Model **loading/inference** , **fine-tuning** or\n**creation** might not be supported in TF1/2 based on the model formats.\n\nCompatibility of the TF1 Hub format\n-----------------------------------\n\n|---------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Operation | TF1/ TF1 compat mode in TF2 [\\[1\\]](#compatfootnote) | TF2 |\n| Loading / Inference | Fully supported ([complete TF1 Hub format loading guide](https://www.tensorflow.org/hub/tf1_hub_module#using_a_module)) ``` m = hub.Module(handle) outputs = m(inputs) ``` | It's recommended to use either hub.load ``` m = hub.load(handle) outputs = m.signatures[\"sig\"](inputs) ``` or hub.KerasLayer ``` m = hub.KerasLayer(handle, signature=\"sig\") outputs = m(inputs) ``` |\n| Fine-tuning | Fully supported ([complete TF1 Hub format fine-tuning guide](https://www.tensorflow.org/hub/tf1_hub_module#for_consumers)) ``` m = hub.Module(handle, trainable=True, tags=[\"train\"]*is_training) outputs = m(inputs) ``` Note: modules that don't need a separate train graph don't have a train tag. | Not supported |\n| Creation | Fully supported (see [complete TF1 Hub format creation guide](https://www.tensorflow.org/hub/tf1_hub_module#general_approach)) Note: The TF1 Hub format is geared towards TF1 and is only partially supported in TF2. Consider creating a TF2 SavedModel. | Not supported |\n\nCompatibility of TF2 SavedModel\n-------------------------------\n\nNot supported before TF1.15.\n\n|---------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Operation | TF1.15/ TF1 compat mode in TF2 [\\[1\\]](#compatfootnote) | TF2 |\n| Loading / Inference | Use either hub.load ``` m = hub.load(handle) outputs = m(inputs) ``` or hub.KerasLayer ``` m = hub.KerasLayer(handle) outputs = m(inputs) ``` | Fully supported ([complete TF2 SavedModel loading guide](https://www.tensorflow.org/hub/tf2_saved_model#using_savedmodels_from_tf_hub)). Use either hub.load ``` m = hub.load(handle) outputs = m(inputs) ``` or hub.KerasLayer ``` m = hub.KerasLayer(handle) outputs = m(inputs) ``` |\n| Fine-tuning | Supported for a hub.KerasLayer used in tf.keras.Model when trained with Model.fit() or trained in an Estimator whose model_fn wraps the Model per the [custom model_fn guide](https://www.tensorflow.org/guide/migrate#using_a_custom_model_fn). Note: hub.KerasLayer does not fill in graph collections like the old tf.compat.v1.layers or hub.Module APIs did. | Fully supported ([complete TF2 SavedModel fine-tuning guide](https://www.tensorflow.org/hub/tf2_saved_model#for_savedmodel_consumers)). Use either hub.load: ``` m = hub.load(handle) outputs = m(inputs, training=is_training) ``` or hub.KerasLayer: ``` m = hub.KerasLayer(handle, trainable=True) outputs = m(inputs) ``` |\n| Creation | The TF2 API [tf.saved_model.save()](https://www.tensorflow.org/api_docs/python/tf/saved_model/save) can be called from within compat mode. | Fully supported (see [complete TF2 SavedModel creation guide](https://www.tensorflow.org/hub/tf2_saved_model#creating_savedmodels_for_tf_hub)) |\n\n\u003cbr /\u003e\n\n\\[1\\] \"TF1 compat mode in TF2\" refers to the combined\neffect of importing TF2 with\n`import tensorflow.compat.v1 as tf`\nand running\n`tf.disable_v2_behavior()`\nas described in the\n[TensorFlow Migration guide](https://www.tensorflow.org/guide/migrate)."]]