11. import tensorflow as tf
from tensorflow.python.framework.function import Defun
@Defun(tf.float32, tf.float32)
def fn(x, y):
return x + y
a = tf.constant(1.5)
b = tf.constant(2.7)
c = a * b
d = a + b
e = fn(c, d)
f = tf.constant(-7.8)
out = e + f
tf.Session().run(out)
計算グラフの最適化を確認しよう
❖ 最適化を狙った計算グラフを作ってみる
TensorBoard
サブグラフ
全入力が
Constの演算
33. ❖ 演算効率が良くなるように、CPUでの演算に変更
Pin to Host Optimizer
tensorflow/core/grappler/optimizers/pin_to_host_optimizer.cc
Grappler
Op
Const
Const
GPU
GPU
入力は
CPUメモリを期待
Op
Const
Const
CPU
CPU
デバイス割り当てを
CPUに変更
47. Eager ModeとGraph Mode
❖ TensorFlow 2.0では、Eager Modeがデフォルト化
➢ Eager Modeでは、最適化処理が適用されない
❖ tf.functionを利用することで、Graph Modeで動作可能
➢ Graph Modeでは、従来の最適化処理が適用される
import tensorflow as tf
@tf.function
def graph_mode():
a = tf.constant(1.5)
b = tf.constant(2.7)
c = a * b
d = a + b
return c + d
out = graph_mode()
GraphModeで動作するため、
最適化処理も適用される