Skip to Content
《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版
book

《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版

by Aurélien Géron
May 2025
Intermediate to advanced
864 pages
12h 32m
Chinese
O'Reilly Media, Inc.
Content preview from 《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版

附录 D. TensorFlow 图形

在本附录中,我们将探讨 TF 函数生成的图形(参见第 12 章)。

TF 函数和具体函数

TF 函数是多态的,这意味着它们支持不同类型(和形状)的输入。例如,请看下面的tf_cube() 函数:

@tf.function
def tf_cube(x):
    return x ** 3

每次使用新的输入类型或形状组合调用 TF 函数时,它都会生成一个新的具体函数,并为这一特定组合专门设计了自己的图形。这种参数类型和形状的组合称为输入签名。如果使用 TF 函数之前已经使用过的输入签名调用该函数,它将重复使用之前生成的具体函数。例如,如果您调用tf_cube(tf.constant(3.0)) ,TF 函数将重复使用它在tf_cube(tf.constant(2.0)) (float32 标量张量)中使用的具体函数。但是,如果您调用tf_cube(tf.constant([2.0])) 或tf_cube(tf.constant([3.0])) (针对形状为 [1] 的 float32 张量),它将生成一个新的具体函数;如果您调用tf_cube(tf.constant([[1.0, 2.0], [3.0, 4.0]])) (针对形状为 [2, 2] 的 float32 张量),它将生成另一个新的具体函数。您可以通过调用 TF 函数的get_concrete_function() 方法,获取特定输入组合的具体函数。然后就可以像普通函数一样调用它,但它只支持一种输入特征(在本例中为 float32 标量张量):

>>> concrete_function = tf_cube.get_concrete_function(tf.constant(2.0))
>>> concrete_function
<ConcreteFunction tf_cube(x) at 0x7F84411F4250>
>>> concrete_function(tf.constant(2.0))
<tf.Tensor: shape=(), dtype=float32, numpy=8.0>

图 D-1显示了tf_cube() TF 函数,在我们调用tf_cube(2) 和tf_cube(tf.constant(2.0)) 之后:生成了两个具体函数,每个签名一个,每个函数都有自己的优化函数图(FuncGraph) 和自己的函数定义(FunctionDef)。函数定义指向图中与函数输入和输出相对应的部分。在每个FuncGraph 中,节点(椭圆形)代表操作(如幂、常量或参数占位符,如x ),而边(操作之间的实心箭头)代表将流经图的张量。左边的具体函数专门用于x=2 ,因此 TensorFlow 将其简化为一直只输出 8(注意,函数定义甚至没有输入)。右侧的具体函数专门用于 float32 标量张量,因此无法简化。如果我们调用tf_cube(tf.constant(5.0)) ,第二个具体函数将被调用,x 的占位符操作将输出 5.0,然后幂运算将计算5.0 ** 3 ,因此输出将是 125.0。

mls3 ad01
图 D-1. tf_cube() TF 函数及其ConcreteFunctions 和FuncGraphs

这些图中的张量是符号张量,也就是说它们没有实际值,只有数据类型、形状和名称。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
学习 API 风格

学习 API 风格

Lukasz Dynowski, Marcin Dulak
《高性能 Python》第二版

《高性能 Python》第二版

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9798341656598