TensorFlow Lite 开发手册(1)——TensorFlow 2.0安装
(一) TensorFlow 2.0安装
创建虚拟环境:
conda create --name py36-tf20 python=3.6
conda activate py36-tf20
目前conda源尚已维护至2.0版本,可以使用conda命令安装:
# 安装CPU版本
conda install tensorflow
# 安装GPU版本,CUDA支持请查看TensoFlow官网
conda install tensorflow-gpu
(二) TensorFlow 2.0 新特性简介
-
API Cleanup
移除了许多库,如tf.app,tf.logging,tf.flags等,将原有的函数库整合进了tf.keras,如tf.layers->tf.keras.layers -
Eager execution
在2.0中,动态图机制成为默认机制,不再需要用户手动创建会话,也不需要使用 sess.run() 来指定输入输出的张量。 -
No more globals
不再依赖隐式全局命名空间,即不再依赖tf.Variable()来声明变量,而是采用默认机制:“ Keep track of your variables!”,如果不再追溯某个tf.Variable,其就会被回收。 -
Functions, not sessions(个人认为是很重要、也很厉害的一点)
在2.0中提供了名为 @tf.function() 的装饰器,它可以对普通的Python函数进行标记以进行JIT编译,然后TensorFlow就可以将其作为单一的计算图来运行,这使得该函数可以直接被优化和作为模型导出。并且为了帮助用户在添加**@tf.function时避免重写代码,AutoGraph将python中的一些函数转换为其TensorFlow**包含的等价函数:
for/while -> tf.while_loop (break and continue are supported)
if -> tf.cond
for _ in dataset -> dataset.reduce
- 个人补充
在2.0中,Keras被全面整合,Google也推荐大家使用tf.keras更高效构建模型,并且使用tf.data构建数据流(有关tf.data使用的流程可以参照我的博客https://blog.csdn.net/weixin_42499236/article/category/8331677),而tf.keras保存的模型也可以直接被转换为TensorFlow Lite模型,所以还是用Keras比较好。