[tensorflow]tf.linalg.band_part (삼각,대각 행렬)

Luckydl21·2022년 2월 17일
1
post-thumbnail
tf.linalg.band_part(
    input, num_lower, num_upper, name=None
)

tf.lingalg.band_part를 통해 다음과 같이 삼각행렬 또는 대각행렬을 구현할 수 있다.

tensor 예시
tensor=tf.ones((5,5))
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
array([[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.]], dtype=float32)>
[1] num_lower가 음수일 경우 --> 하삼각행렬
tf.linalg.band_part(tensor,-1,0)
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
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)>
[2] num_upper가 음수일 경우 --> 상삼각행렬
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)>
[3] num_lower,num_upper 모두 0 --> 대각행렬
tf.linalg.band_part(tensor,0,0)
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]], dtype=float32)>

0개의 댓글