Trace operator

The trace operator Tr(A) of matrix A gives the sum of all of the diagonal entries of a matrix. The following example shows how to use a trace operator on tensor objects:

import tensorflow as tfmat = tf.constant([ [0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=tf.float32)  # get trace ('sum of diagonal elements') of the matrix mat = tf.trace(mat)  with tf.Session() as sess:    print(sess.run(mat))

The output of the listing is 12.0.

Get Neural Network Programming with TensorFlow now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.