我定义了一个自定义损失函数,如下所示:
def MCC_loss(true,y_pred):
mcc = matthews_corrcoef(true,y_pred)
return(keras.backend.abs(1-mcc))
然后我尝试将其输入到我的模型中,但是最后却给了我错误。
metrics = [
keras.metrics.FalseNegatives(name="fn"),
keras.metrics.FalsePositives(name="fp"),
keras.metrics.TrueNegatives(name="tn"),
keras.metrics.TruePositives(name="tp"),
keras.metrics.Precision(name="precision"),
keras.metrics.Recall(name="recall"),
]
model2.compile(
optimizer=keras.optimizers.Adam(1e-5), loss=MCC_loss, metrics=metrics
)
callbacks = [keras.callbacks.ModelCheckpoint("fraud_model_at_epoch_{epoch}.h5")]
class_weight = {0: 1, 1: 2.5}
model2.fit(
train_features,
train_labels,
batch_size=2048,
epochs=10,
verbose=2,
callbacks=callbacks,
validation_data=(val_features, val_labels),
class_weight=class_weight,
)
ValueError:预期的类似数组的数组(数组或非字符串序列),得到
我不确定如何解决此问题。有人吗?