AI/tensorflow

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

교 향 2023. 1. 27. 18:29

https://stackoverflow.com/questions/63649503/attributeerror-functional-object-has-no-attribute-predict-proba

 

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

For printing the ROC curve with multiple models, I am facing this particular error. Help is needed from tensorflow.keras.models import load_model def dense(): return (load_model('DenseNet...

stackoverflow.com

 
model.predict_proba 가 먹히지 않아서 수동으로 list를 넣으려니 값이 이상했다.
 

결론은 매우 단순하게도 y_pred와 y_score를 헷갈렸기 때문.

 

이진분류에서 y_pred_list는 [0,1,1, ....] 이런식이지만

1(참)일 확률인 y_score_list는 [0.78, 0.25,0.34 ...] 이런식이다.

 

# from sklearn.metrics import roc_curve
# fpr, tpr, thresholds = roc_curve(y_test, rand_clf.predict_proba(X_test)[:,1])

 

 

fpr, tpr, thresholds = roc_curve(y_true_test, y_score_test)

 

와 같이 변경하였으며

 

y_score_test는
for image, label in test_ds.batch(1):

                           ...

의 반복문 안에서

y_score_val.append(model.predict_on_batch(image)[0][0])

 

를 추가해 주었다. ( [0][0]은 3차원을 1차원으로 뽑으려고 추가함 )

 

from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score

fpr, tpr, thresholds = roc_curve(y_true_val, y_score_val)

rand_score_val = roc_auc_score(y_true_val, y_score_val)
rand_score_val

# plt.plot([0,1], [0,1], "k--", "r+")
plt.plot([0,1], [0,1])
# plt.plot(fpr, tpr, label='DenseNet121')
plt.plot(fpr, tpr)
plt.xlabel('FPR')
plt.ylabel('TPR')
plt.title('Val Data - ROC curve(0119ver.)')
plt.text(0.75, 0, f"AUC score : {round(rand_score_val, 4)}")

plt.show()

https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html

 

sklearn.metrics.roc_curve

Examples using sklearn.metrics.roc_curve: Species distribution modeling Species distribution modeling Visualizations with Display Objects Visualizations with Display Objects Detection error tradeof...

scikit-learn.org

 

항상 공식문서를 참고하자!!!

공식문서에 대놓고

 

y_score   ndarray of shape (n_samples,)

Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).

 

라고 친절히 적혀있었다.