import tensorflow as tf
import numpy as np
Deeplearning framework는 기본적으로 Tensor를 다루는 도구다.
Tensor를 다룰 때 가장 중요한 것!
→ SHAPE!!
(해보면 알겠지만, 제일 이러가 많이 나는 이유, 제일 헷갈리는 것, 개발할 때 우리가 이론을 알아야 하는 이유, 함수들의 설정값을 확인해야 하는 이유)
우리가 생성하는 것은 tf.Tensor 데이터!
항상 체크 해야 할 것!
Constant(상수)
li_ten = tf.constant([1, 2, 3])
li_ten
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 3])>
li_ten_f = tf.constant([1., 2., 3.])
li_ten_f
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 2., 3.], dtype=float32)>
tu_ten = tf.constant(((1, 2, 3), (1, 2, 3)), name='sample')
tu_ten
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
[1, 2, 3]])>
arr = np.array([1., 2., 3.])
arr_ten = tf.constant(arr)
arr_ten
<tf.Tensor: shape=(3,), dtype=float64, numpy=array([1., 2., 3.])>
잠깐 옆으로 새서...
arr_ten.numpy(), type(arr_ten.numpy())
(array([1., 2., 3.]), numpy.ndarray)
li_ten.numpy(), type(li_ten.numpy())
(array([1, 2, 3]), numpy.ndarray)
# 구조가 일치하지 않기 때문에 tensor로 변환 불가능
not_a_matrix = [[1, 2, 3], [4, 5], [6, 7, 8]]
tf.constant(not_a_matrix)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-16-3994252598bc> in <module>
2
3 not_a_matrix = [[1, 2, 3], [4, 5], [6, 7, 8]]
----> 4 tf.constant(not_a_matrix)
c:\Users\theo\miniconda3\envs\ds_study\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name)
269 ValueError: if called on a symbolic tensor.
270 """
--> 271 return _constant_impl(value, dtype, shape, name, verify_shape=False,
272 allow_broadcast=True)
273
c:\Users\theo\miniconda3\envs\ds_study\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
281 with trace.Trace("tf.constant"):
282 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 283 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
284
285 g = ops.get_default_graph()
c:\Users\theo\miniconda3\envs\ds_study\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
306 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
307 """Creates a constant on the current device."""
--> 308 t = convert_to_eager_tensor(value, ctx, dtype)
309 if shape is None:
310 return t
c:\Users\theo\miniconda3\envs\ds_study\lib\site-packages\tensorflow\python\framework\constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
104 dtype = dtypes.as_dtype(dtype).as_datatype_enum
105 ctx.ensure_initialized()
--> 106 return ops.EagerTensor(value, ctx.device_name, dtype)
107
108
ValueError: Can't convert non-rectangular Python sequence to Tensor.
li_ten.shape, tu_ten.shape
(TensorShape([3]), TensorShape([2, 3]))
arr_ten.dtype, li_ten.dtype
(tf.float64, tf.int32)
# 1번 케이스 - 랭크가 달라서 오류
tf.matmul(li_ten, tu_ten)
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-20-ac85cb2920e4> in <module>
1 # 1번 케이스
----> 2 tf.matmul(li_ten, tu_ten)
c:\Users\theo\miniconda3\envs\ds_study\lib\site-packages\tensorflow\python\util\traceback_utils.py in error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb
c:\Users\theo\miniconda3\envs\ds_study\lib\site-packages\tensorflow\python\framework\ops.py in raise_from_not_ok_status(e, name)
7105 def raise_from_not_ok_status(e, name):
7106 e.message += (" name: " + name if name is not None else "")
-> 7107 raise core._status_to_exception(e) from None # pylint: disable=protected-access
7108
7109
InvalidArgumentError: In[0] and In[1] has different ndims: [3] vs. [2,3] [Op:MatMul]
# 2번 케이스 - 랭크가 달라서 오류
tf.matmul(tu_ten, li_ten)
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-21-4c647f53952b> in <module>
1 # 2번 케이스
----> 2 tf.matmul(tu_ten, li_ten)
c:\Users\theo\miniconda3\envs\ds_study\lib\site-packages\tensorflow\python\util\traceback_utils.py in error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb
c:\Users\theo\miniconda3\envs\ds_study\lib\site-packages\tensorflow\python\framework\ops.py in raise_from_not_ok_status(e, name)
7105 def raise_from_not_ok_status(e, name):
7106 e.message += (" name: " + name if name is not None else "")
-> 7107 raise core._status_to_exception(e) from None # pylint: disable=protected-access
7108
7109
InvalidArgumentError: In[0] and In[1] has different ndims: [2,3] vs. [3] [Op:MatMul]
# 3번 케이스 - dtype이 달라서 오류
arr_ten * li_ten
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-26-a20bc446db4d> in <module>
1 # 3번 케이스
----> 2 arr_ten * li_ten
c:\Users\theo\miniconda3\envs\ds_study\lib\site-packages\tensorflow\python\util\traceback_utils.py in error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb
c:\Users\theo\miniconda3\envs\ds_study\lib\site-packages\tensorflow\python\framework\ops.py in raise_from_not_ok_status(e, name)
7105 def raise_from_not_ok_status(e, name):
7106 e.message += (" name: " + name if name is not None else "")
-> 7107 raise core._status_to_exception(e) from None # pylint: disable=protected-access
7108
7109
InvalidArgumentError: cannot compute Mul as input #1(zero-based) was expected to be a double tensor but is a int32 tensor [Op:Mul]
# 미리 지정해주거나
tensor = tf.constant([1, 2, 3], dtype=tf.float32)
tensor
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 2., 3.], dtype=float32)>
# tf.cast를 사용. 다만, 많은 경우 미리 데이터타입을 정리해둘 수 있다.
tf.cast(tensor, dtype=tf.int16)
<tf.Tensor: shape=(3,), dtype=int16, numpy=array([1, 2, 3], dtype=int16)>
간단 퀴즈
아래 코드를 에러 없이 실행 하시오.
arr_ten, li_ten
(<tf.Tensor: shape=(3,), dtype=float64, numpy=array([1., 2., 3.])>,
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 3])>)
li_ten = tf.cast(li_ten, dtype=tf.float64)
arr_ten * li_ten
<tf.Tensor: shape=(3,), dtype=float64, numpy=array([1., 4., 9.])>
tf.ones(1)
<tf.Tensor: shape=(1,), dtype=float32, numpy=array([1.], dtype=float32)>
tf.zeros((2, 5), dtype='int32')
<tf.Tensor: shape=(2, 5), dtype=int32, numpy=
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])>
tf.range(10)
<tf.Tensor: shape=(10,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>
tf.range(1, 11)
<tf.Tensor: shape=(10,), dtype=int32, numpy=array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])>
간단 퀴즈
n을 입력하면 첫항이 1이고 공비가 2인 등비수열을 생성하는 함수를 만드시오(이 때 결과값은 tf.Tensor 데이터이고, 데이터 타입은 tf.int32)
def geometric_sequence(n):
tf.range(1, 11, dtype=tf.int32)
return 2 ** tf.range(n, dtype=tf.int32)
print(geometric_sequence(10))
tf.Tensor([ 1 2 4 8 16 32 64 128 256 512], shape=(10,), dtype=int32)
tf.random 에 구현 되어 있음.
shape = (3, 3)
tf.random.normal(shape)
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[ 0.5714613 , -0.36360994, 0.49137077],
[ 0.4931649 , -0.16883597, 0.66534084],
[-0.47609696, 0.2612795 , -0.7017815 ]], dtype=float32)>
tf.random.uniform(shape)
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[0.42731535, 0.9264877 , 0.9957193 ],
[0.8619703 , 0.51153636, 0.58214355],
[0.15178788, 0.13090932, 0.7399502 ]], dtype=float32)>
Random seed 관리 하기!!!
tf.random.set_seed({seed_number})
=> 항상 Random seed를 고정해두고 개발 한다!!!
(주의 할 점은 해당 개발물에 사용되는 난수가 모두 TensorFlow에서 생성된것이 아닐 수 있다는 것이다.)
seed = 7777
tf.random.set_seed(seed)
a = tf.random.uniform([1])
b = tf.random.uniform([1])
print(a, b, sep='\n')
tf.Tensor([0.959749], shape=(1,), dtype=float32)
tf.Tensor([0.8677443], shape=(1,), dtype=float32)
a = tf.random.uniform([1])
b = tf.random.uniform([1])
print(a, b, sep='\n')
tf.Tensor([0.22878075], shape=(1,), dtype=float32)
tf.Tensor([0.87772965], shape=(1,), dtype=float32)
tf.random.set_seed(seed)
a = tf.random.uniform([1])
b = tf.random.uniform([1])
print(a, b, sep='\n')
tf.Tensor([0.959749], shape=(1,), dtype=float32)
tf.Tensor([0.8677443], shape=(1,), dtype=float32)