去年8月13日,谷歌宣布 “TensorFlow 2.0 is coming”,
最近几天,谷歌 TensorFlow 团队刚刚发布了 TensorFlow 2.0 Preview 版,
可以来这里查看: <https://www.tensorflow.org/versions/r2.0/api_docs/python/tf>

现在还不是最终版本,而且可能还不太稳定,不过大家都可以开始尝试,并提出建议和反馈,请查看如何提交报告:
<https://github.com/tensorflow/community/blob/master/governance/tensorflow-testing.md>

本文结构:

* 从 TensorFlow 1.12 到 TensorFlow 2.0 preview 代码转换方法
* TensorFlow 2.0 安装方法
* 为什么要有 TensorFlow 2.0
* TensorFlow 2.0 有哪些新的变化
* 代码:Logistic Regression
<>1. 还有从 TensorFlow 1.12 到 TensorFlow 2.0 preview 的代码转换工具 tf_upgrade_v2,项目地址:
<https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/compatibility>

用法举例:
!tf_upgrade_v2 # 选择 input file,输出 output file tf_upgrade_v2 --infile foo.py
--outfile foo-upgraded.py # 将整个目录进行变换 tf_upgrade_v2 --intree coolcode --outtree
coolcode-upgraded
转换后的结果会保存在 report.txt

<>2. TensorFlow 2.0 preview 的安装方法如下:
# 创建一个名为 python36 的 Python3.6 环境 conda create -n python36 python=3.6 # 进入环境
source activate python36 # 使用 pip 安装 TensorFlow 2.0 Preview: pip install
tf-nightly-2.0-preview # 安装 GPU 版本 pip install tf-nightly-gpu-2.0-preview
<>3. 在 Github 上有一个 TensorFlow 2.0 的教程 repo:
<https://github.com/ageron/tf2_course>



目前有三个练习,

* Neural Nets with Keras: 用 tf.keras 进行神经网络的一些基本操作。
* Low-Level TensorFlow API: 一些基本的 tensorflow API,如层的定义。
* Loading and Preprocessing Data:一些数据预处理的操作。
<>4. 再来看看为什么要有 TensorFlow 2.0

《Hands-On Machine Learning with Scikit-Learn and TensorFlow》这本书大家应该都知道,它的作者
Aurélien Géron 对这个原因以及有哪些变化做了详细的讲解:
https://www.youtube.com/watch?v=WTNH0tcscqo&t=304s
<https://www.youtube.com/watch?v=WTNH0tcscqo&t=304s>

去年 3 月,Andrej Karpathy 发了一条 twitter,画出了机器学习论文中涉及的深度学习工具:



可以看到:
Caffe, Theano and Torch 趋势下降,
MXNet, CNTK and Chainer 依然很低,
TensorFlow and Keras 上升很快,
因为大多数 Keras 用户使用 TensorFlow 作为后端,所以 TensorFlow 的曲线可能会更高。
同时 PyTorch 也在上升,

1. 因为 TensorFlow 的代码比较麻烦

例如要计算 1 + ½ + ¼ + ⅛ + … ,使用 PyTorch 的代码明显比 TensorFlow 简单:



2. TensorFlow 不容易 debug

例如当得到一个错误的结果时,NaN 或异常,它不会告诉你问题的来源,
当有很多操作时,调试起来会很棘手,
虽然有一个专门的调试器,但并不易使用。



3. graph 模式很难描述

还是上图代码的例子,可以轻松测量 z 的计算时间,但不​​会告诉你计算 x 和 y 花了多少时间

后来从 TensorFlow 1.4 开始,可以使用 eager 模式。



可以发现和 PyTorch 一样简单,简化了编码,调试和分析,
没有 construction phase, execution phase, graphs,sessions,

<>5. TensorFlow 2.0 有哪些变化呢?

* Eager Execution 将成为核心功能
* 代码风格以 Keras 为主
* 删除弃用的 API,减少重复
* 支持更多的平台和语言
* 很多函数如 optimizer,loss,metrics 会统合到 Keras 中,例如: tf.layers → tf.keras.layers
tf.train → tf.keras.optimizers tf.losses → tf.keras.losses
<>1. 在 TensorFlow 2.0, eager execution 是默认的,不需要启用它:



<>2. TensorFlow 支持 eager execution 和 graph mode,同时具有二者的优点:

前面说 graph 模式很难描述,但 graph mode 的好处也很多:

* 可以直接在 GPU 上运行,而不用来回使用 Python 和 CPU,可以显着提高性能。
* 创建了图表就可以使用 XLA 功能,XLA 可以分析图形并在执行速度和内存方面上提高其性能,运行更快,使用内存更少。
* 将模型部署到任何设备上,无论是移动电话还是集群都比较简单。
二者一起使用的方法有:

