52

终版API已定型,TensorFlow 2.0 Beta蜕变归来

 4 years ago
source link: https://www.tuicool.com/articles/ErmqyeQ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

TensorFlow2.0 Beta 已来,RC 还会远吗?

TensorFlow 发布以来,已经成为全世界最广泛使用的深度学习库。但 Tensorflow 1.x 时代最广受诟病的问题是:学习门槛较高、API 重复且复杂、模型部署和使用不够方便。之后,谷歌下定决心改变这一问题,在今年早些时候,发布了 Tensorflow 2.0 的 Alpha 版本。Alpha 版本一经问世,便受到深度学习研究者、开发者和在校学生的好评,其简洁的 API 和快速易上手的特性吸引了更多用户的加入。今天,Tensorflow 官方发布了 2.0 时代的 Beta 版本,标志着 Tensorflow 这一经典的代码库进一步成熟。

vaQfQbf.jpg!web

Tensorflow 今天发布 Tensorflow 2.0 Beta 版本。Tensorflow 官方表示,在 Tensorflow2.0 Alpha 版本发布后,已有超过 130000 名学生加入了 deeplearing.ai 和 Udacity 的相关课程。Github 上则已经收到 128000 颗星和超过 7500 次 fork。

在本次 beta 版本更新后,TensorFlow团队已经完成了对 2.0 API 的对象重命名和移除工作。现有的 API 已经是最终版本,并且会作为 Tensorflow1.14 版本中的 v2 可兼容模块。同时 Tensorflow2.0 也增加了对 Keras 特性的支持,如简化定制化训练循环 API、增加对大部分硬件的分布式策略的支持等。

Tensorflow 产品生态中的核心组件,如TensorBoard、TensorHub、TensorFlowLite、和TensorFlow.js 可以和 Beta 版本一起使用。但是对TensorFlowExtended (TFX) 组件和端到端管道的支持工作仍在继续。

在 Alpha 版本发布后,TensorFlow团队已经解决了超过 100 个 Github 的 issue,并仍对剩余的 issue 进行解决,他们希望开发者能够继续在提供相关的建议和反馈。在 release candidate(RC)版本发布前,TensorFlow团队会完成云 TPU 和 TPU 对 Keras 模型的支持,继续对 Tensorflow 的表现进行优化,并解决更多用户提出的问题。

TensorFlow2.0 Beta 到底有什么

如果读者们想体验一番 TF 2.0 Beta,那么直接使用如下的命令安装就行了:

> pip install tensorflow==2.0.0-beta0

但在使用前,我们需要先了解它的一些重要特性。其实,Tensorflow 2.0 版本和核心观点即致力于简单便捷的使用体验。围绕这个核心观点,TF 2.0 Beta 其更新了如下一些特性。

  • 使用 Keras 和 eager execution 方便地搭建模型

  • 在任何生产平台上都能稳健地部署模型

  • 在研究中可以有力支持实验

  • 简化、去重,并移除生命周期终结的 API

为了更具体地感受新版特性,以下给出了一些参考资料:

新版本亮点

  • 分布式训练策略:2.0 版本使用新的 tf.distribute.Strategy API,可以在尽量不改动代码的情况下用于分布式训练模型,达到良好的表现。同时 2.0 版本还支持更多的分布式策略,如对定制化训练循环的支持,以及对 Keras 子类模型的支持。官方发布了一个文档,用于查看这些新的支持(链接: https://www.tensorflow.org/beta/guide/distribute_strategy )。

  • API 冻结:完成了 API 中的符号重命名和修改。2.0 中的 API 已经是最终版本,并且会作为 Tensorflow1.14 版本中 compat.v2 模块。所有的变更都可以在这个文档看到(链接: https://docs.google.com/spreadsheets/d/1FLFJLzg7WNP6JHODX5q8BDgptKafq_slHpnHVbJIteQ/edit#gid=0 )。

qMZVvyr.jpg!web

API 变更表示例。

重大更改

  • tf.contrib 目前已经完全被移除了,它里面的各种 API 要么移植到了TensorFlow的核心 API、要么移到了独立的第三方库 tensorflow_addons、或者完全移除了。

  • tf.estimator.DNN/Linear/DNNLinearCombined 系列 API 中的预制 estimator 已经更新了,可以直接使用 tf.keras.optimizers 中的最优化器,而不需要使用以前的 tf.compat.v1.train.Optimizers。如果我们没有将参数传入 optimizer =,或者直接使用字符串调用最优化器,那么预制的 Estimator 将直接使用 Keras 最优化器。

2.0 Beta 实战 Demo

那么 TF 2.0 Beta 版用起来什么感觉呢?TensorFlow官方提供了一个 Demo 以展示如何用 Beta 版快速处理分类问题。因为整个过程已经通过 API 高度封装了起来,即使我们不懂底层细节,也能快速实现分类模型。

首先当然是安装 TF 2.0 Beta,并导入库了。注意 tf.contrib 已经正式删除了,且很多 API 都已经并入 Keras 中,所以整个代码有一种浓厚的 Keras 风格。

<code>!pip install tensorflow==2.0.0-beta0</code><code>from __future__ import absolute_import, division, print_function, unicode_literals</code><code># <mark data-type="technologies" data-id="58170354-4618-4a25-a2fe-c4ed169f2177">TensorFlow</mark> and tf.keras</code><code>import tensorflow as tf</code><code>from tensorflow import keras</code><code># Helper libraries</code><code>import numpy as np</code><code>import matplotlib.pyplot as plt</code>

利用 API 导入 Fashion-MNIST 数据集也是很简单了,目前 tf.keras.datasets 可以导入 MNIST、Cifar-10/100 等视觉数据集,也可以导入 imdb 和 reuters 等 NLP 数据集。

<code>fashion_mnist = keras.datasets.fashion_mnist</code><code>(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()</code><code>class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',</code><code> 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']</code><code> </code><code>train_images = train_images / 255.0</code><code>test_images = test_images / 255.0</code>

因为使用 Keras 高级 API,搭建和编译模型也非常简单,基本就是调用已有的成熟方法就行了。其中搭建模型架构可以像堆积木那样完成,编译只需要给定最优化器、 损失函数 和度量方法三个关键要素就行了。

<code>model = keras.Sequential([</code><code> keras.layers.Flatten(input_shape=(28, 28)),</code><code> keras.layers.Dense(128, activation='relu'),</code><code> keras.layers.Dense(10, activation='softmax')</code><code>])</code><code>model.compile(optimizer='adam',</code><code> loss='sparse_categorical_crossentropy',</code><code> metrics=['accuracy'])</code>

下面,我们可以看看模型训练与评估的风格。没有 tf.Session()、没有 sess.run()、没有各种占位符的 feed_dict,一切都只要简单的一行代码。

<code>model.fit(train_images, train_labels, epochs=10)</code><code>test_loss, test_acc = model.evaluate(test_images, test_labels)</code><code>print('\nTest accuracy:', test_acc)</code>

训练完模型后,最后就是做推断了:

predictions = model.predict(test_images)

以上是模型最主要的过程,当然 Demo 中间还会有一些展示,例如如何可视化数据或打印训练信息等,感兴趣的读者可查阅原始 Demo。总体而言,如同 TF 2.0 的一贯风格,新版TensorFlow力求最简洁、最直观地构建模型。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK