首页 > 解决方案 > 为什么 TensorFlow 和 Scipy 之间的 Pearson 相关性不同

问题描述

我以两种方式计算 Pearson 相关性:


在 Tensorflow 中,我使用以下指标:

tf.contrib.metrics.streaming_pearson_correlation(y_pred, y_true)

当我根据测试数据评估我的网络时,我得到了以下结果:

损失 = 0.5289223349094391

皮尔逊 = 0.3701728057861328

(损失为mean_squared_error


然后我预测测试数据并使用 Scipy 计算相同的指标:

import scipy.stats as measures
per_coef = measures.pearsonr(y_pred, y_true)[0]
mse_coef = np.mean(np.square(np.array(y_pred) - np.array(y_true)))

我得到以下结果:

皮尔逊 = 0.5715300096509959

MSE = 0.5289223312665985


这是一个已知问题吗?正常吗?

最小、完整和可验证的示例

import tensorflow as tf
import scipy.stats as measures

y_pred = [2, 2, 3, 4, 5, 5, 4, 2]
y_true = [1, 2, 3, 4, 5, 6, 7, 8]

## Scipy
val2 = measures.pearsonr(y_pred, y_true)[0]
print("Scipy's Pearson = {}".format(val2))

## Tensorflow
logits = tf.placeholder(tf.float32, [8])
labels = tf.to_float(tf.Variable(y_true))

acc, acc_op = tf.contrib.metrics.streaming_pearson_correlation(logits,labels)

sess = tf.Session()
sess.run(tf.local_variables_initializer())
sess.run(tf.global_variables_initializer())
sess.run(acc, {logits:y_pred})
sess.run(acc_op, {logits:y_pred})

print("Tensorflow's Pearson:{}".format(sess.run(acc,{logits:y_pred})))

标签: pythonpython-3.xtensorflowscipymetrics

解决方案


在您给出的最小可验证示例中,y_predy_true是整数列表。在scipy.stats.measures.pearsonr 源代码的第一行,您将看到输入被转换为 numpy 数组x = np.asarray(x)。我们可以通过以下方式查看这些数组的结果数据类型:

print(np.asarray(y_pred).dtype)  # Prints 'int64'

在除以两个int64数字时,SciPy 使用float64精度,而 TensorFlow 将float32在上面的示例中使用精度。即使对于单个部门,差异也可能非常大:

>>> '%.15f' % (8.5 / 7)
'1.214285714285714'
>>> '%.15f' % (np.array(8.5, dtype=np.float32) / np.array(7, dtype=np.float32))
'1.214285731315613'
>>> '%.15f' % (np.array(8.5, dtype=np.float32) / np.array(7, dtype=np.float32) - 8.5 / 7)
'0.000000017029899'

您可以通过对 和 使用float32精度来获得 SciPyy_pred和TensorFlow 相同的结果y_true

import numpy as np
import tensorflow as tf
import scipy.stats as measures

y_pred = np.array([2, 2, 3, 4, 5, 5, 4, 2], dtype=np.float32)
y_true = np.array([1, 2, 3, 4, 5, 6, 7, 8], dtype=np.float32)

## Scipy
val2 = measures.pearsonr(y_pred, y_true)[0]
print("Scipy's Pearson: \t\t{}".format(val2))

## Tensorflow
logits = tf.placeholder(tf.float32, [8])
labels = tf.to_float(tf.Variable(y_true))

acc, acc_op = tf.contrib.metrics.streaming_pearson_correlation(logits,labels)

sess = tf.Session()
sess.run(tf.local_variables_initializer())
sess.run(tf.global_variables_initializer())
sess.run(acc, {logits:y_pred})
sess.run(acc_op, {logits:y_pred})

print("Tensorflow's Pearson: \t{}".format(sess.run(acc,{logits:y_pred})))

印刷

Scipy's Pearson:        0.38060760498046875
Tensorflow's Pearson:   0.38060760498046875

SciPy 和 TensorFlow 计算之间的差异

在您报告的测试分数中,差异很大。我查看了源代码,发现了以下差异:

1.更新操作

结果tf.contrib.metrics.streaming_pearson_correlation不是无状态的。它返回相关系数 op 以及update_op新传入数据的 a。如果在使用实际调用系数 op 之前使用不同的数据调用更新 op y_pred,它将给出完全不同的结果:

sess.run(tf.global_variables_initializer())

for _ in range(20):
    sess.run(acc_op, {logits: np.random.randn(*y_pred.shape)})

print("Tensorflow's Pearson: \t{}".format(sess.run(acc,{logits:y_pred})))

印刷

Scipy's Pearson:        0.38060760498046875
Tensorflow's Pearson:   -0.0678008571267128

2.不同的公式

科学:

TensorFlow:

虽然数学上相同,但 TensorFlow 中相关系数的计算是不同的。它使用 (x, x)、(x, y) 和 (y, y) 的样本协方差来计算相关系数,这会引入不同的舍入误差。


推荐阅读