Creating Pearson Correlation metrics using Tensorflow Tensor

I wanted to create a pearson correlation coefficient metrics using tensorflow tensor. They do have a tensorflow probability package tfp.stats.correlation  |  TensorFlow Probability but this have dependency issues with the current version of tensorflow. I am afraid that this will cause the cuda to break. Any standalone implementation of pearson correlation coefficient metrics in tensorflow will help…

So I want something like this:


def p_corr(y_true, y_pred):
    # calculate the pearson correlation coefficient here
    return pearson_correlation_coefficient

Here y_true and y_pred will be a list of numbers of same dimension.

I figured it out:


from keras import backend as K

def pearson_r(y_true, y_pred):
    # use smoothing for not resulting in NaN values
    # pearson correlation coefficient
    # https://github.com/WenYanger/Keras_Metrics
    epsilon = 10e-5
    x = y_true
    y = y_pred
    mx = K.mean(x)
    my = K.mean(y)
    xm, ym = x - mx, y - my
    r_num = K.sum(xm * ym)
    x_square_sum = K.sum(xm * xm)
    y_square_sum = K.sum(ym * ym)
    r_den = K.sqrt(x_square_sum * y_square_sum)
    r = r_num / (r_den + epsilon)
    return K.mean(r)
1 Like