我想使用GridSearchCV相对于指标f1-score来调整我的角膜神经网络,因为我在数据集中存在很高的失衡。这是一个二进制分类问题。这是我尝试过的
def f1(y_true, y_pred):
y_pred = K.round(y_pred)
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
# tn = K.sum(K.cast((1-y_true)*(1-y_pred), 'float'), axis=0)
fp = K.sum(K.cast((1-y_true)*y_pred, 'float'), axis=0)
fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
p = tp / (tp + fp + K.epsilon())
r = tp / (tp + fn + K.epsilon())
f1 = 2*p*r / (p+r+K.epsilon())
f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
return K.mean(f1)
def create_model():
model = Sequential()
model.add(Dense(899, activation = 'relu', input_dim = 899))
model.add(Dense(100, activation = 'relu'))
model.add(Dense(1, activation = 'sigmoid'))
model.compile(optimizer= 'adamax', loss = 'binary_crossentropy', metrics = [f1])
return model
param_grid = dict(batch_size = batch_size, epochs = epochs,class_weight = class_weight)
grid = GridSearchCV(estimator = model, param_grid = param_grid, n_jobs =7, cv = 2, scoring = f1 )
但是我得到了错误
**
score = scorer(estimator, X_test, y_test)
TypeError: f1() takes 2 positional arguments but 3 were given
"""
**
所以看起来我也需要创建一个得分手?如何正确地做到这一点?
更新:
这似乎可以解决它,我认为它可以按预期工作
from sklearn.metrics import f1_score
from sklearn.metrics import make_scorer
f1_scorer = make_scorer(f1_score)
然后我将其传递给GridsearchCV函数
grid = GridSearchCV(estimator = model, param_grid = param_grid, n_jobs =7, cv = 2, scoring = f1_scorer )