1.x vs 2.x

执行机制

TensorFlow 1.x -- 延迟执行机制(deferred execution)

16151658449989

在计算图中只保存计算过程,并不实际计算,因此,称为延迟计算图/静态图,我们需要创建一个 session,在其中进行计算。

特点:

  1. 代码运行效率高,便于优化
  2. 静态图只需要创建一次,可以重复使用;静态图运行之前,可以优化,效率更高。
  3. 程序不够简洁

Tensorflow 2.x -- 动态图机制(Eager execution)

16151667742943
  1. 无需首先创建静态图,可以立刻执行计算,并返回结果
  2. 能够快速的建立和调试模型
  3. 执行效率不高

可以在程序调试阶段使用动态图,快速建立模型、调试程序;在部署阶段,采用静态图机制,从而提高模型的性能和部署能力。

API 整合

TensorFlow 1.x -- 重复、冗余的 API

  1. 构建神经网络:tf.slim,tf.layers,tf.contrib.layers,tf.keras
  2. 混乱,不利于程序共享,维护的成本高

TensorFlow 2.0 -- 清理/整合 API

  1. 清理、整合了重复的 API
  2. 将 tf.keras 作为构建和训练模型的标准高级 API

在 2.x 中运行 1.x

1
2
3
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tf.compat.v1.disable_eager_execution()

TensorFlow 环境配置

包管理与环境管理

conda 包命令 安装包: conda install 卸载包: conda remove 更新包:conda update 模糊查询:conda search

通常使用 conda 进行包管理,如果遇到 conda 不能管理,可以使用 pip

conda 环境管理

conda create --name conda activate deactivate conda remove --name --all conda env list

配置 anaconda 清华大学镜像 参考:Anaconda 镜像使用帮助

tensorflow 2.0 安装

  1. 创建独立环境,并激活

    1
    2
    conda create --name rltest python==3.7
    conda activate rltest

  2. 安装相关软件包

    1
    2
    conda install numpy matplotlib PIL scikit-learn pandas
    pip install numpy matplotlib Pillow scikit-learn pandas -i https://pypi.tuna.tsinghua.edu.cn/simple

  3. 安装 tensorflow2.0

    1
    pip install tensorflow==2.0.0-beta -i https://pypi.tuna.tsinghua.edu.cn/simple

    由于 tensorflow 版本与 numpy 版本的兼容问题,出现了 Future waring,因此我重新安装了 pip install "numpy<1.17",安装了 numpy 1.16.6 版本

参考

  1. FutureWarning: Deprecated numpy API calls in tf.python.framework.dtypes

  2. “synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.” problem in TensorFlow