Graph나 session을 생성하지 않고 즉시 실행 가능함을 뜻한다.
쓰여진 순서에 따라 즉시 실행되는 것이 Eager Execution으로, 결과를 바로 확인할 수 있다.
TensorFlow 2.0 이후 버전부터 해당되면, Keras(High-Level API)만 제공
TensorFlow 1.15 vs TensorFlow 2.x
import tensorflow as tf
a = tf.constant(1.0) b = tf.constandt(2.0) c = a + b print(c)
TensorFlow 1.x는 다음과 같이 저장된 값이 아니라 현재 정의 되어 있는 노드의 상태가 출력된다.
# TensorFlow 1.x c = Tensor("add_7:0", shape=(), dtype=float32)
그래서 아래와 같이 session을 만든 후에 연산 실행해야한다.
with tf.Session() as sess: print(sess.run(c))
#결과 3.0
하지만 TensorFlow 2.x는 Eager Execution 특성으로 실행결과를 바로 알 수 있음.
이는 오퍼레이션을 실행하는 순간 연산이 수행되기 때문에 실행결과를 numpy 값으로 즉시 알 수 있기 때문이다.
# TensorFlow 2.x
또한 TensorFlow에서는 tf.Variable() 값을 초기화하기 위해 세션 내에서 tf.global_variables_initializer() 과정이 필요 없다!
변수를 정의함과 동시에 초기 값이 할당된다.
#TensorFlow 2.x #가우시안 분포 W = tf.Variable(tf.random.normal([1])) print('initial W = ', W.numpy()) for step in range(2): W = W + 1.0 print(W.numpy())
TensorFlow 1.x에서는 Session 초기화가 필요
아래와 같이 값을 할당해야 했다.
with tf.Session() as sess:
#변수 노드 값 초기화
sess.run(tf.global_variables_initializer())
for step in range(2):
W = W + 1.0
print(sess.fun(W))
회사에서 딥러닝 교육을 수강했을 때는 TensorFlow 1.x 버전이라 조금 불편했는데
2.x 버전은 확실히 코드 짜기 직관적이고 간단한 것 같다!
https://www.youtube.com/watch?v=KOYVxcUo16s&list=PLS8gIc2q83OhM0RTktKDitgZGX5dHo7Vs&index=2