TensorFlow中的图

2021-09-21  本文已影响0人  LabVIEW_Python

在开发过程中,我们希望TensorFlow跟Python融合的好,这样有利于开发、调试和训练模型。

但在部署模型的过程中,我们希望:

这时,TensorFlow中的图(Graph)就非常有用了:

由此,TensorFlow分为Eager Execution和Graph Execution,使用函数 tf.config.run_functions_eagerly来切换

二者性能测试范例代码:

import tensorflow as tf 
import timeit 

x = tf.random.uniform(shape=[10, 10], minval=-1, maxval=2, dtype=tf.dtypes.int32)

def power(x, y):
    result = tf.eye(10, dtype=tf.dtypes.int32)
    for _ in range(y):
        result = tf.matmul(x, result)
    return result
# Eager Execution执行性能
print("Eager execution:", timeit.timeit(lambda: power(x, 100), number=1000))
# Graph Execution执行性能
tf.config.run_functions_eagerly(False)
power_as_graph = tf.function(power)
print("Graph execution:", timeit.timeit(lambda: power_as_graph(x, 100), number=1000))

Eager execution: 1.8359077000000001
2021-09-21 11:32:31.167202: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
Graph execution: 0.33811800000000014

由此可以看出Graph execution的性能远远好于Eager execution,使用tf.function is commonly used to speed up training loops

上一篇 下一篇

猜你喜欢

热点阅读