使用TensorFlow Tensor创建Pearson相关指标

发布于 2025-02-09 13:38:59 字数 526 浏览 2 评论 0 原文

我想使用张量张量创建Pearson相关系数指标。他们确实有一个TensorFlow概率软件包但这与当前版本的TensorFlow具有依赖性问题。恐怕这会导致Cuda破裂。 Pearson相关系数在TensorFlow中的任何独立实现都会有所帮助...

因此,我想要这样的东西:


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

y_true和y_pred将是相同维度的数字列表。

I wanted to create a pearson correlation coefficient metrics using tensorflow tensor. They do have a tensorflow probability package https://www.tensorflow.org/probability/api_docs/python/tfp/stats/correlation 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢好甜 2025-02-16 13:39:00

这很好:


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)

This works fine:


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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文