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

model.predict_classes已废弃,应该使用什么替代方法?

model.predict_classes 废弃后的替代实现方案

你遇到的NameError是因为model.predict_classes()方法已被TensorFlow官方移除,赋值语句执行失败导致y_pred_nn变量未被定义,可根据你的模型输出激活类型选择对应替代代码:

  • 二分类场景(输出层用sigmoid激活)
    原方法默认以0.5为阈值划分正负类,替代代码如下:

    y_pred_nn = (model.predict(X_test) > 0.5).astype("int32")
    
  • 多分类场景(输出层用softmax激活)
    原方法默认取概率最高的类别索引作为预测结果,替代代码如下:

    import numpy as np
    y_pred_nn = np.argmax(model.predict(X_test), axis=-1)
    

如果使用TensorFlow 2.10及以上版本,也可以直接调用TensorFlow内置的argmax方法实现,无需额外引入numpy:

import tensorflow as tf
y_pred_nn = tf.argmax(model.predict(X_test), axis=-1).numpy()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 19:12:03