TensorFlow 生成上三角和下三角矩阵

本文介绍了如何利用TensorFlow库在深度学习中生成上三角和下三角矩阵。通过示例代码,详细阐述了利用tf.linalg.band_part函数来定制矩阵结构,以满足特定的上三角或下三角需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

>>> 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值