- Neural Network Programming with TensorFlow
- Manpreet Singh Ghotra Rajdeep Dua
- 140字
- 2021-07-02 15:17:08
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]]
推薦閱讀
- Building Computer Vision Projects with OpenCV 4 and C++
- Hands-On Machine Learning with Microsoft Excel 2019
- 數據庫基礎與應用:Access 2010
- Libgdx Cross/platform Game Development Cookbook
- Access 2016數據庫技術及應用
- 中國數字流域
- 金融商業算法建模:基于Python和SAS
- 深入淺出Greenplum分布式數據庫:原理、架構和代碼分析
- Apache Kylin權威指南
- INSTANT Apple iBooks How-to
- 大數據技術原理與應用:概念、存儲、處理、分析與應用
- 數據庫原理與設計實驗教程(MySQL版)
- 改進的群智能算法及其應用
- 數字化轉型方法論:落地路徑與數據中臺
- 精通Neo4j