What is Tensorflow? - pai-plznw4me/tensorflow_basic GitHub Wiki
Deep in dive Tensorflow ์ฐ์ฌ ๊ธ์ ์ฒซ๋ฒ์งธ ํฌ์คํธ ์
๋๋ค.
Deep In Dive Tensorflow ์ฐ์ฌ๋ ํ
์ํ๋ก์ฐ๋ฅผ ์ข ๋ ๊น๊ฒ ์์๋ณด๊ธฐ ์ํ ๋ชฉ์ ์ผ๋ก ์ ์๋๊ณ ์์ต๋๋ค.
ํด๋น ํฌ์คํธ๋ tensorflow ๊ณต์ ํํ์ด์ง ๋ฐ tensorflow github ์ ๊ธฐ๋ฐ์ผ๋ก ์ ์ ๋์์ต๋๋ค.
1. Tensorflow๋?
๊ณต์ ๋ฌธ์๋ฅผ ๋ณด๋ฉด ์๋์ ๊ฐ์ด ํ ์ํ๋ก์ฐ๋ฅผ ์๊ฐํ ์ ์์ต๋๋ค.
TensorFlow๋ ๋จธ์ ๋ฌ๋์ ์ํ ์๋ ํฌ ์๋ ์คํ์์ค ํ๋ซํผ์ ๋๋ค.
๋๊ตฌ, ๋ผ์ด๋ธ๋ฌ๋ฆฌ, ์ปค๋ฎค๋ํฐ ๋ฆฌ์์ค๋ก ๊ตฌ์ฑ๋ ํฌ๊ด์ ์ด๊ณ ์ ์ฐํ ์ํ๊ณ๋ฅผ ํตํด ์ฐ๊ตฌ์๋ค์ ML์์ ์ฒจ๋จ ๊ธฐ์ ์ ๊ตฌํํ ์ ์๊ณ
๊ฐ๋ฐ์๋ค์ ML์ด ์ ๋ชฉ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์์ฝ๊ฒ ๋น๋ ๋ฐ ๋ฐฐํฌํ ์ ์์ต๋๋ค.
ํ์ฌ ์์ ํ
์ํ๋ก์ฐ๋ ์ฌ๋ฌ๊ฐ์ง ๊ธฐ๋ฅ๋ค์ด ์ถ๊ฐ ๋จ์ผ๋ก์ ํ๋ซํผ์ผ๋ก ์๊ฐ ๋๊ณ ์์ง๋ง
๊ธฐ๋ณธ์ ์ผ๋ก Tensorflow ๋ Deep Learning ์ ์ํ Framework ๋ผ๊ณ ์๊ฐํด๋ ๋ฌด๋ฐฉํฉ๋๋ค.
์ฆ, ํ
์ํ๋ก์ฐ๋ ์ ์ ๊ฐ ์ฝ๊ฒ ML Application ๊ตฌํ ํ๋๋ก ๋ผ๋ ๋ฐ ๋ผ๋์๋ค ์ด์ ๋ถ์ด๊ธฐ ์ํ ์ฌ๋ฌ๊ฐ์ง ๋๊ตฌ๋ค์ ์ ๊ณตํฉ๋๋ค.
ML Application ์ ๋ง๋ค๊ธฐ ์ํ ๋ณต์กํ ์ ์ด ๊ณผ์ ๋ฐ ๊ธฐํ ๊ณผ์ ์(state ๊ด๋ฆฌ , auto diff ๋ฑ) Tensorflow๊ฐ ๋ท๋จ์์ ์ฒ๋ฆฌํ๊ณ
์ ์ ๋ ๋ง๋ค๊ณ ์ ํ๋ ์๊ณ ๋ฆฌ์ฆ์๋ง ์ง์คํ ์ ์๋๋ก ํจ์ผ๋ก์ ํจ์จ์ ์ด๊ณ ๋ฅ๋ฅ ์ ์ผ๋ก ML Application์ ๋ง๋ค์ ์๊ฒ ๋์ ์ค๋๋ค.
์กฐ๊ธ๋ง ๋ ๊น๊ฒ Tensorlfow ๋ฐ๋ผ๋ณด๊ธฐ
ํ ์ํ๋ก์ฐ๋ฅผ ์ดํดํ๊ธฐ ์ํด Tensorflow ์ ์ค๋ช ํ ๋ ผ๋ฌธ์ ํ๋ฒ ์ด์ด ๋ด ์๋ค.
๋
ผ๋ฌธ์ ์๊ฐํ๋ ํ
์ํ๋ก์ฐ๋ ML ๋ชจ๋ธ ๊ตฌํ๊ณผ ML ์๊ณ ๋ฆฌ์ฆ์ ์ธํฐํ์ด์ค๋ผ๊ณ ์๊ฐ ํ๊ณ ์์ต๋๋ค.
์ฆ ์ฌ์ฉ์๊ฐ ์๊ฐํ๋ ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํ ํ ์ ์๋๋ก ํด์ฃผ๋ ๋ถ๋ถ์ Tensorflow ๊ฐ ๋ด๋นํ๊ณ ์๋ค๋ผ๋ ๊ฒ ์
๋๋ค.
๋ํ ํ
์ํ๋ก์ฐ๋ก ๊ตฌํ๋ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ๋ฌ ์์คํ
(heterogeneous)์ ๋ฐ๋ก ์ ์ฉํ๋ค๋ผ๊ณ ์ด์ผ๊ธฐ ํฉ๋๋ค.(๋ชจ๋ฐ์ผ์ด๋ ์ปดํจํฐ๋ ์๋ฒ ๋๋ ๊ธฐ๊ธฐ๋ ์๊ด์์ด)
์ค๋ฅธ์ชฝ ๊ธ์ HighLight ์ ์ดํด ๋ณด๋ฉด ํ
์ํ๋ก์ฐ๋ก ๊ตฌํ๋ ์๊ณ ๋ฆฌ์ฆ์ stateful dataflow garph
๋ก ํํ๋๋ค ๋ผ๋ ๋ฌธ์ฅ์ ํ์ธํ ์ ์์ต๋๋ค.
stateful
, dataflow
, graph
๋จ์ด๋ฅผ ํ๋ํ๋ ์ดํด๋ณด๋ฉด์ ํ
์ํ๋ก์ฐ๋ฅผ ์ข๋ ๊น๊ฒ ์ดํดํด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
Graph
Graph๋ Node์ Edge๋ก ์ด๋ฃจ์ด์ง ๋ถ๋ถ์งํฉ์ ์๋ฏธํฉ๋๋ค.
๊ทธ๋ํ(์์ด: graph, ๋ฌธํ์ด: ๊ทธ๋ผํ)๋ ์ผ๋ถ ๊ฐ์ฒด๋ค์ ์๋ค์ด ์๋ก ์ฐ๊ด๋ ๊ฐ์ฒด์ ์งํฉ์ ์ด๋ฃจ๋ ๊ตฌ์กฐ์ด๋ค.
์ผ๋ จ์ ๊ผญ์ง์ ๋ค๊ณผ ๊ทธ ์ฌ์ด๋ฅผ ์๋ ๋ณ๋ค๋ก ๊ตฌ์ฑ๋ ์กฐํฉ๋ก ์ ๊ตฌ์กฐ๋ก ๋ณผ ์ ์๋ค.
๊ทธ๋ํ๋ฅผ ์ฐ๊ตฌํ๋ ์ํ์ ๋ถ์ผ๋ฅผ ๊ทธ๋ํ ์ด๋ก ์ด๋ผ๊ณ ํ๋ค.
โ๊ทธ๋ํโ๋ผ๋ ์ฉ์ด๋ 1878๋ J. J. ์ค๋ฒ ์คํฐ์ ์ํด ์ฒ์ ์ฌ์ฉ๋์๋ค [from wiki] (https://ko.wikipedia.org/wiki/%EA%B7%B8%EB%9E%98%ED%94%84_(%EC%88%98%ED%95%99))
์ ๊ทธ๋ฆผ์์ ๋ณด๋๋ ๋๊ทธ๋ผ๋ฏธ๋ฅผ Node
๋ผ ๋ถ๋ฅด๊ณ ๋
ธ๋์ ๋
ธ๋๋ฅผ ์ฐ๊ฒฐํ๋ ์ ์ Edge
๋ผ๊ณ ๋ถ๋ฆ
๋๋ค.
Tensorflow ๋ ์ Graph์ ํ์ฉํด ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํํฉ๋๋ค.
Graph ์ ๋
ธ๋๋ ํน๋ณํ Operation
์ด๋ผ๊ณ ๋ถ๋ฅด๊ณ , Edge ๋ Tensor
๋ผ๊ณ ๋ถ๋ฆ
๋๋ค.
์๊ณ ๋ฆฌ์ฆ์ ๋ง์ ์ฐ์ฐ ๋ฐ ๊ธฐ๋ฅ๋ค๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.
ํ
์ํ๋ก์ฐ๋ ์ฐ์ฐ ๋ฐ ๊ธฐ๋ฅ์ Operation ์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ Operation ๊ณผ Operation ์ผ๋ก ์ฐ๊ฒฐ๋๋ Edge ๋ Tensor ๋ผ๊ณ ํํํฉ๋๋ค.
๊ทธ๋ผ Tensor์ Operation ์ด ์ ํํ ๋ฌด์์ธ์ง ์ดํด ๋ด
์๋ค.
Tensor
ํ ์ํ๋ก์ฐ ๊ณต์ ํํ์ด์ง์ Tensor์ ์ ์๋ฅผ ๋ณด๋ฉด ์๋์ ๊ฐ์ต๋๋ค.
Tensors are multi-dimensional arrays with a uniform type (called a dtype). You can see all supported dtypes at tf.dtypes.DType.
ํ ์ํ๋ก์ฐ์์๋ ํ ์๋ฅผ ๋ค์ฐจ์์ด๋ฉด์ ๋์์ ๋ฐฐ์ด์ ๋ชจ๋ element ์ DataType ์ด ๋์ผํ ๋ฐฐ์ด ์ด๋ผ๊ณ ์ ์ ํ์์ต๋๋ค.
์๋ ์์๋ฅผ ์ฝ๊ฒ ์ดํดํ ์ ์์ต๋๋ค. ์๋ ๊ทธ๋ฆผ์ ๋ณด๋ฉด 0์ฐจ์ , 1์ฐจ์, 2์ฐจ์ ,3์ฐจ์ ํ
์๋ฅผ ํ์ธํด ๋ณผ ์ ์์ต๋๋ค.
(๋ง์ฝ ์ฐจ์์ ํ์
ํ๊ธฐ ์ด๋ ต๋ค๋ฉด ๋ฐฉ๋ฒ์ ์๋ ๋๊ดํธ ๊ฐ ์๋ฅผ ์ธ๋ ๊ฒ ์
๋๋ค. 3์ฐจ์์ ์๋ ๋๊ดํธ์ ๊ฐ ์ ๊ฐ 3๊ฐ์
๋๋ค.)
ํ
์ํ๋ก์ฐ์์ ์ฐ๋ฆฌ๋ ๋ฐ์ดํฐ๋ฅผ ํ
์๋ก ํํํฉ๋๋ค.
์๋์ ๊ฐ์ ์์๊ฐ ์๋ค๋ฉด ์ฐ๋ฆฌ๋ ๋ฐ์ดํฐ๋ฅผ ์๋์ ๊ฐ์ด ํ
์๋ก ํํํ ์ ์์ต๋๋ค.
Cancer size | Age | label |
---|---|---|
9 | 3 | malignant |
8 | 9 | benign |
2 | 2 | benign |
import tensorflow as tf
data = tf.constant([9, 3, 1], [8, 2, 0], [2, 2, 0](/pai-plznw4me/tensorflow_basic/wiki/9,-3,-1],-[8,-2,-0],-[2,-2,-0))
print(x)
# >>> <tf.Tensor: shape=(3, 3), dtype=int32, numpy=
# array([[9, 3, 1],
# [8, 2, 0],
# [2, 2, 0]], dtype=int32)>
Operation
ํ
์ํ๋ก์ฐ Node ์ Operation ์ด๋ผ๊ณ ๋ถ๋ฆ
๋๋ค.
ํ
์ํ๋ก์ฐ์์ Opearation ์ ์ ์๋ฅผ ์ดํด ๋ณด๊ฒ ์ต๋๋ค.
An Operation is a node in a tf.Graph that takes zero or more Tensor objects as input, and produces zero or more Tensor objects as output. Objects of type Operation are created by calling a Python op constructor (such as tf.matmul) within a tf.function or under a tf.Graph.as_default context manager.
์์ฝ ํ์๋ฉด Operation ์ input ์ผ๋ก Tensor์ 0๊ฐ ์ด์ ๋ฐ์์ ์๊ณ ์ถ๋ ฅ ๋ํ 0๊ฐ ์ด์์ Tensor์ ์ถ๋ ฅ์ผ๋ก ๋ด๋ณด๋ผ์ ์๋ค.
๋ผ๊ณ ์ด์ผ๊ธฐ ํ๊ณ ์์ต๋๋ค. ์ข ๋ ์๊ฐํด ๋ณด๋ฉด Operation์ ์
๋ ฅ์ผ๋ก ์๋ฌด๊ฒ๋ ๋ฐ์ง ์์์ ์์ง๋ง ๋ฐ๋๋ค๋ฉด ํ
์๋ฅผ ๋ฐ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ถ๋ ฅ ๋ํ ์๋ฌด๊ฒ๋ ์ถ๋ ฅํ์ง ์์์ ์์ง๋ง ์ถ๋ ฅ์ ํ๋ค๋ฉด ํ๋ ์ด์์ ํ
์๋ฅผ ์ถ๋ ฅํ๋ค. ๋ผ๊ณ ์ด์ผ๊ธฐ ํฉ๋๋ค.
์ฆ ์ด๋ป์ Operation์ ์ฃผ์ ๊ธฐ๋ฅ์ ํ
์๋ฅผ ๊ณ์ฐํ๊ฑฐ๋ ์ฐ์ฐํ๋ ๊ธฐ๋ฅ์ ๊ฐ์ง๊ณ ์๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
์๋ฅผ ๋ค์ด๋ณด์๋ฉด
๊ฐ๋ น 3 + 2 ๋ฅผ ํด๋ณธ๋ค๊ณ ํ์ ๋
์์ฐจ์ ์ผ๋ก ์๊ฐํด ๋ณด๋ฉด ์๋์ ๊ฐ์ต๋๋ค.
3์ด๋ผ๋ ์์๋ฅผ ๋ง๋ค์ด์ผ ํ๊ณ
2๋ผ๋ ์์๋ฅผ ๋ง๋ค์ด์ผ ํ๊ณ
3๊ณผ 2๋ฅผ ๋ํ๋ ์ฐ์ฐ์ ์ํํด์ผ ํฉ๋๋ค.
์ด๋ฅผ Tensorflow ๋ก ๊ตฌํํ๋ฉด ์๋์ ๊ฐ์ต๋๋ค.
# 3์ด๋ผ๋ ์์๋ฅผ ๋ง๋ญ๋๋ค.
a = tf.constant(3)
# 2์ด๋ผ๋ ์์๋ฅผ ๋ง๋ญ๋๋ค.
b = tf.constant(2)
# a, b๋ฅผ ๋ํฉ๋๋ค.
c = tf.add(a, b)
์ ์ฝ๋์์ tf.constant
ํจ์๋ ์
๋ ฅ์ผ๋ก ์ซ์๋ฅผ ๋ฐ๊ณ ํด๋น ์ซ์๋ฅผ Tensor๋ก ๋ณํํ๋ ๊ธฐ๋ฅ์ ํฉ๋๋ค.
์ฆ ๊ธฐ๋ฅ์ ํ๊ธฐ์ ์ ํ
์ํ๋ก์ฐ ํจ์๋ Operation
์ ์์ฑํ๋ ๊ธฐ๋ฅ์ ๊ฐ์ง๊ณ ์๋ค๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ํ ๊ฐ์ง ์๊ฐ์ ๋ํ์๋ฉด ํด๋น Operation ์ ์ถ๋ ฅ๊ฐ์ return ํ๋ ๊ฒ ๋ ์์ ์์ต๋๋ค.
์ฝ๋๋ฅผ ๋ณด๋ฉด์ ์๊ฐํด ๋ด ์๋ค.(์๋ด๋ ๊ด์ฐฎ์ต๋๋ค.)
@tf_export("constant", v1=[])
def constant(value, dtype=None, shape=None, name="Const"):
# ...(์๋ต)
g = ops.get_default_graph()
tensor_value = attr_value_pb2.AttrValue()
tensor_value.tensor.CopyFrom(
tensor_util.make_tensor_proto(
value, dtype=dtype, shape=shape, verify_shape=verify_shape,
allow_broadcast=allow_broadcast))
dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
attrs = {"value": tensor_value, "dtype": dtype_value}
const_tensor = g._create_op_internal( # pylint: disable=protected-access
"Const", [], [dtype_value.type], attrs=attrs, name=name).outputs[0]
if op_callbacks.should_invoke_op_callbacks():
callback_outputs = op_callbacks.invoke_op_callbacks(
"Const", tuple(), attrs, (const_tensor,), op_name=name, graph=g)
if callback_outputs is not None:
const_tensor, = callback_outputs
return const_tensor
์ฌ๊ธฐ์ ์ฃผ์ ๊น๊ฒ๋ด์ผ ๋ด์ผ ํ ์ฝ๋๋ ์๋ ์ฝ๋ ์
๋๋ค.
ํนํ tf.constant ํจ์ ๋ด์ฉ์ค _create_op_internal
์ ์ดํด๋ด
์๋ค.
ํด๋น ์ฝ๋๋ฅผ ์ดํด ๋ณด๋ฉด ์ธ๋ถ์ ๋ณด์ด์ง ์์ผ๋ฉด์ ๋ด๋ถ์ ์ผ๋ก operation ์ ์์ฑํ๋ค๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ graph instance ์ method์ธ๊ฒ์ ๋ณด์ graph ์ operation ์ ์ถ๊ฐํ๋ค. ๋ผ๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
single underscore ์์ผ๋ก ์ธ๋ถ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ง์ง ์์ ํจ์์์ ์์ ์์ต๋๋ค.
์ด๋ ์ฆ ํ
์ํ๋ก์ฐ๊ฐ operation ์์ฑ์ tensorflow ๊ฐ ์ ๊ณตํ๋ op.constructor ์ ์ฌ์ฉํด operation ์ ์์ฑํ๋๊ฒ์ ์๋ํ๊ณ ์์์ ์ดํด ๋ณผ์ ์์ต๋๋ค.
const_tensor = g._create_op_internal( # pylint: disable=protected-access
"Const", [], [dtype_value.type], attrs=attrs, name=name).outputs[0]
๊ทธ๋ฆฌ๊ณ ํด๋น ํจ์(_create_op_internal
)์ outputs ๋ก๋ ์ฌ๋ฌ๊ฐ์ tensor ๊ฐ ์์ฑ๋จ์ ํ์
ํ ์ ์์ต๋๋ค.
Dataflow
์ Tensorflow ์ Dataflow ๋ผ๊ณ ํ ๊น์?
ํด๋น ์ฌ์ ์ ๋ํด ์๊ฐํด ๋ณด๊ฒ ์ต๋๋ค.
์ฐ๋ฆฌ๋ tensor ๋ถ๋ถ์์ data ์ tensor๋ก ํํ ํ ์ ์๋๋ ๊ฒ์ ๋ฐฐ์ ์ต๋๋ค.
์ฆ ์ด๋ป์ data ๊ฐ ํ๋ฅธ๋ค, Tensor ๊ฐ ํ๋ฅธ๋ค๋ก ์ดํด ํ ์ ์์ต๋๋ค.
๊ทธ๋ผ ์ด๋๋ก ํ๋ฅผ๊น? ๋ผ๊ณ ์๊ฐํ ์ ์๋๋ฐ ๊ทธ๊ฒ์ ๋ฐ๋ก operation ๊ณผ operation ์ฌ์ด๋ฅผ ํ๋ฅธ๋ค ๋ผ๋ ๊ฒ ์
๋๋ค.
์ฆ Tensorflow ๋ operation ๊ณผ operation ์ฌ์ด๋ฅผ tensor๊ฐ ํ๋ฅด๊ฒ ํฉ๋๋ค.
ํ๋ฅธ๋ค๋ผ๋ ๊ฐ๋
์ด ๋ชจํธํ๊ฒ ๋๊ปด์ง์ ์๋๋ฐ ์๋ ์์๋ฅผ ๋ณด๋ฉด์ ๋ณด๋ค ์์ธํ ์ค๋ช
ํ๋๋ก ํ๊ฒ ์ต๋๋ค.
์ ๊ทธ๋ํ์์ ๊ฐ๋ น tensor d ์ ์์ฑํ๋ค๊ณ ๊ฐ์ ํ์๋
tensor d ์ ์ํํ๊ธฐ ์ํด tensor a, b ์ ์์ฑํด์ผ ํ๊ณ
์์ฑ๋ tensor a, b๋ ์๋ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด + operation ์ผ๋ก ํ๋ฌ๊ฐ์ผ ํฉ๋๋ค.
๋ฐ๋ฉด tensor d ๋ฅผ ์์ฑํ ์ ํ์ ์๋ tensor c ๋ ์์ฑ๋์ง ์์ต๋๋ค.
์ด ์ ์ด ๋ฐ๋ก tensorflow ์ dataflow ํน์ง์ ๋ณด์ฌ์ค๋๋ค.
ํน์ operation๋ tensor ์ ์คํ ํ๊ธฐ ์ํด ํ์ํ ๋
ธ๋๋ ํ
์๋ค๋ง ์ํ ํ๋ค๋ ์ ์ด์ฃ .
(โ ๋ฌผ๋ก ์ด ๊ฐ๋
๋ tensorflow 2.x ์ค๋ฉด์ graph ์ ์๊ฒ ์๊ฒ ์ชผ๊ฐ๋ฉด์ ํด๋น ํน์ง๋ค์ด ์ ์ ๋ค์๊ฒ ๋๋๋ฌ์ ธ ๋ณด์ด์ง ์์ต๋๋ค. )
ํ์ง๋ง ๋จ์ํ graph ๊ฐ๋
์์๋ ์ฐ๊ฒฐ๋ ๋
ธ๋ ์ฌ์ด์ ์์๋ ๊ฒฐ์ ๋์ง ์์ต๋๋ค. ๋จ์ํ ์ฐ๊ฒฐ ๋์ด ์์ ๋ฟ์ด์ฃ .
๊ทธ๋์ Tensorflow ์์๋ ์ด๋ค ๋
ธ๋๊ฐ ๋จผ์ ์คํ ๋์ด์ผ ํ๋ dependnecy์ ์ง์ ํฉ๋๋ค.
(โ ๋ฌผ๋ก ์ด ๊ฐ๋
๋ tensorflow 2.x ์ค๋ฉด์ tensorflow ์์ฒด์์ ์ฒ๋ฆฌํ๊ฒ ๋ฉ๋๋ค.)
์ง์ ๋ ์์์ ๋ฐ๋ผ ๋
ธ๋๋ฅผ ์คํํ๊ณ ๋ค์ ๋
ธ๋๋ก tensor์ ํ๋ ค์ฃผ๊ฒ ๋ฉ๋๋ค.
์ฐ๋ฆฌ๋ ์ด ๊ณผ์ ๋ค์ ํตํด tensorflow๊ฐ ์ฐจ์ฉํ๊ณ ์๋ dataflow ๊ฐ๋ ์ ๋ํด ๋ฐฐ์ฐ๊ณ ํด๋น ๊ฐ๋ ์ ํตํด ํ ์ํ๋ก์ฐ๋ฅผ ๋ณด๋ค ๊น๊ฒ ์ดํดํ ์ ์์์ต๋๋ค.
Stateful
Stateful , Stateless ๋ ์ผ๋ฐ์ ์ผ๋ก server-client ์์ ์์ฃผ ์ฌ์ฉ๋๋ ์ฉ์ด ์
๋๋ค.
Stateful ์ด๋ ์๋ฒ๊ฐ client ์ ์ํ๋ฅผ ์ ์ฅํ๋ ๊ฒ์ด์ฃ .
Tensorflow ๋ Graph ์ํ๋ฅผ ์ ์ฅํ๊ธฐ ์ํด Session
๋ฐฉ๋ฒ์ ์ฌ์ฉํฉ๋๋ค.
(๋ค ๋ง์ต๋๋ค. ๋ณดํต Web programming ์์ client ์ ์ํ๋ฅผ ์ ์ฅํ๊ธฐ์ํด ์ฌ์ฉ๋๋ cookie , session์์ ๊ทธ session ๊ฐ๋
๊ณผ ์ ์ฌํฉ๋๋ค.)
์ง๊ธ์ tensorflow 2.x ๋ฒ์ ์ด ๋์ค๋ฉด์ session์ ๊ธฐ๋ฅ์ด ๋ค๋ก ๊ฐ์ณ์ก์ง๋ง
๊ทธ๋๋ ํด๋น ๊ฐ๋
์ ์ตํ๋๊ฒ์ ํ
์ํ๋ก์ฐ๋ฅผ ๋ณด๋ค ๊น๊ฒ ์กฐ๋งํ ์ ์๋ ๊ธฐํ๋ฅผ ์ค๊ฑฐ๋ผ ์๊ฐํฉ๋๋ค.
tensorflow 1.x ๋ฒ์ ์ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ฉด์ Tensorflow ์์ stateful ์ด๋ผ๋ ๊ฐ๋
์ด ์ด๋ป๊ฒ ์ฌ์ฉ๋๋์ง ํ์
ํด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
Graph ์ ์ ์์๋ ์ค์ ๊ฐ์ด ์์ฑ ๋์ง ์์ต๋๋ค.
๋จ์ํ ์ด๋ค Tensor๊ฐ ์ด๋ค Operation ์ผ๋ก ํ๋ฌ๊ฐ์ง๋ฅผ ๊ฒฐ์ ํ ๋ฟ์ด์ฃ .
Python ์ Interpreter ์ธ์ด์ด๊ณ ๋ฐ๋ก ๋ฐ๋ก ๊ฐ๋ค์ด ์ถ๋ ฅ๋ฉ๋๋ค.
ํ์ง๋ง Tensorflow 1.x ์ฝ๋๋ ๋ฐ๋ก๋ฐ๋ก ๊ฐ์ ์์ฑํ์ง ์์ต๋๋ค.
(ํด๋น ํฌ์คํธ์์๋ Tensorflow eager excution ์ ๊ณ ๋ คํ์ง ์์ต๋๋ค.)
์์๋ฅผ ๋ค์ด ๋ณด๊ฒ ์ต๋๋ค.
์๋ ๊ทธ๋ฆผ์์ 2๊ฐ์ Random Variable(์ ๊ท ๋ถํฌ)์ ์์ฑํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ ๋๊ฐ์ ๋ณ์๋ฅผ ๋ํ๋ ๊ฐ๋จํ Graph ์
๋๋ค.
์ด ๋ Graph ์ ์์ฑํ๋ค๊ณ ํด์ ํ
์(a, b, c)์ ๊ฐ์ด ๋ค์ด๊ฐ ์์ง ์์ต๋๋ค.
a_init = tf.random.normal(shape=[])
a = tf.Variable(a_init)
b_init = tf.random.normal(shape=[])
a = tf.Variable(b_init)
c = tf.add(a, b)
print(a, b, c)
graph ์ ๋ง๋ ๋ค๋ ๊ฒ์ ๋จ์ํ ์ด๋ค ํ
์๊ฐ ์ด๋ค operation ์ ๋ค์ด๊ฐ๊ณ ์ถ๋ ฅ๋๋์ง๋ฅผ ์ ์ ํ ๋ฟ์
๋๋ค.
์ค์ ๋ก Graph ์ ๊ฐ ์์๋ฅผ ๊ตฌ๋์ํค๊ณ ๊ฐ์ ์ป์ด๋ผ๋ ค๋ฉด Graph๋ฅผ ์คํํ๋ Session ์ ์์ฑํด์ผ ํฉ๋๋ค.
์๋ ์ฝ๋์์ Session ์ instance ์ ์์ฑํ๊ณ tensor c ์ ์คํ ์์ผ ๋ด
๋๋ค.
์์์ ๋ณธ ๊ฒ ์ฒ๋ผ Graph๊ฐ ์๋๋ผ Session์ ํตํด Graph ์ ์ค์ ๊ฐ๋ค์ ์คํํ๊ณ Variable์ ์ํ(state)๋ฅผ ์ ์ฅ ํ ์ ์์ต๋๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ Tensorflow ๊ฐ stateful
ํ๋ค๋ผ๊ณ ๋งํ ์ ์์ต๋๋ค.
๊ณ์ ๋ณํํ๋ ๋ณ์ ์ํ์ ๊ฐ์ ์ ์ฅ ํ ์ ์๋ ๊ฒ์ด์ฃ .
# define graph
a_init = tf.random.normal(shape=[])
a = tf.Variable(a_init)
b_init = tf.random.normal(shape=[])
a = tf.Variable(b_init)
c = tf.add(a, b)
# execute graph
sess = tf.Session()
sess.run(c)
์ด๋ฐ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋ฉด ๋ค์ํ ํ๊ฒฝ์์ ์ด์ ์ด ๋ง์ต๋๋ค.
์๋ ์์๋ฅผ ํตํด ์ด๋ค ์ด์ ์ด ์๋์ง ์ดํด ๋ด
๋๋ค.
ํ๋์ Graph์ ์์ฑํ๊ณ ์ฌ๋ฌ๊ฐ์ Session ์ ์์ฑํด ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค.
์ด๋ ๋ณ๋ ฌ์ฒ๋ฆฌ์ ๋งค์ฐ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋๊ฑฐ๋ ํด๋น ํ๋ก๊ทธ๋จ์ Web ์๋น์ค์ ๋ถ์ฐ์ฒ๋ฆฌ์ ๋งค์ฐ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋ ์ ์์์ ์์ ์์ต๋๋ค.
(์ค์ ๋ก Web programming ์์ Web Framework(ex. django)๊ฐ WAS ์๋ฒ์์ ์๋๋๋ ์๋ฆฌ๋ฅผ ์๊ฐํด๋ณด๋ฉด ์์ ์์ต๋๋ค.)
ํ๋์ ๊ทธ๋ํ๋ฅผ ์ฌ๋ฌ๊ฐ์ Session์ ์ฌ์ฉํด ์์ฑํ๋ ์ฝ๋๋ฅผ ์์ฑํด ๋ด ๋๋ค.
%tensorflow_version 1.x
import tensorflow as tf
# Define graph
a_init = tf.random.normal(shape=[])
a = tf.Variable(a_init)
b_init = tf.random.normal(shape=[])
a = tf.Variable(b_init)
c = tf.add(a, b)
# Generate Multi Session
sess1 = tf.Session()
print(sess1.run(c))
sess2 = tf.Session()
print(sess2.run(c))
sess3 = tf.Session()
print(sess3.run(c))
# >>> 1.3981419
# -0.042078532
# 0.48863256
์ฌ๊ธฐ์ ๋งค๋ฒ ์๋ก์ด ๊ฐ์ ์์ฑํ๋ Random Variable ์ ํตํด ๊ฐ Session ๋ง๋ค ๋ค๋ฅธ ๊ฐ๋ค์ด ๋ค์ด๊ฐ ์๋ ๊ฒ๋ค์ ํ์ธํด ๋ณผ ์ ์์ต๋๋ค.
์ฆ Session ์ ์๋ก ๋ถ๋ฆฌ๋์ด ์์ผ๋ฉฐ ๊ฐ๋ค์ ๊ณต์ ํ์ง ์๋๋ค๋ ๊ฒ๋ค์ ํ์ธํ ์ ์์ต๋๋ค.
์ด๋ฅผ ํตํด ํ๋์ ๊ทธ๋ํ๋ฅผ ๊ฐ์ง๊ณ ์ฌ๋ฌ ์ ์ ์๊ฒ ๋
๋ฆฝ์ ์ธ ์๋น์ค๋ฅผ ์ ๊ณต์ด ์๋นํ ๊ฐ๋จํด ์ง๊ฒ๋ค์ ํ์ธ ํ ์ ์์ต๋๋ค.
Sum up
ํด๋น ํฌ์คํธ๋ฅผ ํตํด Tensorflow ๊ฐ ์ด๋ค ๋ชฉ์ ์ผ๋ก ์ค๊ณ๋์ด ์๊ณ ์ด๋ป๊ฒ ์ ์ ๋ด๋ฆด์ ์๋์ง๋ฅผ ์ดํด ๋ณด์์ต๋๋ค.
ํนํ tensorflow ์ ์ค๋ช
ํ๋ ํค์๋์ธ stateful
graph
dataflow
์ ๋ํ ์ ์๋ฅผ ๋ณด๋ค ์์ธํ ๋ค์ฌ๋ค ๋ณด๋ฉด์
tensorflow ์ ์ค๊ณ์ ์์ด๋์ด ๊ทธ๋ฆฌ๊ณ ํ์ฉ์ ์ดํด ๋ณด์์ต๋๋ค.
๊ฐ์ฌํฉ๋๋ค.