M.Zhu
M.Zhu

Reputation: 152

how to get covariance matrix in tensorflow?

How could I get covariance matrix in tensorflow? Like numpy.cov() in numpy.

For example, I want to get covariance matrix of tensor A, now I have to use numpy instead

    A = sess.run(model.A, feed)
    cov = np.cov(np.transpose(A))

Is there anyway to get cov by tensorflow instead of numpy?

It is differnet from the problem how to compute covariance in tensorflow, where their problem is to compute covariance for two vector, while mine is to compute covariance matrix of a matrix(a 2D tensor) effectively using tensorflow API

Upvotes: 10

Views: 9118

Answers (5)

Batta
Batta

Reputation: 465

This is months late but anyway posting for completeness.

import numpy as np
import tensorflow as tf

def tf_cov(x):
    mean_x = tf.reduce_mean(x, axis=0, keep_dims=True)
    mx = tf.matmul(tf.transpose(mean_x), mean_x)
    vx = tf.matmul(tf.transpose(x), x)/tf.cast(tf.shape(x)[0], tf.float32)
    cov_xx = vx - mx
    return cov_xx

data = np.array([[1., 4, 2], [5, 6, 24], [15, 1, 5], [7,3,8], [9,4,7]])

with tf.Session() as sess:
    print(sess.run(tf_cov(tf.constant(data, dtype=tf.float32))))

    
## validating with numpy solution
pc = np.cov(data.T, bias=True)
print(pc)

Upvotes: 13

Heisenberger
Heisenberger

Reputation: 11

We can use tfp aka tensorflow-probability to computer cov matrix:

import tensorflow-probability as tfp

x=tf.random.normal(shape=(3,3))
cov = tfp.stats.covariance(x)
## which are same as:
np_cov = np.cov(tf.transpose(x_zero),bias=True)

Upvotes: 0

Wabinab
Wabinab

Reputation: 39

Following up on @Souradeep Nanda, if you experiment with it you'll find that tfp.stats.covariance is only have the value (elementwise) for np.cov(..., rowvar=False), so you will have to multiply by 2 after the calculation. (This applies for v0.11.1, tested on 2x2 matrix).

For 3x3 matrix, the values are NOT EQUIVALENT so perhaps you might want to stay using np.cov. This applies too if you're not using rowvar=False for np.cov() version. I am not sure why.

Upvotes: 0

Souradeep Nanda
Souradeep Nanda

Reputation: 3288

Answering from 2019. Tensorflow probability now supports effortless correlation and covariance.

https://www.tensorflow.org/probability/api_docs/python/tfp/stats/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)

Upvotes: 3

ViniciusArruda
ViniciusArruda

Reputation: 990

Equivalent to np.cov(data):

import tensorflow as tf
import numpy as np

data = np.array([[1., 4, 2], [5, 6, 24], [15, 1, 5], [7,3,8], [9,4,7]])

def tf_conv(x):
    x = x - tf.expand_dims(tf.reduce_mean(x, axis=1), 1)
    fact = tf.cast(tf.shape(x)[1] - 1, tf.float32)
    return tf.matmul(x, tf.conj(tf.transpose(x))) / fact

with tf.Session() as sess:
    print(sess.run(tf_cov(tf.constant(data, dtype=tf.float32))))

Upvotes: 0

Related Questions