>>> import tensorflow as tf
>>> a = tf.linalg.band_part(tf.ones((5,5)),-1,0)
>>> print(a)
Tensor("MatrixBandPart:0", shape=(5, 5), dtype=float32)
>>> sess = tf.Session()
2021-12-29 10:18:26.173019: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX512F
2021-12-29 10:18:26.183988: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2494140000 Hz
2021-12-29 10:18:26.188170: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x4c81610 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2021-12-29 10:18:26.188205: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
>>> sess.run(a)
array([[1., 0., 0., 0., 0.],
[1., 1., 0., 0., 0.],
[1., 1., 1., 0., 0.],
[1., 1., 1., 1., 0.],
[1., 1., 1., 1., 1.]], dtype=float32)
>>> b = 1 -a
>>> sess.run(b)
array([[0., 1., 1., 1., 1.],
[0., 0., 1., 1., 1.],
[0., 0., 0., 1., 1.],
[0., 0., 0., 0., 1.],
[0., 0., 0., 0., 0.]], dtype=float32)
TensorFlow 生成上三角和下三角矩阵
最新推荐文章于 2026-02-23 01:20:45 发布
本文介绍了如何利用TensorFlow库在深度学习中生成上三角和下三角矩阵。通过示例代码,详细阐述了利用tf.linalg.band_part函数来定制矩阵结构,以满足特定的上三角或下三角需求。


2951

被折叠的 条评论
为什么被折叠?



