how to compute covariance in tensorflow?

雨燕双飞 提交于 2019-12-24 10:33:17

问题


I have a problem that I don't know how to compute the covariance of two tensor. I have tried the contrib.metrics.streaming_covariance. But is always returns 0. There must be some errors.


回答1:


You could use the definition of the covariance of two random variables X and Y with the expected values x0 and 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))

The crucial point is to estimate x0 and y0 here, since you normally do not know the probability distribution. In many cases, the mean of the x_i or y_i is estimated to be x_0 or y_0, respectively, i.e. the distribution is estimated to be uniform.

Then you can compute the elements of the covariance matrix as follows:

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())

Of course, if you need the covariance in a matrix form, you can modify the last part as follows:

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())

To verify the elements of the TensorFlow way, you can check with 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)



回答2:


The function contrib.metrics.streaming_covariance creates an update_op operation, which updates the underlying variables and returns the updated covariance. So your code should be:

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]



回答3:


You can also try tensorflow probability for easy calculation of correlation or covariance.

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)


来源:https://stackoverflow.com/questions/45874928/how-to-compute-covariance-in-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!