블로그 이미지
윤영식
Full Stacker, Application Architecter, KnowHow Dispenser and Bike Rider

Publication

Category

Recent Post

'manipulation'에 해당되는 글 1

  1. 2018.07.06 [Tensorflow] Manipulation - matmul, reshape, zip, etc

Tensorflow Manipulation 강좌를 정리한다.





Array


- rank, shape, axis을 array로 나타냄.

- [-1] array의 마지막 item

- t[2:5] index 3부터 5까지 items

- t[:2] 처음부터 index 2까지 

- t[3:] index 3에서 끝까지 


[1,2,3] rank(1), shape(3)

[[1,2],[3,4]] rank(2), shape(2,2)


axis=0 가장 바깥쪽, axis=-1 가장 안쪽을 표현하고 axis을 통해 shape을 이동한다. 







Matrix Manipulation


matmul


matrix 곱은 반드시 matmul을 사용한다. 

- tf.matmul((2,2) , (2,1)) => (2,1)




broadcasting


matrix끼리 shape이 다르더라도 matrix끼리 연산을 가능토록 shape을 자동으로 맞춰준다. 잘 알고 사용하면 좋지만 조심해라. 



reduce_mean 


(평균) 호출시 입력값들의 type에 주의한다.


axis를 0 이냐 1 이냐에 따라 다른다.

>>> import tensorflow as tf

>>> sess = tf.Session()

>>> sess.run(tf.global_variables_initializer())

>>> x = [[1.,2.],[3.,4.]]

>>> tf.reduce_mean(x).eval(session=sess)

2.5


>>> tf.reduce_mean(x, axis=1).eval(session=sess)

array([1.5, 3.5], dtype=float32)



reduce_sum 


axis=-1  제일 안쪽값을 합치고 이에 대한 평균을 낸다. 



argmax


큰값이 있는 위치 index를 구하는 것이다. 



reshape


주로 (...., z) 가장 안쪽에 있는 z 값은 그대로 가져가고, 앞의 것을 reshape한다.



reshape 하나의 형태 sqeeze는 펴주는 역할, expand_dims 차원을 더 추가할 경우 




One Hot 


가장 큰 값이 있는 곳을 1로 하고 나머지는 0으로 바꿈. One hot을 하면 dimension (rank)가 하나 더 생기므로 다시 reshape을 한다. 

마지막 3만 남기고 나머지는 차원 1로 만들어주는 것 



Casting


타입 바꿔주기 



Stack


쌓기를 만들어줌. 주어진 데이터들에서 axis와 stack을 이용해 새로운 matrix를 만듦



Ones and Zeros like


가지고 있는 shape과 똑같은 0 또는 1로 채워진 shape을 만들때 사용



Zip


복수의 tensor를 한방에 처리하기 





참조


- 김성훈교수님의 Tensorflow Manipulation 강좌

- reduce_sum reduce_mean 사용법

posted by 윤영식
prev 1 next