TensorFlow Create a Random Orthogonal Matrix: A Beginner Guide – TensorFLow Tutorial

By | October 9, 2020

We have learned how to create a random orthogonal matrix by scipy in python. In this tutorial, we will introduce how to creat a random orthogonal matrix using tensorflow.

To create a random orthogonal matrix using scipy. we can read:

Python Create a Random Orthogonal Matrix: A Beginner Guide – Python Tutorial

How to create a random orthogonal matrix using tensorflow?

We can use tf.orthogonal_initializer() to implement it.

Here is an example:

import tensorflow as tf
import numpy as np   

w_ = tf.Variable(tf.orthogonal_initializer()([5, 10]), name = 'weight_')

where w_ is a random orthogonal matrix.

In order to evaluate whether w_ is an orthogonal matrix or not, we can do as follows:

s_ = tf.matmul(w_, w_, transpose_b = True)

orthogonal matrix feature

Then we can output w_ and s_

with tf.Session() as sess:  
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    np.set_printoptions(precision=3, suppress=True)
    print(sess.run(w_))
    print(sess.run(s_))

Run this code, we can find w_ and s_ are:

[[ 0.345  0.044 -0.055  0.208  0.262  0.31  -0.809  0.034 -0.087  0.072]
 [-0.043  0.269  0.627 -0.297 -0.201 -0.414 -0.348 -0.106 -0.219 -0.228]
 [ 0.295 -0.186  0.029  0.105 -0.51  -0.072  0.039 -0.18  -0.389  0.645]
 [ 0.232 -0.295 -0.391 -0.368  0.374 -0.365  0.032 -0.07  -0.507 -0.186]
 [ 0.419 -0.001  0.155 -0.127  0.012  0.365  0.212 -0.721  0.122 -0.268]]
[[ 1. -0.  0.  0.  0.]
 [-0.  1.  0.  0. -0.]
 [ 0.  0.  1.  0.  0.]
 [ 0.  0.  0.  1. -0.]
 [ 0. -0.  0. -0.  1.]]

It means that we have created a random orthogonal matrix w_ using tensorflow.

Leave a Reply

Your email address will not be published. Required fields are marked *