Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow symmetric matrix

I want to create a symmetric matrix of n*n and train this matrix in TensorFlow. Effectively I should only train (n+1)*n/2 parameters. How should I do this?

I saw some previous threads which suggest do the following:

X = tf.Variable(tf.random_uniform([d,d], minval=-.1, maxval=.1, dtype=tf.float64))

X_symm = 0.5 * (X + tf.transpose(X))

However, this means I have to train n*n variables, not n*(n+1)/2 variables.

Even there is no function to achieve this, a patch of self-written code would help!

Thanks!

like image 750
user348207 Avatar asked Oct 12 '17 20:10

user348207


2 Answers

You can use tf.matrix_band_part(input, 0, -1) to create an upper triangular matrix from a square one, so this code would allow you to train on n(n+1)/2 variables although it has you create n*n:

X = tf.Variable(tf.random_uniform([d,d], minval=-.1, maxval=.1, dtype=tf.float64))
X_upper = tf.matrix_band_part(X, 0, -1)
X_symm = 0.5 * (X_upper + tf.transpose(X_upper))
like image 78
gdelab Avatar answered Nov 04 '22 07:11

gdelab


Referring to answer of gdelab: in Tensorflow 2.x, you have to use following code.

X_upper = tf.linalg.band_part(X, 0, -1)
like image 25
Suman Avatar answered Nov 04 '22 09:11

Suman