美文网首页
TensorFlow中的图

TensorFlow中的图

作者: LabVIEW_Python | 来源:发表于2021-09-21 11:26 被阅读0次

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

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

  • 模型无Python无关,可移植性好
  • 模型容易被优化,能并行运行,能在不同计算设备中运行
  • 模型运行速度高效

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

  • 它是一种数据结构,tf.Operation定义操作,tf.Tensor为经过基本操作的数据
  • 与Python代码无关,不需要Python解释器
  • 可以用Grappler进行优化

由此,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

相关文章

  • TensorFlow基本概念

    TensorFlow是一个编程系统。 在TensorFlow中,使用图(graphs)来表示计算任务,图(grap...

  • Tensorflow入门

    基本概念 一、计算模型——计算图 1.1基本概念 计算图是Tensorflow最基本的概念,Tensorflow中...

  • TensorFlow基本使用

    一、TensorFlow计算模型——计算图 TensorFlow中的所有计算都会被转化为计算图上的节点 计算图的概...

  • TensorFlow中的图

    在开发过程中,我们希望TensorFlow跟Python融合的好[https://www.jianshu.com/...

  • TensorFlow入门

    1.TensorFlow计算模型--计算图 1.1 计算图概念 TensorFlow中的每一个计算都是计算图上的一...

  • [翻译]第三章(2)TensorFlow graphs

    TensorFlow graphs (TensorFlow的图) 目前为止,我们只将“图”作为Tensorflow...

  • lesson2 : Tensorflow 入门

    2.1 Tensorflow的计算模型——计算图   根据lesson1中所讲,Tensorflow实际原理是张量...

  • Tensorflow的一些笔记

    tensorflow中的图及其持久化和加载 在我们创建tensorflow工作的时候,tf就默认生成了一个图,这就...

  • Tensorflow初识

    1. 初识tensorflow tensorflow中需要明白的几点:使用tensor表示数据使用图来表示计算任务...

  • TensorFlow2简单入门-张量数据结构(Tensor)

    程序 = 数据结构+算法 TensorFlow程序 = 张量数据结构 + 计算图算法语言 TensorFlow中的...

网友评论

      本文标题:TensorFlow中的图

      本文链接:https://www.haomeiwen.com/subject/ibubgltx.html