与这个问题类似,我想转换这个张量
tensor = tf.ones((5, 5))
tf.Tensor(
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]], shape=(5, 5), dtype=float32)
Run Code Online (Sandbox Code Playgroud)
到其他地方都为 1 和 0 的上三角张量:
tf.Tensor(
[[1. 1. 1. 1. 1.]
[0. 1. 1. 1. 1.]
[0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1.]
[0. 0. 0. 0. 1.]], shape=(5, 5), dtype=float32)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
您可以使用tf.linalg.band_part:
>>> tensor = tf.ones((5, 5))
>>> tf.linalg.band_part(tensor, 0, -1)
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
array([[1., 1., 1., 1., 1.],
[0., 1., 1., 1., 1.],
[0., 0., 1., 1., 1.],
[0., 0., 0., 1., 1.],
[0., 0., 0., 0., 1.]], dtype=float32)>
Run Code Online (Sandbox Code Playgroud)
请注意,您可以使用该函数从张量中提取任意数量的对角线。有用的案例包括:
tf.linalg.band_part(input, 0, -1) ==> Upper triangular part.
tf.linalg.band_part(input, -1, 0) ==> Lower triangular part.
tf.linalg.band_part(input, 0, 0) ==> Diagonal.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1839 次 |
| 最近记录: |