1

我正在尝试使用 scikit-learn GridSearchCV 和 KerasClassifier 进行网格搜索,以解决具有 346 个标签的多标签分类问题。我正在尝试根据自定义指标评估模型。但我总是遇到以下错误。我的训练数据大小是 5334。

ValueError:操作数无法与形状一起广播 (5334,346) (5334,)

def my_custom_scorer(y_true,y_pred):
   true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
   possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
   recall = true_positives / (possible_positives + K.epsilon())
   return recall    


  custom_scorer = make_scorer(my_custom_scorer, greater_is_better=True)

   def create_model(learn_rate = 0.01): 
    # create model
    model = Sequential()
    model.add(Dense(100, activation = 'sigmoid',input_dim=4))

    model.add(Dense(346, activation='sigmoid'))
    # Compile model

    optimizer = SGD(lr=learn_rate, momentum=0)

    model.compile(optimizer=optimizer,
          loss=custom_loss(weights),
          metrics=['accuracy'])

   return model

model = KerasClassifier(build_fn=create_model,verbose=0)

learn_rate = [0.001, 0.01, 0.1]

batch_size = [10]
epochs = [1] 

param_grid = dict(epochs=epochs,batch_size=batch_size,learn_rate=learn_rate)
grid = GridSearchCV(estimator=model, 
param_grid=param_grid,n_jobs=1,scoring=custom_scorer)

grid_result = grid.fit(X_train, Y_train)

print("Best: %f using %s" % (grid_result.best_score_, 
grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
   print("%f (%f) with: %r" % (mean, stdev, param))
4

0 回答 0