TensorFlow发布Eager,便于Debug!

来源:互联网 发布:mac pdf转word网页版 编辑:程序博客网 时间:2024/06/05 12:48

要查看网络中间结果,使用TensorFlow的小伙伴都知道,必须要run一下session,返回对应的值才能查看,这是很不方便的。为此,TensorFlow团队发布了eager,可以实时查看中间结果,便于大家Debug。那么怎么来用呢?今天简单介绍一下。

1. 安装

Eager execution已经包含在最新的1.4版本中,有两个办法可以安装,一是本地编译安装TensorFlow 1.4可以生成eager,二是安装轻量级的库:

pip install tf-nightly # (for CPU-only TensorFlow)pip install tf-nightly-gpu # (for GPU-enabled TensorFlow)

2. 使用

import tensorflow.contrib.eager as tfetfe.enable_eager_execution()

在头文件中import eager库,并激活eager execution,后面默认eager一直处于激活状态。这个时候,中间结果像numpy结构的数据一样,可以直接打印输出。



此外,还有一个优势,tensor和numpy可以自动互相调用。

import numpy as npnp_x = np.array(2., dtype=np.float32)x = tf.constant(np_x)py_y = 3.y = tf.constant(py_y)z = x + y + 1print(z)print(z.numpy())# Outputtf.Tensor(6.0, shape=(), dtype=float32)6.0
这个真的展示了TF的特点,快速,灵活!



更多资料,请查看:

TensorFlow Eager Execution



原创粉丝点击