- 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]]
推薦閱讀
- Google Visualization API Essentials
- Access 2007數據庫應用上機指導與練習
- Learning Spring Boot
- Lean Mobile App Development
- Scratch 3.0 藝術進階
- OracleDBA實戰攻略:運維管理、診斷優化、高可用與最佳實踐
- 白話大數據與機器學習
- 信息學競賽寶典:數據結構基礎
- Chef Essentials
- Oracle數據庫管理、開發與實踐
- MySQL DBA修煉之道
- Visual Studio 2013 and .NET 4.5 Expert Cookbook
- Filecoin原理與實現
- Scratch 2.0 Game Development HOTSHOT
- 領域驅動設計精粹