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

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]]
主站蜘蛛池模板: 南宁市| 福贡县| 西宁市| 新和县| 康马县| 唐河县| 巨鹿县| 江达县| 呼伦贝尔市| 车险| 金昌市| 青岛市| 苏尼特左旗| 青川县| 苏尼特右旗| 同心县| 太湖县| 麟游县| 安吉县| 龙陵县| 九江县| 呼图壁县| 沈阳市| 湖南省| 宝鸡市| 根河市| 电白县| 青州市| 六枝特区| 明水县| 旌德县| 靖安县| 武隆县| 四会市| 定南县| 东阿县| 铜鼓县| 高碑店市| 和静县| 光山县| 兰西县|