4

predict_proba returns the error in the neural network

i saw the example on this link 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

I am using Tensorflow Version: 2.6.0

Code:

#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)

Error:

---------------------------------------------------------------------------
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'

Edit: i need sklearn's like predict_proba output it is needed for visualization

skplt.metrics.plot_precision_recall_curve(y_test['cEXT'].values, y_prob)
plt.title('Precision-Recall Curve - cEXT')
plt.show()
5
  • 1
    What is your question? The error is clear. Tensorflow is not scikit-learn. Commented Aug 29, 2021 at 9:02
  • wants to print probability like scikit-learn's predict_proba Commented Aug 29, 2021 at 9:04
  • but i saw the example on deep learning too machinelearningmastery.com/… Commented Aug 29, 2021 at 9:05
  • 2
    @JunedAnsari predict_proba is deprecated. Instead, simply use predict to get the probabilities. See my answer here for more details and let me know if it helps. stackoverflow.com/a/67467084/9215780 Commented Aug 29, 2021 at 14:15
  • Huh, my apologies. I wasn't aware old keras versions had this method. Commented Aug 29, 2021 at 20:03

2 Answers 2

7

Use this code instead

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

predict_classes=np.argmax(predict_prob,axis=1)
Sign up to request clarification or add additional context in comments.

Comments

1

New Version might not have predict_proba method so i have creadted my own using .predict method

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 

2 Comments

Note, It's NOT the solution. The mode. predict already gives the probabilities. The new version is more precise. In your above code snippet, it doesn't make sense to compute twice the probabilities of the same class.
@M.Innat, i know it returns probability, but if you compare it with sklearns predict_prob you will see the difference. sklearns prdict_prob will return two output like true class probability and the false class probability it was needed for the visualization skplt.metrics.plot_precision_recall_curve

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.