1、特性介绍

  1. tensorflow2.0默认是Eager execution模式;
  2. eager模式对numpy的支持很友好,具体如下:
    1. numpy的操作可以接受Tensor作为参数;
    2. Tensorflow的数学操作会将python对象和numpy的arrays转换成Tensor;
    3. tf.Tensor.numpy方法返回numpy的ndarray。
  3. 可逐行动态控制流,逐行控制代码的运行;
  4. 一切皆函数,无须手动搭建tensorflow数据结构。

    2、相关API

    2.1 即刻输出

    1. import tensorflow as tf# 导入TensorFlow
    2. scalar_tf=tf.constant(3.14)#创建张量
    3. m=tf.add(scalar_tf,scalar_tf)#执行操作
    4. m#输出操作结果
    image.png

    2.2 状态查看和启动

    默认情况下,Eager execution处于启用状态,可以用tf.executing_eargerly()查看Eager Execution当前的启动状态,返回True则是开启,False是关闭。可以用tf.compat.v1.enable_eager_execution()启动eager模式。
    1. tf.compat.v1.executing_eagerly()#查看状态
    image.png

    2.3 关闭与启动eager模式

    关闭eager模式的函数是 tf.compat.v1.disable_eager_execution()
    启动eager模式的函数是tf.compat.v1.enable_eager_execution()