Shape, Rank, Axis


Tensor의 Shape와 Rank

>>> import tensorflow as tf
>>> sess = tf.Session()

>>> t1 = tf.constant([1, 2, 3, 4])

>>> tf.shape(t1).eval(session=sess)
array([4])

>>> tf.rank(t1).eval(session=sess)
1


>>> t2 = tf.constant([[1 ,2],
>>>                   [3, 4]])

>>> tf.shape(t2).eval(session=sess)
array([2, 2])

>>> tf.rank(t2).eval(session=sess)
2


>>> t3 = tf.constant([
                      [
                       [
                        [ 1,  2,  3,  4],
                        [ 5,  6,  7,  8],
                        [ 9, 10, 11, 12]
                       ], 
                       [
                        [13, 14, 15, 16],
                        [17, 18, 19, 20],
                        [21, 22, 23, 24]
                       ]
                      ]
                     ]) 
>>> tf.shape(t3).eval(session=sess)
array([1, 2, 3, 4])

>>> tf.rank(t3).eval(session=sess)
4



Tensor에서 matmul() 함수와 multiply() 함수의 차이

  • matmul() 함수
>>> import tensorflow as tf
>>> sess = tf.Session()

>>> matrix1 = tf.constant([
                           [1, 2],
                           [3, 4]
                          ])
>>> matrix2 = tf.constant([
                           [1],
                           [2]
                          ])
>>> matrix3 = tf.constant([[1, 2]])

>>> print(f'Matrix 1\'s shape is {matrix1.shape}')
Matrix 1's shape is (2, 2)


>>> print(f'Matrix 2\'s shape is {matrix2.shape}')
Matrix 2's shape is (2, 1)

>>> print(f'Matrix 2\'s shape is {matrix3.shape}')
Matrix 2's shape is (1, 2)

>>> print(tf.matmul(matrix1, matrix2).eval(session=sess))
[[ 5]
 [11]]

>>> print(tf.matmul(matrix3, matrix1).eval(session=sess))
[[ 7 10]]

>>> print(tf.matmul(matrix1, matrix2).eval(session=sess))
[[1 2]
 [2 4]]

>>> print(tf.matmul(matrix1, matrix3).eval(session=sess))
ValueError: Dimensions must be equal, but are 2 and 1 for 'MatMul_3' (op: 'MatMul') with input shapes: [2,2], [1,2].


  • multiply() 함수
    • broadcasting된 후 계산
>>> print(tf.multiply(matrix1, matrix2).eval(session=sess))
[[1 2]
 [6 8]]

>>> print(tf.multiply(matrix3, matrix1).eval(session=sess))
[[1 4]
 [3 8]]

>>> print(tf.multiply(matrix1, matrix2).eval(session=sess))
[[1 2]
 [2 4]]

>>> print(tf.multiply(matrix1, matrix3).eval(session=sess))
[[1 4]
 [3 8]]


reduce_mean() 함수

>>> import tensorflow as tf
>>> sess = tf.Session()


>>> t1 = tf.constant([1., 2.])
>>> t2 = tf.constant([[1., 2.],
                      [3., 4.]])

>>> print(tf.reduce_mean(t1).eval(session=sess))
1.5

>>> print(tf.reduce_mean(t1, axis=0).eval(session=sess))
1.5

>>> print(tf.reduce_mean(t2).eval(session=sess))
2.5

>>> print(tf.reduce_mean(t2, axis=0).eval(session=sess))
[2.  3.]

>>> print(tf.reduce_mean(t2, axis=1).eval(session=sess))
[1.5  3.5]

>>> print(tf.reduce_mean(t2, axis=-1).eval(session=sess))
[1.5  3.5]



reduce_sum() 함수

>>> import tensorflow as tf
>>> sess = tf.Session()

>>> t = tf.constant([[1., 2.],
                     [3., 4.]])

>>> print(tf.reduce_sum(t).eval(session=sess))
10.0

>>> print(tf.reduce_sum(t, axis=0).eval(session=sess))
[4.  6.]

>>> print(tf.reduce_sum(t, axis=1).eval(session=sess))
[3.  7.]

