TensorFlow 2.7 有哪些新变化?

2021 年 12 月 2 日 谷歌开发者


发布人:TensorFlow 团队 Goldie Gadde 和 Josh Gordon


近期重磅上线的 TensorFlow 2.7 通过更加清晰的错误消息、简化的堆栈信息提升了易用性,并为迁移到 TF2 的用户增加了新工具和文档。

  • TensorFlow 2.7

    https://github.com/tensorflow/tensorflow/releases


改善调试体验


调试代码的过程,是机器学习框架用户体验的一个基本组成部分。在 TensorFlow 2.7 中,我们大幅改善了 TensorFlow 的调试体验,提高了其效率和用户体验,这些改善包括以下三个主要变化:简化堆栈错误信息、在自定义 Keras 层的错误中显示额外的上下文信息,以及对 Keras 和 TensorFlow 中所有错误消息进行广泛审查。



简化堆栈错误信息


TensorFlow 现在默认对出现错误时显示的堆栈信息进行过滤,以隐藏任何来自 TensorFlow 内部代码的报错信息,让信息集中在对您而言比较重要的地方:您自己的代码。如此一来,堆栈信息变得更简单、更简短,让您能够更加轻松地理解和修复代码中的问题。


如果您实际上是在调试 TensorFlow 代码库本身(例如准备 TensorFlow 的 PR),您可以通过调用 tf.debugging.disable_traceback_filtering() 来关闭过滤机制。



针对 Keras 层异常的自动上下文注入


编写低阶代码最常见的用例之一是创建自定义的 Keras 层,所以我们想要尽可能地降低您调试层的难度,提高调试的效率。对层进行调试时,您要做的第一件事就是打印其输入的形状和 dtype,以及其 trainingmask 参数的值。现在,我们将这些信息自动添加到所有自定义 Keras 层的堆栈信息中。


在下图中可以看到堆栈信息过滤和调用上下文信息显示的实际效果:


TensorFlow 2.7 中简化的堆栈信息


审查并改进 TensorFlow 和 

Keras 代码库中的所有错误消息


最后,我们审查了 Keras 和 TensorFlow 代码库中的每一条错误消息(数以千计的错误位置!),并对它们进行了改进,以确保其遵循用户体验的最佳实践。一条合格的错误消息需要能够告诉您框架的预期,指出不符合框架预期的操作,并给出修复问题的相应提示。


改进 tf.function 错误消息


通过在用户代码中加入指向错误源的回溯,我们改进了两种常见的 tf.function 错误消息:运行时错误消息和“计算图”张量错误消息。对于其他模糊和不准确的 tf.function 错误消息,我们也进行了更新,提高了其清晰度和准确性。


对于由用户代码引起的运行时错误消息:

@tf.function
def f():
l = tf.range(tf.random.uniform((), minval=1, maxval=10, dtype=tf.int32))
return l[20]


旧的错误消息摘要如下:

# … Python stack trace of the function call …

InvalidArgumentError: slice index 20 of dimension 0 out of bounds.
[[node strided_slice (defined at <'ipython-input-8-250c76a76c0e'>:5) ]] [Op:__inference_f_75]

