user348207
user348207

Reputation: 87

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!

Upvotes: 7

Views: 2734

Answers (3)

Tobbe
Tobbe

Reputation: 41

gdelab's answer is correct and will work, since a neural network can adjust the 0.5 factor by itself. I aimed for a solution, where the neural network actually only has (n+1)*n/2 output neurons. The following function transforms these into a symmetric matrix:

def create_symmetric_matrix(x,n):
    x_rev = tf.reverse(x[:, n:], [1])
    xc = tf.concat([x, x_rev], axis=1)
    x_res = tf.reshape(xc, [-1, n, n])
    x_upper_triangular = tf.linalg.band_part(x_res, 0, -1)
    x_lower_triangular = tf.linalg.set_diag( tf.transpose(x_upper_triangular, perm=[0, 2, 1]), tf.zeros([tf.shape(x)[0], n], dtype=tf.float32))
    return x_upper_triangular + x_lower_triangular

with x as a vector of rank [batch,n*(n+1)/2] and n as the rank of the output matrix. The code is inspired by tfp.math.fill_triangular.

Upvotes: 0

Suman
Suman

Reputation: 55

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

X_upper = tf.linalg.band_part(X, 0, -1)

Upvotes: 1

gdelab
gdelab

Reputation: 6220

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

Upvotes: 8

Related Questions