【问题标题】:AttributeError: 'Sequential' object has no attribute 'predict_proba'AttributeError:“顺序”对象没有属性“predict_proba”
【发布时间】:2021-10-28 10:53:56
【问题描述】:

predict_proba 返回神经网络中的误差

我在此链接上看到了示例https://machinelearningmastery.com/how-to-make-classification-and-regression-predictions-for-deep-learning-models-in-keras/

https://faroit.com/keras-docs/1.0.0/models/sequential/#the-sequential-model-api

我使用的是 TensorFlow 版本:2.6.0

代码:

#creating the object (Initializing the ANN)
import tensorflow as tf
from tensorflow import keras
LAYERS = [
         tf.keras.layers.Dense(50, activation="relu", input_shape=X_train.shape[1:]),
         tf.keras.layers.LeakyReLU(),
         tf.keras.layers.Dense(25, activation="relu"),
         tf.keras.layers.Dense(10, activation="relu"),
         tf.keras.layers.Dense(5, activation="relu"),
         tf.keras.layers.Flatten(),
         tf.keras.layers.Dense(1, activation='sigmoid')
]

LOSS = "binary_crossentropy"
OPTIMIZER = tf.keras.optimizers.Adam(learning_rate=1e-3)

model_cEXT = tf.keras.models.Sequential(LAYERS)
model_cEXT.compile(loss=LOSS , optimizer=OPTIMIZER, metrics=['accuracy'])

EPOCHS = 100

checkpoint_cb = tf.keras.callbacks.ModelCheckpoint("model_cEXT.h5", save_best_only=True)
early_stopping_cb = tf.keras.callbacks.EarlyStopping(patience=10, restore_best_weights=True)
tensorboard_cb = tf.keras.callbacks.TensorBoard(log_dir="logs")
CALLBACKS = [checkpoint_cb, early_stopping_cb, tensorboard_cb]

model_cEXT.fit(X_train, y_train['cEXT'], epochs = EPOCHS, validation_data=(X_test, y_test['cEXT']), callbacks = CALLBACKS)

model_cEXT.predict_proba(X_test)

错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-72-8f06353cf345> in <module>()
----> 1 model_cEXT.predict_proba(X_test)

AttributeError: 'Sequential' object has no attribute 'predict_proba'

编辑: 我需要 sklearn 之类的 predict_proba 输出,这是可视化所需要的

skplt.metrics.plot_precision_recall_curve(y_test['cEXT'].values, y_prob)
plt.title('Precision-Recall Curve - cEXT')
plt.show()

【问题讨论】:

  • 你有什么问题?错误很明显。 TensorFlow 不是 scikit-learn。
  • 想像 scikit-learn 的 predict_proba 一样打印概率
  • 但是我也看到了深度学习的例子machinelearningmastery.com/…
  • @JunedAnsari predict_proba 已弃用。相反,只需使用predict 来获取概率。请在此处查看我的答案以获取更多详细信息,如果有帮助,请告诉我。 stackoverflow.com/a/67467084/9215780
  • 嗯,我很抱歉。我不知道旧的 keras 版本有这种方法。

标签: tensorflow deep-learning


【解决方案1】:

新版本可能没有 predict_proba 方法,所以我使用 .predict 方法创建了自己的方法

def predict_prob(number):
  return [number[0],1-number[0]]

y_prob = np.array(list(map(predict_prob, model_cEXT.predict(X_test))))
y_prob 

【讨论】:

  • 注意,这不是解决方案。 mode. predict 已经给出了概率。新版本更精确。在上面的代码 sn-p 中,计算同一类概率的两倍是没有意义的。
  • @M.Innat,我知道它返回概率,但如果你将它与 sklearns predict_prob 进行比较,你会看到不同之处。 sklearns prdict_prob 将返回两个输出,如真类概率和可视化所需的假类概率 skplt.metrics.plot_precision_recall_curve
【解决方案2】:

改用此代码

predict_prob=model.predict([testa,testb])

predict_classes=np.argmax(predict_prob,axis=1)

【讨论】:

    猜你喜欢
    • 2018-04-28
    • 1970-01-01
    • 2020-12-18
    • 2022-01-04
    • 2021-10-23
    • 2020-01-29
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多