[tensorflow2.x 기초 - 1] tensorflow의 가장 기본적인 함수들 - .constant / .shape / .dtype / .cast / .numpy / tf.random.normal / tf.random.uniform

2020. 8. 30. 14:49DL in Python/Tensorflow2.x 기초

 

 

 

 

 
  • TensorFlow 2.0 간단 사용법
In [2]:
import numpy as np
import tensorflow as tf
 

Tensor 생성

 
  • []
    • List 생성
In [3]:
[1,2,3]
Out[3]:
[1, 2, 3]
In [4]:
[[1,2,3], [4,5,6]]
Out[4]:
[[1, 2, 3], [4, 5, 6]]
 

Array 생성

 
  • tuple이나 list 둘 다 np.array()로 씌어서 array를 만들 수 있음
In [5]:
arr = np.array([1,2,3])
In [6]:
arr.shape
Out[6]:
(3,)
In [7]:
arr = np.array([[1,2,3],[4,5,6]])
In [8]:
arr.shape
Out[8]:
(2, 3)
 

Tensor 생성

 
  • tf.constant()
    • list -> Tensor
In [9]:
tf.constant([1,2,3])
Out[9]:
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 3])>
 
  • tf.constant()
    • tuple -> Tensor
In [10]:
tf.constant(((1,2,3),(4,5,6)))
Out[10]:
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [4, 5, 6]])>
 
  • tf.constant()
    • Array -> Tensor
In [11]:
arr = np.array([1,2,3])
arr
Out[11]:
array([1, 2, 3])
In [12]:
tensor = tf.constant(arr)
tensor
Out[12]:
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 3])>
 

Tensor에 담긴 정보 확인

 
  • shape 확인
In [13]:
tensor.shape
Out[13]:
TensorShape([3])
 
  • data type 확인
    • 주의: Tensor 생성 할 때도 data type을 정해주지 않기 때문에 data type에 대한 혼동이 올 수 있음
    • Data Type에 따라 모델의 무게나 성능 차이에도 영향을 줄 수 있음
In [14]:
tensor.dtype
Out[14]:
tf.int32
 
  • data type 정의
In [15]:
tf.constant([1,2,3], dtype = tf.uint8)
tensor = tf.constant([1,2,3], dtype = tf.float32)
 
  • data type 변환
    • Numpy에서 astype()을 주었듯이, TensorFlow에서는 tf.cast를 사용
In [16]:
arr = np.array([1,2,3], dtype = np.float32)
In [17]:
arr.astype(np.uint8)
Out[17]:
array([1, 2, 3], dtype=uint8)
In [18]:
tensor
Out[18]:
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 2., 3.], dtype=float32)>
In [19]:
tf.cast(tensor, dtype = tf.uint8)
Out[19]:
<tf.Tensor: shape=(3,), dtype=uint8, numpy=array([1, 2, 3], dtype=uint8)>
 
  • Tensor에서 Numpy 불러오기
    • .numpy()
In [20]:
tensor.numpy()
Out[20]:
array([1., 2., 3.], dtype=float32)
 
  • Tensor에서 Numpy 불러오기
    • np.array()
In [21]:
np.array(tensor)
Out[21]:
array([1., 2., 3.], dtype=float32)
 

type()를 사용하여 numpy array로 변환된 것 확인

In [22]:
type(tensor.numpy())
Out[22]:
numpy.ndarray
In [23]:
type(tensor)
Out[23]:
tensorflow.python.framework.ops.EagerTensor
 

난수 생성

 
image.png
 
  • Normal Distribution은 중심극한 이론에 의한 연속적인 모양
  • Uniform Distribution은 중심 극한 이론과는 무관하며 불연속적이며 일정한 분포
 
  • numpy에서는 normal distribution을 기본적으로 생성
    • np.random.randn()
In [24]:
np.random.randn(9)
Out[24]:
array([-0.15899158,  0.1575718 , -1.75959036,  0.20383348,  0.25867405,
       -0.48118881, -1.30691657, -0.63793082,  1.78042704])
 
  • tf.random.normal
    • TensorFlow에서 Normal Distribution
In [25]:
tf.random.normal([3,3])
Out[25]:
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[-0.41466862, -0.21604665,  1.0374897 ],
       [-1.0842748 ,  1.3169315 ,  0.44033718],
       [-0.20421009, -0.3760217 ,  0.9834707 ]], dtype=float32)>
 
  • tf.random.uniform
    • TensorFlow에서 Uniform Distribution
In [26]:
tf.random.uniform([4,4])
Out[26]:
<tf.Tensor: shape=(4, 4), dtype=float32, numpy=
array([[0.17260396, 0.5215993 , 0.81475794, 0.02093637],
       [0.08261132, 0.53196514, 0.4558599 , 0.26433837],
       [0.6774601 , 0.04147327, 0.56001437, 0.10782743],
       [0.4361142 , 0.5128963 , 0.50784445, 0.74408734]], dtype=float32)>
In [ ]: