[텐서플로우 정리] 05. Shapes & Shaping
텐서플로우에 이러한 역할을 하는 함수가 몇 개 없어 간략하게 정리할 수 있었다.
rank. 배열의 차원.
Rank
Math
Entity
Python example
0
Scalar
(magnitude only)
s = 483
1
Vector
(magnitude and direction)
v = [1.1, 2.2, 3.3]
2
Matrix
(table of numbers)
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
3
3-Tensor
(cube of numbers)
t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]]
n
n-Tensor
(you get the idea)
....
shape. 배열의 모양(2x5는 2행 5열)
Rank
Shape
Dimension number
Example
0
[]
0-D
A 0-D tensor. A scalar.
1
[D0]
1-D
A 1-D tensor with shape [5].
2
[D0, D1]
2-D
A 2-D tensor with shape [3, 4].
3
[D0, D1, D2]
3-D
A 3-D tensor with shape [1, 4, 3].
n
[D0, D1, ... Dn-1]
n-D
A tensor with shape [D0, D1, ... Dn-1].
내부 정보를 출력하는 함수로 functions.py에 만들었던 함수다.
def showConstantDetail(t):
sess = tf.InteractiveSession()
print(t.eval())
print('shape :', tf.shape(t))
print('size :', tf.size(t))
print('rank :', tf.rank(t))
print(t.get_shape())
sess.close()
shape을 변경할 수 있는 2개의 함수를 살펴 본다.
import tensorflow as tf
import functions
c1 = tf.constant([1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 3, 7])
v1 = tf.Variable([[1, 2, 3], [7, 8, 9]])
print('-----------reshape------------')
functions.showOperation(tf.reshape(c1, [2, -1])) # [[1 3 5 7 9 0] [2 4 6 8 3 7]]
functions.showOperation(tf.reshape(c1, [-1, 3])) # [[1 3 5] [7 9 0] [2 4 6] [8 3 7]]
functions.showOperation(tf.reshape(v1, [-1])) # [1 2 3 7 8 9]
c2 = tf.reshape(c1, [2, 2, 1, 3])
c3 = tf.reshape(c1, [1, 4, 1, 3, 1])
print('-----------squeeze------------') # reemoves dimensions of size 1
# [[[[1 3 5]] [[7 9 0]]] [[[2 4 6]] [[8 3 7]]]]
functions.showOperation(c2)
functions.showOperation(tf.squeeze(c2)) # [[[1 3 5] [7 9 0]] [[2 4 6] [8 3 7]]]
# [[[[[1] [3] [5]]] [[[7] [9] [0]]] [[[2] [4] [6]]] [[[8] [3] [7]]]]]
functions.showOperation(c3)
functions.showOperation(tf.squeeze(c3)) # [[1 3 5] [7 9 0] [2 4 6] [8 3 7]]
reshape 함수에 사용된 -1은 나누어 떨어지는 숫자를 자동으로 적용하겠다는 뜻이다. 12개의 요소가 있을 때, [3,-1]이라는 것은 [3,4]와 같고, [2,2,-1]이라는 것은 [2,2,3]과 같다. 행과 열, 페이지에서 중심이 되는 것을 지정한 다음에, 나머지를 계산하기 싫을 때 주로 사용된다.
'텐서플로' 카테고리의 다른 글
| [텐서플로우 정리] 07. 세션과 구구단 (0) | 2016年08月15日 |
|---|---|
| [텐서플로우 정리] 06. 수학 함수 (0) | 2016年08月15日 |
| [텐서플로우 정리] 04. Slicing & Joining (0) | 2016年08月15日 |
| [텐서플로우 정리] 03. 관계 연산, 논리 연산 (0) | 2016年08月15日 |
| [텐서플로우 정리] 02. 산술 연산 (0) | 2016年08月15日 |
공지사항
카테고리
얼떨결에 붙인 이름 파이썬_킴
딥러닝과 서핑을 함께 합니다!!
제가 강의하는 곳입니다!!