用于sklearn校准模型的Tensorflow Keras包装器存在的问题

时间:2020-06-30 18:45:28

标签: python tensorflow keras scikit-learn

我已将sklearn.calibration中的CalirasatedClassifierCV与Keras模型对象一起使用。我尝试为Keras使用sklearn包装器,但是没有用。当我运行scipt时,会出现错误消息:TypeError:无法腌制_thread.RLock对象。

脚本:

from tensorflow.keras.models import load_model
from keras.models import Sequential
#Tensorflow Keras wrapper for sklearn and Keras wrapper
def getModel():
 model = Sequential()
 model = load_model('/content/drive/My Drive/Colab Notebooks/CalibrRaquel/efficientnetB6-RMSprop-0.5- 
 best_model.h5', compile=False)

 model.compile(optimizer='RMSprop', loss='binary_crossentropy', metrics=['acc'])
 return model


# predict uncalibrated probabilities
   model = getModel()
   pred = model.predict(x_test)
   probs = model.predict(x_test)
   return probs

# predict calibrated probabilities
from sklearn.base import clone
import keras
from keras.datasets import mnist
from keras.wrappers.scikit_learn import KerasClassifier
from keras import backend as K

from sklearn.model_selection import GridSearchCV
def calibrated(x_train, x_test, y_train):
  # define model: using loaded model
  # define and fit calibration model
    KerasModel = KerasClassifier(build_fn=getModel(), batch_size=32, verbose=1)
    calibrated = CalibratedClassifierCV(KerasModel, method='sigmoid', cv=None)
    calibrated.fit(x_train, y_train,sample_weight=None)
    probs = calibrated.predict(x_test)
    print('model.predict')
    print(model.predict)
    print('model.predict_proba')
    print(model.predict_proba)
    return probs

 # uncalibrated predictions
from keras.models import Sequential
from sklearn.calibration import CalibratedClassifierCV, calibration_curve
yhat_uncalibrated = uncalibrated(x_test)

# calibrated predictions
yhat_calibrated = calibrated(x_train, x_test, y_train)

0 个答案:

没有答案