*
* 下面这个程序以 eager 模式运行,但在with块中,它以 graph 运行:


*
* 有一种混合 eager 和 graph 模式简单方法是在 eager 模式下运行,并使用defun :
TensorFlow 将自动开始创建图形,会话,提供数据,并获得最终的张量。


*
* 另一种创建 graph 的简单方法是使用新的 autograph 模块。
例如,要写一个 fibonacci()函数,来计算 fibonacci 数列的第 n 个数字:


这里面包含一个循环,当 autograph 将这个函数转换成 TensorFlow 版本时,它知道当它在图形模式下运行时,应该创建一个使用
TensorFlow 的while_loop() 操作的 graph。

<>3. PyTorch 比较 pythonic,TensorFlow 2.0 也会是 pythonic, object-oriented

以前构建多个模型时,可能会遇到各种名称冲突,需要依赖于 tf.variable_scope()和tf.get_variable(),比较难理解,而且非常复杂,
在 TensorFlow 2.0 将完全放弃这种方法,避免依赖于全局状态等等。

而且更加面向对象,TF 2.0 将非常依赖 Keras API,共享变量更容易



<>4. 弃用 collections

以前训练模型,创建了一个优化器,要减少损失,但 TensorFlow 如何知道它应该按什么顺序来调整哪些变量?
它需要查找 trainable_variables()列表才能知道。
这种设计并不理想,它依赖于附加到图表的一组 collections,并且在程序中使用全局状态是不好的。

所以 TensorFlow 2.0 将弃用 collections,代码会更清晰。
如果使用 tf.keras,则每个层都会处理自己的变量,如果需要获取可训练变量的列表,可以直接查询每个层:



<>5. 关于变量



删除 Variable_scopes,必须以面向对象的方式处理变量共享
tf.get_variable()也被删除,应该使用对象来处理变量
必须传递变量列表给优化器的 “minimize()” 方法
tf.assign(a,b)将被删除,应该使用 a.assign(b)

<>6. 删除杂乱无章的 API

之前 TensorFlow 包含很多重复的 API 或者不推荐使用的 API,杂乱无章,
例如,可以使用 tf.layers 或 tf.keras.layers 创建图层,这样会造成很多重复和混乱的代码。

TensorFlow 2.0 正在摆脱 tf.layers,应该使用 Keras 层,
keras loss 和 keras metrics 也将基于 tf.losses 和 tf.metrics。,
tf.contrib 的各种项目也已经被合并到 Keras 等核心 API 中,或者移动到单独的项目中,还有一些将被删除。

TensorFlow 2.0 会更好地组织 API,使编码更简洁。



<>6. 下面我们建立一个 Logistic Regression 作为例子:
x_train_ = tf.convert_to_tensor(train_inputs_of_numpy) y_train_ =
tf.convert_to_tensor(train_labels_of_numpy) # Logistic regression model model =
tf.keras.layers.Dense(output_size, activation="sigmoid") # loss function def
loss_fn(model, x, y): predict_y = model(x) # return shape is (x.shape[0], ) ;
each element is each data's loss. return tf.keras.losses.binary_crossentropy(y,
predict_y) def accuracy_fn(model, x, y): predict_y = model(x) # return shape is
(x.shape[0], ) ; each element is 1 or 0.(If y[i] == y_pre[i], the i th element
is 1). return tf.keras.metrics.binary_accuracy(y, predict_y) # optimizer
optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate) for epoch in
range(num_epochs): with tf.GradientTape() as tape: loss = loss_fn(model,
x_train_, y_train_) grads = tape.gradient(loss, model.variables) accuracy =
accuracy_fn(model=model, x=x_train_, y=y_train_) if (epoch+1) % 5 == 0: print(
"loss: {:0.3f}, acc: {}".format( tf.reduce_sum(loss).numpy(), # using TF
function or numpy method accuracy.numpy().mean() # both of ok. ) ) # update
prameters using grads optimizer.apply_gradients(zip(grads, model.variables))
TensorFlow 2.0 的代码简洁易懂了很多,更容易上手实践,性能提高,有 Keras,Data API,TF Hub,Google 的 TPU
等等资源,还有公开设计审核流程,相信 TensorFlow 2.0 会越来越好玩的。

Enjoy!

学习资料:
https://www.hellocybernetics.tech/entry/2019/01/12/155900
<https://www.hellocybernetics.tech/entry/2019/01/12/155900>
https://qiita.com/tomo_makes/items/9c22f306f1b781e8a7c2
<https://qiita.com/tomo_makes/items/9c22f306f1b781e8a7c2>
https://www.youtube.com/watch?v=WTNH0tcscqo&t=304s
<https://www.youtube.com/watch?v=WTNH0tcscqo&t=304s>

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信