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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.