Eager execution Vs Graph execution - pai-plznw4me/tensorflow_basic GitHub Wiki
Eager execution Vs Graph execution
ν΄λΉ ν¬μ€νΈμμλ Tensorflowμ Eager execution κ³Ό Graph executionμ μ°¨μ΄μ μ μλ μ£Όμ λ‘ μ€λͺ ν©λλ€.
- Debug
- λ³μ κ΄λ¦¬
- Computation Speed
Debug
Graph Execution Mode
Tensorflow 2.x
λ²μ μΌλ‘ μ
λ°μ΄νΈ λλ©΄μ
Tensorflow
λ Eager Excution
μ΄λΌλ Tensorflow 1.x
μ μλ μλ‘μ΄ κ°λ
μ λμ
ν©λλ€.
κΈ°μ‘΄μ Tensorflow 1.x
μ Graph
μ μμ± ν ν
ν΄λΉ κ·Έλνμ μ°κ²°λ Session
μ μμ±νκ³ Session
μ ν΅ν΄ μ€ννκ³ μ νλ Tensor
λλ Operation
μ μνν΄ μνλ κ²°κ³Όλ₯Ό μ»λ κ³Όμ μ μν νμμ΅λλ€.
μλ μμ μ½λλ₯Ό μ΄ν΄ λ΄
μλ€.
# create new graph
g = tf.Graph()
# set graph to default graph
# add node, tensor to graph
with g.as_default():
a = tf.constant(3)
b = tf.constant(5)
c = a + b
# create session with graph g
sess = tf.Session(graph=g)
# get result from tensor c
sess.run(c)
μ μμ μμ 4κ°μ§ λ¨κ³λ‘ Tensorflow 1.x
κ° μνλλ κ²μ μμ μμ΅λλ€.
Graph
μμ±Graph
μ λ Έλ λ° ν μ μΆκ°Session
μμ± λ°Session
κ³ΌGraph
μ°λ- μννκ³ μ νλ
Tensor
μν
νμ§λ§ Tensorflow 1.x
μ μ½λ ꡬν λ°©μμ Python μ μ₯μ μ μΆ©λΆν μ΄λ¦¬μ§ λͺ»νλ λ¨μ μ΄ μμ΅λλ€.
Python μ Interpreter
μΈμ΄λ‘μ μ€ν κ²°κ³Όλ₯Ό λ°λ‘λ°λ‘ μμ μμ΅λλ€.
μ΄λ₯Ό ν΅ν΄ λλ²κ·Έλ λ§€μ° νΈνλ€λ μ₯μ μ κ°μ§κ³ μμ΅λλ€.
νμ§λ§ Graph
μ λͺ¨λ μμ±ν ν Session
μ ν΅ν΄ μνλ κ²°κ³Όλ₯Ό μ»μ΄μΌ νλ Tensorflow 1.x
λ
λ°λ‘ λ°λ‘ κ²°κ³Όλ₯Ό νμΈν μ μμ΅λλ€.
μ΄λ μ½λ λλ²κ·Έλ₯Ό μ΄λ ΅κ² ν©λλ€.
Tensorflow 1.x λ²μ μμμ Debug λ°©λ²μΌλ‘ Tensorboard
λλ tfdbg
μ μΆμ²ν©λλ€.
Eager Execution mode
Tensorflow 2.x λ²μ μμλ Tensorflow 1.x μ λ¨μ μ 극볡νκ³ μ Eager execution
κΈ°λ₯μ κΈ°λ³Έ λͺ¨λλ‘ μ 곡ν©λλ€.
Eager Execution λͺ¨λλ Graphμ μμ±νμ§ μκ³ κ³μ°κ°μ λ°λ‘λ°λ‘ μλ €μ£Όλ λͺ
λ Ήν(imperative) νλ‘κ·Έλλ° νκ²½μ
λλ€.
Eager Execution λͺ¨λλ python μ½λλ₯Ό μλμν€λ μΌλ°μ μΈ κ³Όμ κ³Ό λ§€μ° μ μ¬ν©λλ€. μ¦ νμ€ νμ€ λ°λ‘ μ€ννλ©΄μ λ°λ‘ κ²°κ³Όλ₯Ό νμΈν΄ λ³Ό μ μμ΅λλ€.
a = tf.constant(3)
b = tf.constant(5)
c = a + b
κ·Έλ κΈ°μ μ κ·Έλ¦Όμ²λΌ ν μ μμ μλ κ°λ€μ λ°λ‘λ°λ‘ νμΈ κ°λ₯ν©λλ€.
λ³μ κ΄λ¦¬
Graph
Tensorflow Graphμ λͺ¨λ λ
Έλ
μ ν
μ
μλ κ³ μ ν μ΄λ¦μ κ°μ§κ³ μμ΅λλ€.
tf.constant(3, name='const')
μ μ½λλ₯Ό μννκ² λλ©΄ μ΄λ¦μ΄ 'const'μΈ operation
κ³Ό μ΄λ¦μ΄ 'const:0' ν
μ
λ₯Ό λ§λ€κ² λ©λλ€.
κ·Έλ κΈ°μ python λ³μμ κ²°κ³Όλ₯Ό μμλ μ°λ¦¬λ tensor
λλ operation
μ νΈμΆ ν μ μμ΅λλ€.
μλ μ½λλ₯Ό ν΅ν΄ νμΈν΄ λ³΄κ² μ΅λλ€.
tf.constant(3, name='const')
# tensorflow version 1.x
g = tf.get_default_graph()
# call 'const:0' tensor
g.get_tensor_by_name('const:0')
# call 'const' operation
g.get_operation_by_name('const')
μ μ½λλ₯Ό 보면
Graphλ λ
Έλμ ν
μμ κ³ μ ν μ΄λ¦μ λΆμ¬νκ³ κ·Έ μ΄λ¦μΌλ‘ ν
μμ λ
Έλλ₯Ό κ΄λ¦¬νκΈ° λλ¬Έμ
μλ©΄ κ΅³μ΄ νμ΄μ¬ λ³μμ λ±λ‘νμ§ μμλ λλ€λ μ μ
λλ€.
Eager Execution
λ°λ©΄ Eager Executionμ νμ΄μ¬ λ³μλ₯Ό ν΅ν΄ Tensorμ κ΄λ¦¬ν©λλ€.
a = tf.constant(3, name='const')
Computation Speed
κ³μ° μ€νΌλλ Graph κ° Eager Execution λ³΄λ€ λΉ λ¦
λλ€.
eager execution μ line-by-line μΌλ‘ νμ€νμ€ μ€ννμ§λ§
graph execution μ compile μ ν΅ν΄ ν¨μ¨μ μΌλ‘ κ·Έλνλ₯Ό build νκ³ μννκΈ° λλ¬Έμ eager execution λ³΄λ€ λΉ λ₯΄κ² μν λ©λλ€.
4μΈ΅ μ§λ¦¬ Dense Layer μ λ§λ ν μνν΄ λ³΄λ©΄μ μλλ₯Ό νμΈν΄λ΄ λλ€.
Eager execution mode
# tensorflow 2.x eager Execution
import tensorflow as tf
import numpy as np
import time
from tqdm import tqdm
# load MNIST Dataset
train_data, test_data = tf.keras.datasets.mnist.load_data()
(train_xs, train_ys) = train_data
train_xs = train_xs.reshape([-1, 784])
train_xs = train_xs.astype(np.float32)
# Generate W, b
def generate_w_b(in_, out):
w_init = tf.random.normal([in_, out])
w = tf.Variable(w_init, dtype=tf.float32)
b_init = tf.zeros(out)
b = tf.Variable(b_init,dtype=tf.float32)
return w, b
w1, b1 = generate_w_b(784, 128)
w2, b2 = generate_w_b(128, 128)
w3, b3 = generate_w_b(128, 128)
w4, b4 = generate_w_b(128, 10)
# Run Dense Layer , 100 times
s = time.time()
for i in tqdm(range(100)):
layer = train_xs
z1 = tf.matmul(layer, w1) + b1
a1 = tf.nn.relu(z1)
z2 = tf.matmul(a1, w2) + b2
a2 = tf.nn.relu(z2)
z3 = tf.matmul(a2, w3) + b3
a3 = tf.nn.relu(z3)
logits = tf.matmul(a3, w4) + b4
print('Consume time : {}'.format(time.time() - s))
# >>> 100%|ββββββββββ| 100/100 [00:31<00:00, 3.22it/s] Consume time : 31.025146961212158
Graph execution mode
μ°μ°μ΄ μνλλ λΆλΆμ Graph λ‘ λ³νν μ°μ°ν©λλ€.
# tensorflow 2.x Graph Execution
import tensorflow as tf
import numpy as np
import time
from tqdm import tqdm
# load MNIST Dataset
train_data, test_data = tf.keras.datasets.mnist.load_data()
(train_xs, train_ys) = train_data
train_xs = train_xs.reshape([-1, 784])
train_xs = train_xs.astype(np.float32)
# Generate W, b
def generate_w_b(in_, out):
w_init = tf.random.normal([in_, out])
w = tf.Variable(w_init, dtype=tf.float32)
b_init = tf.zeros(out)
b = tf.Variable(b_init,dtype=tf.float32)
return w, b
w1, b1 = generate_w_b(784, 128)
w2, b2 = generate_w_b(128, 128)
w3, b3 = generate_w_b(128, 128)
w4, b4 = generate_w_b(128, 10)
# Run Dense Layer, 100 times
@tf.function
def graph_execution(layer):
z1 = tf.matmul(layer, w1) + b1
a1 = tf.nn.relu(z1)
z2 = tf.matmul(a1, w2) + b2
a2 = tf.nn.relu(z2)
z3 = tf.matmul(a2, w3) + b3
a3 = tf.nn.relu(z3)
logits = tf.matmul(a3, w4) + b4
# execute Graph, 100 times
s = time.time()
for i in tqdm(range(100)):
graph_execution(train_xs)
print('Consume time : {}'.format(time.time() - s))
# >>> 100%|ββββββββββ| 100/100 [00:00<00:00, 847.78it/s] Consume time : 0.12074828147888184
μλκ° Eager Execution mode
μ Graph Execution Mode
κ° μ½ 266λ°° μ λ μ°¨μ΄κ° λλκ²μ νμΈν μ μμ΅λλ€.
Sum-up
Tensorflow
μ ν΅ν΄ μ°μ°(Computation)μ μννλ λ°©λ²μ ν¬κ² Eager execution
, Graph execution
λͺ¨λκ° μμ΅λλ€.
Eager execution
λͺ¨λλ ν΅ν΄ κΈ°μ‘΄μ μ¬μ©νλ νμ΄μ¬ λ³μ κ΄λ¦¬ λ°©μμ μ¬μ©ν μ μμΌλ©° Debug λͺ¨λλ₯Ό μ¬μ© κ°λ₯ν©λλ€.
νμ§λ§ Graph λͺ¨λλ³΄λ€ μλκ° λ립λλ€.
κ·Έλ κΈ°μ μ°λ¦¬λ Eager execution
μ ν΅ν΄ λ¨Όμ λμκ°λ μ½λλ₯Ό λ§λ€κ³ μ΄ν
eager Execution
μμ Computation
λΆλΆμ Graph Execution
μΌλ‘ λ°κΎΈλ μ½λ μμ
μ μννκ² λ©λλ€.