Errors may have originated from an input operation.
Input Source operations connected to node strided_slice:
range (defined at':4)

Function call stack:
f


新的错误消息摘要如下:

# … Python stack trace of the function call …

InvalidArgumentError: slice index 20 of dimension 0 out of bounds.
[[node strided_slice
(defined at:5)
]] [Op:__inference_f_15]

Errors may have originated from an input operation.
Input Source operations connected to node strided_slice:
In[0] range (defined at:4)
In[1] strided_slice/stack:
In[2] strided_slice/stack_1:
In[3] strided_slice/stack_2:

Operation defined at: (most recent call last)
# … Stack trace of the error within the function …
>>> File "", line 7, in
>>> f()
>>>
>>> File "", line 5, in f
>>> return l[20]
>>>


主要的区别在于:现在执行 tf.function 时引发的运行时错误包含堆栈信息,可以显示错误在用户代码中的来源。

# … Original error message and information …
# … More stack frames …
>>> File "<ipython-input-3-250c76a76c0e>", line 7, in <module>
>>> f()
>>>
>>> File "<ipython-input-3-250c76a76c0e>", line 5, in f
>>> return l[20]
>>>


对于由以下用户代码引起的“计算图”张量错误消息:

x = None

@tf.function
def leaky_function(a):
global x
x = a + 1# Bad - leaks local tensor
return a + 2

@tf.function
def captures_leaked_tensor(b):
b += x
return b

leaky_function(tf.constant(1))
captures_leaked_tensor(tf.constant(2))


旧的错误消息摘要如下:

# … Python stack trace of the function call …

TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
@tf.function
def has_init_scope():
my_constant = tf.constant(1.)
with tf.init_scope():
added = my_constant * 2
The graph tensor has name: add:0


新的错误消息摘要如下:

# … Python stack trace of the function call …

TypeError: Originated from a graph execution error.

The graph execution error is detected at a node built at (most recent call last):
# … Stack trace of the error within the function …
>>> File, line 6, in leaky_function
# … More stack trace of the error within the function …

Error detected in node 'add' defined at: File "", line 6, in leaky_function

TypeError: tf.Graph captured an external symbolic tensor. The symbolic tensor 'add:0' created by node 'add'is captured by the tf.Graph being executed as an input. But a tf.Graph isnot allowed to take symbolic tensors from another graph as its inputs. Make sure all captured inputs of the executing tf.Graph are not symbolic tensors. Use return values, explicit Python locals or TensorFlow collections to access it. Please see https://www.tensorflow.org/guide/function#all_outputs_of_a_tffunction_must_be_return_values for more information.


主要的区别在于:试图捕捉从无法访问的计算图所溢出张量的错误信息,现在包含堆栈报错信息,可显示张量在用户代码中的创建位置。

# … Original error message and information …

# … More stack frames …
>>> File <ipython-input-5-95ca3a98778f>, line 6, in leaky_function

Error detected in node 'add' defined at: File "<ipython-input-5-95ca3a98778f>", line 6, in leaky_function

TypeError: tf.Graph captured an external symbolic tensor. The symbolic tensor 'add:0' created by node 'add'is captured by the tf.Graph being executed as an input. But a tf.Graph isnot allowed to take symbolic tensors from another graph as its inputs. Make sure all captured inputs of the executing tf.Graph are not symbolic tensors. Use return values, explicit Python locals or TensorFlow collections to access it. Please see https://www.tensorflow.org/guide/function#all_outputs_of_a_tffunction_must_be_return_values for more information.


引入 tf.experimental.ExtensionTYpe


用户定义的类型可以提高您项目的可读性、模块化程度和可维护性。TensorFlow 2.7.0 引入了 ExtensionType API,可用于创建用户定义的、面向对象的类型,与 TensorFlow 的 API 无缝协作。扩展程序类型是对复杂模型所使用的张量进行跟踪和组织的一个好方法。扩展程序类型还可以用于定义新的类张量类型,这种类型对“张量”的基本概念进行了专门化或扩展。要创建扩展程序类型,只需定义一个以 tf.experimental.ExtensionType 为基础的 Python 类,并使用类型注释来指定每个字段的类型:

  • ExtensionType

    https://tensorflow.google.cn/guide/extension_type

  • 类型注释

    https://www.python.org/dev/peps/pep-0484/


class TensorGraph(tf.experimental.ExtensionType):
"""A collection of labeled nodes connected by weighted edges."""
edge_weights: tf.Tensor # shape=[num_nodes, num_nodes]
node_labels: typing.Mapping[str, tf.Tensor] # shape=[num_nodes]; dtype=any

class MaskedTensor(tf.experimental.ExtensionType):
"""A tensor paired with a boolean mask, indicating which values are valid."""
values: tf.Tensor
mask: tf.Tensor # shape=values.shape; false for missing/invalid values.

class CSRSparseMatrix(tf.experimental.ExtensionType):
"""Compressed sparse row matrix (https://en.wikipedia.org/wiki/Sparse_matrix)."""
values: tf.Tensor # shape=[num_nonzero]; dtype=any
col_index: tf.Tensor # shape=[num_nonzero]; dtype=int64
row_index: tf.Tensor # shape=[num_rows+1]; dtype=int64


ExtensionType 基类增加了一个构造函数和一些基于字段类型注释的特殊方法(类似于标准 Python 库中的 typing.NamedTuple@dataclasses.dataclass )。您可以通过覆盖这些默认值,或添加新的方法、属性或子类来选择性地自定义该类型。

  • typing.NamedTuple

    https://docs.python.org/3/library/typing.html#typing.NamedTuple

  • @dataclasses.dataclass

    https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass


以下 TensorFlow API 支持扩展程序类型:


Keras:可以将扩展程序类型用作 Keras Models 和 Layers 的输入和输出


数据集:可以在 Datasets 中加入扩展程序类型,并通过数据集 Iterators 进行返回。


TensorFlow hub:可以将扩展程序类型用作 tf.hub 模块的输入和输出。


SavedModel:可以将扩展程序类型用作 SavedModel 数的输入和输出。


tf.function:可以将扩展程序类型用作与 @tf.function 修饰器一起打包的函数的参数和返回值。


控制流:可以通过 tf.cond 和 tf.while_loop  之类的控制流算子来使用扩展程序类型。其中包括通过 AutoGraph 添加的控制流算子。


tf.py_function:可以将扩展程序类型用作 func 参数至 tf.py_function 的参数和返回值。


Tensor 算子:可以使用分派装饰器对扩展程序类型进行扩展,以支持大多数接收 Tensor 输入的 TensorFlow 算子(如,tf.matmultf.gather 和 tf.reduce_sum


分发策略:可以将扩展程序类型用作每个副本的值。

  • 分派装饰器

    https://tensorflow.google.cn/guide/extension_type#tensor_api_dispatch


若要了解更多有关扩展程序类型的信息,请参阅扩展程序类型指南

  • 扩展程序类型指南

    https://tensorflow.google.cn/guide/extension_type


注意:tf.experimental 前缀表明这是一个新的 API,我们希望从实际使用中收集反馈;除非有任何不可预见的设计问题,我们计划根据 TF 实验性政策ExtensionType 迁移出实验性软件包。

  • TF 实验性政策

    https://github.com/tensorflow/community/blob/master/governance/api-reviews.md#experimental-apis


TF2 迁移更加简单


为了支持有兴趣将工作负载从 TF1 迁移到 TF2 的用户,我们在 TensorFlow 网站上创建了一个新的 Migrate to TF2 标签,其中包括更新的指南和全新的文档,以及 Colab 中具体、可运行的示例。

  • Migrate to TF2

    https://tensorflow.google.cn/guide/migrate

  • Colab

    https://colab.research.google.com/


我们还增加了一个新的 Shim 工具,可显著简化 variable_scope-based 模型向 TF2 的迁移。它有望使大多数 TF1 用户在 TF2 管道中按原样(或仅进行微小调整)运行现有模型架构,而无需重写建模代码。您可以在模型映射指南中了解更多相关信息。

  • 新的 Shim 工具

    https://tensorflow.google.cn/guide/migrate/model_mapping

  • 模型映射

    https://tensorflow.google.cn/guide/migrate/model_mapping


TensorFlow Hub 上新的社区贡献模型


自上一版 TensorFlow 发布以来,整个社区热切合作,在 TensorFlow Hub 上提供了许多新模型。

  • TensorFlow Hub

    https://tensorflow.google.cn/hub


现在您可以找到 MLP-MixerVision TransformersWav2Vec2RoBERTaConvMixerDistillBERTYoloV5 等诸多模型。

  • MLP-Mixer

    https://hub.tensorflow.google.cn/sayakpaul/collections/mlp-mixer/1

  • Vision Transformers

    https://hub.tensorflow.google.cn/sayakpaul/collections/vision_transformer/1

  • Wav2Vec2

    https://hub.tensorflow.google.cn/s?q=wav2vec

  • RoBERTa

    https://hub.tensorflow.google.cn/jeongukjae/xlm_roberta_multi_cased_L-12_H-768_A-12/1

  • ConvMixer

    https://hub.tensorflow.google.cn/rishit-dagli/collections/convmixer

  • DistillBERT

    https://hub.tensorflow.google.cn/s?q=distilbert

  • YoloV5

    https://hub.tensorflow.google.cn/neso613/lite-model/yolo-v5-tflite/tflite_model/1


所有这些模型都可以通过 TensorFlow Hub 使用。您可以在此处进一步了解有关发布模型的更多信息。

  • 此处

    https://tensorflow.google.cn/hub/publish


相关信息


请参阅版本说明了解更多信息。

  • 版本说明

    https://github.com/tensorflow/tensorflow/releases


欢迎随时关注 TensorFlow 博客Twitter 或 Youtube,获悉最新动态。

  • 博客

    https://blog.tensorflow.google.cn/

  • Twitter

    http://twitter.com/tensorflow

  • Youtube

    https://youtube.com/tensorflow


您可以通过 Community Spotlight 计划 向我们提交作品,分享构建成果。通过 GitHub 提交问题,或在 TensorFlow 论坛上发帖,分享您的反馈。我们欢迎您的贡献和参与,谢谢!

  • Community Spotlight 计划

    http://goo.gle/TFCS

  • GitHub

    https://github.com/tensorflow/tensorflow/issues

  • TensorFlow 论坛

    https://discuss.tensorflow.google.cn/



登录查看更多
0

相关内容

Google发布的第二代深度学习系统TensorFlow
专知会员服务
90+阅读 · 2020年12月26日
TensorFlow Lite指南实战《TensorFlow Lite A primer》,附48页PPT
专知会员服务
68+阅读 · 2020年1月17日
TensorFlow 2.0 学习资源汇总
专知会员服务
66+阅读 · 2019年10月9日
如何参与PyTorch社区开源贡献?
极市平台
0+阅读 · 2021年11月30日
TensorFlow 决策森林来啦!
TensorFlow
0+阅读 · 2021年6月1日
TensorFlow 2.0深度强化学习指南
云栖社区
18+阅读 · 2019年2月1日
TF Boys必看!一文搞懂TensorFlow 2.0新架构!
引力空间站
18+阅读 · 2019年1月16日
TensorFlow图像分类教程
机器学习研究会
33+阅读 · 2017年12月29日
手把手教TensorFlow(附代码)
深度学习世界
15+阅读 · 2017年10月17日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2013年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
1+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2011年12月31日
国家自然科学基金
0+阅读 · 2008年12月31日
Arxiv
0+阅读 · 2022年4月20日
Arxiv
56+阅读 · 2021年5月3日
Arxiv
24+阅读 · 2021年1月25日
Arxiv
25+阅读 · 2018年8月19日
VIP会员
相关资讯
如何参与PyTorch社区开源贡献?
极市平台
0+阅读 · 2021年11月30日
TensorFlow 决策森林来啦!
TensorFlow
0+阅读 · 2021年6月1日
TensorFlow 2.0深度强化学习指南
云栖社区
18+阅读 · 2019年2月1日
TF Boys必看!一文搞懂TensorFlow 2.0新架构!
引力空间站
18+阅读 · 2019年1月16日
TensorFlow图像分类教程
机器学习研究会
33+阅读 · 2017年12月29日
手把手教TensorFlow(附代码)
深度学习世界
15+阅读 · 2017年10月17日
相关基金
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2013年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
1+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2012年12月31日
国家自然科学基金
0+阅读 · 2011年12月31日
国家自然科学基金
0+阅读 · 2008年12月31日
Top
微信扫码咨询专知VIP会员