'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!



Solution 1:[1]

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

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

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Suman
Solution 2 Tobbe