Python 如何计算张量流中的协方差?
我有一个问题,我不知道如何计算两个张量的协方差。我已经尝试了Python 如何计算张量流中的协方差?,python,tensorflow,covariance,Python,Tensorflow,Covariance,我有一个问题,我不知道如何计算两个张量的协方差。我已经尝试了contrib.metrics.streaming\u协方差。但是它总是返回0。一定有错误。您可以使用两个随机变量的协方差定义X和Y,以及预期值x0和y0: cov_xx=1/(N-1)*Sum_i((x_i-x0)^2) cov_yy=1/(N-1)*Sum_i((y_i-y0)^2) cov_xy=1/(N-1)*Sum_i((x_i-x0)*(y_i-y0)) 关键点是在这里估计x0和y0,因为您通常不知道概率分布。在许多情况下,
contrib.metrics.streaming\u协方差
。但是它总是返回0
。一定有错误。您可以使用两个随机变量的协方差定义X
和Y
,以及预期值x0
和y0
:
cov_xx=1/(N-1)*Sum_i((x_i-x0)^2)
cov_yy=1/(N-1)*Sum_i((y_i-y0)^2)
cov_xy=1/(N-1)*Sum_i((x_i-x0)*(y_i-y0))
关键点是在这里估计x0
和y0
,因为您通常不知道概率分布。在许多情况下,x_i
或y_i
的平均值分别被估计为x_0
或y_0
,即,估计分布是均匀的
然后,可以按如下方式计算协方差矩阵的元素:
import tensorflow as tf
x = tf.constant([1, 4, 2, 5, 6, 24, 15], dtype=tf.float64)
y = tf.constant([8, 5, 4, 6, 2, 1, 1], dtype=tf.float64)
cov_xx = 1 / (tf.shape(x)[0] - 1) * tf.reduce_sum((x - tf.reduce_mean(x))**2)
cov_yy = 1 / (tf.shape(x)[0] - 1) * tf.reduce_sum((y - tf.reduce_mean(y))**2)
cov_xy = 1 / (tf.shape(x)[0] - 1) * tf.reduce_sum((x - tf.reduce_mean(x)) * (y - tf.reduce_mean(y)))
with tf.Session() as sess:
sess.run([cov_xx, cov_yy, cov_xy])
print(cov_xx.eval(), cov_yy.eval(), cov_xy.eval())
with tf.Session() as sess:
sess.run([cov_xx, cov_yy, cov_xy])
print(cov_xx.eval(), cov_yy.eval(), cov_xy.eval())
cov = tf.constant([[cov_xx.eval(), cov_xy.eval()], [cov_xy.eval(),
cov_yy.eval()]])
print(cov.eval())
当然,如果需要矩阵形式的协方差,可以按如下方式修改最后一部分:
import tensorflow as tf
x = tf.constant([1, 4, 2, 5, 6, 24, 15], dtype=tf.float64)
y = tf.constant([8, 5, 4, 6, 2, 1, 1], dtype=tf.float64)
cov_xx = 1 / (tf.shape(x)[0] - 1) * tf.reduce_sum((x - tf.reduce_mean(x))**2)
cov_yy = 1 / (tf.shape(x)[0] - 1) * tf.reduce_sum((y - tf.reduce_mean(y))**2)
cov_xy = 1 / (tf.shape(x)[0] - 1) * tf.reduce_sum((x - tf.reduce_mean(x)) * (y - tf.reduce_mean(y)))
with tf.Session() as sess:
sess.run([cov_xx, cov_yy, cov_xy])
print(cov_xx.eval(), cov_yy.eval(), cov_xy.eval())
with tf.Session() as sess:
sess.run([cov_xx, cov_yy, cov_xy])
print(cov_xx.eval(), cov_yy.eval(), cov_xy.eval())
cov = tf.constant([[cov_xx.eval(), cov_xy.eval()], [cov_xy.eval(),
cov_yy.eval()]])
print(cov.eval())
要验证TensorFlow方式的元素,可以使用numpy进行检查:
import numpy as np
x = np.array([1,4,2,5,6, 24, 15], dtype=float)
y = np.array([8,5,4,6,2,1,1], dtype=float)
pc = np.cov(x,y)
print(pc)
函数
contrib.metrics.streaming\u convariance
创建一个update\u op
操作,该操作更新基础变量并返回更新后的协方差。因此,您的代码应该是:
x = tf.constant([1, 4, 2, 5, 6, 24, 15], dtype=tf.float32)
y = tf.constant([8, 5, 4, 6, 2, 1, 1], dtype=tf.float32)
z, op = tf.contrib.metrics.streaming_covariance(x,y)
with tf.Session() as sess:
tf.global_variables_initializer().run()
tf.local_variables_initializer().run()
sess.run([op])
print(sess.run([z]))
#Output
[-17.142859]
您还可以尝试轻松计算相关性或协方差
x = tf.random_normal(shape=(100, 2, 3))
y = tf.random_normal(shape=(100, 2, 3))
# cov[i, j] is the sample covariance between x[:, i, j] and y[:, i, j].
cov = tfp.stats.covariance(x, y, sample_axis=0, event_axis=None)
# cov_matrix[i, m, n] is the sample covariance of x[:, i, m] and y[:, i, n]
cov_matrix = tfp.stats.covariance(x, y, sample_axis=0, event_axis=-1)