[ML] TensorFlow Variable and Constant
TensorFlow Variable and Constant
- 인프런 - Tensorflow 사용메뉴얼 - Constant and Variable Tensors를 수강하고 개인적으로 정리한 내용입니다.
예시코드는 강의내용 그대로 사용하지않고, 수정하였음을 참고하여주시기 바랍니다.
모델 학습 Flow
- DataLoader → Data → (Model → Loss → Optimizer → Model 반복)
- Data: 학습 X → immutable(tf.constant, EagerTensor)
- Model의 Weight, Bias: 학습 O → Mutable(tf.Variable, ResourceVariable)
import tensorflow as tf
import numpy as np
# Save some gpu memories
physical_devices = tf.config.list_physical_devices('GPU')
for physical_device in physical_devices:
tf.config.experimental.set_memory_growth(device=physical_device, enable=True)
t_var = tf.Variable([1, 2, 3])
t_const = tf.constant([1, 2, 3])
print(f't_var: {t_var}')
print('t_var(type): ', type(t_var))
print('=' * 80)
print(f't_const: {t_const}')
print('t_const(type): ', type(t_const))
t_var: <tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([1, 2, 3], dtype=int32)>
t_var(type): <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
================================================================================
t_const: [1 2 3]
t_const(type): <class 'tensorflow.python.framework.ops.EagerTensor'>
- tf.Variable의 type:
<class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
- tf.constant의 type:
<class 'tensorflow.python.framework.ops.EagerTensor'>
- Python data type 또는 numpy의 ndarray는 tensorflow의 tensor로 변환가능하다.
tensor_list_const = tf.constant([1, 2, 3])
tensor_np_const = tf.constant(np.array([1, 2, 3]))
print(f'tensor_list_const: {tensor_list_const}')
print('tensor_list_const(type): ', type(tensor_list_const))
print('=' * 80)
print(f'tensor_np_const: {tensor_np_const}')
print('tensor_np_const(type): ', type(tensor_np_const))
tensor_list_const: [1 2 3]
tensor_list_const(type): <class 'tensorflow.python.framework.ops.EagerTensor'>
================================================================================
tensor_np_const: [1 2 3]
tensor_np_const(type): <class 'tensorflow.python.framework.ops.EagerTensor'>
tensor_list_var = tf.Variable([1, 2, 3])
tensor_np_var = tf.Variable(np.array([1, 2, 3]))
print(f'tensor_list_var: {tensor_list_var}')
print('tensor_list_var(type): ', type(tensor_list_var))
print('=' * 80)
print(f'tensor_np_var: {tensor_np_var}')
print('tensor_np_var(type): ', type(tensor_np_var))
tensor_list_var: <tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([1, 2, 3], dtype=int32)>
tensor_list_var(type): <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
================================================================================
tensor_np_var: <tf.Variable 'Variable:0' shape=(3,) dtype=int64, numpy=array([1, 2, 3])>
tensor_np_var(type): <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
- Variable과 constant는 컨버팅 가능하다. (양방향으로 가능하다.)
tensor_const = tf.constant([1, 2, 3])
tensor_var = tf.Variable([1, 2, 3])
# Constant to Variable
cvt_const2var = tf.Variable(tensor_const)
# Variable to Constant
cvt_var2const = tf.constant(tensor_var)
cvt_var2const_2 = tf.convert_to_tensor(tensor_var)
print('cvt_const2var: ', cvt_const2var)
print('=' * 80)
print('cvt_var2const: ', cvt_var2const)
print('cvt_var2const_2: ', cvt_var2const_2)
cvt_const2var: <tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([1, 2, 3], dtype=int32)>
================================================================================
cvt_var2const: tf.Tensor([1 2 3], shape=(3,), dtype=int32)
cvt_var2const_2: tf.Tensor([1 2 3], shape=(3,), dtype=int32)
- Tensor의 사칙연산 결과는 EagerTensor(constant)가 된다.
add_const_const = tensor_const + tensor_const
add_const_var = tensor_const + tensor_var
add_var_var = tensor_var + tensor_var
print('add_const_const(type): ', type(add_const_const))
print('=' * 80)
print('add_const_var(type): ', type(add_const_var))
print('add_var_var(type): ', type(add_var_var))
Leave a comment