TensorFlow 2.7 有哪些新变化?

TensorFlow 2.7 有哪些新变化?
www.zeeklog.com  - TensorFlow 2.7 有哪些新变化?
www.zeeklog.com  - TensorFlow 2.7 有哪些新变化?

发布人: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 中所有错误消息进行广泛审查。

www.zeeklog.com  - TensorFlow 2.7 有哪些新变化?

简化堆栈错误信息

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

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

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

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

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

www.zeeklog.com  - TensorFlow 2.7 有哪些新变化?

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 ModelsLayers 的输入和输出。

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

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

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

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

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

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

Tensor 算子:可以使用分派装饰器对扩展程序类型进行扩展,以支持大多数接收 Tensor 输入的 TensorFlow 算子(如,tf.matmultf.gathertf.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-Mixer、Vision Transformers、Wav2Vec2、RoBERTa、ConvMixer、DistillBERT、YoloV5 等诸多模型。

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/

www.zeeklog.com  - TensorFlow 2.7 有哪些新变化?
www.zeeklog.com  - TensorFlow 2.7 有哪些新变化?
www.zeeklog.com  - TensorFlow 2.7 有哪些新变化?

Read more

60个“特征工程”计算函数(Python代码)

60个“特征工程”计算函数(Python代码)

转自:coggle数据科学 近期一些朋友询问我关于如何做特征工程的问题,有没有什么适合初学者的有效操作。 特征工程的问题往往需要具体问题具体分析,当然也有一些暴力的策略,可以在竞赛初赛前期可以带来较大提升,而很多竞赛往往依赖这些信息就可以拿到非常好的效果,剩余的则需要结合业务逻辑以及很多其他的技巧,此处我们将平时用得最多的聚合操作罗列在下方。 最近刚好看到一篇文章汇总了非常多的聚合函数,就摘录在下方,供许多初入竞赛的朋友参考。 聚合特征汇总 pandas自带的聚合函数 * 其它重要聚合函数 其它重要聚合函数&分类分别如下。 def median(x):     return np.median(x) def variation_coefficient(x):     mean = np.mean(x)     if mean != 0:         return np.std(x) / mean     else:         return np.nan def variance(x):     return

By Ne0inhk
90w,确实可以封神了!

90w,确实可以封神了!

要说24年一定最热的技术,还得是AIGC! 前段时间阿里旗下的开源项目,登上GitHub热榜! AI大热,如今ChatGPT的优异表现,必然会出现各种细分场景应用的工具软件,和大量岗位项目! 山雨欲来风满楼,强人工智能的出现,所有科技公司已经开始巨量扩招此领域的人才。算法的岗位,近三个月已经增长68%!这件事在HR届也是相当震撼的。 目前各行各业都不景气的市场,人工智能岗位却一直保持常青!甚至同属AI边缘岗都比其他岗薪资高40%! 与此同时,AI算法岗上岸也不简单,竞争激烈,好公司核心岗位不用说,谁都想去。 所以事实就是,想要上岸,门槛也逐渐变高,项目经历、实习经历都很重要,越早明白这个道理就越能提前建立起自己的优势。 但我在b站逛知识区的时候,经常看到有些同学,因为一些客观原因导致无法参加实习,这种情况下,如果你想提升背景,增加项目经历的话,可以试试这个《CV/NLP 算法工程师培养计划》。 目前已经有上千位同学通过该计划拿到offer了,最新一期学员就业薪资最高能拿到78K!年薪94w! 优势就是有BAT大厂讲师带领,手把手带做AI真实企业项目(包含CV、NLP等

By Ne0inhk
再见nohup!试试这个神器,Python Supervisor!

再见nohup!试试这个神器,Python Supervisor!

👇我的小册 45章教程:() ,原价299,限时特价2杯咖啡,满100人涨10元。 作者丨Ais137 https://juejin.cn/post/7354406980784373798 1. 概述 Supervisor 是一个 C/S 架构的进程监控与管理工具,本文主要介绍其基本用法和部分高级特性,用于解决部署持久化进程的稳定性问题。 2. 问题场景 在实际的工作中,往往会有部署持久化进程的需求,比如接口服务进程,又或者是消费者进程等。这类进程通常是作为后台进程持久化运行的。 一般的部署方法是通过 nohup cmd & 命令来部署。但是这种方式有个弊端是在某些情况下无法保证目标进程的稳定性运行,有的时候 nohup 运行的后台任务会因为未知原因中断,从而导致服务或者消费中断,进而影响项目的正常运行。 为了解决上述问题,通过引入 Supervisor 来部署持久化进程,提高系统运行的稳定性。 3. Supervisor 简介 Supervisor is a client/

By Ne0inhk
第一本给程序员看的AI Agent图书上市了!

第一本给程序员看的AI Agent图书上市了!

AI Agent火爆到什么程度? OpenAI创始人奥特曼预测,未来各行各业,每一个人都可以拥有一个AI Agent;比尔·盖茨在2023年层预言:AI Agent将彻底改变人机交互方式,并颠覆整个软件行业;吴恩达教授在AI Ascent 2024演讲中高赞:AI Agent是一个令人兴奋的趋势,所有从事AI开发的人都应该关注。而国内的各科技巨头也纷纷布局AI Agent平台,如:钉钉的AI PaaS、百度智能云千帆大模型平台等等。 Agent 是未来最重要的智能化工具。对于程序员来说,是时候将目光转向大模型的应用开发了,率先抢占AI的下一个风口AI Agent。 小异带来一本新书《大模型应用开发 动手做 AI Agent》,这本书由《GPT图解》的作者黄佳老师创作,从0到1手把手教你做AI Agent。现在下单享受5折特惠! ▼点击下方,即可5折起购书 有这样一本秘籍在手,程序员们这下放心了吧,让我们先来揭开 Agent 的神秘面纱。 AI Agent 面面观

By Ne0inhk