>>> print(tf.reduce_mean(tf.reduce_sum(t, axis=0)).eval(session=sess))
5.0

>>> print(tf.reduce_mean(tf.reduce_sum(t, axis=1)).eval(session=sess))
5.0


argmax() 함수와 argmin() 함수

  • argmax() 함수는 배열 중에서 가장 큰 원소의 index를 반환
  • argmin() 함수는 배열 중에서 가장 작은 원소의 index를 반환
>>> import tensorflow as tf
>>> sess = tf.Session()

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

>>> print(tf.argmax(t).eval(session=sess))
[1 0 1 0]

>>> print(tf.argmax(t, axis=0).eval(session=sess))
[1 0 1 0]

>>> print(tf.argmax(t, axis=1).eval(session=sess))
[3 2]

>>> print(tf.argmin(t, axis=0).eval(session=sess))
[0 1 0 0]

>>> print(tf.argmin(t, axis=1).eval(session=sess))
[0 1]


reshape() 함수

  • 매개변수 -1은 나누어 떨어지는 숫자로 대치한다는 의미
>>> import tensorflow as tf
>>> sess = tf.Session()

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

>>> print(t.shape)
(2, 2, 3)

>>> # x행 3열의 행렬로 변환(x = 12 / 3 = 4)
>>> print(tf.reshape(t, shape=[-1, 3]).eval(session=sess))
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

>>> # x행 2열의 행렬이 3개로 중첩된 3차원 행렬로 변환(x = 12 / 3 / 2 = 2)
>>> print(tf.reshape(t, shape=[3, 2, -1]).eval(session=sess))
[[[ 1  2]
  [ 3  4]]

 [[ 5  6]
  [ 7  8]]

 [[ 9 10]
  [11 12]]]

squeeze() 함수

  • tensor의 dimension을 하나 줄임
>>> import tensorflow as tf
>>> sess = tf.Session()

>>> t = tf.constant([[1], [2], [3]])
>>> print(tf.rank(t).eval(session=sess))
2

>>> s = tf.squeeze(t)
>>> print(s.eval(session=sess))
[1 2 3]

>>> print(tf.rank(s).eval(session=sess))
1

expaned_dims() 함수

  • tensor의 dimension을 하나 늘임
>>> import tensorflow as tf
>>> sess = tf.Session()

>>> t = tf.constant([1, 2, 3, 4])

>>> print(tf.rank(t).eval(session=sess))
1

>>> s = tf.expand_dims(t, axis=0)
>>> print(s.eval(session=sess))
[[1 2 3 4]]

>>> print(tf.rank(s).eval(session=sess))
2

>>> s = tf.expand_dims(t, axis=1)
>>> print(s.eval(session=sess))
[[1]
 [2]
 [3]
 [4]]

>>> print(tf.rank(s).eval(session=sess))
2

one_hot() 함수

  • tensor의 원소값에 해당하는 index의 값만 1이고 나머지는 0인 tensor로 변환
    • 매개변수 depth는 만들고자 하는 tensor의 shape 중 가장 마지막 요소의 값
>>> import tensorflow as tf
>>> sess = tf.Session()

>>> t = tf.constant([[1], [0], [2], [1], [3]])

>>> s = tf.one_hot(t, depth=4)
>>> print(s.eval(session=sess))
[[[ 0.  1.  0.  0.]]

 [[ 1.  0.  0.  0.]]

 [[ 0.  0.  1.  0.]]

 [[ 0.  1.  0.  0.]]

 [[ 0.  0.  0.  1.]]]

>>> print(tf.shape(s).eval(session=sess))
[5 1 4]

>>> s = tf.one_hot(t, depth=6)
>>> print(s.eval(session=sess))
[[[ 0.  1.  0.  0.  0.  0.]]

 [[ 1.  0.  0.  0.  0.  0.]]

 [[ 0.  0.  1.  0.  0.  0.]]

 [[ 0.  1.  0.  0.  0.  0.]]

 [[ 0.  0.  0.  1.  0.  0.]]]

>>> print(tf.shape(s).eval(session=sess))
[5 1 6]
 


+ Recent posts