[개념] Tensor Ranks, Shapes, Type


TensorFlow 프로그램은 모든 데이터를 tensor 데이터 구조를 사용해서 표현한다. TensorFlow의 tensor는 n-차원 배열 또는 리스트라고 생각해도 된다. 하나의 tensor는 정적 타입과 동적 차원을 갖고 있다. 컴퓨테이션 그래프의 노드들은 오직 tensor만을 전달 할 수 있다.


Rank


TensorFlow 시스템에서, tensor는 rank라는 차원 단위로 표현된다. Tensor rank는 행렬의 rank와 다르다. Tensor rank(order, degree, -n_dimension 으로도 언급됨)는 tensor의 차원수다. 예를 들어, 아래 tensor(Python 리스트로 정의)의 rank는 2다.

t = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


rank 2인 tensor는 행렬, rank 1인 tensor는 벡터로 생각할 수 있다. rank 2인 tensor는 t[i, j] 형식으로 원소에 접근할 수 있다. rank 3인 tensor는 t[i, j, k] 형식으로 원소를 지정할 수 있다.


RankMath entityPython example
0Scalar (magnitude only)s = 483
1Vector (magnitude and direction)v = [1.1, 2.2, 3.3]
2Matrix (table of numbers)m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
33-Tensor (cube of numbers)t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]]
nn-Tensor (you get the idea)....


출처: Tensor Ranks, Shapes, Type


Tensorflow Rank 결과

In [1]: import tensorflow as tf

In [2]: const_s_0 = tf.constant(1)

In [3]: const_v_1_1 = tf.constant([1, 2])

In [4]: const_v_1_2 = tf.constant([1, 2, 3])

In [5]: const_m_2_1 = tf.constant([[1, 2, 3], [4, 5, 6]])

In [6]: const_m_2_2 = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

In [7]: const_n_3_1 = tf.constant([[[2], [4]], [[8], [10]], [[14], [16]]]

   ...: )

In [8]: const_n_3_2 = tf.constant([[[2], [4], [6]], [[8], [10], [12]], [[

   ...: 14], [16], [18]]])

In [9]:     sess = tf.InteractiveSession()
In [10]: sess.run(tf.rank(const_s_0))
Out[10]: 0
In [11]: sess.run(tf.rank(const_v_1_1))
Out[11]: 1
In [12]: sess.run(tf.rank(const_v_1_2))
Out[12]: 1
In [13]: sess.run(tf.rank(const_m_2_1))
Out[13]: 2
In [14]: sess.run(tf.rank(const_m_2_2))
Out[14]: 2
In [15]: sess.run(tf.rank(const_n_3_1))
Out[15]: 3
In [16]: sess.run(tf.rank(const_n_3_2))
Out[16]: 3


Shape


TensorFlow 문서는 tensor 차원을 표현할 때 세 가지 기호를 사용한다. rank, shape, 차원수. 아래 표는 그 세 가지의 관계를 보여준다:


RankShapeDimension numberExample
0[]0-DA 0-D tensor. A scalar.
1[D0]1-DA 1-D tensor with shape [5].
2[D0, D1]2-DA 2-D tensor with shape [3, 4].
3[D0, D1, D2]3-DA 3-D tensor with shape [1, 4, 3].
n[D0, D1, ... Dn-1]n-DA tensor with shape [D0, D1, ... Dn-1].

Shape는 Python 리스트 / 정수형 튜플 또는 TensorShape class로 표현 할 수 있다. 


출처: Tensor Ranks, Shapes, Type


Tensorflow Shape 결과

In [1]: import tensorflow as tf

In [2]: const_s_0 = tf.constant(1)

In [3]: const_v_1_1 = tf.constant([1, 2])

In [4]: const_v_1_2 = tf.constant([1, 2, 3])

In [5]: const_m_2_1 = tf.constant([[1, 2, 3], [4, 5, 6]])

In [6]: const_m_2_2 = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

In [7]: const_n_3_1 = tf.constant([[[2], [4]], [[8], [10]], [[14], [16]]]

   ...: )

In [8]: const_n_3_2 = tf.constant([[[2], [4], [6]], [[8], [10], [12]], [[

   ...: 14], [16], [18]]])

In [9]:     sess = tf.InteractiveSession()
In [10]: sess.run(tf.shape(const_s_0))
Out[10]: array([], dtype=int32)
In [11]: sess.run(tf.shape(const_v_1_1))
Out[11]: array([2], dtype=int32)
In [12]: sess.run(tf.shape(const_v_1_2))
Out[12]: array([3], dtype=int32)
In [13]: sess.run(tf.shape(const_m_2_1))
Out[13]: array([2, 3], dtype=int32)
In [14]: sess.run(tf.shape(const_m_2_2))
Out[14]: array([3, 3], dtype=int32)
In [15]: sess.run(tf.shape(const_n_3_1))
Out[15]: array([3, 2, 1], dtype=int32)
In [16]: sess.run(tf.shape(const_n_3_2))
Out[16]: array([3, 3, 1], dtype=int32)


Data types


Tensor는 차원 말고도 데이터 타입도 갖는다. 아래의 데이터 타입을 tensor에 지정할 수 있다.


Data typePython typeDescription
DT_FLOATtf.float3232 비트 부동 소수.
DT_DOUBLEtf.float6464 비트 부동 소수.
DT_INT8tf.int88 비트 부호 있는 정수.
DT_INT16tf.int1616 비트 부호 있는 정수.
DT_INT32tf.int3232 비트 부호 있는 정수.
DT_INT64tf.int6464 비트 부호 있는 정수.
DT_UINT8tf.uint88 비트 부호 없는 정수.
DT_STRINGtf.string가변 길이 바이트 배열. Tensor의 각 원소는 바이트 배열.
DT_BOOLtf.bool불리언.
DT_COMPLEX64tf.complex642개의 32 비트 부동 소수로 만든 복소수 : 실수부 + 허수부
DT_COMPLEX128tf.complex1282개의 64 비트 부동 소수로 만든 복소수 : 실수부 + 허수부
DT_QINT8tf.qint88 비트 부호 있는 정수로 quantized Ops에서 사용.
DT_QINT32tf.qint3232 비트 부호 있는 정수로 quantized Ops에서 사용.
DT_QUINT8tf.quint88 비트 부호 없는 정수로 quantized Ops에서 사용.


출처: Tensor Ranks, Shapes, Type


이 포스팅은 머신러닝/딥러닝 오픈소스 Tensorflow 개발을 위한 선행학습으로 Tensorflow API Document의 Python API 대한 학습노트입니다.

Posted by 이성윤