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

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]]
主站蜘蛛池模板: 元阳县| 乌拉特后旗| 临潭县| 隆尧县| 会理县| 报价| 濮阳县| 杨浦区| 滨海县| 泌阳县| 深水埗区| 濉溪县| 洛浦县| 苏州市| 吉安市| 新竹县| 赞皇县| 孝感市| 当涂县| 吉林省| 长春市| 泰州市| 罗甸县| 玉树县| 沙田区| 罗江县| 呼玛县| 台南市| 长沙市| 阿尔山市| 永清县| 鹤山市| 太仆寺旗| 云南省| 钟山县| 五华县| 长治市| 定襄县| 江永县| 乌苏市| 抚顺市|