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

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]]
主站蜘蛛池模板: 开阳县| 宜兰县| 昆山市| 岳普湖县| 泽普县| 张掖市| 舞钢市| 鲜城| 永靖县| 商水县| 集贤县| 大姚县| 兰西县| 名山县| 西乌珠穆沁旗| 沿河| 英山县| 长阳| 宁晋县| 上思县| 裕民县| 晋中市| 钟祥市| 曲松县| 刚察县| 于田县| 河北省| 信宜市| 合江县| 丹巴县| 山阳县| 株洲县| 崇阳县| 明水县| 集安市| 农安县| 兴城市| 乐亭县| 香格里拉县| 阿坝| 祁阳县|