Tensorflow 2 Keras的Pearson相关度量

问题描述

我有一个模型,每个样本有8个输出。我想为每个样本的皮尔逊相关性计算,然后计算样本的皮尔逊值的平均值。然后,我想将其作为模型的度量插入。现在我有了这个功能

def tf_pearson(x,y):    
    mx = tf.math.reduce_mean(input_tensor=x)
    my = tf.math.reduce_mean(input_tensor=y)
    xm,ym = x-mx,y-my
    r_num = tf.math.reduce_mean(input_tensor=tf.multiply(xm,ym))        
    r_den = tf.math.reduce_std(xm) * tf.math.reduce_std(ym)
    return  r_num / r_den

我认为应该立即计算所有样本的皮尔逊值。

我想做这样的事情:

def tf_pearson(x,y):
    print(x.shape)
    print(y.shape)    
    mx = tf.math.reduce_mean(input_tensor=x,axis=1)
    my = tf.math.reduce_mean(input_tensor=y,axis=1)
    print(mx.shape)
    print(my.shape)  
    xm,ym),axis=1)        
    r_den = tf.math.reduce_std(xm,axis=1) * tf.math.reduce_std(ym,axis=1)
    return   tf.math.reduce_mean(input_tensor=r_num / r_den)

但是由于张量一维形状为无,所以reduce_mean总是减小。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)