LiveDraw:终极屏幕实时绘图工具,告别截图标注的烦恼
2026/5/16 13:27:55
SavedModel是 TensorFlow 官方的模型序列化格式,能完整保存模型的「权重+计算图+签名」,支持跨平台部署(如TensorFlow Serving、TFLite)、离线复用;
RaggedTensor 与 SavedModel 兼容的核心规则:
下面结合你提供的两个核心示例(Keras模型、自定义tf.Module模型),逐行解析代码逻辑、原理和关键注意事项。
importtensorflowastfimporttempfile# 用于创建临时目录存储SavedModel# 1. 定义数据(复用之前的句子分类任务)sentences=tf.constant(['What makes you think she is a witch?','She turned me into a newt.','A newt?','Well, I got better.'])is_question=tf.constant([True,False,True,False])# 2. 预处理:字符串→RaggedTensor(单词哈希编码)hash_buckets=1000words=tf.strings.split(sentences,' ')hashed_words=tf.strings.to_hash_bucket_fast(words,hash_buckets)# RaggedTensor# 3. 构建适配RaggedTensor的Keras模型(修正后版本,避免LSTM报错)keras_model=tf.keras.Sequential([tf.keras.layers.Input(shape=[None],dtype=tf.int64,ragged=True),# 声明Ragged输入tf.keras.layers.Embedding(hash_buckets,16),tf.keras.layers.LSTM(32,use_bias=False),tf.keras.layers.Dense(32),tf.keras.layers.Activation(tf.nn.relu),tf.keras.layers.Dense(1)])keras_model.compile(loss='binary_crossentropy',optimizer='rmsprop')keras_model.fit(hashed_words.to_tensor(default_value=0),is_question,epochs=1)# 补0后训练# 创建临时目录(避免手动管理路径)keras_module_path=tempfile.mkdtemp()# 保存模型:自动将Keras模型转换为SavedModel格式,包含RaggedTensor的处理逻辑tf.saved_model.save(keras_model,keras_module_path)print(f"Keras模型已保存到:{keras_module_path}")# 加载保存的模型imported_model=tf.saved_model.load(keras_module_path)# 直接传入RaggedTensor(hashed_words),模型透明处理result=imported_model(hashed_words)print("\n加载后模型的预测结果:")print(result)WARNING:absl:Function `_wrapped_model` contains input name(s) args_0 with unsupported characters which will be renamed to args_0_1 in the SavedModel. INFO:tensorflow:Assets written to: /tmp/xxx/assets <tf.Tensor: shape=(4, 1), dtype=float32, numpy= array([[0.05265009], [0.000567 ], [0.03915224], [0.0021234 ]], dtype=float32)>args_0)重命名为args_0_1,不影响模型功能;ragged=True,保存为SavedModel时,TF会自动生成适配RaggedTensor的具体函数(Concrete Function),加载后可直接传入RaggedTensor;自定义tf.Module是TF原生的模型封装方式(比Keras更灵活),但需手动构建具体函数(指定RaggedTensor的输入签名),否则SavedModel无法正确处理RaggedTensor。
classCustomModule(tf.Module):def__init__(self,variable_value):super(CustomModule,self).__init__()# 定义可训练变量(会被SavedModel保存)self.v=tf.Variable(variable_value,dtype=tf.float32)# 用@tf.function装饰:编译为计算图,支持RaggedTensor@tf.functiondefgrow(self,x):# 核心逻辑:RaggedTensor的每个元素 × 变量vreturnx*self.v# 实例化模块(变量v=100.0)module=CustomModule(100.0)# 必须先构建具体函数:指定输入为RaggedTensorSpec(二维、float32、可变长度)# 作用:让SavedModel记录RaggedTensor的输入签名,避免加载后调用报错concrete_func=module.grow.get_concrete_function(tf.RaggedTensorSpec(shape=[None,None],dtype=tf.float32))print("预构建的具体函数:",concrete_func)# 创建临时目录custom_module_path=tempfile.mkdtemp()# 保存模块:包含变量v、grow函数的计算图、RaggedTensor的输入签名tf.saved_model.save(module,custom_module_path)print(f"\n自定义模型已保存到:{custom_module_path}")# 加载保存的模块imported_module=tf.saved_model.load(custom_module_path)# 传入RaggedTensor调用grow函数ragged_input=tf.ragged.constant([[1.0,4.0,3.0],[2.0]],dtype=tf.float32)result=imported_module.grow(ragged_input)print("\n自定义模型调用结果:")print(result)INFO:tensorflow:Assets written to: /tmp/yyy/assets <tf.RaggedTensor [[100.0, 400.0, 300.0], [200.0]]>tf.Module的@tf.function函数默认是“动态跟踪”的,未指定输入签名时,SavedModel无法确定输入类型(密集张量/RaggedTensor);get_concrete_function+tf.RaggedTensorSpec预构建后,SavedModel会固化RaggedTensor的处理逻辑,加载后可直接调用。[1.0,4.0,3.0] × 100 → [100.0,400.0,300.0];[2.0] × 100 → [200.0]。# 低版本兼容:拆解RaggedTensorrt=tf.ragged.constant([[1.0,2.0],[3.0]])rt_values=rt.values# 所有元素值:[1.0,2.0,3.0]rt_row_splits=rt.row_splits# 行分割点:[0,2,3]# 保存/加载时传递values+row_splits,加载后用tf.RaggedTensor.from_row_splits重构ragged=True的Input层生成具体函数,无需手动构建;get_concrete_function(tf.RaggedTensorSpec)预构建,否则加载后调用RaggedTensor会报错。shape=[None, None]表示二维RaggedTensor,两个维度长度均可变;shape=[32, None]。保存后的SavedModel目录包含:
assets/:静态资源(如词汇表);variables/:模型权重(如CustomModule的变量v);saved_model.pb:计算图+签名(包含RaggedTensor的处理逻辑)。| 模型类型 | 保存关键步骤 | 加载后调用方式 |
|---|---|---|
| Keras模型 | Input层设置ragged=True,直接tf.saved_model.save | 直接传入RaggedTensor |
| 自定义tf.Module | 用get_concrete_function(tf.RaggedTensorSpec)预构建具体函数,再保存 | 传入符合RaggedTensorSpec的RaggedTensor |
这套方案是生产环境中部署“处理可变长度数据(文本、序列)”模型的标准流程,既保留RaggedTensor无冗余的优势,又能利用SavedModel实现模型的序列化和部署。