【问题标题】:How to specify spearman rank correlation as a loss function in keras?如何在keras中将spearman等级相关指定为损失函数?
【发布时间】:2021-05-09 07:39:28
【问题描述】:

我想写一个损失函数来最大化 keras 中两个向量之间的 spearman 等级相关性。不幸的是我找不到现有的实现,也找不到计算keras中向量等级的好方法,因此我可以使用公式自己实现它

def rank_correlation(y_true, y_pred):
    pass

model = tensorflow.keras.Sequential()
#### More model code
model.compile(loss=rank_correlation)

谁能帮我实现rank_correlation

【问题讨论】:

    标签: tensorflow keras tensorflow2.0 tf.keras


    【解决方案1】:

    您可以尝试如下方式,referenced

    from scipy.stats import spearmanr
    def compute_spearmanr(y, y_pred):
        spearsum = 0
        cnt = 0 
        for col in range(y_pred.shape[1]):
            v = spearmanr(y_pred[:,col], y[:,col]).correlation
            if np.isnan(v):
                continue
            spearsum += v
            cnt += 1
        res = spearsum / cnt
        return res
    
    a = np.array([[2., 1., 2., 3.],[3., 3., 4., 5.]] )
    b = np.array([[1., 0., 0., 3.], [1., 0., 3., 3.]])
    compute_spearmanr(a, b) 
    0.9999999999999999
    

    【讨论】:

      猜你喜欢
      • 2018-03-19
      • 2017-05-11
      • 2019-07-16
      • 2018-10-06
      • 2017-08-17
      • 1970-01-01
      • 2020-11-26
      • 2020-12-19
      • 1970-01-01
      相关资源
      最近更新 更多