官术网_书友最值得收藏!

Matrix diagonals

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 matrix
diag_mat = tf.diag_part(mat)

# create matrix with given diagonal
mat = 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:

 [ 0.  4.  8.]
[[1 0 0 0][0 2 0 0] [0 0 3 0] [0 0 0 4]]
主站蜘蛛池模板: 霍山县| 义乌市| 勃利县| 静安区| 临桂县| 张家川| 临海市| 白朗县| 阜新市| 黄石市| 湾仔区| 商都县| 阿坝| 清水河县| 阳谷县| 华池县| 莆田市| 宁远县| 方城县| 青河县| 白城市| 隆德县| 五峰| 白河县| 巴楚县| 康马县| 大悟县| 姜堰市| 沁水县| 鄯善县| 新龙县| 崇左市| 扶绥县| 鄢陵县| 张家口市| 沙湾县| 新郑市| 双辽市| 彰化县| 泾源县| 海阳市|