November 2017
Intermediate to advanced
274 pages
6h 16m
English
Matrices that are diagonal in nature consist mostly of zeros and have non-zero entries only along the main diagonal. Not all diagonal matrices need to be square.
Using the diagonal part operation, we can get the diagonal of a given matrix, and to create a matrix with a given diagonal, we use the diag operation from tensorflow. The following example shows how to use diagonal operators on tensor objects:
import tensorflow as tf mat = tf.constant([ [0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=tf.float32) # get diagonal of the matrixdiag_mat = tf.diag_part(mat) # create matrix with given diagonalmat = tf.diag([1,2,3,4]) with tf.Session() as sess: print(sess.run(diag_mat)) print(sess.run(mat))
The output of this is shown as follows: ...
Read now
Unlock full access