You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow 2.15.0中model.fit报错:无法序列化Ellipsis求助

TypeError: Cannot serialize object Ellipsis (TensorFlow 2.15.0 + VGG16预训练模型)

问题重现

执行model.fit时触发错误:

TypeError: Cannot serialize object Ellipsis of type <class 'ellipsis'>.
To be serializable, a class must implement the 'get_config()' method.

环境:TensorFlow 2.15.0,基于Kaggle猫狗分类数据集,使用预训练VGG16做特征提取+数据增强。

问题原因

你直接将keras.applications.vgg16.preprocess_input作为普通函数调用嵌入模型流程中,这个函数不是Keras Layer的子类。当ModelCheckpoint回调尝试序列化模型时,无法识别这个普通函数节点,进而抛出序列化错误。

解决方案

把preprocess_input包装成Keras的Lambda层,让它成为模型的可序列化组件。

修改核心代码片段

将原代码中:

x = keras.applications.vgg16.preprocess_input(x)

替换为:

x = keras.layers.Lambda(keras.applications.vgg16.preprocess_input)(x)

完整修改后的模型构建代码

inputs = keras.Input(shape=(180, 180, 3))
x = data_augmentation(inputs)
# 用Lambda层包裹预处理函数,使其可序列化
x = keras.layers.Lambda(keras.applications.vgg16.preprocess_input)(x)
x = conv_base(x)
x = keras.layers.Flatten()(x)
x = keras.layers.Dense(256)(x)
x = keras.layers.Dropout(0.5)(x)
outputs = keras.layers.Dense(1, activation="sigmoid")(x)

model = keras.Model(inputs, outputs)

验证效果

修改后重新运行model.fit,ModelCheckpoint将能正常序列化并保存模型,原错误会被解决。

内容的提问来源于stack exchange,提问作者Qwerty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 07:12:03