【问题标题】:Join metrics of every output in Keras (in multiple output)加入 Keras 中每个输出的指标(在多个输出中)
【发布时间】:2020-05-22 16:03:21
【问题描述】:

我正在研究 Keras 中的多输出模型。我已经实现了两个自定义指标 auroc 和 auprc,它们被传递给 Keras 模型的 compile 方法:

def auc(y_true, y_pred, curve='PR'):
    score, up_opt = tf.compat.v1.metrics.auc(y_true, y_pred, curve=curve, summation_method="careful_interpolation")
    K.get_session().run(tf.local_variables_initializer())
    with tf.control_dependencies([up_opt]):
        score = tf.identity(score)
    return score

def auprc(y_true, y_pred):
    return auc(y_true, y_pred, curve='PR')

def auroc(y_true, y_pred):
    return auc(y_true, y_pred, curve='ROC')

mlp_model.compile(loss=...,
                    optimizer=...,
                    metrics=[auprc, auroc])

使用这种方法,我获得了每个输出的 auprc/auroc 值,但是,为了使用贝叶斯优化器优化我的超参数,我需要一个指标(例如:每个输出的 auprc 的平均值或总和)。我不知道如何将我的指标合并为一个指标。

编辑:这里是期望结果的示例

现在每个时期都会打印以下指标:

out1_auprc: 0.0267 - out2_auprc: 0.0277 - out3_auprc: 0.0294

out1out2out3 是我的神经网络输出,我希望获得类似的东西:

average_auprc: 0.0279 - out1_auprc: 0.0267 - out2_auprc: 0.0277 - out3_auprc: 0.0294

我正在使用 Keras Tuner 进行贝叶斯优化。

感谢任何帮助,谢谢。

【问题讨论】:

    标签: python tensorflow machine-learning keras deep-learning


    【解决方案1】:

    我覆盖了创建自定义回调的问题

    class MergeMetrics(Callback):
    
        def __init__(self,**kargs):
            super(MergeMetrics,self).__init__(**kargs)
    
        def on_epoch_begin(self,epoch, logs={}):
            return
    
        def on_epoch_end(self, epoch, logs={}):
            logs['merge_metrics'] = 0.5*logs["y1_mse"]+0.5*logs["y2_mse"]
    

    我使用此回调来合并来自 2 个不同输出的 2 个指标。例如,我使用了一个简单的问题,但您可以轻松地将其集成到您的问题中并将其与验证集集成

    这是一个虚拟的例子

    X = np.random.uniform(0,1, (1000,10))
    y1 = np.random.uniform(0,1, 1000)
    y2 = np.random.uniform(0,1, 1000)
    
    
    inp = Input((10))
    x = Dense(32, activation='relu')(inp)
    out1 = Dense(1, name='y1')(x)
    out2 = Dense(1, name='y2')(x)
    m = Model(inp, [out1,out2])
    m.compile('adam','mae', metrics='mse')
    
    
    checkpoint = MergeMetrics()
    m.fit(X, [y1,y2], epochs=10, callbacks=[checkpoint])
    

    打印输出

    loss: ..... y1_mse: 0.0863 - y2_mse: 0.0875 - merge_metrics: 0.0869
    

    【讨论】:

      猜你喜欢
      • 2018-04-08
      • 2019-03-06
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 2019-02-25
      • 1970-01-01
      • 2018-08-20
      相关资源
      最近更新 更多