TensorFlow¶
关于此页面
这是 BentoML 中 TensorFlow 的 API 参考。有关如何在 BentoML 中使用 TensorFlow 的更多信息,请参阅 /frameworks/tensorflow。
注意
您可以在我们的 BentoML/examples 目录中找到更多 **TensorFlow** 示例。
- bentoml.tensorflow.save_model(name: Tag | str, model: tf_ext.KerasModel | tf_ext.Module, *, tf_signatures: tf_ext.ConcreteFunction | None = None, tf_save_options: tf_ext.SaveOptions | None = None, signatures: dict[str, ModelSignature] | dict[str, ModelSignatureDict] | None = None, labels: dict[str, str] | None = None, custom_objects: dict[str, t.Any] | None = None, external_modules: list[ModuleType] | None = None, metadata: dict[str, t.Any] | None = None) bentoml.Model ¶
将模型实例保存到 BentoML 模型仓库。
- 参数:
name – 给定模型的名称。这应通过 Python 标识符检查。
model – 要保存的模型实例
tf_signatures – 更多信息请参阅 Tensorflow 文档中的 Signatures 解释。
tf_save_options – TensorFlow 保存选项。
signatures – 暴露用于在目标模型上运行推理的方法。signatures 用于在使用 bentoml.Service 提供模型服务时创建 Runner 实例
labels – 用于管理模型的用户定义标签,例如 team=nlp, stage=dev
custom_objects – 用户定义的与模型一同保存的其他 Python 对象,例如分词器实例、预处理函数、模型配置 JSON
external_modules – 用户定义的与模型或 custom objects 一同保存的其他 Python 模块,例如分词器模块、预处理器模块、模型配置模块
metadata – 给定模型的自定义元数据。
- 抛出:
ValueError – 如果
obj
不可追踪。- 返回:
格式为
name:version
的tag
,其中name
是用户定义的模型名称,version
是由 BentoML 生成的版本。- 返回类型:
Tag
示例
import tensorflow as tf import numpy as np import bentoml class NativeModel(tf.Module): def __init__(self): super().__init__() self.weights = np.asfarray([[1.0], [1.0], [1.0], [1.0], [1.0]]) self.dense = lambda inputs: tf.matmul(inputs, self.weights) @tf.function( input_signature=[tf.TensorSpec(shape=[1, 5], dtype=tf.float64, name="inputs")] ) def __call__(self, inputs): return self.dense(inputs) # then save the given model to BentoML modelstore: model = NativeModel() bento_model = bentoml.tensorflow.save_model("native_toy", model)
注意
bentoml.tensorflow.save_model
API 也支持保存 RaggedTensor 模型和 Keras 模型。如果您选择使用bentoml.tensorflow.save_model
保存 Keras 模型,那么模型将以SavedModel
格式保存,而不是h5
格式。
- bentoml.tensorflow.load_model(bento_model: str | Tag | bentoml.Model, device_name: str = '/device:CPU:0') tf_ext.AutoTrackable | tf_ext.Module ¶
从 BentoML 本地模型仓库加载给定名称的 TensorFlow 模型。
- 参数:
bento_model – 要从仓库获取的模型标签,或是要从中加载模型的 BentoML ~bentoml.Model 实例。
device_name – 加载模型的设备 ID。设备 ID 格式应与 tf.device 兼容
- 返回:
一个来自 BentoML 模型仓库的
SavedModel
格式实例。- 返回类型:
SavedModel
示例
import bentoml # load a model back into memory model = bentoml.tensorflow.load_model("my_tensorflow_model")