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

TensorFlow中model.predict报错:缺少steps_per_execution参数如何解决?

解决model.predict()触发steps_per_execution缺失的TypeError

问题根源

这个报错多和TensorFlow版本兼容性、模型训练时的API使用有关——fit_generator在TensorFlow 2.x中已被弃用,旧API的训练逻辑可能导致模型内部执行函数的签名异常,进而在调用predict()时触发参数缺失错误。

可行解决方案

1. 替换fit_generator为原生model.fit()

直接使用TF2.x支持的model.fit()重新训练模型,它原生兼容生成器输入,能规避旧API的遗留问题:

# 假设你的训练生成器为train_generator,验证生成器为val_generator
model.fit(
    train_generator,
    epochs=你的训练轮数,
    validation_data=val_generator,
    verbose=1
)

2. 显式传入steps_per_execution参数

若暂时无法重新训练,可在调用predict()时手动指定该参数(通常设为1或与批量大小匹配的值):

predictions = model.predict(
    test_preprocessed_images,
    batch_size=1,
    verbose=1,
    steps_per_execution=1
)

3. 重新保存并加载模型

如果是加载已训练模型时出现问题,确保用标准方式保存模型后重新加载:

# 训练完成后保存模型
model.save("skin_disease_cnn_model.h5")
# 重新加载模型
from tensorflow.keras.models import load_model
model = load_model("skin_disease_cnn_model.h5")
# 执行预测
predictions = model.predict(test_preprocessed_images, batch_size=1, verbose=1)

4. 调整TensorFlow版本

若以上方法无效,尝试切换到稳定版本:

  • 当前用TF2.6+的话,可降级到TF2.5;
  • 用旧版本的话,升级到TF2.15等稳定发行版

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 22:10:34