본문 바로가기
Data Science/Deep Learning

Tensorflow axis = -1 의미 파악하기

by Queen2 2022. 11. 4.
728x90
반응형

텐서플로우 페이지를 보다가 아래 코드를 발견하고 axis = -1 의 정체를 탐색해봤습니다

 

tf.nn.softmax(x,axis=-1)

 

정답부터 말하자면 우리가 파이썬의 리스트에서 이해하듯, axis = -1 은 주어진 배열의 마지막 axis 를 의미합니다

 

예시를 통해 보겠습니다

 

x라는 [2,3] shape의 텐서가 있을 때, 각각에 여러 max 관련 함수를 적용시켜보겠습니다.

import tensorflow as tf

x = tf.constant([[1., 2., 3.],
                 [4., 5., 6.]])

 

마지막 차원(default axis = -1)을 기준으로 softmax 함수를 적용한 모습입니다

tf.nn.softmax(x,axis=-1)
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[0.09003057, 0.24472848, 0.66524094],
       [0.09003057, 0.24472848, 0.66524094]], dtype=float32)>

 

이는 tensorflow와 keras 에 적용한 결과값인데요

자세히 보면 shape 반환값이 (2,)로 제시했던 axis= -1에 해당하는 축을 제외한 모습으로 도출됨을 알 수 있습니다

tf.argmax(x,axis=-1)
<tf.Tensor: shape=(2,), dtype=int64, numpy=array([2, 2], dtype=int64)>
from tensorflow import keras
keras.backend.argmax(x,axis=-1)
<tf.Tensor: shape=(2,), dtype=int64, numpy=array([2, 2], dtype=int64)>

 

스택오버플로우를 참고하자면, argmax에 사용되는 aixs 파라미터를 사용해 결과값을 반환할 시에는 

제시한 axis를 제외한 shape이 반환된다고 합니다.

 

가령, x.shape = (19,19,5,80) 텐서가 있을 때

keras.backend,argmax(x,axis = -1) ==> (19,19,5)

keras.backend,argmax(x,axis = -2) ==> (19,19,80)

keras.backend,argmax(x,axis = -3) ==> (19,5,80)

keras.backend,argmax(x,axis = -4) ==> (19,5,80)

 

이렇게 기준값으로 제시한 axis를 제외한 shape으로 결과값이 나오게 됩니다

 

 

 

Source:

https://stackoverflow.com/questions/47435526/what-is-the-meaning-of-axis-1-in-keras-argmax

728x90
반응형

댓글