Getting Started with
TensorFlow 2.0
bit.ly/getting-started-tf2
박해선 - ML GDE
haesun.park@tensorflow.blog
What is TensorFlow
What is TensorFlow 2.0
Install TensorFlow
Windows, macOS, Linux + Python (2.7), 3.x
$ pip install tensorflow
$ pip install tensorflow-gpu
$ pip install tensorflow==2.0.0-beta1
$ pip install tensorflow-gpu==2.0.0-beta1
TF 2.0 Concept Diagram
Eager Execution
TensorFlow 1.x
TensorFlow 2.x
>>> import tensorflow as tf
>>>
>>> t = tf.nn.sigmoid([0.])
>>>
>>> print(t)
Tensor("Sigmoid_1:0", shape=(1,), dtype=float32)
>>> import tensorflow as tf
>>>
>>> t = tf.nn.sigmoid([0.])
>>>
>>> print(t)
tf.Tensor([0.5], shape=(1,), dtype=float32)
>>>
>>> print(t.numpy())
[0.5]
TensorFlow 1.x
TensorFlow 2.x
import tensorflow as tf
## 그래프를 정의합니다
g = tf.Graph()
with g.as_default():
x = tf.placeholder(dtype=tf.float32,
shape=(None), name='x')
w = tf.Variable(2.0, name='weight')
b = tf.Variable(0.7, name='bias')
z = w * x + b
init = tf.global_variables_initializer()
## 세션을 만들고 그래프 g를 전달합니다
with tf.Session(graph=g) as sess:
## w와 b를 초기화합니다
sess.run(init)
## z를 평가합니다
for t in [1.0, 0.6, -1.8]:
print('x=%4.1f --> z=%4.1f'%(
t, sess.run(z, feed_dict={x:t})))
import tensorflow as tf
w = tf.Variable(2.0, name='weight')
b = tf.Variable(0.7, name='bias')
# z를 평가합니다
for x in [1.0, 0.6, -1.8]:
z = w * x + b
print('x=%4.1f --> z=%4.1f'%(x, z))
AutoGraph
tf.Graph() + tf.session() → @tf.function
# TensorFlow 1.x
outputs = session.run(ops, feed_dict={placeholder: input})
@tf.function
def simple_func():
# complex computation with pure python
...
return z
# TensorFlow 2.0
outputs = simple_func(input)
Keras
Why Keras
Handson-Machine Leanring with Scikit-Learn and TensorFlow 2/E
Swift !
Sequential API
from tensorflow import tf
model = tf.keras.Sequential()
# 64개의 유닛을 가진 완전 연결 층을 모델에 추가합니다:
model.add(tf.keras.layers.Dense(64, activation='relu'))
# 또 하나를 추가합니다:
model.add(tf.keras.layers.Dense(64, activation='relu'))
# 10개의 출력 유닛을 가진 소프트맥스 층을 추가합니다:
model.add(tf.keras.layers.Dense(10, activation='softmax'))
# 컴파일
model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
# 모델 훈련
model.fit(train_data, labels, epochs=10, batch_size=32)
# 모델 평가
model.evaluate(test_data, labels)
# 샘플 예측
model.predict(new_sample)
model = tf.keras.Sequential([
tf.keras.layers.Dense(64),
tf.keras.layers.Dense(64),
tf.keras.layers.Dense(10),
])
Functional API
from tensorflow import tf
# 입력과 출력을 연결
inputs = tf.keras.Input(shape=(784,), name='img')
x = tf.keras.layers.Dense(64, activation='relu')(inputs)
x = tf.keras.layers.Dense(64, activation='relu')(x)
outputs = tf.keras.layers.Dense(10, activation='softmax')(x)
# 모델 생성
model = tf.keras.keras.Model(inputs=inputs, outputs=outputs, name='mnist_model')
# 컴파일
...
# 훈련
...
API Cleanup
Upgrade to 2.0
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
TensorFlow 2.0 Recommendation
Google I/O ’19 highlights
Keras Tuner
Keras Tuner
On-Device ML
Google Coral
USB Accelerator
Dev Board
Edge TPU
Coral Demo
USB Accelerator + Raspberry Pi
+
Live Demo !
감사합니다